aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/validations
diff options
context:
space:
mode:
authorPrem Sichanugrist <s@sikachu.com>2011-03-11 13:35:23 +0800
committerJosé Valim <jose.valim@gmail.com>2011-04-10 18:49:28 +0800
commit58594be680e8712c9e7352f184e15972d02cd4af (patch)
treeb53b6b145b980d8f4ccc833052a2aa8e87eb7904 /activemodel/test/cases/validations
parent22a3416298523d5e8ecb432a263e4bee9441e6c5 (diff)
downloadrails-58594be680e8712c9e7352f184e15972d02cd4af.tar.gz
rails-58594be680e8712c9e7352f184e15972d02cd4af.tar.bz2
rails-58594be680e8712c9e7352f184e15972d02cd4af.zip
Add support for proc or lambda as an option for InclusionValidator, ExclusionValidator, and FormatValidator
You can now use a proc or lambda in :in option for InclusionValidator and ExclusionValidator, and :with, :without option for FormatValidator
Diffstat (limited to 'activemodel/test/cases/validations')
-rw-r--r--activemodel/test/cases/validations/exclusion_validation_test.rb21
-rw-r--r--activemodel/test/cases/validations/format_validation_test.rb40
-rw-r--r--activemodel/test/cases/validations/inclusion_validation_test.rb21
3 files changed, 82 insertions, 0 deletions
diff --git a/activemodel/test/cases/validations/exclusion_validation_test.rb b/activemodel/test/cases/validations/exclusion_validation_test.rb
index be9d98d644..fe0d13ec33 100644
--- a/activemodel/test/cases/validations/exclusion_validation_test.rb
+++ b/activemodel/test/cases/validations/exclusion_validation_test.rb
@@ -42,4 +42,25 @@ class ExclusionValidationTest < ActiveModel::TestCase
ensure
Person.reset_callbacks(:validate)
end
+
+ def test_validates_exclusion_of_with_lambda
+ Topic.validates_exclusion_of :title, :in => lambda{ |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) }
+
+ p = Topic.new
+ p.title = "elephant"
+ p.author_name = "sikachu"
+ assert p.invalid?
+
+ p.title = "wasabi"
+ assert p.valid?
+ end
+
+ def test_validates_exclustion_with_invalid_lambda_return
+ Topic.validates_exclusion_of :title, :in => lambda{ |topic| false }
+
+ p = Topic.new
+ p.title = "wasabi"
+ p.author_name = "sikachu"
+ assert_raise(ArgumentError){ p.valid? }
+ end
end
diff --git a/activemodel/test/cases/validations/format_validation_test.rb b/activemodel/test/cases/validations/format_validation_test.rb
index 6c4fb36d52..1ff29134ee 100644
--- a/activemodel/test/cases/validations/format_validation_test.rb
+++ b/activemodel/test/cases/validations/format_validation_test.rb
@@ -98,6 +98,46 @@ class PresenceValidationTest < ActiveModel::TestCase
assert_raise(ArgumentError) { Topic.validates_format_of(:title, :without => "clearly not a regexp") }
end
+ def test_validates_format_of_with_lambda
+ Topic.validates_format_of :content, :with => lambda{ |topic| topic.title == "digit" ? /\A\d+\Z/ : /\A\S+\Z/ }
+
+ p = Topic.new
+ p.title = "digit"
+ p.content = "Pixies"
+ assert p.invalid?
+
+ p.content = "1234"
+ assert p.valid?
+ end
+
+ def test_validates_format_of_without_lambda
+ Topic.validates_format_of :content, :without => lambda{ |topic| topic.title == "characters" ? /\A\d+\Z/ : /\A\S+\Z/ }
+
+ p = Topic.new
+ p.title = "characters"
+ p.content = "1234"
+ assert p.invalid?
+
+ p.content = "Pixies"
+ assert p.valid?
+ end
+
+ def test_validates_format_of_with_lambda_not_returns_regexp
+ Topic.validates_format_of :content, :with => lambda{ |topic| false }
+
+ p = Topic.new
+ p.content = "Pixies"
+ assert_raise(ArgumentError){ p.valid? }
+ end
+
+ def test_validates_format_of_without_lambda_not_returns_regexp
+ Topic.validates_format_of :content, :without => lambda{ |topic| false }
+
+ p = Topic.new
+ p.content = "Pixies"
+ assert_raise(ArgumentError){ p.valid? }
+ end
+
def test_validates_format_of_for_ruby_class
Person.validates_format_of :karma, :with => /\A\d+\Z/
diff --git a/activemodel/test/cases/validations/inclusion_validation_test.rb b/activemodel/test/cases/validations/inclusion_validation_test.rb
index 62f2ec785d..e6adb6d187 100644
--- a/activemodel/test/cases/validations/inclusion_validation_test.rb
+++ b/activemodel/test/cases/validations/inclusion_validation_test.rb
@@ -74,4 +74,25 @@ class InclusionValidationTest < ActiveModel::TestCase
ensure
Person.reset_callbacks(:validate)
end
+
+ def test_validates_inclusion_of_with_lambda
+ Topic.validates_inclusion_of :title, :in => lambda{ |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) }
+
+ p = Topic.new
+ p.title = "wasabi"
+ p.author_name = "sikachu"
+ assert p.invalid?
+
+ p.title = "elephant"
+ assert p.valid?
+ end
+
+ def test_validates_inclustion_with_invalid_lambda_return
+ Topic.validates_inclusion_of :title, :in => lambda{ |topic| false }
+
+ p = Topic.new
+ p.title = "monkey"
+ p.author_name = "sikachu"
+ assert_raise(ArgumentError){ p.valid? }
+ end
end