diff options
Diffstat (limited to 'activemodel/test/cases/validations')
12 files changed, 201 insertions, 202 deletions
diff --git a/activemodel/test/cases/validations/acceptance_validation_test.rb b/activemodel/test/cases/validations/acceptance_validation_test.rb index de04e11258..dc413bef30 100644 --- a/activemodel/test/cases/validations/acceptance_validation_test.rb +++ b/activemodel/test/cases/validations/acceptance_validation_test.rb @@ -30,7 +30,7 @@ class AcceptanceValidationTest < ActiveModel::TestCase end def test_eula - Topic.validates_acceptance_of(:eula, :message => "must be abided") + Topic.validates_acceptance_of(:eula, message: "must be abided") t = Topic.new("title" => "We should be confirmed","eula" => "") assert t.invalid? @@ -41,7 +41,7 @@ class AcceptanceValidationTest < ActiveModel::TestCase end def test_terms_of_service_agreement_with_accept_value - Topic.validates_acceptance_of(:terms_of_service, :accept => "I agree.") + Topic.validates_acceptance_of(:terms_of_service, accept: "I agree.") t = Topic.new("title" => "We should be confirmed", "terms_of_service" => "") assert t.invalid? diff --git a/activemodel/test/cases/validations/conditional_validation_test.rb b/activemodel/test/cases/validations/conditional_validation_test.rb index e06b04af19..41a4c33727 100644 --- a/activemodel/test/cases/validations/conditional_validation_test.rb +++ b/activemodel/test/cases/validations/conditional_validation_test.rb @@ -11,7 +11,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_if_validation_using_method_true # When the method returns true - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", :if => :condition_is_true ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -20,7 +20,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_method_true # When the method returns true - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", :unless => :condition_is_true ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: :condition_is_true) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? @@ -28,7 +28,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_if_validation_using_method_false # When the method returns false - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", :if => :condition_is_true_but_its_not ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true_but_its_not) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? @@ -36,7 +36,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_method_false # When the method returns false - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", :unless => :condition_is_true_but_its_not ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: :condition_is_true_but_its_not) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -45,7 +45,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_if_validation_using_string_true # When the evaluated string returns true - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", :if => "a = 1; a == 1" ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: "a = 1; a == 1") t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -54,7 +54,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_string_true # When the evaluated string returns true - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", :unless => "a = 1; a == 1" ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: "a = 1; a == 1") t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? @@ -62,7 +62,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_if_validation_using_string_false # When the evaluated string returns false - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", :if => "false") + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: "false") t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? @@ -70,7 +70,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_string_false # When the evaluated string returns false - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", :unless => "false") + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: "false") t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -79,8 +79,8 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_if_validation_using_block_true # When the block returns true - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", - :if => Proc.new { |r| r.content.size > 4 } ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", + if: Proc.new { |r| r.content.size > 4 }) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -89,8 +89,8 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_block_true # When the block returns true - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", - :unless => Proc.new { |r| r.content.size > 4 } ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", + unless: Proc.new { |r| r.content.size > 4 }) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? @@ -98,8 +98,8 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_if_validation_using_block_false # When the block returns false - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", - :if => Proc.new { |r| r.title != "uhohuhoh"} ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", + if: Proc.new { |r| r.title != "uhohuhoh"}) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? @@ -107,8 +107,8 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_block_false # When the block returns false - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}", - :unless => Proc.new { |r| r.title != "uhohuhoh"} ) + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", + unless: Proc.new { |r| r.title != "uhohuhoh"} ) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -120,7 +120,7 @@ class ConditionalValidationTest < ActiveModel::TestCase # ensure that it works correctly def test_validation_with_if_as_string Topic.validates_presence_of(:title) - Topic.validates_presence_of(:author_name, :if => "title.to_s.match('important')") + Topic.validates_presence_of(:author_name, if: "title.to_s.match('important')") t = Topic.new assert t.invalid?, "A topic without a title should not be valid" diff --git a/activemodel/test/cases/validations/confirmation_validation_test.rb b/activemodel/test/cases/validations/confirmation_validation_test.rb index 814eec3f59..f03de2c24a 100644 --- a/activemodel/test/cases/validations/confirmation_validation_test.rb +++ b/activemodel/test/cases/validations/confirmation_validation_test.rb @@ -13,7 +13,7 @@ class ConfirmationValidationTest < ActiveModel::TestCase def test_no_title_confirmation Topic.validates_confirmation_of(:title) - t = Topic.new(:author_name => "Plutarch") + t = Topic.new(author_name: "Plutarch") assert t.valid? t.title_confirmation = "Parallel Lives" @@ -57,8 +57,8 @@ class ConfirmationValidationTest < ActiveModel::TestCase I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new I18n.backend.store_translations('en', { - :errors => {:messages => {:confirmation => "doesn't match %{attribute}"}}, - :activemodel => {:attributes => {:topic => {:title => 'Test Title'}}} + errors: { messages: { confirmation: "doesn't match %{attribute}" } }, + activemodel: { attributes: { topic: { title: 'Test Title'} } } }) Topic.validates_confirmation_of(:title) diff --git a/activemodel/test/cases/validations/exclusion_validation_test.rb b/activemodel/test/cases/validations/exclusion_validation_test.rb index 7d5af27f3d..81455ba519 100644 --- a/activemodel/test/cases/validations/exclusion_validation_test.rb +++ b/activemodel/test/cases/validations/exclusion_validation_test.rb @@ -11,14 +11,14 @@ class ExclusionValidationTest < ActiveModel::TestCase end def test_validates_exclusion_of - Topic.validates_exclusion_of( :title, :in => %w( abe monkey ) ) + Topic.validates_exclusion_of(:title, in: %w( abe monkey )) assert Topic.new("title" => "something", "content" => "abc").valid? assert Topic.new("title" => "monkey", "content" => "abc").invalid? end def test_validates_exclusion_of_with_formatted_message - Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => "option %{value} is restricted" ) + Topic.validates_exclusion_of(:title, in: %w( abe monkey ), message: "option %{value} is restricted") assert Topic.new("title" => "something", "content" => "abc") @@ -29,7 +29,7 @@ class ExclusionValidationTest < ActiveModel::TestCase end def test_validates_exclusion_of_with_within_option - Topic.validates_exclusion_of( :title, :within => %w( abe monkey ) ) + Topic.validates_exclusion_of(:title, within: %w( abe monkey )) assert Topic.new("title" => "something", "content" => "abc") @@ -39,7 +39,7 @@ class ExclusionValidationTest < ActiveModel::TestCase end def test_validates_exclusion_of_for_ruby_class - Person.validates_exclusion_of :karma, :in => %w( abe monkey ) + Person.validates_exclusion_of :karma, in: %w( abe monkey ) p = Person.new p.karma = "abe" @@ -54,7 +54,7 @@ class ExclusionValidationTest < ActiveModel::TestCase 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 ) } + Topic.validates_exclusion_of :title, in: lambda { |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) } t = Topic.new t.title = "elephant" @@ -66,7 +66,7 @@ class ExclusionValidationTest < ActiveModel::TestCase end def test_validates_inclusion_of_with_symbol - Person.validates_exclusion_of :karma, :in => :reserved_karmas + Person.validates_exclusion_of :karma, in: :reserved_karmas p = Person.new p.karma = "abe" diff --git a/activemodel/test/cases/validations/format_validation_test.rb b/activemodel/test/cases/validations/format_validation_test.rb index 308a3c6cef..26e8dbf19c 100644 --- a/activemodel/test/cases/validations/format_validation_test.rb +++ b/activemodel/test/cases/validations/format_validation_test.rb @@ -11,7 +11,7 @@ class PresenceValidationTest < ActiveModel::TestCase end def test_validate_format - Topic.validates_format_of(:title, :content, :with => /\AValidation\smacros \w+!\z/, :message => "is bad data") + Topic.validates_format_of(:title, :content, with: /\AValidation\smacros \w+!\z/, message: "is bad data") t = Topic.new("title" => "i'm incorrect", "content" => "Validation macros rule!") assert t.invalid?, "Shouldn't be valid" @@ -27,7 +27,7 @@ class PresenceValidationTest < ActiveModel::TestCase end def test_validate_format_with_allow_blank - Topic.validates_format_of(:title, :with => /\AValidation\smacros \w+!\z/, :allow_blank => true) + Topic.validates_format_of(:title, with: /\AValidation\smacros \w+!\z/, allow_blank: true) assert Topic.new("title" => "Shouldn't be valid").invalid? assert Topic.new("title" => "").valid? assert Topic.new("title" => nil).valid? @@ -36,7 +36,7 @@ class PresenceValidationTest < ActiveModel::TestCase # testing ticket #3142 def test_validate_format_numeric - Topic.validates_format_of(:title, :content, :with => /\A[1-9][0-9]*\z/, :message => "is bad data") + Topic.validates_format_of(:title, :content, with: /\A[1-9][0-9]*\z/, message: "is bad data") t = Topic.new("title" => "72x", "content" => "6789") assert t.invalid?, "Shouldn't be valid" @@ -63,24 +63,24 @@ class PresenceValidationTest < ActiveModel::TestCase end def test_validate_format_with_formatted_message - Topic.validates_format_of(:title, :with => /\AValid Title\z/, :message => "can't be %{value}") - t = Topic.new(:title => 'Invalid title') + Topic.validates_format_of(:title, with: /\AValid Title\z/, message: "can't be %{value}") + t = Topic.new(title: 'Invalid title') assert t.invalid? assert_equal ["can't be Invalid title"], t.errors[:title] end def test_validate_format_of_with_multiline_regexp_should_raise_error - assert_raise(ArgumentError) { Topic.validates_format_of(:title, :with => /^Valid Title$/) } + assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: /^Valid Title$/) } end def test_validate_format_of_with_multiline_regexp_and_option assert_nothing_raised(ArgumentError) do - Topic.validates_format_of(:title, :with => /^Valid Title$/, :multiline => true) + Topic.validates_format_of(:title, with: /^Valid Title$/, multiline: true) end end def test_validate_format_with_not_option - Topic.validates_format_of(:title, :without => /foo/, :message => "should not contain foo") + Topic.validates_format_of(:title, without: /foo/, message: "should not contain foo") t = Topic.new t.title = "foobar" @@ -97,19 +97,19 @@ class PresenceValidationTest < ActiveModel::TestCase end def test_validates_format_of_with_both_regexps_should_raise_error - assert_raise(ArgumentError) { Topic.validates_format_of(:title, :with => /this/, :without => /that/) } + assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: /this/, without: /that/) } end def test_validates_format_of_when_with_isnt_a_regexp_should_raise_error - assert_raise(ArgumentError) { Topic.validates_format_of(:title, :with => "clearly not a regexp") } + assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: "clearly not a regexp") } end def test_validates_format_of_when_not_isnt_a_regexp_should_raise_error - assert_raise(ArgumentError) { Topic.validates_format_of(:title, :without => "clearly not a regexp") } + 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/ } + Topic.validates_format_of :content, with: lambda { |topic| topic.title == "digit" ? /\A\d+\Z/ : /\A\S+\Z/ } t = Topic.new t.title = "digit" @@ -121,7 +121,7 @@ class PresenceValidationTest < ActiveModel::TestCase 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/ } + Topic.validates_format_of :content, without: lambda { |topic| topic.title == "characters" ? /\A\d+\Z/ : /\A\S+\Z/ } t = Topic.new t.title = "characters" @@ -133,7 +133,7 @@ class PresenceValidationTest < ActiveModel::TestCase end def test_validates_format_of_for_ruby_class - Person.validates_format_of :karma, :with => /\A\d+\Z/ + Person.validates_format_of :karma, with: /\A\d+\Z/ p = Person.new p.karma = "Pixies" diff --git a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb index 302cbe9761..40a5aee997 100644 --- a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb @@ -10,29 +10,29 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase # validates_inclusion_of: generate_message(attr_name, :inclusion, message: custom_message, value: value) def test_generate_message_inclusion_with_default_message - assert_equal 'is not included in the list', @person.errors.generate_message(:title, :inclusion, :value => 'title') + assert_equal 'is not included in the list', @person.errors.generate_message(:title, :inclusion, value: 'title') end def test_generate_message_inclusion_with_custom_message - assert_equal 'custom message title', @person.errors.generate_message(:title, :inclusion, :message => 'custom message %{value}', :value => 'title') + assert_equal 'custom message title', @person.errors.generate_message(:title, :inclusion, message: 'custom message %{value}', value: 'title') end # validates_exclusion_of: generate_message(attr_name, :exclusion, message: custom_message, value: value) def test_generate_message_exclusion_with_default_message - assert_equal 'is reserved', @person.errors.generate_message(:title, :exclusion, :value => 'title') + assert_equal 'is reserved', @person.errors.generate_message(:title, :exclusion, value: 'title') end def test_generate_message_exclusion_with_custom_message - assert_equal 'custom message title', @person.errors.generate_message(:title, :exclusion, :message => 'custom message %{value}', :value => 'title') + assert_equal 'custom message title', @person.errors.generate_message(:title, :exclusion, message: 'custom message %{value}', value: 'title') end # validates_format_of: generate_message(attr_name, :invalid, message: custom_message, value: value) def test_generate_message_invalid_with_default_message - assert_equal 'is invalid', @person.errors.generate_message(:title, :invalid, :value => 'title') + assert_equal 'is invalid', @person.errors.generate_message(:title, :invalid, value: 'title') end def test_generate_message_invalid_with_custom_message - assert_equal 'custom message title', @person.errors.generate_message(:title, :invalid, :message => 'custom message %{value}', :value => 'title') + assert_equal 'custom message title', @person.errors.generate_message(:title, :invalid, message: 'custom message %{value}', value: 'title') end # validates_confirmation_of: generate_message(attr_name, :confirmation, message: custom_message) @@ -41,7 +41,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase end def test_generate_message_confirmation_with_custom_message - assert_equal 'custom message', @person.errors.generate_message(:title, :confirmation, :message => 'custom message') + assert_equal 'custom message', @person.errors.generate_message(:title, :confirmation, message: 'custom message') end # validates_acceptance_of: generate_message(attr_name, :accepted, message: custom_message) @@ -50,7 +50,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase end def test_generate_message_accepted_with_custom_message - assert_equal 'custom message', @person.errors.generate_message(:title, :accepted, :message => 'custom message') + assert_equal 'custom message', @person.errors.generate_message(:title, :accepted, message: 'custom message') end # add_on_empty: generate_message(attr, :empty, message: custom_message) @@ -59,7 +59,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase end def test_generate_message_empty_with_custom_message - assert_equal 'custom message', @person.errors.generate_message(:title, :empty, :message => 'custom message') + assert_equal 'custom message', @person.errors.generate_message(:title, :empty, message: 'custom message') end # add_on_blank: generate_message(attr, :blank, message: custom_message) @@ -68,71 +68,71 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase end def test_generate_message_blank_with_custom_message - assert_equal 'custom message', @person.errors.generate_message(:title, :blank, :message => 'custom message') + assert_equal 'custom message', @person.errors.generate_message(:title, :blank, message: 'custom message') end # validates_length_of: generate_message(attr, :too_long, message: custom_message, count: option_value.end) def test_generate_message_too_long_with_default_message - assert_equal "is too long (maximum is 10 characters)", @person.errors.generate_message(:title, :too_long, :count => 10) + assert_equal "is too long (maximum is 10 characters)", @person.errors.generate_message(:title, :too_long, count: 10) end def test_generate_message_too_long_with_custom_message - assert_equal 'custom message 10', @person.errors.generate_message(:title, :too_long, :message => 'custom message %{count}', :count => 10) + assert_equal 'custom message 10', @person.errors.generate_message(:title, :too_long, message: 'custom message %{count}', count: 10) end # validates_length_of: generate_message(attr, :too_short, default: custom_message, count: option_value.begin) def test_generate_message_too_short_with_default_message - assert_equal "is too short (minimum is 10 characters)", @person.errors.generate_message(:title, :too_short, :count => 10) + assert_equal "is too short (minimum is 10 characters)", @person.errors.generate_message(:title, :too_short, count: 10) end def test_generate_message_too_short_with_custom_message - assert_equal 'custom message 10', @person.errors.generate_message(:title, :too_short, :message => 'custom message %{count}', :count => 10) + assert_equal 'custom message 10', @person.errors.generate_message(:title, :too_short, message: 'custom message %{count}', count: 10) end # validates_length_of: generate_message(attr, :wrong_length, message: custom_message, count: option_value) def test_generate_message_wrong_length_with_default_message - assert_equal "is the wrong length (should be 10 characters)", @person.errors.generate_message(:title, :wrong_length, :count => 10) + assert_equal "is the wrong length (should be 10 characters)", @person.errors.generate_message(:title, :wrong_length, count: 10) end def test_generate_message_wrong_length_with_custom_message - assert_equal 'custom message 10', @person.errors.generate_message(:title, :wrong_length, :message => 'custom message %{count}', :count => 10) + assert_equal 'custom message 10', @person.errors.generate_message(:title, :wrong_length, message: 'custom message %{count}', count: 10) end # validates_numericality_of: generate_message(attr_name, :not_a_number, value: raw_value, message: custom_message) def test_generate_message_not_a_number_with_default_message - assert_equal "is not a number", @person.errors.generate_message(:title, :not_a_number, :value => 'title') + assert_equal "is not a number", @person.errors.generate_message(:title, :not_a_number, value: 'title') end def test_generate_message_not_a_number_with_custom_message - assert_equal 'custom message title', @person.errors.generate_message(:title, :not_a_number, :message => 'custom message %{value}', :value => 'title') + assert_equal 'custom message title', @person.errors.generate_message(:title, :not_a_number, message: 'custom message %{value}', value: 'title') end # validates_numericality_of: generate_message(attr_name, option, value: raw_value, default: custom_message) def test_generate_message_greater_than_with_default_message - assert_equal "must be greater than 10", @person.errors.generate_message(:title, :greater_than, :value => 'title', :count => 10) + assert_equal "must be greater than 10", @person.errors.generate_message(:title, :greater_than, value: 'title', count: 10) end def test_generate_message_greater_than_or_equal_to_with_default_message - assert_equal "must be greater than or equal to 10", @person.errors.generate_message(:title, :greater_than_or_equal_to, :value => 'title', :count => 10) + assert_equal "must be greater than or equal to 10", @person.errors.generate_message(:title, :greater_than_or_equal_to, value: 'title', count: 10) end def test_generate_message_equal_to_with_default_message - assert_equal "must be equal to 10", @person.errors.generate_message(:title, :equal_to, :value => 'title', :count => 10) + assert_equal "must be equal to 10", @person.errors.generate_message(:title, :equal_to, value: 'title', count: 10) end def test_generate_message_less_than_with_default_message - assert_equal "must be less than 10", @person.errors.generate_message(:title, :less_than, :value => 'title', :count => 10) + assert_equal "must be less than 10", @person.errors.generate_message(:title, :less_than, value: 'title', count: 10) end def test_generate_message_less_than_or_equal_to_with_default_message - assert_equal "must be less than or equal to 10", @person.errors.generate_message(:title, :less_than_or_equal_to, :value => 'title', :count => 10) + assert_equal "must be less than or equal to 10", @person.errors.generate_message(:title, :less_than_or_equal_to, value: 'title', count: 10) end def test_generate_message_odd_with_default_message - assert_equal "must be odd", @person.errors.generate_message(:title, :odd, :value => 'title', :count => 10) + assert_equal "must be odd", @person.errors.generate_message(:title, :odd, value: 'title', count: 10) end def test_generate_message_even_with_default_message - assert_equal "must be even", @person.errors.generate_message(:title, :even, :value => 'title', :count => 10) + assert_equal "must be even", @person.errors.generate_message(:title, :even, value: 'title', count: 10) end end diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index 4c01b47608..e29771d6b7 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -12,7 +12,7 @@ class I18nValidationTest < ActiveModel::TestCase @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new - I18n.backend.store_translations('en', :errors => {:messages => {:custom => nil}}) + I18n.backend.store_translations('en', errors: { messages: { custom: nil } }) end def teardown @@ -22,21 +22,21 @@ class I18nValidationTest < ActiveModel::TestCase end def test_full_message_encoding - I18n.backend.store_translations('en', :errors => { - :messages => { :too_short => '猫舌' }}) - Person.validates_length_of :title, :within => 3..5 + I18n.backend.store_translations('en', errors: { + messages: { too_short: '猫舌' } }) + Person.validates_length_of :title, within: 3..5 @person.valid? assert_equal ['Title 猫舌'], @person.errors.full_messages end def test_errors_full_messages_translates_human_attribute_name_for_model_attributes @person.errors.add(:name, 'not found') - Person.expects(:human_attribute_name).with(:name, :default => 'Name').returns("Person's name") + Person.expects(:human_attribute_name).with(:name, default: 'Name').returns("Person's name") assert_equal ["Person's name not found"], @person.errors.full_messages end def test_errors_full_messages_uses_format - I18n.backend.store_translations('en', :errors => {:format => "Field %{attribute} %{message}"}) + I18n.backend.store_translations('en', errors: { format: "Field %{attribute} %{message}" }) @person.errors.add('name', 'empty') assert_equal ["Field Name empty"], @person.errors.full_messages end @@ -49,10 +49,10 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES = [ # [ case, validation_options, generate_message_options] [ "given no options", {}, {}], - [ "given custom message", {:message => "custom"}, {:message => "custom"}], - [ "given if condition", {:if => lambda { true }}, {}], - [ "given unless condition", {:unless => lambda { false }}, {}], - [ "given option that is not reserved", {:format => "jpg"}, {:format => "jpg" }] + [ "given custom message", { message: "custom" }, { message: "custom" }], + [ "given if condition", { if: lambda { true }}, {}], + [ "given unless condition", { unless: lambda { false }}, {}], + [ "given option that is not reserved", { format: "jpg" }, { format: "jpg" }] ] # validates_confirmation_of w/ mocha @@ -61,7 +61,7 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_confirmation_of on generated message #{name}" do Person.validates_confirmation_of :title, validation_options @person.title_confirmation = 'foo' - @person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(:attribute => 'Title')) + @person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(attribute: 'Title')) @person.valid? end end @@ -70,7 +70,7 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_acceptance_of on generated message #{name}" do - Person.validates_acceptance_of :title, validation_options.merge(:allow_nil => false) + Person.validates_acceptance_of :title, validation_options.merge(allow_nil: false) @person.errors.expects(:generate_message).with(:title, :accepted, generate_message_options) @person.valid? end @@ -90,8 +90,8 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_length_of for :withing on generated message when too short #{name}" do - Person.validates_length_of :title, validation_options.merge(:within => 3..5) - @person.errors.expects(:generate_message).with(:title, :too_short, generate_message_options.merge(:count => 3)) + Person.validates_length_of :title, validation_options.merge(within: 3..5) + @person.errors.expects(:generate_message).with(:title, :too_short, generate_message_options.merge(count: 3)) @person.valid? end end @@ -100,9 +100,9 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_length_of for :too_long generated message #{name}" do - Person.validates_length_of :title, validation_options.merge(:within => 3..5) + Person.validates_length_of :title, validation_options.merge(within: 3..5) @person.title = 'this title is too long' - @person.errors.expects(:generate_message).with(:title, :too_long, generate_message_options.merge(:count => 5)) + @person.errors.expects(:generate_message).with(:title, :too_long, generate_message_options.merge(count: 5)) @person.valid? end end @@ -111,8 +111,8 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_length_of for :is on generated message #{name}" do - Person.validates_length_of :title, validation_options.merge(:is => 5) - @person.errors.expects(:generate_message).with(:title, :wrong_length, generate_message_options.merge(:count => 5)) + Person.validates_length_of :title, validation_options.merge(is: 5) + @person.errors.expects(:generate_message).with(:title, :wrong_length, generate_message_options.merge(count: 5)) @person.valid? end end @@ -121,9 +121,9 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_format_of on generated message #{name}" do - Person.validates_format_of :title, validation_options.merge(:with => /\A[1-9][0-9]*\z/) + Person.validates_format_of :title, validation_options.merge(with: /\A[1-9][0-9]*\z/) @person.title = '72x' - @person.errors.expects(:generate_message).with(:title, :invalid, generate_message_options.merge(:value => '72x')) + @person.errors.expects(:generate_message).with(:title, :invalid, generate_message_options.merge(value: '72x')) @person.valid? end end @@ -132,9 +132,9 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_inclusion_of on generated message #{name}" do - Person.validates_inclusion_of :title, validation_options.merge(:in => %w(a b c)) + Person.validates_inclusion_of :title, validation_options.merge(in: %w(a b c)) @person.title = 'z' - @person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(:value => 'z')) + @person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(value: 'z')) @person.valid? end end @@ -143,9 +143,9 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_inclusion_of using :within on generated message #{name}" do - Person.validates_inclusion_of :title, validation_options.merge(:within => %w(a b c)) + Person.validates_inclusion_of :title, validation_options.merge(within: %w(a b c)) @person.title = 'z' - @person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(:value => 'z')) + @person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(value: 'z')) @person.valid? end end @@ -154,9 +154,9 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_exclusion_of generated message #{name}" do - Person.validates_exclusion_of :title, validation_options.merge(:in => %w(a b c)) + Person.validates_exclusion_of :title, validation_options.merge(in: %w(a b c)) @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(:value => 'a')) + @person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(value: 'a')) @person.valid? end end @@ -165,9 +165,9 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_exclusion_of using :within generated message #{name}" do - Person.validates_exclusion_of :title, validation_options.merge(:within => %w(a b c)) + Person.validates_exclusion_of :title, validation_options.merge(within: %w(a b c)) @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(:value => 'a')) + @person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(value: 'a')) @person.valid? end end @@ -178,7 +178,7 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_numericality_of generated message #{name}" do Person.validates_numericality_of :title, validation_options @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :not_a_number, generate_message_options.merge(:value => 'a')) + @person.errors.expects(:generate_message).with(:title, :not_a_number, generate_message_options.merge(value: 'a')) @person.valid? end end @@ -187,9 +187,9 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_numericality_of for :only_integer on generated message #{name}" do - Person.validates_numericality_of :title, validation_options.merge(:only_integer => true) + Person.validates_numericality_of :title, validation_options.merge(only_integer: true) @person.title = '0.0' - @person.errors.expects(:generate_message).with(:title, :not_an_integer, generate_message_options.merge(:value => '0.0')) + @person.errors.expects(:generate_message).with(:title, :not_an_integer, generate_message_options.merge(value: '0.0')) @person.valid? end end @@ -198,9 +198,9 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_numericality_of for :odd on generated message #{name}" do - Person.validates_numericality_of :title, validation_options.merge(:only_integer => true, :odd => true) + Person.validates_numericality_of :title, validation_options.merge(only_integer: true, odd: true) @person.title = 0 - @person.errors.expects(:generate_message).with(:title, :odd, generate_message_options.merge(:value => 0)) + @person.errors.expects(:generate_message).with(:title, :odd, generate_message_options.merge(value: 0)) @person.valid? end end @@ -209,9 +209,9 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_numericality_of for :less_than on generated message #{name}" do - Person.validates_numericality_of :title, validation_options.merge(:only_integer => true, :less_than => 0) + Person.validates_numericality_of :title, validation_options.merge(only_integer: true, less_than: 0) @person.title = 1 - @person.errors.expects(:generate_message).with(:title, :less_than, generate_message_options.merge(:value => 1, :count => 0)) + @person.errors.expects(:generate_message).with(:title, :less_than, generate_message_options.merge(value: 1, count: 0)) @person.valid? end end @@ -226,8 +226,8 @@ class I18nValidationTest < ActiveModel::TestCase end # test "validates_confirmation_of finds custom model key translation when blank" test "#{validation} finds custom model key translation when #{error_type}" do - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {attribute => {error_type => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :errors => {:messages => {error_type => 'global message'}} + I18n.backend.store_translations 'en', activemodel: { errors: { models: { person: { attributes: { attribute => { error_type => 'custom message' } } } } } } + I18n.backend.store_translations 'en', errors: { messages: { error_type => 'global message'}} yield(@person, {}) @person.valid? @@ -236,17 +236,17 @@ class I18nValidationTest < ActiveModel::TestCase # test "validates_confirmation_of finds custom model key translation with interpolation when blank" test "#{validation} finds custom model key translation with interpolation when #{error_type}" do - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {attribute => {error_type => 'custom message with %{extra}'}}}}}} - I18n.backend.store_translations 'en', :errors => {:messages => {error_type => 'global message'}} + I18n.backend.store_translations 'en', activemodel: { errors: { models: { person: { attributes: { attribute => { error_type => 'custom message with %{extra}' } } } } } } + I18n.backend.store_translations 'en', errors: { messages: {error_type => 'global message'} } - yield(@person, {:extra => "extra information"}) + yield(@person, { extra: "extra information" }) @person.valid? assert_equal ['custom message with extra information'], @person.errors[attribute] end # test "validates_confirmation_of finds global default key translation when blank" test "#{validation} finds global default key translation when #{error_type}" do - I18n.backend.store_translations 'en', :errors => {:messages => {error_type => 'global message'}} + I18n.backend.store_translations 'en', errors: { messages: {error_type => 'global message'} } yield(@person, {}) @person.valid? @@ -264,7 +264,7 @@ class I18nValidationTest < ActiveModel::TestCase # validates_acceptance_of w/o mocha set_expectations_for_validation "validates_acceptance_of", :accepted do |person, options_to_merge| - Person.validates_acceptance_of :title, options_to_merge.merge(:allow_nil => false) + Person.validates_acceptance_of :title, options_to_merge.merge(allow_nil: false) end # validates_presence_of w/o mocha @@ -276,36 +276,36 @@ class I18nValidationTest < ActiveModel::TestCase # validates_length_of :within w/o mocha set_expectations_for_validation "validates_length_of", :too_short do |person, options_to_merge| - Person.validates_length_of :title, options_to_merge.merge(:within => 3..5) + Person.validates_length_of :title, options_to_merge.merge(within: 3..5) end set_expectations_for_validation "validates_length_of", :too_long do |person, options_to_merge| - Person.validates_length_of :title, options_to_merge.merge(:within => 3..5) + Person.validates_length_of :title, options_to_merge.merge(within: 3..5) person.title = "too long" end # validates_length_of :is w/o mocha set_expectations_for_validation "validates_length_of", :wrong_length do |person, options_to_merge| - Person.validates_length_of :title, options_to_merge.merge(:is => 5) + Person.validates_length_of :title, options_to_merge.merge(is: 5) end # validates_format_of w/o mocha set_expectations_for_validation "validates_format_of", :invalid do |person, options_to_merge| - Person.validates_format_of :title, options_to_merge.merge(:with => /\A[1-9][0-9]*\z/) + Person.validates_format_of :title, options_to_merge.merge(with: /\A[1-9][0-9]*\z/) end # validates_inclusion_of w/o mocha set_expectations_for_validation "validates_inclusion_of", :inclusion do |person, options_to_merge| - Person.validates_inclusion_of :title, options_to_merge.merge(:in => %w(a b c)) + Person.validates_inclusion_of :title, options_to_merge.merge(in: %w(a b c)) end # validates_exclusion_of w/o mocha set_expectations_for_validation "validates_exclusion_of", :exclusion do |person, options_to_merge| - Person.validates_exclusion_of :title, options_to_merge.merge(:in => %w(a b c)) + Person.validates_exclusion_of :title, options_to_merge.merge(in: %w(a b c)) person.title = 'a' end @@ -319,55 +319,54 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of with :only_integer w/o mocha set_expectations_for_validation "validates_numericality_of", :not_an_integer do |person, options_to_merge| - Person.validates_numericality_of :title, options_to_merge.merge(:only_integer => true) + Person.validates_numericality_of :title, options_to_merge.merge(only_integer: true) person.title = '1.0' end # validates_numericality_of :odd w/o mocha set_expectations_for_validation "validates_numericality_of", :odd do |person, options_to_merge| - Person.validates_numericality_of :title, options_to_merge.merge(:only_integer => true, :odd => true) + Person.validates_numericality_of :title, options_to_merge.merge(only_integer: true, odd: true) person.title = 0 end # validates_numericality_of :less_than w/o mocha set_expectations_for_validation "validates_numericality_of", :less_than do |person, options_to_merge| - Person.validates_numericality_of :title, options_to_merge.merge(:only_integer => true, :less_than => 0) + Person.validates_numericality_of :title, options_to_merge.merge(only_integer: true, less_than: 0) person.title = 1 end # test with validates_with def test_validations_with_message_symbol_must_translate - I18n.backend.store_translations 'en', :errors => {:messages => {:custom_error => "I am a custom error"}} - Person.validates_presence_of :title, :message => :custom_error + I18n.backend.store_translations 'en', errors: { messages: { custom_error: "I am a custom error" } } + Person.validates_presence_of :title, message: :custom_error @person.title = nil @person.valid? assert_equal ["I am a custom error"], @person.errors[:title] end def test_validates_with_message_symbol_must_translate_per_attribute - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}} - Person.validates_presence_of :title, :message => :custom_error + I18n.backend.store_translations 'en', activemodel: { errors: { models: { person: { attributes: { title: { custom_error: "I am a custom error" } } } } } } + Person.validates_presence_of :title, message: :custom_error @person.title = nil @person.valid? assert_equal ["I am a custom error"], @person.errors[:title] end def test_validates_with_message_symbol_must_translate_per_model - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:custom_error => "I am a custom error"}}}} - Person.validates_presence_of :title, :message => :custom_error + I18n.backend.store_translations 'en', activemodel: { errors: { models: { person: { custom_error: "I am a custom error" } } } } + Person.validates_presence_of :title, message: :custom_error @person.title = nil @person.valid? assert_equal ["I am a custom error"], @person.errors[:title] end def test_validates_with_message_string - Person.validates_presence_of :title, :message => "I am a custom error" + Person.validates_presence_of :title, message: "I am a custom error" @person.title = nil @person.valid? assert_equal ["I am a custom error"], @person.errors[:title] end - end diff --git a/activemodel/test/cases/validations/inclusion_validation_test.rb b/activemodel/test/cases/validations/inclusion_validation_test.rb index 117e9109fc..ceec9dc256 100644 --- a/activemodel/test/cases/validations/inclusion_validation_test.rb +++ b/activemodel/test/cases/validations/inclusion_validation_test.rb @@ -11,7 +11,7 @@ class InclusionValidationTest < ActiveModel::TestCase end def test_validates_inclusion_of_range - Topic.validates_inclusion_of( :title, :in => 'aaa'..'bbb' ) + Topic.validates_inclusion_of(:title, in: 'aaa'..'bbb') assert Topic.new("title" => "bbc", "content" => "abc").invalid? assert Topic.new("title" => "aa", "content" => "abc").invalid? assert Topic.new("title" => "aaa", "content" => "abc").valid? @@ -20,7 +20,7 @@ class InclusionValidationTest < ActiveModel::TestCase end def test_validates_inclusion_of - Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) ) + Topic.validates_inclusion_of(:title, in: %w( a b c d e f g )) assert Topic.new("title" => "a!", "content" => "abc").invalid? assert Topic.new("title" => "a b", "content" => "abc").invalid? @@ -33,16 +33,16 @@ class InclusionValidationTest < ActiveModel::TestCase assert t.errors[:title].any? assert_equal ["is not included in the list"], t.errors[:title] - assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => nil ) } - assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => 0) } + assert_raise(ArgumentError) { Topic.validates_inclusion_of(:title, in: nil) } + assert_raise(ArgumentError) { Topic.validates_inclusion_of(:title, in: 0) } - assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => "hi!" ) } - assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => {} ) } - assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => [] ) } + assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of(:title, in: "hi!") } + assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of(:title, in: {}) } + assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of(:title, in: []) } end def test_validates_inclusion_of_with_allow_nil - Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :allow_nil => true ) + Topic.validates_inclusion_of(:title, in: %w( a b c d e f g ), allow_nil: true) assert Topic.new("title" => "a!", "content" => "abc").invalid? assert Topic.new("title" => "", "content" => "abc").invalid? @@ -50,7 +50,7 @@ class InclusionValidationTest < ActiveModel::TestCase end def test_validates_inclusion_of_with_formatted_message - Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => "option %{value} is not in the list" ) + Topic.validates_inclusion_of(:title, in: %w( a b c d e f g ), message: "option %{value} is not in the list") assert Topic.new("title" => "a", "content" => "abc").valid? @@ -61,7 +61,7 @@ class InclusionValidationTest < ActiveModel::TestCase end def test_validates_inclusion_of_with_within_option - Topic.validates_inclusion_of( :title, :within => %w( a b c d e f g ) ) + Topic.validates_inclusion_of(:title, within: %w( a b c d e f g )) assert Topic.new("title" => "a", "content" => "abc").valid? @@ -71,7 +71,7 @@ class InclusionValidationTest < ActiveModel::TestCase end def test_validates_inclusion_of_for_ruby_class - Person.validates_inclusion_of :karma, :in => %w( abe monkey ) + Person.validates_inclusion_of :karma, in: %w( abe monkey ) p = Person.new p.karma = "Lifo" @@ -86,7 +86,7 @@ class InclusionValidationTest < ActiveModel::TestCase 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 ) } + Topic.validates_inclusion_of :title, in: lambda{ |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) } t = Topic.new t.title = "wasabi" @@ -98,7 +98,7 @@ class InclusionValidationTest < ActiveModel::TestCase end def test_validates_inclusion_of_with_symbol - Person.validates_inclusion_of :karma, :in => :available_karmas + Person.validates_inclusion_of :karma, in: :available_karmas p = Person.new p.karma = "Lifo" diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index 6742a4bab0..84332ed014 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -30,84 +30,84 @@ class NumericalityValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_with_nil_allowed - Topic.validates_numericality_of :approved, :allow_nil => true + Topic.validates_numericality_of :approved, allow_nil: true invalid!(JUNK + BLANK) valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY) end def test_validates_numericality_of_with_integer_only - Topic.validates_numericality_of :approved, :only_integer => true + Topic.validates_numericality_of :approved, only_integer: true invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY) valid!(INTEGERS) end def test_validates_numericality_of_with_integer_only_and_nil_allowed - Topic.validates_numericality_of :approved, :only_integer => true, :allow_nil => true + Topic.validates_numericality_of :approved, only_integer: true, allow_nil: true invalid!(JUNK + BLANK + FLOATS + BIGDECIMAL + INFINITY) valid!(NIL + INTEGERS) end def test_validates_numericality_with_greater_than - Topic.validates_numericality_of :approved, :greater_than => 10 + Topic.validates_numericality_of :approved, greater_than: 10 invalid!([-10, 10], 'must be greater than 10') valid!([11]) end def test_validates_numericality_with_greater_than_or_equal - Topic.validates_numericality_of :approved, :greater_than_or_equal_to => 10 + Topic.validates_numericality_of :approved, greater_than_or_equal_to: 10 invalid!([-9, 9], 'must be greater than or equal to 10') valid!([10]) end def test_validates_numericality_with_equal_to - Topic.validates_numericality_of :approved, :equal_to => 10 + Topic.validates_numericality_of :approved, equal_to: 10 invalid!([-10, 11] + INFINITY, 'must be equal to 10') valid!([10]) end def test_validates_numericality_with_less_than - Topic.validates_numericality_of :approved, :less_than => 10 + Topic.validates_numericality_of :approved, less_than: 10 invalid!([10], 'must be less than 10') valid!([-9, 9]) end def test_validates_numericality_with_less_than_or_equal_to - Topic.validates_numericality_of :approved, :less_than_or_equal_to => 10 + Topic.validates_numericality_of :approved, less_than_or_equal_to: 10 invalid!([11], 'must be less than or equal to 10') valid!([-10, 10]) end def test_validates_numericality_with_odd - Topic.validates_numericality_of :approved, :odd => true + Topic.validates_numericality_of :approved, odd: true invalid!([-2, 2], 'must be odd') valid!([-1, 1]) end def test_validates_numericality_with_even - Topic.validates_numericality_of :approved, :even => true + Topic.validates_numericality_of :approved, even: true invalid!([-1, 1], 'must be even') valid!([-2, 2]) end def test_validates_numericality_with_greater_than_less_than_and_even - Topic.validates_numericality_of :approved, :greater_than => 1, :less_than => 4, :even => true + Topic.validates_numericality_of :approved, greater_than: 1, less_than: 4, even: true invalid!([1, 3, 4]) valid!([2]) end def test_validates_numericality_with_other_than - Topic.validates_numericality_of :approved, :other_than => 0 + Topic.validates_numericality_of :approved, other_than: 0 invalid!([0, 0.0]) valid!([-1, 42]) @@ -115,7 +115,7 @@ class NumericalityValidationTest < ActiveModel::TestCase def test_validates_numericality_with_proc Topic.send(:define_method, :min_approved, lambda { 5 }) - Topic.validates_numericality_of :approved, :greater_than_or_equal_to => Proc.new {|topic| topic.min_approved } + Topic.validates_numericality_of :approved, greater_than_or_equal_to: Proc.new {|topic| topic.min_approved } invalid!([3, 4]) valid!([5, 6]) @@ -124,7 +124,7 @@ class NumericalityValidationTest < ActiveModel::TestCase def test_validates_numericality_with_symbol Topic.send(:define_method, :max_approved, lambda { 5 }) - Topic.validates_numericality_of :approved, :less_than_or_equal_to => :max_approved + Topic.validates_numericality_of :approved, less_than_or_equal_to: :max_approved invalid!([6]) valid!([4, 5]) @@ -132,13 +132,13 @@ class NumericalityValidationTest < ActiveModel::TestCase end def test_validates_numericality_with_numeric_message - Topic.validates_numericality_of :approved, :less_than => 4, :message => "smaller than %{count}" + Topic.validates_numericality_of :approved, less_than: 4, message: "smaller than %{count}" topic = Topic.new("title" => "numeric test", "approved" => 10) assert !topic.valid? assert_equal ["smaller than 4"], topic.errors[:approved] - Topic.validates_numericality_of :approved, :greater_than => 4, :message => "greater than %{count}" + Topic.validates_numericality_of :approved, greater_than: 4, message: "greater than %{count}" topic = Topic.new("title" => "numeric test", "approved" => 1) assert !topic.valid? @@ -146,7 +146,7 @@ class NumericalityValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_for_ruby_class - Person.validates_numericality_of :karma, :allow_nil => false + Person.validates_numericality_of :karma, allow_nil: false p = Person.new p.karma = "Pix" @@ -161,11 +161,11 @@ class NumericalityValidationTest < ActiveModel::TestCase end def test_validates_numericality_with_invalid_args - assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :greater_than_or_equal_to => "foo" } - assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :less_than_or_equal_to => "foo" } - assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :greater_than => "foo" } - assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :less_than => "foo" } - assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :equal_to => "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, greater_than_or_equal_to: "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, less_than_or_equal_to: "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, greater_than: "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, less_than: "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, equal_to: "foo" } end private @@ -185,7 +185,7 @@ class NumericalityValidationTest < ActiveModel::TestCase end def with_each_topic_approved_value(values) - topic = Topic.new(:title => "numeric test", :content => "whatever") + topic = Topic.new(title: "numeric test", content: "whatever") values.each do |value| topic.approved = value yield topic, value diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index 144532d6f4..c1914b32bc 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -17,20 +17,20 @@ class ValidatesTest < ActiveModel::TestCase end def test_validates_with_messages_empty - Person.validates :title, :presence => {:message => "" } + Person.validates :title, presence: { message: "" } person = Person.new assert !person.valid?, 'person should not be valid.' end def test_validates_with_built_in_validation - Person.validates :title, :numericality => true + Person.validates :title, numericality: true person = Person.new person.valid? assert_equal ['is not a number'], person.errors[:title] end def test_validates_with_attribute_specified_as_string - Person.validates "title", :numericality => true + Person.validates "title", numericality: true person = Person.new person.valid? assert_equal ['is not a number'], person.errors[:title] @@ -41,14 +41,14 @@ class ValidatesTest < ActiveModel::TestCase end def test_validates_with_built_in_validation_and_options - Person.validates :salary, :numericality => { :message => 'my custom message' } + Person.validates :salary, numericality: { message: 'my custom message' } person = Person.new person.valid? assert_equal ['my custom message'], person.errors[:salary] end def test_validates_with_validator_class - Person.validates :karma, :email => true + Person.validates :karma, email: true person = Person.new person.valid? assert_equal ['is not an email'], person.errors[:karma] @@ -62,33 +62,33 @@ class ValidatesTest < ActiveModel::TestCase end def test_validates_with_if_as_local_conditions - Person.validates :karma, :presence => true, :email => { :unless => :condition_is_true } + Person.validates :karma, presence: true, email: { unless: :condition_is_true } person = Person.new person.valid? 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.validates :karma, presence: true, email: true, if: :condition_is_true person = Person.new person.valid? assert_equal ["can't be blank", "is not an email"], person.errors[:karma].sort end def test_validates_with_unless_shared_conditions - Person.validates :karma, :presence => true, :email => true, :unless => :condition_is_true + Person.validates :karma, presence: true, email: true, unless: :condition_is_true person = Person.new assert person.valid? end def test_validates_with_allow_nil_shared_conditions - Person.validates :karma, :length => { :minimum => 20 }, :email => true, :allow_nil => true + Person.validates :karma, length: { minimum: 20 }, email: true, allow_nil: true person = Person.new assert person.valid? end def test_validates_with_regexp - Person.validates :karma, :format => /positive|negative/ + Person.validates :karma, format: /positive|negative/ person = Person.new assert person.invalid? assert_equal ['is invalid'], person.errors[:karma] @@ -97,7 +97,7 @@ class ValidatesTest < ActiveModel::TestCase end def test_validates_with_array - Person.validates :gender, :inclusion => %w(m f) + Person.validates :gender, inclusion: %w(m f) person = Person.new assert person.invalid? assert_equal ['is not included in the list'], person.errors[:gender] @@ -106,7 +106,7 @@ class ValidatesTest < ActiveModel::TestCase end def test_validates_with_range - Person.validates :karma, :length => 6..20 + Person.validates :karma, length: 6..20 person = Person.new assert person.invalid? assert_equal ['is too short (minimum is 6 characters)'], person.errors[:karma] @@ -115,25 +115,25 @@ class ValidatesTest < ActiveModel::TestCase end def test_validates_with_validator_class_and_options - Person.validates :karma, :email => { :message => 'my custom message' } + Person.validates :karma, email: { message: 'my custom message' } person = Person.new person.valid? assert_equal ['my custom message'], person.errors[:karma] end def test_validates_with_unknown_validator - assert_raise(ArgumentError) { Person.validates :karma, :unknown => true } + assert_raise(ArgumentError) { Person.validates :karma, unknown: true } end def test_validates_with_included_validator - PersonWithValidator.validates :title, :presence => true + PersonWithValidator.validates :title, presence: true person = PersonWithValidator.new person.valid? assert_equal ['Local validator'], person.errors[:title] end def test_validates_with_included_validator_and_options - PersonWithValidator.validates :title, :presence => { :custom => ' please' } + PersonWithValidator.validates :title, presence: { custom: ' please' } person = PersonWithValidator.new person.valid? assert_equal ['Local validator please'], person.errors[:title] @@ -141,7 +141,7 @@ class ValidatesTest < ActiveModel::TestCase def test_validates_with_included_validator_and_wildcard_shortcut # Shortcut for PersonWithValidator.validates :title, like: { with: "Mr." } - PersonWithValidator.validates :title, :like => "Mr." + PersonWithValidator.validates :title, like: "Mr." person = PersonWithValidator.new person.title = "Ms. Pacman" person.valid? @@ -149,7 +149,7 @@ class ValidatesTest < ActiveModel::TestCase end def test_defining_extra_default_keys_for_validates - Topic.validates :title, :confirmation => true, :message => 'Y U NO CONFIRM' + Topic.validates :title, confirmation: true, message: 'Y U NO CONFIRM' topic = Topic.new topic.title = "What's happening" topic.title_confirmation = "Not this" diff --git a/activemodel/test/cases/validations/validations_context_test.rb b/activemodel/test/cases/validations/validations_context_test.rb index b795861f95..5f99b320a6 100644 --- a/activemodel/test/cases/validations/validations_context_test.rb +++ b/activemodel/test/cases/validations/validations_context_test.rb @@ -19,21 +19,21 @@ class ValidationsContextTest < ActiveModel::TestCase end test "with a class that adds errors on create and validating a new model with no arguments" do - Topic.validates_with(ValidatorThatAddsErrors, :on => :create) + Topic.validates_with(ValidatorThatAddsErrors, on: :create) topic = Topic.new assert topic.valid?, "Validation doesn't run on valid? if 'on' is set to create" end test "with a class that adds errors on update and validating a new model" do - Topic.validates_with(ValidatorThatAddsErrors, :on => :update) + Topic.validates_with(ValidatorThatAddsErrors, on: :update) topic = Topic.new assert topic.valid?(:create), "Validation doesn't run on create if 'on' is set to update" end test "with a class that adds errors on create and validating a new model" do - Topic.validates_with(ValidatorThatAddsErrors, :on => :create) + Topic.validates_with(ValidatorThatAddsErrors, on: :create) topic = Topic.new assert topic.invalid?(:create), "Validation does run on create if 'on' is set to create" assert topic.errors[:base].include?(ERROR_MESSAGE) end -end
\ No newline at end of file +end diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index daa7a8d5c4..efe16d9aa9 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -72,26 +72,26 @@ class ValidatesWithTest < ActiveModel::TestCase end test "with if statements that return false" do - Topic.validates_with(ValidatorThatAddsErrors, :if => "1 == 2") + Topic.validates_with(ValidatorThatAddsErrors, if: "1 == 2") topic = Topic.new assert topic.valid? end test "with if statements that return true" do - Topic.validates_with(ValidatorThatAddsErrors, :if => "1 == 1") + Topic.validates_with(ValidatorThatAddsErrors, if: "1 == 1") topic = Topic.new assert topic.invalid? assert topic.errors[:base].include?(ERROR_MESSAGE) end test "with unless statements that return true" do - Topic.validates_with(ValidatorThatAddsErrors, :unless => "1 == 1") + Topic.validates_with(ValidatorThatAddsErrors, unless: "1 == 1") topic = Topic.new assert topic.valid? end test "with unless statements that returns false" do - Topic.validates_with(ValidatorThatAddsErrors, :unless => "1 == 2") + Topic.validates_with(ValidatorThatAddsErrors, unless: "1 == 2") topic = Topic.new assert topic.invalid? assert topic.errors[:base].include?(ERROR_MESSAGE) @@ -100,10 +100,10 @@ class ValidatesWithTest < ActiveModel::TestCase test "passes all configuration options to the validator class" do topic = Topic.new validator = mock() - validator.expects(:new).with(:foo => :bar, :if => "1 == 1").returns(validator) + validator.expects(:new).with(foo: :bar, if: "1 == 1").returns(validator) validator.expects(:validate).with(topic) - Topic.validates_with(validator, :if => "1 == 1", :foo => :bar) + Topic.validates_with(validator, if: "1 == 1", foo: :bar) assert topic.valid? end @@ -130,15 +130,15 @@ class ValidatesWithTest < ActiveModel::TestCase end test "validates_with with options" do - Topic.validates_with(ValidatorThatValidatesOptions, :field => :first_name) + Topic.validates_with(ValidatorThatValidatesOptions, field: :first_name) topic = Topic.new assert topic.invalid? assert topic.errors[:base].include?(ERROR_MESSAGE) end test "validates_with each validator" do - Topic.validates_with(ValidatorPerEachAttribute, :attributes => [:title, :content]) - topic = Topic.new :title => "Title", :content => "Content" + Topic.validates_with(ValidatorPerEachAttribute, attributes: [:title, :content]) + topic = Topic.new title: "Title", content: "Content" assert topic.invalid? assert_equal ["Value is Title"], topic.errors[:title] assert_equal ["Value is Content"], topic.errors[:content] @@ -146,7 +146,7 @@ class ValidatesWithTest < ActiveModel::TestCase test "each validator checks validity" do assert_raise RuntimeError do - Topic.validates_with(ValidatorCheckValidity, :attributes => [:title]) + Topic.validates_with(ValidatorCheckValidity, attributes: [:title]) end end @@ -157,25 +157,25 @@ class ValidatesWithTest < ActiveModel::TestCase end test "each validator skip nil values if :allow_nil is set to true" do - Topic.validates_with(ValidatorPerEachAttribute, :attributes => [:title, :content], :allow_nil => true) - topic = Topic.new :content => "" + Topic.validates_with(ValidatorPerEachAttribute, attributes: [:title, :content], allow_nil: true) + topic = Topic.new content: "" assert topic.invalid? assert topic.errors[:title].empty? assert_equal ["Value is "], topic.errors[:content] end test "each validator skip blank values if :allow_blank is set to true" do - Topic.validates_with(ValidatorPerEachAttribute, :attributes => [:title, :content], :allow_blank => true) - topic = Topic.new :content => "" + Topic.validates_with(ValidatorPerEachAttribute, attributes: [:title, :content], allow_blank: true) + topic = Topic.new content: "" assert topic.valid? assert topic.errors[:title].empty? assert topic.errors[:content].empty? end test "validates_with can validate with an instance method" do - Topic.validates :title, :with => :my_validation + Topic.validates :title, with: :my_validation - topic = Topic.new :title => "foo" + topic = Topic.new title: "foo" assert topic.valid? assert topic.errors[:title].empty? @@ -185,9 +185,9 @@ class ValidatesWithTest < ActiveModel::TestCase end test "optionally pass in the attribute being validated when validating with an instance method" do - Topic.validates :title, :content, :with => :my_validation_with_arg + Topic.validates :title, :content, with: :my_validation_with_arg - topic = Topic.new :title => "foo" + topic = Topic.new title: "foo" assert !topic.valid? assert topic.errors[:title].empty? assert_equal ['is missing'], topic.errors[:content] |