mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
Check all values when validating against an array.
Clean up tests a bit.
This commit is contained in:
parent
e40e2cb1ac
commit
2910dfa999
5 changed files with 36 additions and 54 deletions
|
@ -1,7 +1,7 @@
|
|||
PATH
|
||||
remote: /Users/gabe/thoughtbot/shoulda-matchers
|
||||
specs:
|
||||
shoulda-matchers (1.0.0)
|
||||
shoulda-matchers (1.1.0)
|
||||
activesupport (>= 3.0.0)
|
||||
|
||||
GEM
|
||||
|
@ -49,7 +49,7 @@ GEM
|
|||
bourne (1.1.2)
|
||||
mocha (= 0.10.5)
|
||||
builder (2.1.2)
|
||||
childprocess (0.3.1)
|
||||
childprocess (0.3.2)
|
||||
ffi (~> 1.0.6)
|
||||
cucumber (1.1.9)
|
||||
builder (>= 2.1.2)
|
||||
|
@ -112,7 +112,7 @@ GEM
|
|||
railties (~> 3.0)
|
||||
rspec (~> 2.6.0)
|
||||
shoulda-context (1.0.0)
|
||||
sqlite3 (1.3.5)
|
||||
sqlite3 (1.3.6)
|
||||
term-ansicolor (1.0.7)
|
||||
thor (0.14.6)
|
||||
treetop (1.4.10)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
PATH
|
||||
remote: /Users/gabe/thoughtbot/shoulda-matchers
|
||||
specs:
|
||||
shoulda-matchers (1.0.0)
|
||||
shoulda-matchers (1.1.0)
|
||||
activesupport (>= 3.0.0)
|
||||
|
||||
GEM
|
||||
|
@ -50,7 +50,7 @@ GEM
|
|||
bourne (1.1.2)
|
||||
mocha (= 0.10.5)
|
||||
builder (3.0.0)
|
||||
childprocess (0.3.1)
|
||||
childprocess (0.3.2)
|
||||
ffi (~> 1.0.6)
|
||||
cucumber (1.1.9)
|
||||
builder (>= 2.1.2)
|
||||
|
@ -77,7 +77,7 @@ GEM
|
|||
mime-types (1.18)
|
||||
mocha (0.10.5)
|
||||
metaclass (~> 0.0.1)
|
||||
multi_json (1.2.0)
|
||||
multi_json (1.3.2)
|
||||
polyglot (0.3.3)
|
||||
rack (1.3.6)
|
||||
rack-cache (1.2)
|
||||
|
@ -131,7 +131,7 @@ GEM
|
|||
hike (~> 1.2)
|
||||
rack (~> 1.0)
|
||||
tilt (~> 1.1, != 1.3.0)
|
||||
sqlite3 (1.3.5)
|
||||
sqlite3 (1.3.6)
|
||||
term-ansicolor (1.0.7)
|
||||
thor (0.14.6)
|
||||
tilt (1.3.3)
|
||||
|
|
|
@ -22,12 +22,12 @@ module Shoulda # :nodoc:
|
|||
end
|
||||
|
||||
class EnsureInclusionOfMatcher < ValidationMatcher # :nodoc:
|
||||
|
||||
|
||||
def in_array(array)
|
||||
@array = array
|
||||
self
|
||||
end
|
||||
|
||||
|
||||
def in_range(range)
|
||||
@range = range
|
||||
@minimum = range.first
|
||||
|
@ -53,7 +53,6 @@ module Shoulda # :nodoc:
|
|||
self
|
||||
end
|
||||
|
||||
|
||||
def description
|
||||
"ensure inclusion of #{@attribute} in #{inspect_message}"
|
||||
end
|
||||
|
@ -70,26 +69,27 @@ module Shoulda # :nodoc:
|
|||
disallows_higher_value &&
|
||||
allows_maximum_value
|
||||
elsif @array
|
||||
unless allows_all_values_in_array
|
||||
@failure_message = "#{@array} doesn't include #{@attribute}"
|
||||
return false
|
||||
if allows_all_values_in_array?
|
||||
true
|
||||
else
|
||||
@failure_message = "#{@array} doesn't match array in validation"
|
||||
false
|
||||
end
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def inspect_message
|
||||
@range.nil? ? @array.inspect : @range.inspect
|
||||
@range.nil? ? @array.inspect : @range.inspect
|
||||
end
|
||||
|
||||
def allows_all_values_in_array
|
||||
@array.any? do |value|
|
||||
allows_value_of(@attribute, :inclusion)
|
||||
def allows_all_values_in_array?
|
||||
@array.all? do |value|
|
||||
allows_value_of(value, @low_message)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def disallows_lower_value
|
||||
@minimum == 0 || disallows_value_of(@minimum - 1, @low_message)
|
||||
end
|
||||
|
|
|
@ -18,7 +18,7 @@ module Shoulda # :nodoc:
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
|
||||
def allows_value_of(value, message = nil)
|
||||
allow = AllowValueMatcher.
|
||||
new(value).
|
||||
|
|
|
@ -65,47 +65,29 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
|
|||
it "should accept ensuring the correct range and messages" do
|
||||
@model.should ensure_inclusion_of(:attr).in_range(2..5).with_low_message(/low/).with_high_message(/high/)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "an attribute which must be included in a array" do
|
||||
context "an attribute which must be included in an array" do
|
||||
before do
|
||||
@model = define_model(:example_foo, :attr => :boolean) do
|
||||
validates_inclusion_of :attr, :in => [true,false]
|
||||
end.new(:attr=>true)
|
||||
end
|
||||
|
||||
it "should accept ensuring the correct array" do
|
||||
@model.should ensure_inclusion_of(:attr).in_array [true,false]
|
||||
end
|
||||
it "should have the array in the description" do
|
||||
ensure_inclusion_of(:attr).in_array([true,false]).description.should == "ensure inclusion of attr in [true, false]"
|
||||
end
|
||||
end
|
||||
|
||||
context "an attribute not included in the array" do
|
||||
before do
|
||||
@model = define_model(:example_foo, :attr => :boolean) do
|
||||
validate :custom_validation
|
||||
def custom_validation
|
||||
unless [true,false].include? attr
|
||||
errors.add(:attr, 'not boolean')
|
||||
end
|
||||
end
|
||||
@model = define_model(:example, :attr => :string) do
|
||||
validates_inclusion_of :attr, :in => %w(one two)
|
||||
end.new
|
||||
end
|
||||
|
||||
it "should have an error on attr" do
|
||||
@model.should ensure_inclusion_of(:attr).in_array([true,false]).with_message(/boolean/)
|
||||
it "accepts with correct array" do
|
||||
@model.should ensure_inclusion_of(:attr).in_array(%w(one two))
|
||||
end
|
||||
context "should not accept other value then specified" do
|
||||
before do
|
||||
@model.attr = "foo"
|
||||
end
|
||||
it "should not be valid" do
|
||||
@model.should ensure_inclusion_of(:attr).in_array([true,false]).with_message(/inclusion/)
|
||||
end
|
||||
|
||||
it "rejects when only part of array matches" do
|
||||
@model.should_not ensure_inclusion_of(:attr).in_array(%w(one wrong_value))
|
||||
end
|
||||
|
||||
it "rejects when array doesn't match at all" do
|
||||
@model.should_not ensure_inclusion_of(:attr).in_array(%w(cat dog))
|
||||
end
|
||||
|
||||
it "has correct description" do
|
||||
ensure_inclusion_of(:attr).in_array([true, 'dog']).description.should == 'ensure inclusion of attr in [true, "dog"]'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue