From 58594be680e8712c9e7352f184e15972d02cd4af Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 11 Mar 2011 13:35:23 +0800 Subject: 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 --- .../cases/validations/exclusion_validation_test.rb | 21 ++++++++++++ .../cases/validations/format_validation_test.rb | 40 ++++++++++++++++++++++ .../cases/validations/inclusion_validation_test.rb | 21 ++++++++++++ 3 files changed, 82 insertions(+) (limited to 'activemodel/test') 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 -- cgit v1.2.3