diff options
author | José Valim <jose.valim@gmail.com> | 2010-01-08 08:37:58 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-01-08 21:36:04 +0100 |
commit | 7045c4c279499eb7340fb420398d613497739eef (patch) | |
tree | c62d42b74d68a738a3baa935add403e10305f70d /activemodel/test | |
parent | fa14d6d51ed89acde66b49e8d3a6423396c3d553 (diff) | |
download | rails-7045c4c279499eb7340fb420398d613497739eef.tar.gz rails-7045c4c279499eb7340fb420398d613497739eef.tar.bz2 rails-7045c4c279499eb7340fb420398d613497739eef.zip |
Allow validates to map some types to specific options. So now you can do:
validates :email, :presence => true, :format => /@/
validates :genre, :inclusion => %w(m f)
validates :password, :length => 6..20
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/validations/validates_test.rb | 48 | ||||
-rw-r--r-- | activemodel/test/models/person.rb | 2 |
2 files changed, 38 insertions, 12 deletions
diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index 181ff38b64..966690d75f 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -10,43 +10,42 @@ class ValidatesTest < ActiveModel::TestCase def reset_callbacks Person.reset_callbacks(:validate) + PersonWithValidator.reset_callbacks(:validate) end def test_validates_with_built_in_validation Person.validates :title, :numericality => true person = Person.new person.valid? - assert person.errors[:title].include?('is not a number') + assert_equal ['is not a number'], person.errors[:title] end def test_validates_with_built_in_validation_and_options - Person.validates :title, :numericality => { :message => 'my custom message' } + Person.validates :salary, :numericality => { :message => 'my custom message' } person = Person.new person.valid? - assert person.errors[:title].include?('my custom message') + assert_equal ['my custom message'], person.errors[:salary] end def test_validates_with_validator_class Person.validates :karma, :email => true person = Person.new person.valid? - assert person.errors[:karma].include?('is not an email') + assert_equal ['is not an email'], person.errors[:karma] end def test_validates_with_if_as_local_conditions Person.validates :karma, :presence => true, :email => { :unless => :condition_is_true } person = Person.new person.valid? - assert !person.errors[:karma].include?('is not an email') - assert person.errors[:karma].include?('can\'t be blank') + assert_equal ["can't be blank"], person.errors[:karma] end def test_validates_with_if_as_shared_conditions Person.validates :karma, :presence => true, :email => true, :if => :condition_is_true person = Person.new person.valid? - assert person.errors[:karma].include?('is not an email') - assert person.errors[:karma].include?('can\'t be blank') + assert ["can't be blank", "is not an email"], person.errors[:karma].sort end def test_validates_with_unless_shared_conditions @@ -61,11 +60,38 @@ class ValidatesTest < ActiveModel::TestCase assert person.valid? end + def test_validates_with_regexp + Person.validates :karma, :format => /positive|negative/ + person = Person.new + assert person.invalid? + assert_equal ['is invalid'], person.errors[:karma] + person.karma = "positive" + assert person.valid? + end + + def test_validates_with_array + Person.validates :genre, :inclusion => %w(m f) + person = Person.new + assert person.invalid? + assert_equal ['is not included in the list'], person.errors[:genre] + person.genre = "m" + assert person.valid? + end + + def test_validates_with_range + Person.validates :password, :length => 6..20 + person = Person.new + assert person.invalid? + assert_equal ['is too short (minimum is 6 characters)'], person.errors[:password] + person.password = '123456' + assert person.valid? + end + def test_validates_with_validator_class_and_options Person.validates :karma, :email => { :message => 'my custom message' } person = Person.new person.valid? - assert person.errors[:karma].include?('my custom message') + assert_equal ['my custom message'], person.errors[:karma] end def test_validates_with_unknown_validator @@ -76,13 +102,13 @@ class ValidatesTest < ActiveModel::TestCase PersonWithValidator.validates :title, :presence => true person = PersonWithValidator.new person.valid? - assert person.errors[:title].include?('Local validator') + assert_equal ['Local validator'], person.errors[:title] end def test_validates_with_included_validator_and_options PersonWithValidator.validates :title, :presence => { :custom => ' please' } person = PersonWithValidator.new person.valid? - assert person.errors[:title].include?('Local validator please') + assert_equal ['Local validator please'], person.errors[:title] end end
\ No newline at end of file diff --git a/activemodel/test/models/person.rb b/activemodel/test/models/person.rb index 53ac68055f..86e8d73bbb 100644 --- a/activemodel/test/models/person.rb +++ b/activemodel/test/models/person.rb @@ -2,7 +2,7 @@ class Person include ActiveModel::Validations extend ActiveModel::Translation - attr_accessor :title, :karma, :salary + attr_accessor :title, :karma, :salary, :genre, :password def condition_is_true true |