From 1e1f93fd10f186a764cdff0e89936c55274171a9 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 24 Jul 2007 01:48:17 +0000 Subject: Added :unless clause to validations (closes #8003) [monki] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7215 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/validations_test.rb | 65 +++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 6 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 105b8edd40..19bca29fd8 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -988,7 +988,7 @@ class ValidationsTest < Test::Unit::TestCase assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic).last end - def test_conditional_validation_using_method_true + def test_if_validation_using_method_true # When the method returns true Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true ) t = Topic.create("title" => "uhohuhoh", "content" => "whatever") @@ -997,7 +997,15 @@ class ValidationsTest < Test::Unit::TestCase assert_equal "hoo 5", t.errors["title"] end - def test_conditional_validation_using_method_false + def test_unless_validation_using_method_true + # When the method returns true + Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => :condition_is_true ) + t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + assert t.valid? + assert !t.errors.on(:title) + end + + def test_if_validation_using_method_false # When the method returns false Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true_but_its_not ) t = Topic.create("title" => "uhohuhoh", "content" => "whatever") @@ -1005,7 +1013,16 @@ class ValidationsTest < Test::Unit::TestCase assert !t.errors.on(:title) end - def test_conditional_validation_using_string_true + def test_unless_validation_using_method_false + # When the method returns false + Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => :condition_is_true_but_its_not ) + t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + assert !t.valid? + assert t.errors.on(:title) + assert_equal "hoo 5", t.errors["title"] + end + + def test_if_validation_using_string_true # When the evaluated string returns true Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "a = 1; a == 1" ) t = Topic.create("title" => "uhohuhoh", "content" => "whatever") @@ -1014,7 +1031,15 @@ class ValidationsTest < Test::Unit::TestCase assert_equal "hoo 5", t.errors["title"] end - def test_conditional_validation_using_string_false + def test_unless_validation_using_string_true + # When the evaluated string returns true + Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => "a = 1; a == 1" ) + t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + assert t.valid? + assert !t.errors.on(:title) + end + + def test_if_validation_using_string_false # When the evaluated string returns false Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "false") t = Topic.create("title" => "uhohuhoh", "content" => "whatever") @@ -1022,7 +1047,16 @@ class ValidationsTest < Test::Unit::TestCase assert !t.errors.on(:title) end - def test_conditional_validation_using_block_true + def test_unless_validation_using_string_false + # When the evaluated string returns false + Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => "false") + t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + assert !t.valid? + assert t.errors.on(:title) + assert_equal "hoo 5", t.errors["title"] + end + + def test_if_validation_using_block_true # When the block returns true Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => Proc.new { |r| r.content.size > 4 } ) @@ -1032,7 +1066,16 @@ class ValidationsTest < Test::Unit::TestCase assert_equal "hoo 5", t.errors["title"] end - def test_conditional_validation_using_block_false + def test_unless_validation_using_block_true + # When the block returns true + Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", + :unless => Proc.new { |r| r.content.size > 4 } ) + t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + assert t.valid? + assert !t.errors.on(:title) + end + + def test_if_validation_using_block_false # When the block returns false Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => Proc.new { |r| r.title != "uhohuhoh"} ) @@ -1041,6 +1084,16 @@ class ValidationsTest < Test::Unit::TestCase assert !t.errors.on(:title) end + def test_unless_validation_using_block_false + # When the block returns false + Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", + :unless => Proc.new { |r| r.title != "uhohuhoh"} ) + t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + assert !t.valid? + assert t.errors.on(:title) + assert_equal "hoo 5", t.errors["title"] + end + def test_validates_associated_missing Reply.validates_presence_of(:topic) r = Reply.create("title" => "A reply", "content" => "with content!") -- cgit v1.2.3