diff options
Diffstat (limited to 'activemodel/test')
40 files changed, 959 insertions, 945 deletions
diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index baaf842222..e9cb5ccc96 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -10,7 +10,7 @@ class ModelWithAttributes end def attributes - { :foo => 'value of foo', :baz => 'value of baz' } + { foo: 'value of foo', baz: 'value of baz' } end private @@ -80,7 +80,7 @@ class ModelWithRubyKeywordNamedAttributes include ActiveModel::AttributeMethods def attributes - { :begin => 'value of begin', :end => 'value of end' } + { begin: 'value of begin', end: 'value of end' } end private @@ -194,7 +194,7 @@ class AttributeMethodsTest < ActiveModel::TestCase assert_raises(NoMethodError) { ModelWithAttributes.new.foo } end - test 'acessing a suffixed attribute' do + test 'accessing a suffixed attribute' do m = ModelWithAttributes2.new m.attributes = { 'foo' => 'bar' } @@ -202,17 +202,6 @@ class AttributeMethodsTest < ActiveModel::TestCase assert_equal 'bar', m.foo_test end - test 'explicitly specifying an empty prefix/suffix is deprecated' do - klass = Class.new(ModelWithAttributes) - - assert_deprecated { klass.attribute_method_suffix '' } - assert_deprecated { klass.attribute_method_prefix '' } - - klass.define_attribute_methods(:foo) - - assert_equal 'value of foo', klass.new.foo - end - test 'should not interfere with method_missing if the attr has a private/protected method' do m = ModelWithAttributes2.new m.attributes = { 'private_method' => '<3', 'protected_method' => 'O_o' } diff --git a/activemodel/test/cases/callbacks_test.rb b/activemodel/test/cases/callbacks_test.rb index 086e7266ff..5fede098d1 100644 --- a/activemodel/test/cases/callbacks_test.rb +++ b/activemodel/test/cases/callbacks_test.rb @@ -15,9 +15,9 @@ class CallbacksTest < ActiveModel::TestCase extend ActiveModel::Callbacks define_model_callbacks :create - define_model_callbacks :initialize, :only => :after - define_model_callbacks :multiple, :only => [:before, :around] - define_model_callbacks :empty, :only => [] + define_model_callbacks :initialize, only: :after + define_model_callbacks :multiple, only: [:before, :around] + define_model_callbacks :empty, only: [] before_create :before_create around_create CallbackValidator.new @@ -107,7 +107,7 @@ class CallbacksTest < ActiveModel::TestCase test "after_create callbacks with both callbacks declared in one line" do assert_equal ["callback1", "callback2"], Violin1.new.create.history end - test "after_create callbacks with both callbacks declared in differnt lines" do + test "after_create callbacks with both callbacks declared in different lines" do assert_equal ["callback1", "callback2"], Violin2.new.create.history end diff --git a/activemodel/test/cases/conversion_test.rb b/activemodel/test/cases/conversion_test.rb index d679ad41aa..3bb177591d 100644 --- a/activemodel/test/cases/conversion_test.rb +++ b/activemodel/test/cases/conversion_test.rb @@ -13,7 +13,7 @@ class ConversionTest < ActiveModel::TestCase end test "to_key default implementation returns the id in an array for persisted records" do - assert_equal [1], Contact.new(:id => 1).to_key + assert_equal [1], Contact.new(id: 1).to_key end test "to_param default implementation returns nil for new records" do @@ -21,7 +21,7 @@ class ConversionTest < ActiveModel::TestCase end test "to_param default implementation returns a string of ids for persisted records" do - assert_equal "1", Contact.new(:id => 1).to_param + assert_equal "1", Contact.new(id: 1).to_param end test "to_partial_path default implementation returns a string giving a relative path" do @@ -29,4 +29,8 @@ class ConversionTest < ActiveModel::TestCase assert_equal "helicopters/helicopter", Helicopter.new.to_partial_path, "ActiveModel::Conversion#to_partial_path caching should be class-specific" end + + test "to_partial_path handles namespaced models" do + assert_equal "helicopter/comanches/comanche", Helicopter::Comanche.new.to_partial_path + end end diff --git a/activemodel/test/cases/deprecated_mass_assignment_security_test.rb b/activemodel/test/cases/deprecated_mass_assignment_security_test.rb deleted file mode 100644 index c1fe8822cd..0000000000 --- a/activemodel/test/cases/deprecated_mass_assignment_security_test.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'cases/helper' -require 'models/project' - -class DeprecatedMassAssignmentSecurityTest < ActiveModel::TestCase - def test_attr_accessible_raise_error - assert_raise RuntimeError, /protected_attributes/ do - Project.attr_accessible :username - end - end - - def test_attr_protected_raise_error - assert_raise RuntimeError, /protected_attributes/ do - Project.attr_protected :username - end - end -end diff --git a/activemodel/test/cases/dirty_test.rb b/activemodel/test/cases/dirty_test.rb index eaaf910bac..a90d0b1299 100644 --- a/activemodel/test/cases/dirty_test.rb +++ b/activemodel/test/cases/dirty_test.rb @@ -3,11 +3,12 @@ require "cases/helper" class DirtyTest < ActiveModel::TestCase class DirtyModel include ActiveModel::Dirty - define_attribute_methods :name, :color + define_attribute_methods :name, :color, :size def initialize @name = nil @color = nil + @size = nil end def name @@ -28,9 +29,17 @@ class DirtyTest < ActiveModel::TestCase @color = val end + def size + @size + end + + def size=(val) + attribute_will_change!(:size) unless val == @size + @size = val + end + def save - @previously_changed = changes - @changed_attributes.clear + changes_applied end end @@ -46,7 +55,7 @@ class DirtyTest < ActiveModel::TestCase assert @model.name_changed? end - test "list of changed attributes" do + test "list of changed attribute keys" do assert_equal [], @model.changed @model.name = "Paul" assert_equal ['name'], @model.changed @@ -78,7 +87,7 @@ class DirtyTest < ActiveModel::TestCase @model.name = "Bob" @model.reset_name! assert_nil @model.name - #assert !@model.name_changed #Doesn't work yet + assert !@model.name_changed? end test "setting color to same value should not result in change being recorded" do @@ -106,6 +115,17 @@ class DirtyTest < ActiveModel::TestCase assert_equal [nil, "Jericho Cane"], @model.previous_changes['name'] end + test "previous value is preserved when changed after save" do + assert_equal({}, @model.changed_attributes) + @model.name = "Paul" + assert_equal({ "name" => nil }, @model.changed_attributes) + + @model.save + + @model.name = "John" + assert_equal({ "name" => "Paul" }, @model.changed_attributes) + end + test "changing the same attribute multiple times retains the correct original value" do @model.name = "Otto" @model.save @@ -115,4 +135,8 @@ class DirtyTest < ActiveModel::TestCase assert_equal @model.name_was, "Otto" end + test "using attribute_will_change! with a symbol" do + @model.size = 1 + assert @model.size_changed? + end end diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 3bc0d58351..4e07e0e00b 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -7,7 +7,7 @@ class ErrorsTest < ActiveModel::TestCase @errors = ActiveModel::Errors.new(self) end - attr_accessor :name + attr_accessor :name, :age attr_reader :errors def validate! @@ -54,7 +54,46 @@ class ErrorsTest < ActiveModel::TestCase assert errors.has_key?(:foo), 'errors should have key :foo' end - test "should return true if no errors" do + test "clear errors" do + person = Person.new + person.validate! + + assert_equal 1, person.errors.count + person.errors.clear + assert person.errors.empty? + end + + test "get returns the errors for the provided key" do + errors = ActiveModel::Errors.new(self) + errors[:foo] = "omg" + + assert_equal ["omg"], errors.get(:foo) + end + + test "sets the error with the provided key" do + errors = ActiveModel::Errors.new(self) + errors.set(:foo, "omg") + + assert_equal({ foo: "omg" }, errors.messages) + end + + test "values returns an array of messages" do + errors = ActiveModel::Errors.new(self) + errors.set(:foo, "omg") + errors.set(:baz, "zomg") + + assert_equal ["omg", "zomg"], errors.values + end + + test "keys returns the error keys" do + errors = ActiveModel::Errors.new(self) + errors.set(:foo, "omg") + errors.set(:baz, "zomg") + + assert_equal [:foo, :baz], errors.keys + end + + test "detecting whether there are errors with empty?, blank?, include?" do person = Person.new person.errors[:foo] assert person.errors.empty? @@ -62,144 +101,203 @@ class ErrorsTest < ActiveModel::TestCase assert !person.errors.include?(:foo) end - test "method validate! should work" do + test "adding errors using conditionals with Person#validate!" do person = Person.new person.validate! assert_equal ["name can not be nil"], person.errors.full_messages assert_equal ["can not be nil"], person.errors[:name] end - test 'should be able to assign error' do + test "assign error" do person = Person.new person.errors[:name] = 'should not be nil' assert_equal ["should not be nil"], person.errors[:name] end - test 'should be able to add an error on an attribute' do + test "add an error message on a specific attribute" do person = Person.new person.errors.add(:name, "can not be blank") assert_equal ["can not be blank"], person.errors[:name] end - test "should be able to add an error with a symbol" do + test "add an error with a symbol" do person = Person.new person.errors.add(:name, :blank) message = person.errors.generate_message(:name, :blank) assert_equal [message], person.errors[:name] end - test "should be able to add an error with a proc" do + test "add an error with a proc" do person = Person.new message = Proc.new { "can not be blank" } person.errors.add(:name, message) assert_equal ["can not be blank"], person.errors[:name] end - test "added? should be true if that error was added" do + test "added? detects if a specific error was added to the object" do person = Person.new person.errors.add(:name, "can not be blank") assert person.errors.added?(:name, "can not be blank") end - test "added? should handle when message is a symbol" do + test "added? handles symbol message" do person = Person.new person.errors.add(:name, :blank) assert person.errors.added?(:name, :blank) end - test "added? should handle when message is a proc" do + test "added? handles proc messages" do person = Person.new message = Proc.new { "can not be blank" } person.errors.add(:name, message) assert person.errors.added?(:name, message) end - test "added? should default message to :invalid" do + test "added? defaults message to :invalid" do person = Person.new - person.errors.add(:name, :invalid) + person.errors.add(:name) assert person.errors.added?(:name) end - test "added? should be true when several errors are present, and we ask for one of them" do + test "added? matches the given message when several errors are present for the same attribute" do person = Person.new person.errors.add(:name, "can not be blank") person.errors.add(:name, "is invalid") assert person.errors.added?(:name, "can not be blank") end - test "added? should be false if no errors are present" do + test "added? returns false when no errors are present" do person = Person.new assert !person.errors.added?(:name) end - test "added? should be false when an error is present, but we check for another error" do + test "added? returns false when checking a nonexisting error and other errors are present for the given attribute" do person = Person.new person.errors.add(:name, "is invalid") assert !person.errors.added?(:name, "can not be blank") end - test 'should respond to size' do + test "size calculates the number of error messages" do person = Person.new person.errors.add(:name, "can not be blank") assert_equal 1, person.errors.size end - test 'to_a should return an array' do + test "to_a returns the list of errors with complete messages containing the attribute names" do person = Person.new person.errors.add(:name, "can not be blank") person.errors.add(:name, "can not be nil") assert_equal ["name can not be blank", "name can not be nil"], person.errors.to_a end - test 'to_hash should return a hash' do + test "to_hash returns the error messages hash" do person = Person.new person.errors.add(:name, "can not be blank") - assert_instance_of ::Hash, person.errors.to_hash + assert_equal({ name: ["can not be blank"] }, person.errors.to_hash) end - test 'full_messages should return an array of error messages, with the attribute name included' do + test "full_messages creates a list of error messages with the attribute name included" do person = Person.new person.errors.add(:name, "can not be blank") person.errors.add(:name, "can not be nil") - assert_equal ["name can not be blank", "name can not be nil"], person.errors.to_a + assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages + end + + test "full_messages_for contains all the error messages for the given attribute" do + person = Person.new + person.errors.add(:name, "can not be blank") + person.errors.add(:name, "can not be nil") + assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages_for(:name) + end + + test "full_messages_for does not contain error messages from other attributes" do + person = Person.new + person.errors.add(:name, "can not be blank") + person.errors.add(:email, "can not be blank") + assert_equal ["name can not be blank"], person.errors.full_messages_for(:name) + end + + test "full_messages_for returns an empty list in case there are no errors for the given attribute" do + person = Person.new + person.errors.add(:name, "can not be blank") + assert_equal [], person.errors.full_messages_for(:email) end - test 'full_message should return the given message if attribute equals :base' do + test "full_message returns the given message when attribute is :base" do person = Person.new assert_equal "press the button", person.errors.full_message(:base, "press the button") end - test 'full_message should return the given message with the attribute name included' do + test "full_message returns the given message with the attribute name included" do person = Person.new assert_equal "name can not be blank", person.errors.full_message(:name, "can not be blank") + assert_equal "name_test can not be blank", person.errors.full_message(:name_test, "can not be blank") end - test 'should return a JSON hash representation of the errors' do + test "as_json creates a json formatted representation of the errors hash" do person = Person.new - person.errors.add(:name, "can not be blank") - person.errors.add(:name, "can not be nil") - person.errors.add(:email, "is invalid") - hash = person.errors.as_json - assert_equal ["can not be blank", "can not be nil"], hash[:name] - assert_equal ["is invalid"], hash[:email] + person.validate! + + assert_equal({ name: ["can not be nil"] }, person.errors.as_json) end - test 'should return a JSON hash representation of the errors with full messages' do + test "as_json with :full_messages option creates a json formatted representation of the errors containing complete messages" do person = Person.new - person.errors.add(:name, "can not be blank") - person.errors.add(:name, "can not be nil") - person.errors.add(:email, "is invalid") - hash = person.errors.as_json(:full_messages => true) - assert_equal ["name can not be blank", "name can not be nil"], hash[:name] - assert_equal ["email is invalid"], hash[:email] + person.validate! + + assert_equal({ name: ["name can not be nil"] }, person.errors.as_json(full_messages: true)) end - test "generate_message should work without i18n_scope" do + test "generate_message works without i18n_scope" do person = Person.new assert !Person.respond_to?(:i18n_scope) assert_nothing_raised { person.errors.generate_message(:name, :blank) } end -end + test "add_on_empty generates message" do + person = Person.new + person.errors.expects(:generate_message).with(:name, :empty, {}) + person.errors.add_on_empty :name + end + + test "add_on_empty generates message for multiple attributes" do + person = Person.new + person.errors.expects(:generate_message).with(:name, :empty, {}) + person.errors.expects(:generate_message).with(:age, :empty, {}) + person.errors.add_on_empty [:name, :age] + end + + test "add_on_empty generates message with custom default message" do + person = Person.new + person.errors.expects(:generate_message).with(:name, :empty, { message: 'custom' }) + person.errors.add_on_empty :name, message: 'custom' + end + + test "add_on_empty generates message with empty string value" do + person = Person.new + person.name = '' + person.errors.expects(:generate_message).with(:name, :empty, {}) + person.errors.add_on_empty :name + end + + test "add_on_blank generates message" do + person = Person.new + person.errors.expects(:generate_message).with(:name, :blank, {}) + person.errors.add_on_blank :name + end + + test "add_on_blank generates message for multiple attributes" do + person = Person.new + person.errors.expects(:generate_message).with(:name, :blank, {}) + person.errors.expects(:generate_message).with(:age, :blank, {}) + person.errors.add_on_blank [:name, :age] + end + + test "add_on_blank generates message with custom default message" do + person = Person.new + person.errors.expects(:generate_message).with(:name, :blank, { message: 'custom' }) + person.errors.add_on_blank :name, message: 'custom' + end +end diff --git a/activemodel/test/cases/helper.rb b/activemodel/test/cases/helper.rb index 7d6f11b5a5..7a63674757 100644 --- a/activemodel/test/cases/helper.rb +++ b/activemodel/test/cases/helper.rb @@ -7,4 +7,4 @@ require 'active_support/core_ext/string/access' # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true -require 'minitest/autorun' +require 'active_support/testing/autorun' diff --git a/activemodel/test/cases/model_test.rb b/activemodel/test/cases/model_test.rb index d93fd96b88..ee0fa26546 100644 --- a/activemodel/test/cases/model_test.rb +++ b/activemodel/test/cases/model_test.rb @@ -3,7 +3,30 @@ require 'cases/helper' class ModelTest < ActiveModel::TestCase include ActiveModel::Lint::Tests + module DefaultValue + def self.included(klass) + klass.class_eval { attr_accessor :hello } + end + + def initialize(*args) + @attr ||= 'default value' + super + end + end + class BasicModel + include DefaultValue + include ActiveModel::Model + attr_accessor :attr + end + + class BasicModelWithReversedMixins + include ActiveModel::Model + include DefaultValue + attr_accessor :attr + end + + class SimpleModel include ActiveModel::Model attr_accessor :attr end @@ -13,14 +36,40 @@ class ModelTest < ActiveModel::TestCase end def test_initialize_with_params - object = BasicModel.new(:attr => "value") - assert_equal object.attr, "value" + object = BasicModel.new(attr: "value") + assert_equal "value", object.attr + end + + def test_initialize_with_params_and_mixins_reversed + object = BasicModelWithReversedMixins.new(attr: "value") + assert_equal "value", object.attr end def test_initialize_with_nil_or_empty_hash_params_does_not_explode assert_nothing_raised do BasicModel.new() + BasicModel.new(nil) BasicModel.new({}) + SimpleModel.new(attr: 'value') end end + + def test_persisted_is_always_false + object = BasicModel.new(attr: "value") + assert object.persisted? == false + end + + def test_mixin_inclusion_chain + object = BasicModel.new + assert_equal 'default value', object.attr + end + + def test_mixin_initializer_when_args_exist + object = BasicModel.new(hello: 'world') + assert_equal 'world', object.hello + end + + def test_mixin_initializer_when_args_dont_exist + assert_raises(NoMethodError) { SimpleModel.new(hello: 'world') } + end end diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index 49d8706ac2..aa683f4152 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -29,6 +29,14 @@ class NamingTest < ActiveModel::TestCase assert_equal 'Track back', @model_name.human end + def test_route_key + assert_equal 'post_track_backs', @model_name.route_key + end + + def test_param_key + assert_equal 'post_track_back', @model_name.param_key + end + def test_i18n_key assert_equal :"post/track_back", @model_name.i18n_key end @@ -237,7 +245,7 @@ class NamingHelpersTest < ActiveModel::TestCase end def test_uncountable - assert uncountable?(@uncountable), "Expected 'sheep' to be uncoutable" + assert uncountable?(@uncountable), "Expected 'sheep' to be uncountable" assert !uncountable?(@klass), "Expected 'contact' to be countable" end diff --git a/activemodel/test/cases/observer_array_test.rb b/activemodel/test/cases/observer_array_test.rb deleted file mode 100644 index fc5f18008b..0000000000 --- a/activemodel/test/cases/observer_array_test.rb +++ /dev/null @@ -1,220 +0,0 @@ -require 'cases/helper' -require 'models/observers' - -class ObserverArrayTest < ActiveModel::TestCase - def teardown - ORM.observers.enable :all - Budget.observers.enable :all - Widget.observers.enable :all - end - - def assert_observer_notified(model_class, observer_class) - observer_class.instance.before_save_invocations.clear - model_instance = model_class.new - model_instance.save - assert_equal [model_instance], observer_class.instance.before_save_invocations - end - - def assert_observer_not_notified(model_class, observer_class) - observer_class.instance.before_save_invocations.clear - model_instance = model_class.new - model_instance.save - assert_equal [], observer_class.instance.before_save_invocations - end - - test "all observers are enabled by default" do - assert_observer_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can disable individual observers using a class constant" do - ORM.observers.disable WidgetObserver - - assert_observer_not_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can enable individual observers using a class constant" do - ORM.observers.disable :all - ORM.observers.enable AuditTrail - - assert_observer_not_notified Widget, WidgetObserver - assert_observer_not_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can disable individual observers using a symbol" do - ORM.observers.disable :budget_observer - - assert_observer_notified Widget, WidgetObserver - assert_observer_not_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can enable individual observers using a symbol" do - ORM.observers.disable :all - ORM.observers.enable :audit_trail - - assert_observer_not_notified Widget, WidgetObserver - assert_observer_not_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can disable multiple observers at a time" do - ORM.observers.disable :widget_observer, :budget_observer - - assert_observer_not_notified Widget, WidgetObserver - assert_observer_not_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can enable multiple observers at a time" do - ORM.observers.disable :all - ORM.observers.enable :widget_observer, :budget_observer - - assert_observer_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_not_notified Widget, AuditTrail - assert_observer_not_notified Budget, AuditTrail - end - - test "can disable all observers using :all" do - ORM.observers.disable :all - - assert_observer_not_notified Widget, WidgetObserver - assert_observer_not_notified Budget, BudgetObserver - assert_observer_not_notified Widget, AuditTrail - assert_observer_not_notified Budget, AuditTrail - end - - test "can enable all observers using :all" do - ORM.observers.disable :all - ORM.observers.enable :all - - assert_observer_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can disable observers on individual models without affecting those observers on other models" do - Widget.observers.disable :all - - assert_observer_not_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_not_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can enable observers on individual models without affecting those observers on other models" do - ORM.observers.disable :all - Budget.observers.enable AuditTrail - - assert_observer_not_notified Widget, WidgetObserver - assert_observer_not_notified Budget, BudgetObserver - assert_observer_not_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can disable observers for the duration of a block" do - yielded = false - ORM.observers.disable :budget_observer do - yielded = true - assert_observer_notified Widget, WidgetObserver - assert_observer_not_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - assert yielded - assert_observer_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "can enable observers for the duration of a block" do - yielded = false - Widget.observers.disable :all - - Widget.observers.enable :all do - yielded = true - assert_observer_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - assert yielded - assert_observer_not_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_not_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "raises an appropriate error when a developer accidentally enables or disables the wrong class (i.e. Widget instead of WidgetObserver)" do - assert_raise ArgumentError do - ORM.observers.enable :widget - end - - assert_raise ArgumentError do - ORM.observers.enable Widget - end - - assert_raise ArgumentError do - ORM.observers.disable :widget - end - - assert_raise ArgumentError do - ORM.observers.disable Widget - end - end - - test "allows #enable at the superclass level to override #disable at the subclass level when called last" do - Widget.observers.disable :all - ORM.observers.enable :all - - assert_observer_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - test "allows #disable at the superclass level to override #enable at the subclass level when called last" do - Budget.observers.enable :audit_trail - ORM.observers.disable :audit_trail - - assert_observer_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_not_notified Widget, AuditTrail - assert_observer_not_notified Budget, AuditTrail - end - - test "can use the block form at different levels of the hierarchy" do - yielded = false - Widget.observers.disable :all - - ORM.observers.enable :all do - yielded = true - assert_observer_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end - - assert yielded - assert_observer_not_notified Widget, WidgetObserver - assert_observer_notified Budget, BudgetObserver - assert_observer_not_notified Widget, AuditTrail - assert_observer_notified Budget, AuditTrail - end -end - diff --git a/activemodel/test/cases/observing_test.rb b/activemodel/test/cases/observing_test.rb deleted file mode 100644 index ade6026602..0000000000 --- a/activemodel/test/cases/observing_test.rb +++ /dev/null @@ -1,181 +0,0 @@ -require 'cases/helper' - -class ObservedModel - include ActiveModel::Observing - - class Observer - end -end - -class FooObserver < ActiveModel::Observer - class << self - public :new - end - - attr_accessor :stub - - def on_spec(record, *args) - stub.event_with(record, *args) if stub - end - - def around_save(record) - yield :in_around_save - end -end - -class Foo - include ActiveModel::Observing -end - -class ObservingTest < ActiveModel::TestCase - def setup - ObservedModel.observers.clear - end - - test "initializes model with no cached observers" do - assert ObservedModel.observers.empty?, "Not empty: #{ObservedModel.observers.inspect}" - end - - test "stores cached observers in an array" do - ObservedModel.observers << :foo - assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}" - end - - test "flattens array of assigned cached observers" do - ObservedModel.observers = [[:foo], :bar] - assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}" - assert ObservedModel.observers.include?(:bar), ":bar not in #{ObservedModel.observers.inspect}" - end - - test "uses an ObserverArray so observers can be disabled" do - ObservedModel.observers = [:foo, :bar] - assert ObservedModel.observers.is_a?(ActiveModel::ObserverArray) - end - - test "instantiates observer names passed as strings" do - ObservedModel.observers << 'foo_observer' - FooObserver.expects(:instance) - ObservedModel.instantiate_observers - end - - test "instantiates observer names passed as symbols" do - ObservedModel.observers << :foo_observer - FooObserver.expects(:instance) - ObservedModel.instantiate_observers - end - - test "instantiates observer classes" do - ObservedModel.observers << ObservedModel::Observer - ObservedModel::Observer.expects(:instance) - ObservedModel.instantiate_observers - end - - test "raises an appropriate error when a developer accidentally adds the wrong class (i.e. Widget instead of WidgetObserver)" do - assert_raise ArgumentError do - ObservedModel.observers = ['string'] - ObservedModel.instantiate_observers - end - assert_raise ArgumentError do - ObservedModel.observers = [:string] - ObservedModel.instantiate_observers - end - assert_raise ArgumentError do - ObservedModel.observers = [String] - ObservedModel.instantiate_observers - end - end - - test "passes observers to subclasses" do - FooObserver.instance - bar = Class.new(Foo) - assert_equal Foo.observers_count, bar.observers_count - end -end - -class ObserverTest < ActiveModel::TestCase - def setup - ObservedModel.observers = :foo_observer - FooObserver.singleton_class.instance_eval do - alias_method :original_observed_classes, :observed_classes - end - end - - def teardown - FooObserver.singleton_class.instance_eval do - undef_method :observed_classes - alias_method :observed_classes, :original_observed_classes - end - end - - test "guesses implicit observable model name" do - assert_equal Foo, FooObserver.observed_class - end - - test "tracks implicit observable models" do - instance = FooObserver.new - assert_equal [Foo], instance.observed_classes - end - - test "tracks explicit observed model class" do - FooObserver.observe ObservedModel - instance = FooObserver.new - assert_equal [ObservedModel], instance.observed_classes - end - - test "tracks explicit observed model as string" do - FooObserver.observe 'observed_model' - instance = FooObserver.new - assert_equal [ObservedModel], instance.observed_classes - end - - test "tracks explicit observed model as symbol" do - FooObserver.observe :observed_model - instance = FooObserver.new - assert_equal [ObservedModel], instance.observed_classes - end - - test "calls existing observer event" do - foo = Foo.new - FooObserver.instance.stub = stub - FooObserver.instance.stub.expects(:event_with).with(foo) - Foo.notify_observers(:on_spec, foo) - end - - test "calls existing observer event from the instance" do - foo = Foo.new - FooObserver.instance.stub = stub - FooObserver.instance.stub.expects(:event_with).with(foo) - foo.notify_observers(:on_spec) - end - - test "passes extra arguments" do - foo = Foo.new - FooObserver.instance.stub = stub - FooObserver.instance.stub.expects(:event_with).with(foo, :bar) - Foo.send(:notify_observers, :on_spec, foo, :bar) - end - - test "skips nonexistent observer event" do - foo = Foo.new - Foo.notify_observers(:whatever, foo) - end - - test "update passes a block on to the observer" do - yielded_value = nil - FooObserver.instance.update(:around_save, Foo.new) do |val| - yielded_value = val - end - assert_equal :in_around_save, yielded_value - end - - test "observe redefines observed_classes class method" do - class BarObserver < ActiveModel::Observer - observe :foo - end - - assert_equal [Foo], BarObserver.observed_classes - - BarObserver.observe(ObservedModel) - assert_equal [ObservedModel], BarObserver.observed_classes - end -end diff --git a/activemodel/test/cases/railtie_test.rb b/activemodel/test/cases/railtie_test.rb new file mode 100644 index 0000000000..96b3b07e50 --- /dev/null +++ b/activemodel/test/cases/railtie_test.rb @@ -0,0 +1,32 @@ +require 'cases/helper' +require 'active_support/testing/isolation' + +class RailtieTest < ActiveModel::TestCase + include ActiveSupport::Testing::Isolation + + def setup + require 'active_model/railtie' + + # Set a fake logger to avoid creating the log directory automatically + fake_logger = Logger.new(nil) + + @app ||= Class.new(::Rails::Application) do + config.eager_load = false + config.logger = fake_logger + end + end + + test 'secure password min_cost is false in the development environment' do + Rails.env = 'development' + @app.initialize! + + assert_equal false, ActiveModel::SecurePassword.min_cost + end + + test 'secure password min_cost is true in the test environment' do + Rails.env = 'test' + @app.initialize! + + assert_equal true, ActiveModel::SecurePassword.min_cost + end +end diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index 509e2fdbb5..41d0b2263e 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -5,13 +5,18 @@ require 'models/visitor' require 'models/administrator' class SecurePasswordTest < ActiveModel::TestCase - setup do + ActiveModel::SecurePassword.min_cost = true + @user = User.new @visitor = Visitor.new @oauthed_user = OauthedUser.new end + teardown do + ActiveModel::SecurePassword.min_cost = false + end + test "blank password" do @user.password = @visitor.password = '' assert !@user.valid?(:create), 'user should be invalid' @@ -63,10 +68,52 @@ class SecurePasswordTest < ActiveModel::TestCase @user.run_callbacks :create end end - + test "Oauthed user can be created with blank digest" do assert_nothing_raised do @oauthed_user.run_callbacks :create end end + + test "Password digest cost defaults to bcrypt default cost when min_cost is false" do + ActiveModel::SecurePassword.min_cost = false + + @user.password = "secret" + assert_equal BCrypt::Engine::DEFAULT_COST, @user.password_digest.cost + end + + test "Password digest cost honors bcrypt cost attribute when min_cost is false" do + ActiveModel::SecurePassword.min_cost = false + BCrypt::Engine.cost = 5 + + @user.password = "secret" + assert_equal BCrypt::Engine.cost, @user.password_digest.cost + end + + test "Password digest cost can be set to bcrypt min cost to speed up tests" do + ActiveModel::SecurePassword.min_cost = true + + @user.password = "secret" + assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost + end + + test "blank password_confirmation does not result in a confirmation error" do + @user.password = "" + @user.password_confirmation = "" + assert @user.valid?(:update), "user should be valid" + end + + test "password_confirmation validations will not be triggered if password_confirmation is not sent" do + @user.password = "password" + assert @user.valid?(:create) + end + + test "will not save if confirmation is blank but password is not" do + @user.password = "password" + @user.password_confirmation = "" + assert_not @user.valid?(:create) + + @user.password_confirmation = "password" + assert @user.valid?(:create) + end end diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb index d2ba9fd95d..4ae41aa19c 100644 --- a/activemodel/test/cases/serialization_test.rb +++ b/activemodel/test/cases/serialization_test.rb @@ -49,32 +49,32 @@ class SerializationTest < ActiveModel::TestCase def test_method_serializable_hash_should_work_with_only_option expected = {"name"=>"David"} - assert_equal expected, @user.serializable_hash(:only => [:name]) + assert_equal expected, @user.serializable_hash(only: [:name]) end def test_method_serializable_hash_should_work_with_except_option expected = {"gender"=>"male", "email"=>"david@example.com"} - assert_equal expected, @user.serializable_hash(:except => [:name]) + assert_equal expected, @user.serializable_hash(except: [:name]) end def test_method_serializable_hash_should_work_with_methods_option expected = {"name"=>"David", "gender"=>"male", "foo"=>"i_am_foo", "email"=>"david@example.com"} - assert_equal expected, @user.serializable_hash(:methods => [:foo]) + assert_equal expected, @user.serializable_hash(methods: [:foo]) end def test_method_serializable_hash_should_work_with_only_and_methods expected = {"foo"=>"i_am_foo"} - assert_equal expected, @user.serializable_hash(:only => [], :methods => [:foo]) + assert_equal expected, @user.serializable_hash(only: [], methods: [:foo]) end def test_method_serializable_hash_should_work_with_except_and_methods expected = {"gender"=>"male", "foo"=>"i_am_foo"} - assert_equal expected, @user.serializable_hash(:except => [:name, :email], :methods => [:foo]) + assert_equal expected, @user.serializable_hash(except: [:name, :email], methods: [:foo]) end def test_should_not_call_methods_that_dont_respond expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} - assert_equal expected, @user.serializable_hash(:methods => [:bar]) + assert_equal expected, @user.serializable_hash(methods: [:bar]) end def test_should_use_read_attribute_for_serialization @@ -83,26 +83,26 @@ class SerializationTest < ActiveModel::TestCase end expected = { "name" => "Jon" } - assert_equal expected, @user.serializable_hash(:only => :name) + assert_equal expected, @user.serializable_hash(only: :name) end def test_include_option_with_singular_association expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com", "address"=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}} - assert_equal expected, @user.serializable_hash(:include => :address) + assert_equal expected, @user.serializable_hash(include: :address) end def test_include_option_with_plural_association expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected, @user.serializable_hash(:include => :friends) + assert_equal expected, @user.serializable_hash(include: :friends) end def test_include_option_with_empty_association @user.friends = [] expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", "friends"=>[]} - assert_equal expected, @user.serializable_hash(:include => :friends) + assert_equal expected, @user.serializable_hash(include: :friends) end class FriendList @@ -120,7 +120,7 @@ class SerializationTest < ActiveModel::TestCase expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected, @user.serializable_hash(:include => :friends) + assert_equal expected, @user.serializable_hash(include: :friends) end def test_multiple_includes @@ -128,13 +128,13 @@ class SerializationTest < ActiveModel::TestCase "address"=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}, "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected, @user.serializable_hash(:include => [:address, :friends]) + assert_equal expected, @user.serializable_hash(include: [:address, :friends]) end def test_include_with_options expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", "address"=>{"street"=>"123 Lane"}} - assert_equal expected, @user.serializable_hash(:include => {:address => {:only => "street"}}) + assert_equal expected, @user.serializable_hash(include: { address: { only: "street" } }) end def test_nested_include @@ -143,19 +143,19 @@ class SerializationTest < ActiveModel::TestCase "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male', "friends"=> [{"email"=>"david@example.com", "gender"=>"male", "name"=>"David"}]}, {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female', "friends"=> []}]} - assert_equal expected, @user.serializable_hash(:include => {:friends => {:include => :friends}}) + assert_equal expected, @user.serializable_hash(include: { friends: { include: :friends } }) end def test_only_include expected = {"name"=>"David", "friends" => [{"name" => "Joe"}, {"name" => "Sue"}]} - assert_equal expected, @user.serializable_hash(:only => :name, :include => {:friends => {:only => :name}}) + assert_equal expected, @user.serializable_hash(only: :name, include: { friends: { only: :name } }) end def test_except_include expected = {"name"=>"David", "email"=>"david@example.com", "friends"=> [{"name" => 'Joe', "email" => 'joe@example.com'}, {"name" => "Sue", "email" => 'sue@example.com'}]} - assert_equal expected, @user.serializable_hash(:except => :gender, :include => {:friends => {:except => :gender}}) + assert_equal expected, @user.serializable_hash(except: :gender, include: { friends: { except: :gender } }) end def test_multiple_includes_with_options @@ -163,6 +163,6 @@ class SerializationTest < ActiveModel::TestCase "address"=>{"street"=>"123 Lane"}, "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected, @user.serializable_hash(:include => [{:address => {:only => "street"}}, :friends]) + assert_equal expected, @user.serializable_hash(include: [{ address: {only: "street" } }, :friends]) end end diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb index e2690f1827..bc185c737f 100644 --- a/activemodel/test/cases/serializers/json_serialization_test.rb +++ b/activemodel/test/cases/serializers/json_serialization_test.rb @@ -4,7 +4,6 @@ require 'models/automobile' require 'active_support/core_ext/object/instance_variables' class Contact - extend ActiveModel::Naming include ActiveModel::Serializers::JSON include ActiveModel::Validations @@ -92,7 +91,7 @@ class JsonSerializationTest < ActiveModel::TestCase end test "should allow attribute filtering with only" do - json = @contact.to_json(:only => [:name, :age]) + json = @contact.to_json(only: [:name, :age]) assert_match %r{"name":"Konata Izumi"}, json assert_match %r{"age":16}, json @@ -199,7 +198,7 @@ class JsonSerializationTest < ActiveModel::TestCase assert_no_match %r{"preferences":}, json end - test "custom as_json options should be extendible" do + test "custom as_json options should be extensible" do def @contact.as_json(options = {}); super(options.merge(only: [:name])); end json = @contact.to_json diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb index e2bb0dda0b..11ee17bb27 100755..100644 --- a/activemodel/test/cases/serializers/xml_serialization_test.rb +++ b/activemodel/test/cases/serializers/xml_serialization_test.rb @@ -4,15 +4,14 @@ require 'active_support/core_ext/object/instance_variables' require 'ostruct' class Contact - extend ActiveModel::Naming include ActiveModel::Serializers::Xml - attr_accessor :address, :friends + attr_accessor :address, :friends, :contact remove_method :attributes if method_defined?(:attributes) def attributes - instance_values.except("address", "friends") + instance_values.except("address", "friends", "contact") end end @@ -25,7 +24,6 @@ class Customer < Struct.new(:name) end class Address - extend ActiveModel::Naming include ActiveModel::Serializers::Xml attr_accessor :street, :city, :state, :zip, :apt_number @@ -37,7 +35,7 @@ end class SerializableContact < Contact def serializable_hash(options={}) - super(options.merge(:only => [:name, :age])) + super(options.merge(only: [:name, :age])) end end @@ -52,12 +50,10 @@ class XmlSerializationTest < ActiveModel::TestCase customer.name = "John" @contact.preferences = customer @contact.address = Address.new - @contact.address.street = "123 Lane" @contact.address.city = "Springfield" - @contact.address.state = "CA" - @contact.address.zip = 11111 @contact.address.apt_number = 35 @contact.friends = [Contact.new, Contact.new] + @contact.contact = SerializableContact.new end test "should serialize default root" do @@ -73,33 +69,33 @@ class XmlSerializationTest < ActiveModel::TestCase end test "should serialize default root with namespace" do - @xml = @contact.to_xml :namespace => "http://xml.rubyonrails.org/contact" + @xml = @contact.to_xml namespace: "http://xml.rubyonrails.org/contact" assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml assert_match %r{</contact>$}, @xml end test "should serialize custom root" do - @xml = @contact.to_xml :root => 'xml_contact' + @xml = @contact.to_xml root: 'xml_contact' assert_match %r{^<xml-contact>}, @xml assert_match %r{</xml-contact>$}, @xml end test "should allow undasherized tags" do - @xml = @contact.to_xml :root => 'xml_contact', :dasherize => false + @xml = @contact.to_xml root: 'xml_contact', dasherize: false assert_match %r{^<xml_contact>}, @xml assert_match %r{</xml_contact>$}, @xml assert_match %r{<created_at}, @xml end test "should allow camelized tags" do - @xml = @contact.to_xml :root => 'xml_contact', :camelize => true + @xml = @contact.to_xml root: 'xml_contact', camelize: true assert_match %r{^<XmlContact>}, @xml assert_match %r{</XmlContact>$}, @xml assert_match %r{<CreatedAt}, @xml end test "should allow lower-camelized tags" do - @xml = @contact.to_xml :root => 'xml_contact', :camelize => :lower + @xml = @contact.to_xml root: 'xml_contact', camelize: :lower assert_match %r{^<xmlContact>}, @xml assert_match %r{</xmlContact>$}, @xml assert_match %r{<createdAt}, @xml @@ -117,7 +113,7 @@ class XmlSerializationTest < ActiveModel::TestCase end test "should allow skipped types" do - @xml = @contact.to_xml :skip_types => true + @xml = @contact.to_xml skip_types: true assert_match %r{<age>25</age>}, @xml end @@ -133,7 +129,7 @@ class XmlSerializationTest < ActiveModel::TestCase end test "should serialize nil" do - assert_match %r{<pseudonyms nil=\"true\"/>}, @contact.to_xml(:methods => :pseudonyms) + assert_match %r{<pseudonyms nil="true"/>}, @contact.to_xml(methods: :pseudonyms) end test "should serialize integer" do @@ -141,50 +137,50 @@ class XmlSerializationTest < ActiveModel::TestCase end test "should serialize datetime" do - assert_match %r{<created-at type=\"dateTime\">2006-08-01T00:00:00Z</created-at>}, @contact.to_xml + assert_match %r{<created-at type="dateTime">2006-08-01T00:00:00Z</created-at>}, @contact.to_xml end test "should serialize boolean" do - assert_match %r{<awesome type=\"boolean\">false</awesome>}, @contact.to_xml + assert_match %r{<awesome type="boolean">false</awesome>}, @contact.to_xml end test "should serialize array" do - assert_match %r{<social type=\"array\">\s*<social>twitter</social>\s*<social>github</social>\s*</social>}, @contact.to_xml(:methods => :social) + assert_match %r{<social type="array">\s*<social>twitter</social>\s*<social>github</social>\s*</social>}, @contact.to_xml(methods: :social) end test "should serialize hash" do - assert_match %r{<network>\s*<git type=\"symbol\">github</git>\s*</network>}, @contact.to_xml(:methods => :network) + assert_match %r{<network>\s*<git type="symbol">github</git>\s*</network>}, @contact.to_xml(methods: :network) end test "should serialize yaml" do - assert_match %r{<preferences type=\"yaml\">--- !ruby/struct:Customer(\s*)\nname: John\n</preferences>}, @contact.to_xml + assert_match %r{<preferences type="yaml">--- !ruby/struct:Customer(\s*)\nname: John\n</preferences>}, @contact.to_xml end test "should call proc on object" do proc = Proc.new { |options| options[:builder].tag!('nationality', 'unknown') } - xml = @contact.to_xml(:procs => [ proc ]) + xml = @contact.to_xml(procs: [ proc ]) assert_match %r{<nationality>unknown</nationality>}, xml end test 'should supply serializable to second proc argument' do proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) } - xml = @contact.to_xml(:procs => [ proc ]) + xml = @contact.to_xml(procs: [ proc ]) assert_match %r{<name-reverse>kcats noraa</name-reverse>}, xml end test "should serialize string correctly when type passed" do - xml = @contact.to_xml :type => 'Contact' + xml = @contact.to_xml type: 'Contact' assert_match %r{<contact type="Contact">}, xml assert_match %r{<name>aaron stack</name>}, xml end test "include option with singular association" do - xml = @contact.to_xml :include => :address, :indent => 0 - assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) + xml = @contact.to_xml include: :address, indent: 0 + assert xml.include?(@contact.address.to_xml(indent: 0, skip_instruct: true)) end test "include option with plural association" do - xml = @contact.to_xml :include => :friends, :indent => 0 + xml = @contact.to_xml include: :friends, indent: 0 assert_match %r{<friends type="array">}, xml assert_match %r{<friend type="Contact">}, xml end @@ -201,61 +197,66 @@ class XmlSerializationTest < ActiveModel::TestCase test "include option with ary" do @contact.friends = FriendList.new(@contact.friends) - xml = @contact.to_xml :include => :friends, :indent => 0 + xml = @contact.to_xml include: :friends, indent: 0 assert_match %r{<friends type="array">}, xml assert_match %r{<friend type="Contact">}, xml end test "multiple includes" do - xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => [ :address, :friends ] - assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) + xml = @contact.to_xml indent: 0, skip_instruct: true, include: [ :address, :friends ] + assert xml.include?(@contact.address.to_xml(indent: 0, skip_instruct: true)) assert_match %r{<friends type="array">}, xml assert_match %r{<friend type="Contact">}, xml end test "include with options" do - xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => { :address => { :only => :city } } + xml = @contact.to_xml indent: 0, skip_instruct: true, include: { address: { only: :city } } assert xml.include?(%(><address><city>Springfield</city></address>)) end test "propagates skip_types option to included associations" do - xml = @contact.to_xml :include => :friends, :indent => 0, :skip_types => true + xml = @contact.to_xml include: :friends, indent: 0, skip_types: true assert_match %r{<friends>}, xml assert_match %r{<friend>}, xml end test "propagates skip-types option to included associations and attributes" do - xml = @contact.to_xml :skip_types => true, :include => :address, :indent => 0 + xml = @contact.to_xml skip_types: true, include: :address, indent: 0 assert_match %r{<address>}, xml assert_match %r{<apt-number>}, xml end test "propagates camelize option to included associations and attributes" do - xml = @contact.to_xml :camelize => true, :include => :address, :indent => 0 + xml = @contact.to_xml camelize: true, include: :address, indent: 0 assert_match %r{<Address>}, xml assert_match %r{<AptNumber type="integer">}, xml end test "propagates dasherize option to included associations and attributes" do - xml = @contact.to_xml :dasherize => false, :include => :address, :indent => 0 + xml = @contact.to_xml dasherize: false, include: :address, indent: 0 assert_match %r{<apt_number type="integer">}, xml end test "don't propagate skip_types if skip_types is defined at the included association level" do - xml = @contact.to_xml :skip_types => true, :include => { :address => { :skip_types => false } }, :indent => 0 + xml = @contact.to_xml skip_types: true, include: { address: { skip_types: false } }, indent: 0 assert_match %r{<address>}, xml assert_match %r{<apt-number type="integer">}, xml end test "don't propagate camelize if camelize is defined at the included association level" do - xml = @contact.to_xml :camelize => true, :include => { :address => { :camelize => false } }, :indent => 0 + xml = @contact.to_xml camelize: true, include: { address: { camelize: false } }, indent: 0 assert_match %r{<address>}, xml assert_match %r{<apt-number type="integer">}, xml end test "don't propagate dasherize if dasherize is defined at the included association level" do - xml = @contact.to_xml :dasherize => false, :include => { :address => { :dasherize => true } }, :indent => 0 + xml = @contact.to_xml dasherize: false, include: { address: { dasherize: true } }, indent: 0 assert_match %r{<address>}, xml assert_match %r{<apt-number type="integer">}, xml end + + test "association with sti" do + xml = @contact.to_xml(include: :contact) + assert xml.include?(%(<contact type="SerializableContact">)) + end end diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb index fd833cdd06..deb4e1ed0a 100644 --- a/activemodel/test/cases/translation_test.rb +++ b/activemodel/test/cases/translation_test.rb @@ -8,22 +8,22 @@ class ActiveModelI18nTests < ActiveModel::TestCase end def test_translated_model_attributes - I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } } + I18n.backend.store_translations 'en', activemodel: { attributes: { person: { name: 'person name attribute' } } } assert_equal 'person name attribute', Person.human_attribute_name('name') end def test_translated_model_attributes_with_default - I18n.backend.store_translations 'en', :attributes => { :name => 'name default attribute' } + I18n.backend.store_translations 'en', attributes: { name: 'name default attribute' } assert_equal 'name default attribute', Person.human_attribute_name('name') end def test_translated_model_attributes_using_default_option - assert_equal 'name default attribute', Person.human_attribute_name('name', :default => "name default attribute") + assert_equal 'name default attribute', Person.human_attribute_name('name', default: "name default attribute") end def test_translated_model_attributes_using_default_option_as_symbol - I18n.backend.store_translations 'en', :default_name => 'name default attribute' - assert_equal 'name default attribute', Person.human_attribute_name('name', :default => :default_name) + I18n.backend.store_translations 'en', default_name: 'name default attribute' + assert_equal 'name default attribute', Person.human_attribute_name('name', default: :default_name) end def test_translated_model_attributes_falling_back_to_default @@ -31,71 +31,74 @@ class ActiveModelI18nTests < ActiveModel::TestCase end def test_translated_model_attributes_using_default_option_as_symbol_and_falling_back_to_default - assert_equal 'Name', Person.human_attribute_name('name', :default => :default_name) + assert_equal 'Name', Person.human_attribute_name('name', default: :default_name) end def test_translated_model_attributes_with_symbols - I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } } + I18n.backend.store_translations 'en', activemodel: { attributes: { person: { name: 'person name attribute'} } } assert_equal 'person name attribute', Person.human_attribute_name(:name) end def test_translated_model_attributes_with_ancestor - I18n.backend.store_translations 'en', :activemodel => {:attributes => {:child => {:name => 'child name attribute'} } } + I18n.backend.store_translations 'en', activemodel: { attributes: { child: { name: 'child name attribute'} } } assert_equal 'child name attribute', Child.human_attribute_name('name') end def test_translated_model_attributes_with_ancestors_fallback - I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } } + I18n.backend.store_translations 'en', activemodel: { attributes: { person: { name: 'person name attribute'} } } assert_equal 'person name attribute', Child.human_attribute_name('name') end def test_translated_model_attributes_with_attribute_matching_namespaced_model_name - I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:gender => 'person gender'}, :"person/gender" => {:attribute => 'person gender attribute'}}} + I18n.backend.store_translations 'en', activemodel: { attributes: { + person: { gender: 'person gender'}, + :"person/gender" => { attribute: 'person gender attribute' } + } } assert_equal 'person gender', Person.human_attribute_name('gender') assert_equal 'person gender attribute', Person::Gender.human_attribute_name('attribute') end def test_translated_deeply_nested_model_attributes - I18n.backend.store_translations 'en', :activemodel => {:attributes => {:"person/contacts/addresses" => {:street => 'Deeply Nested Address Street'}}} + I18n.backend.store_translations 'en', activemodel: { attributes: { :"person/contacts/addresses" => { street: 'Deeply Nested Address Street' } } } assert_equal 'Deeply Nested Address Street', Person.human_attribute_name('contacts.addresses.street') end def test_translated_nested_model_attributes - I18n.backend.store_translations 'en', :activemodel => {:attributes => {:"person/addresses" => {:street => 'Person Address Street'}}} + I18n.backend.store_translations 'en', activemodel: { attributes: { :"person/addresses" => { street: 'Person Address Street' } } } assert_equal 'Person Address Street', Person.human_attribute_name('addresses.street') end def test_translated_nested_model_attributes_with_namespace_fallback - I18n.backend.store_translations 'en', :activemodel => {:attributes => {:addresses => {:street => 'Cool Address Street'}}} + I18n.backend.store_translations 'en', activemodel: { attributes: { addresses: { street: 'Cool Address Street' } } } assert_equal 'Cool Address Street', Person.human_attribute_name('addresses.street') end def test_translated_model_names - I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} } + I18n.backend.store_translations 'en', activemodel: { models: { person: 'person model' } } assert_equal 'person model', Person.model_name.human end def test_translated_model_names_with_sti - I18n.backend.store_translations 'en', :activemodel => {:models => {:child => 'child model'} } + I18n.backend.store_translations 'en', activemodel: { models: { child: 'child model' } } assert_equal 'child model', Child.model_name.human end def test_translated_model_names_with_ancestors_fallback - I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} } + I18n.backend.store_translations 'en', activemodel: { models: { person: 'person model' } } assert_equal 'person model', Child.model_name.human end def test_human_does_not_modify_options - options = { :default => 'person model' } + options = { default: 'person model' } Person.model_name.human(options) - assert_equal({ :default => 'person model' }, options) + assert_equal({ default: 'person model' }, options) end def test_human_attribute_name_does_not_modify_options - options = { :default => 'Cool gender' } + options = { default: 'Cool gender' } Person.human_attribute_name('gender', options) - assert_equal({ :default => 'Cool gender' }, options) + assert_equal({ default: 'Cool gender' }, options) end end diff --git a/activemodel/test/cases/validations/absence_validation_test.rb b/activemodel/test/cases/validations/absence_validation_test.rb new file mode 100644 index 0000000000..c05d71de5a --- /dev/null +++ b/activemodel/test/cases/validations/absence_validation_test.rb @@ -0,0 +1,67 @@ +# encoding: utf-8 +require 'cases/helper' +require 'models/topic' +require 'models/person' +require 'models/custom_reader' + +class AbsenceValidationTest < ActiveModel::TestCase + teardown do + Topic.reset_callbacks(:validate) + Person.reset_callbacks(:validate) + CustomReader.reset_callbacks(:validate) + end + + def test_validate_absences + Topic.validates_absence_of(:title, :content) + t = Topic.new + t.title = "foo" + t.content = "bar" + assert t.invalid? + assert_equal ["must be blank"], t.errors[:title] + assert_equal ["must be blank"], t.errors[:content] + t.title = "" + t.content = "something" + assert t.invalid? + assert_equal ["must be blank"], t.errors[:content] + t.content = "" + assert t.valid? + end + + def test_accepts_array_arguments + Topic.validates_absence_of %w(title content) + t = Topic.new + t.title = "foo" + t.content = "bar" + assert t.invalid? + assert_equal ["must be blank"], t.errors[:title] + assert_equal ["must be blank"], t.errors[:content] + end + + def test_validates_acceptance_of_with_custom_error_using_quotes + Person.validates_absence_of :karma, message: "This string contains 'single' and \"double\" quotes" + p = Person.new + p.karma = "good" + assert p.invalid? + assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last + end + + def test_validates_absence_of_for_ruby_class + Person.validates_absence_of :karma + p = Person.new + p.karma = "good" + assert p.invalid? + assert_equal ["must be blank"], p.errors[:karma] + p.karma = nil + assert p.valid? + end + + def test_validates_absence_of_for_ruby_class_with_custom_reader + CustomReader.validates_absence_of :karma + p = CustomReader.new + p[:karma] = "excellent" + assert p.invalid? + assert_equal ["must be blank"], p.errors[:karma] + p[:karma] = "" + assert p.valid? + end +end 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/callbacks_test.rb b/activemodel/test/cases/validations/callbacks_test.rb index 0015b3c196..6cd0f4ed4d 100644 --- a/activemodel/test/cases/validations/callbacks_test.rb +++ b/activemodel/test/cases/validations/callbacks_test.rb @@ -40,8 +40,29 @@ class DogWithMissingName < Dog validates_presence_of :name end +class DogValidatorWithIfCondition < Dog + before_validation :set_before_validation_marker1, if: -> { true } + before_validation :set_before_validation_marker2, if: -> { false } + + after_validation :set_after_validation_marker1, if: -> { true } + after_validation :set_after_validation_marker2, if: -> { false } + + def set_before_validation_marker1; self.history << 'before_validation_marker1'; end + def set_before_validation_marker2; self.history << 'before_validation_marker2' ; end + + def set_after_validation_marker1; self.history << 'after_validation_marker1'; end + def set_after_validation_marker2; self.history << 'after_validation_marker2' ; end +end + + class CallbacksWithMethodNamesShouldBeCalled < ActiveModel::TestCase + def test_if_condition_is_respected_for_before_validation + d = DogValidatorWithIfCondition.new + d.valid? + assert_equal ["before_validation_marker1", "after_validation_marker1"], d.history + end + def test_before_validation_and_after_validation_callbacks_should_be_called d = DogWithMethodCallbacks.new d.valid? diff --git a/activemodel/test/cases/validations/conditional_validation_test.rb b/activemodel/test/cases/validations/conditional_validation_test.rb index e06b04af19..5049d6dd61 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,23 +20,23 @@ 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? + assert_empty t.errors[:title] end 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? + assert_empty t.errors[:title] end 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,23 +54,23 @@ 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? + assert_empty t.errors[: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 %{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? + assert_empty t.errors[:title] end 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,26 +89,26 @@ 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? + assert_empty t.errors[:title] end 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? + assert_empty t.errors[:title] end 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,11 +120,11 @@ 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" - assert t.errors[:author_name].empty?, "A topic without an 'important' title should not require an author" + assert_empty t.errors[:author_name], "A topic without an 'important' title should not require an author" t.title = "Just a title" assert t.valid?, "A topic with a basic title should be valid" diff --git a/activemodel/test/cases/validations/confirmation_validation_test.rb b/activemodel/test/cases/validations/confirmation_validation_test.rb index f7556a249f..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) @@ -71,4 +71,35 @@ class ConfirmationValidationTest < ActiveModel::TestCase I18n.backend = @old_backend end + test "does not override confirmation reader if present" do + klass = Class.new do + include ActiveModel::Validations + + def title_confirmation + "expected title" + end + + validates_confirmation_of :title + end + + assert_equal "expected title", klass.new.title_confirmation, + "confirmation validation should not override the reader" + end + + test "does not override confirmation writer if present" do + klass = Class.new do + include ActiveModel::Validations + + def title_confirmation=(value) + @title_confirmation = "expected title" + end + + validates_confirmation_of :title + end + + model = klass.new + model.title_confirmation = "new title" + assert_equal "expected title", model.title_confirmation, + "confirmation validation should not override the writer" + end end 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 df0fcd243a..40a5aee997 100644 --- a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb @@ -8,131 +8,131 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase @person = Person.new end - # validates_inclusion_of: generate_message(attr_name, :inclusion, :message => custom_message, :value => value) + # 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) + # 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) + # 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) + # validates_confirmation_of: generate_message(attr_name, :confirmation, message: custom_message) def test_generate_message_confirmation_with_default_message assert_equal "doesn't match Title", @person.errors.generate_message(:title, :confirmation) 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) + # validates_acceptance_of: generate_message(attr_name, :accepted, message: custom_message) def test_generate_message_accepted_with_default_message assert_equal "must be accepted", @person.errors.generate_message(:title, :accepted) 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) + # add_on_empty: generate_message(attr, :empty, message: custom_message) def test_generate_message_empty_with_default_message assert_equal "can't be empty", @person.errors.generate_message(:title, :empty) 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) + # add_on_blank: generate_message(attr, :blank, message: custom_message) def test_generate_message_blank_with_default_message assert_equal "can't be blank", @person.errors.generate_message(:title, :blank) 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) + # 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) + # 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) + # 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) + # 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) + # 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 4f8b7327c0..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 @@ -21,42 +21,22 @@ class I18nValidationTest < ActiveModel::TestCase I18n.backend = @old_backend end - def test_errors_add_on_empty_generates_message - @person.errors.expects(:generate_message).with(:title, :empty, {}) - @person.errors.add_on_empty :title - end - - def test_errors_add_on_empty_generates_message_with_custom_default_message - @person.errors.expects(:generate_message).with(:title, :empty, {:message => 'custom'}) - @person.errors.add_on_empty :title, :message => 'custom' - end - - def test_errors_add_on_blank_generates_message - @person.errors.expects(:generate_message).with(:title, :blank, {}) - @person.errors.add_on_blank :title - end - - def test_errors_add_on_blank_generates_message_with_custom_default_message - @person.errors.expects(:generate_message).with(:title, :blank, {:message => 'custom'}) - @person.errors.add_on_blank :title, :message => 'custom' - 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 @@ -69,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 @@ -81,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 @@ -90,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 @@ -110,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 @@ -120,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 @@ -131,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 @@ -141,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 @@ -152,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 @@ -163,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 @@ -174,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 @@ -185,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 @@ -198,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 @@ -207,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 @@ -218,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 @@ -229,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 @@ -246,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? @@ -256,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? @@ -284,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 @@ -296,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 @@ -339,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..8b90856869 100644 --- a/activemodel/test/cases/validations/inclusion_validation_test.rb +++ b/activemodel/test/cases/validations/inclusion_validation_test.rb @@ -1,5 +1,6 @@ # encoding: utf-8 require 'cases/helper' +require 'active_support/all' require 'models/topic' require 'models/person' @@ -11,16 +12,38 @@ 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" => "aaab", "content" => "abc").invalid? assert Topic.new("title" => "aaa", "content" => "abc").valid? assert Topic.new("title" => "abc", "content" => "abc").valid? assert Topic.new("title" => "bbb", "content" => "abc").valid? end + def test_validates_inclusion_of_time_range + Topic.validates_inclusion_of(:created_at, in: 1.year.ago..Time.now) + assert Topic.new(title: 'aaa', created_at: 2.years.ago).invalid? + assert Topic.new(title: 'aaa', created_at: 3.months.ago).valid? + assert Topic.new(title: 'aaa', created_at: 37.weeks.from_now).invalid? + end + + def test_validates_inclusion_of_date_range + Topic.validates_inclusion_of(:created_at, in: 1.year.until(Date.today)..Date.today) + assert Topic.new(title: 'aaa', created_at: 2.years.until(Date.today)).invalid? + assert Topic.new(title: 'aaa', created_at: 3.months.until(Date.today)).valid? + assert Topic.new(title: 'aaa', created_at: 37.weeks.since(Date.today)).invalid? + end + + def test_validates_inclusion_of_date_time_range + Topic.validates_inclusion_of(:created_at, in: 1.year.until(DateTime.current)..DateTime.current) + assert Topic.new(title: 'aaa', created_at: 2.years.until(DateTime.current)).invalid? + assert Topic.new(title: 'aaa', created_at: 3.months.until(DateTime.current)).valid? + assert Topic.new(title: 'aaa', created_at: 37.weeks.since(DateTime.current)).invalid? + 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 +56,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 +73,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 +84,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 +94,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 +109,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 +121,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/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index 113bfd6337..8b2f886cc4 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -5,13 +5,12 @@ require 'models/topic' require 'models/person' class LengthValidationTest < ActiveModel::TestCase - def teardown Topic.reset_callbacks(:validate) end def test_validates_length_of_with_allow_nil - Topic.validates_length_of( :title, :is => 5, :allow_nil => true ) + Topic.validates_length_of( :title, is: 5, allow_nil: true ) assert Topic.new("title" => "ab").invalid? assert Topic.new("title" => "").invalid? @@ -20,7 +19,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_with_allow_blank - Topic.validates_length_of( :title, :is => 5, :allow_blank => true ) + Topic.validates_length_of( :title, is: 5, allow_blank: true ) assert Topic.new("title" => "ab").invalid? assert Topic.new("title" => "").valid? @@ -29,7 +28,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_minimum - Topic.validates_length_of :title, :minimum => 5 + Topic.validates_length_of :title, minimum: 5 t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? @@ -51,13 +50,13 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_maximum_should_allow_nil - Topic.validates_length_of :title, :maximum => 10 + Topic.validates_length_of :title, maximum: 10 t = Topic.new assert t.valid? end def test_optionally_validates_length_of_using_minimum - Topic.validates_length_of :title, :minimum => 5, :allow_nil => true + Topic.validates_length_of :title, minimum: 5, allow_nil: true t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? @@ -67,7 +66,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_maximum - Topic.validates_length_of :title, :maximum => 5 + Topic.validates_length_of :title, maximum: 5 t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? @@ -82,7 +81,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_optionally_validates_length_of_using_maximum - Topic.validates_length_of :title, :maximum => 5, :allow_nil => true + Topic.validates_length_of :title, maximum: 5, allow_nil: true t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? @@ -92,7 +91,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_within - Topic.validates_length_of(:title, :content, :within => 3..5) + Topic.validates_length_of(:title, :content, within: 3..5) t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long") assert t.invalid? @@ -111,7 +110,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_within_with_exclusive_range - Topic.validates_length_of(:title, :within => 4...10) + Topic.validates_length_of(:title, within: 4...10) t = Topic.new("title" => "9 chars!!") assert t.valid? @@ -125,7 +124,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_optionally_validates_length_of_using_within - Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true + Topic.validates_length_of :title, :content, within: 3..5, allow_nil: true t = Topic.new('title' => 'abc', 'content' => 'abcd') assert t.valid? @@ -135,7 +134,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_is - Topic.validates_length_of :title, :is => 5 + Topic.validates_length_of :title, is: 5 t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? @@ -153,7 +152,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_optionally_validates_length_of_using_is - Topic.validates_length_of :title, :is => 5, :allow_nil => true + Topic.validates_length_of :title, is: 5, allow_nil: true t = Topic.new("title" => "valid", "content" => "whatever") assert t.valid? @@ -167,25 +166,25 @@ class LengthValidationTest < ActiveModel::TestCase bigmax = 2 ** 32 bigrange = bigmin...bigmax assert_nothing_raised do - Topic.validates_length_of :title, :is => bigmin + 5 - Topic.validates_length_of :title, :within => bigrange - Topic.validates_length_of :title, :in => bigrange - Topic.validates_length_of :title, :minimum => bigmin - Topic.validates_length_of :title, :maximum => bigmax + Topic.validates_length_of :title, is: bigmin + 5 + Topic.validates_length_of :title, within: bigrange + Topic.validates_length_of :title, in: bigrange + Topic.validates_length_of :title, minimum: bigmin + Topic.validates_length_of :title, maximum: bigmax end end def test_validates_length_of_nasty_params - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is => -6) } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within => 6) } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum => "a") } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum => "a") } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within => "a") } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is => "a") } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, is: -6) } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, within: 6) } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, minimum: "a") } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, maximum: "a") } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, within: "a") } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, is: "a") } end def test_validates_length_of_custom_errors_for_minimum_with_message - Topic.validates_length_of( :title, :minimum => 5, :message => "boo %{count}" ) + Topic.validates_length_of( :title, minimum: 5, message: "boo %{count}" ) t = Topic.new("title" => "uhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -193,7 +192,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_custom_errors_for_minimum_with_too_short - Topic.validates_length_of( :title, :minimum => 5, :too_short => "hoo %{count}" ) + Topic.validates_length_of( :title, minimum: 5, too_short: "hoo %{count}" ) t = Topic.new("title" => "uhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -201,7 +200,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_custom_errors_for_maximum_with_message - Topic.validates_length_of( :title, :maximum => 5, :message => "boo %{count}" ) + Topic.validates_length_of( :title, maximum: 5, message: "boo %{count}" ) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -209,7 +208,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_custom_errors_for_in - Topic.validates_length_of(:title, :in => 10..20, :message => "hoo %{count}") + Topic.validates_length_of(:title, in: 10..20, message: "hoo %{count}") t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -222,7 +221,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_custom_errors_for_maximum_with_too_long - Topic.validates_length_of( :title, :maximum => 5, :too_long => "hoo %{count}" ) + Topic.validates_length_of( :title, maximum: 5, too_long: "hoo %{count}" ) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -230,21 +229,21 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_custom_errors_for_both_too_short_and_too_long - Topic.validates_length_of :title, :minimum => 3, :maximum => 5, :too_short => 'too short', :too_long => 'too long' + Topic.validates_length_of :title, minimum: 3, maximum: 5, too_short: 'too short', too_long: 'too long' - t = Topic.new(:title => 'a') + t = Topic.new(title: 'a') assert t.invalid? assert t.errors[:title].any? assert_equal ['too short'], t.errors['title'] - t = Topic.new(:title => 'aaaaaa') + t = Topic.new(title: 'aaaaaa') assert t.invalid? assert t.errors[:title].any? assert_equal ['too long'], t.errors['title'] end def test_validates_length_of_custom_errors_for_is_with_message - Topic.validates_length_of( :title, :is => 5, :message => "boo %{count}" ) + Topic.validates_length_of( :title, is: 5, message: "boo %{count}" ) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -252,7 +251,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_custom_errors_for_is_with_wrong_length - Topic.validates_length_of( :title, :is => 5, :wrong_length => "hoo %{count}" ) + Topic.validates_length_of( :title, is: 5, wrong_length: "hoo %{count}" ) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -260,7 +259,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_minimum_utf8 - Topic.validates_length_of :title, :minimum => 5 + Topic.validates_length_of :title, minimum: 5 t = Topic.new("title" => "一二三四五", "content" => "whatever") assert t.valid? @@ -272,7 +271,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_maximum_utf8 - Topic.validates_length_of :title, :maximum => 5 + Topic.validates_length_of :title, maximum: 5 t = Topic.new("title" => "一二三四五", "content" => "whatever") assert t.valid? @@ -284,7 +283,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_within_utf8 - Topic.validates_length_of(:title, :content, :within => 3..5) + Topic.validates_length_of(:title, :content, within: 3..5) t = Topic.new("title" => "一二", "content" => "12三四五六七") assert t.invalid? @@ -296,12 +295,12 @@ class LengthValidationTest < ActiveModel::TestCase end def test_optionally_validates_length_of_using_within_utf8 - Topic.validates_length_of :title, :within => 3..5, :allow_nil => true + Topic.validates_length_of :title, within: 3..5, allow_nil: true - t = Topic.new(:title => "一二三四五") + t = Topic.new(title: "一二三四五") assert t.valid?, t.errors.inspect - t = Topic.new(:title => "一二三") + t = Topic.new(title: "一二三") assert t.valid?, t.errors.inspect t.title = nil @@ -309,7 +308,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_is_utf8 - Topic.validates_length_of :title, :is => 5 + Topic.validates_length_of :title, is: 5 t = Topic.new("title" => "一二345", "content" => "whatever") assert t.valid? @@ -321,9 +320,9 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_with_block - Topic.validates_length_of :content, :minimum => 5, :too_short => "Your essay must be at least %{count} words.", - :tokenizer => lambda {|str| str.scan(/\w+/) } - t = Topic.new(:content => "this content should be long enough") + Topic.validates_length_of :content, minimum: 5, too_short: "Your essay must be at least %{count} words.", + tokenizer: lambda {|str| str.scan(/\w+/) } + t = Topic.new(content: "this content should be long enough") assert t.valid? t.content = "not long enough" @@ -333,18 +332,18 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_for_fixnum - Topic.validates_length_of(:approved, :is => 4) + Topic.validates_length_of(:approved, is: 4) - t = Topic.new("title" => "uhohuhoh", "content" => "whatever", :approved => 1) + t = Topic.new("title" => "uhohuhoh", "content" => "whatever", approved: 1) assert t.invalid? assert t.errors[:approved].any? - t = Topic.new("title" => "uhohuhoh", "content" => "whatever", :approved => 1234) + t = Topic.new("title" => "uhohuhoh", "content" => "whatever", approved: 1234) assert t.valid? end def test_validates_length_of_for_ruby_class - Person.validates_length_of :karma, :minimum => 5 + Person.validates_length_of :karma, minimum: 5 p = Person.new p.karma = "Pix" @@ -359,7 +358,7 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_for_infinite_maxima - Topic.validates_length_of(:title, :within => 5..Float::INFINITY) + Topic.validates_length_of(:title, within: 5..Float::INFINITY) t = Topic.new("title" => "1234") assert t.invalid? @@ -368,11 +367,58 @@ class LengthValidationTest < ActiveModel::TestCase t.title = "12345" assert t.valid? - Topic.validates_length_of(:author_name, :maximum => Float::INFINITY) + Topic.validates_length_of(:author_name, maximum: Float::INFINITY) assert t.valid? t.author_name = "A very long author name that should still be valid." * 100 assert t.valid? end + + def test_validates_length_of_using_maximum_should_not_allow_nil_when_nil_not_allowed + Topic.validates_length_of :title, maximum: 10, allow_nil: false + t = Topic.new + assert t.invalid? + end + + def test_validates_length_of_using_maximum_should_not_allow_nil_and_empty_string_when_blank_not_allowed + Topic.validates_length_of :title, maximum: 10, allow_blank: false + t = Topic.new + assert t.invalid? + + t.title = "" + assert t.invalid? + end + + def test_validates_length_of_using_both_minimum_and_maximum_should_not_allow_nil + Topic.validates_length_of :title, minimum: 5, maximum: 10 + t = Topic.new + assert t.invalid? + end + + def test_validates_length_of_using_minimum_0_should_not_allow_nil + Topic.validates_length_of :title, minimum: 0 + t = Topic.new + assert t.invalid? + + t.title = "" + assert t.valid? + end + + def test_validates_length_of_using_is_0_should_not_allow_nil + Topic.validates_length_of :title, is: 0 + t = Topic.new + assert t.invalid? + + t.title = "" + assert t.valid? + end + + def test_validates_with_diff_in_option + Topic.validates_length_of(:title, is: 5) + Topic.validates_length_of(:title, is: 5, if: Proc.new { false } ) + + assert Topic.new("title" => "david").valid? + assert Topic.new("title" => "david2").invalid? + end end 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/presence_validation_test.rb b/activemodel/test/cases/validations/presence_validation_test.rb index 510c13a7c3..2f228cfa83 100644 --- a/activemodel/test/cases/validations/presence_validation_test.rb +++ b/activemodel/test/cases/validations/presence_validation_test.rb @@ -41,7 +41,7 @@ class PresenceValidationTest < ActiveModel::TestCase end def test_validates_acceptance_of_with_custom_error_using_quotes - Person.validates_presence_of :karma, :message => "This string contains 'single' and \"double\" quotes" + Person.validates_presence_of :karma, message: "This string contains 'single' and \"double\" quotes" p = Person.new assert p.invalid? assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last @@ -70,4 +70,38 @@ class PresenceValidationTest < ActiveModel::TestCase p[:karma] = "Cold" assert p.valid? end + + def test_validates_presence_of_with_allow_nil_option + Topic.validates_presence_of(:title, allow_nil: true) + + t = Topic.new(title: "something") + assert t.valid?, t.errors.full_messages + + t.title = "" + assert t.invalid? + assert_equal ["can't be blank"], t.errors[:title] + + t.title = " " + assert t.invalid?, t.errors.full_messages + assert_equal ["can't be blank"], t.errors[:title] + + t.title = nil + assert t.valid?, t.errors.full_messages + end + + def test_validates_presence_of_with_allow_blank_option + Topic.validates_presence_of(:title, allow_blank: true) + + t = Topic.new(title: "something") + assert t.valid?, t.errors.full_messages + + t.title = "" + assert t.valid?, t.errors.full_messages + + t.title = " " + assert t.valid?, t.errors.full_messages + + t.title = nil + assert t.valid?, t.errors.full_messages + end end diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index 90bc018ae1..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,33 +115,33 @@ 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] end def test_validates_with_included_validator_and_wildcard_shortcut - # Shortcut for PersonWithValidator.validates :title, :like => { :with => "Mr." } - PersonWithValidator.validates :title, :like => "Mr." + # Shortcut for PersonWithValidator.validates :title, like: { with: "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 15a49e38dd..5f99b320a6 100644 --- a/activemodel/test/cases/validations/validations_context_test.rb +++ b/activemodel/test/cases/validations/validations_context_test.rb @@ -18,22 +18,22 @@ class ValidationsContextTest < ActiveModel::TestCase end end - test "with a class that adds errors on update and validating a new model with no arguments" do - Topic.validates_with(ValidatorThatAddsErrors, :on => :create) + 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 = Topic.new - assert topic.valid?, "Validation doesn't run on create if 'on' is set to update" + 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 07c1bd0533..93716f1433 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -50,7 +50,7 @@ class ValidatesWithTest < ActiveModel::TestCase end end - test "vaidation with class that adds errors" do + test "validation with class that adds errors" do Topic.validates_with(ValidatorThatAddsErrors) topic = Topic.new assert topic.invalid?, "A class that adds errors causes the record to be invalid" @@ -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,45 +100,23 @@ 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", class: Topic).returns(validator) validator.expects(:validate).with(topic) - Topic.validates_with(validator, :if => "1 == 1", :foo => :bar) - assert topic.valid? - end - - test "calls setup method of validator passing in self when validator has setup method" do - topic = Topic.new - validator = stub_everything - validator.stubs(:new).returns(validator) - validator.stubs(:validate) - validator.stubs(:respond_to?).with(:setup).returns(true) - validator.expects(:setup).with(Topic).once - Topic.validates_with(validator) - assert topic.valid? - end - - test "doesn't call setup method of validator when validator has no setup method" do - topic = Topic.new - validator = stub_everything - validator.stubs(:new).returns(validator) - validator.stubs(:validate) - validator.stubs(:respond_to?).with(:setup).returns(false) - validator.expects(:setup).with(Topic).never - Topic.validates_with(validator) + Topic.validates_with(validator, if: "1 == 1", foo: :bar) assert topic.valid? 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,36 +124,36 @@ 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 test "each validator expects attributes to be given" do - assert_raise RuntimeError do + assert_raise ArgumentError do Topic.validates_with(ValidatorPerEachAttribute) end 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 +163,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] diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index a9d32808da..039b6b8872 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -26,11 +26,11 @@ class ValidationsTest < ActiveModel::TestCase def test_single_field_validation r = Reply.new r.title = "There's no content!" - assert r.invalid?, "A reply without content shouldn't be saveable" + assert r.invalid?, "A reply without content shouldn't be savable" assert r.after_validation_performed, "after_validation callback should be called" r.content = "Messa content!" - assert r.valid?, "A reply with content should be saveable" + assert r.valid?, "A reply with content should be savable" assert r.after_validation_performed, "after_validation callback should be called" end @@ -166,7 +166,7 @@ class ValidationsTest < ActiveModel::TestCase def test_invalid_validator Topic.validate :i_dont_exist - assert_raise(NameError) do + assert_raises(NoMethodError) do t = Topic.new t.valid? end @@ -190,16 +190,16 @@ class ValidationsTest < ActiveModel::TestCase def test_validation_order Topic.validates_presence_of :title - Topic.validates_length_of :title, :minimum => 2 + Topic.validates_length_of :title, minimum: 2 t = Topic.new("title" => "") assert t.invalid? assert_equal "can't be blank", t.errors["title"].first Topic.validates_presence_of :title, :author_name Topic.validate {errors.add('author_email_address', 'will never be valid')} - Topic.validates_length_of :title, :content, :minimum => 2 + Topic.validates_length_of :title, :content, minimum: 2 - t = Topic.new :title => '' + t = Topic.new title: '' assert t.invalid? assert_equal :title, key = t.errors.keys[0] @@ -213,10 +213,10 @@ class ValidationsTest < ActiveModel::TestCase assert_equal 'is too short (minimum is 2 characters)', t.errors[key][0] end - def test_validaton_with_if_and_on - Topic.validates_presence_of :title, :if => Proc.new{|x| x.author_name = "bad"; true }, :on => :update + def test_validation_with_if_and_on + Topic.validates_presence_of :title, if: Proc.new{|x| x.author_name = "bad"; true }, on: :update - t = Topic.new(:title => "") + t = Topic.new(title: "") # If block should not fire assert t.valid? @@ -239,7 +239,7 @@ class ValidationsTest < ActiveModel::TestCase end def test_validation_with_message_as_proc - Topic.validates_presence_of(:title, :message => proc { "no blanks here".upcase }) + Topic.validates_presence_of(:title, message: proc { "no blanks here".upcase }) t = Topic.new assert t.invalid? @@ -248,7 +248,7 @@ class ValidationsTest < ActiveModel::TestCase def test_list_of_validators_for_model Topic.validates_presence_of :title - Topic.validates_length_of :title, :minimum => 2 + Topic.validates_length_of :title, minimum: 2 assert_equal 2, Topic.validators.count assert_equal [:presence, :length], Topic.validators.map(&:kind) @@ -256,7 +256,7 @@ class ValidationsTest < ActiveModel::TestCase def test_list_of_validators_on_an_attribute Topic.validates_presence_of :title, :content - Topic.validates_length_of :title, :minimum => 2 + Topic.validates_length_of :title, minimum: 2 assert_equal 2, Topic.validators_on(:title).count assert_equal [:presence, :length], Topic.validators_on(:title).map(&:kind) @@ -265,13 +265,13 @@ class ValidationsTest < ActiveModel::TestCase end def test_accessing_instance_of_validator_on_an_attribute - Topic.validates_length_of :title, :minimum => 10 + Topic.validates_length_of :title, minimum: 10 assert_equal 10, Topic.validators_on(:title).first.options[:minimum] end def test_list_of_validators_on_multiple_attributes - Topic.validates :title, :length => { :minimum => 10 } - Topic.validates :author_name, :presence => true, :format => /a/ + Topic.validates :title, length: { minimum: 10 } + Topic.validates :author_name, presence: true, format: /a/ validators = Topic.validators_on(:title, :author_name) @@ -283,7 +283,7 @@ class ValidationsTest < ActiveModel::TestCase end def test_list_of_validators_will_be_empty_when_empty - Topic.validates :title, :length => { :minimum => 10 } + Topic.validates :title, length: { minimum: 10 } assert_equal [], Topic.validators_on(:author_name) end @@ -300,52 +300,52 @@ class ValidationsTest < ActiveModel::TestCase end def test_strict_validation_in_validates - Topic.validates :title, :strict => true, :presence => true + Topic.validates :title, strict: true, presence: true assert_raises ActiveModel::StrictValidationFailed do Topic.new.valid? end end def test_strict_validation_not_fails - Topic.validates :title, :strict => true, :presence => true - assert Topic.new(:title => "hello").valid? + Topic.validates :title, strict: true, presence: true + assert Topic.new(title: "hello").valid? end def test_strict_validation_particular_validator - Topic.validates :title, :presence => { :strict => true } + Topic.validates :title, presence: { strict: true } assert_raises ActiveModel::StrictValidationFailed do Topic.new.valid? end end def test_strict_validation_in_custom_validator_helper - Topic.validates_presence_of :title, :strict => true + Topic.validates_presence_of :title, strict: true assert_raises ActiveModel::StrictValidationFailed do Topic.new.valid? end end def test_strict_validation_custom_exception - Topic.validates_presence_of :title, :strict => CustomStrictValidationException + Topic.validates_presence_of :title, strict: CustomStrictValidationException assert_raises CustomStrictValidationException do Topic.new.valid? end end def test_validates_with_bang - Topic.validates! :title, :presence => true + Topic.validates! :title, presence: true assert_raises ActiveModel::StrictValidationFailed do Topic.new.valid? end end def test_validates_with_false_hash_value - Topic.validates :title, :presence => false + Topic.validates :title, presence: false assert Topic.new.valid? end def test_strict_validation_error_message - Topic.validates :title, :strict => true, :presence => true + Topic.validates :title, strict: true, presence: true exception = assert_raises(ActiveModel::StrictValidationFailed) do Topic.new.valid? @@ -354,14 +354,14 @@ class ValidationsTest < ActiveModel::TestCase end def test_does_not_modify_options_argument - options = { :presence => true } + options = { presence: true } Topic.validates :title, options - assert_equal({ :presence => true }, options) + assert_equal({ presence: true }, options) end def test_dup_validity_is_independent Topic.validates_presence_of :title - topic = Topic.new("title" => "Litterature") + topic = Topic.new("title" => "Literature") topic.valid? duped = topic.dup @@ -373,4 +373,25 @@ class ValidationsTest < ActiveModel::TestCase assert topic.invalid? assert duped.valid? end + + # validator test: + def test_setup_is_deprecated_but_still_receives_klass # TODO: remove me in 4.2. + validator_class = Class.new(ActiveModel::Validator) do + def setup(klass) + @old_klass = klass + end + + def validate(*) + @old_klass == Topic or raise "#setup didn't work" + end + end + + assert_deprecated do + Topic.validates_with validator_class + end + + t = Topic.new + t.valid? + end + end diff --git a/activemodel/test/models/automobile.rb b/activemodel/test/models/automobile.rb index 021ea61c80..ece644c40c 100644 --- a/activemodel/test/models/automobile.rb +++ b/activemodel/test/models/automobile.rb @@ -7,6 +7,6 @@ class Automobile def validations validates_presence_of :make - validates_length_of :model, :within => 2..10 + validates_length_of :model, within: 2..10 end -end
\ No newline at end of file +end diff --git a/activemodel/test/models/contact.rb b/activemodel/test/models/contact.rb index 7bfc542afb..c25be28e1d 100644 --- a/activemodel/test/models/contact.rb +++ b/activemodel/test/models/contact.rb @@ -9,7 +9,7 @@ class Contact end def network - {:git => :github} + { git: :github } end def initialize(options = {}) diff --git a/activemodel/test/models/helicopter.rb b/activemodel/test/models/helicopter.rb index a52b6fb4dd..933f3c463a 100644 --- a/activemodel/test/models/helicopter.rb +++ b/activemodel/test/models/helicopter.rb @@ -1,3 +1,7 @@ class Helicopter include ActiveModel::Conversion end + +class Helicopter::Comanche + include ActiveModel::Conversion +end diff --git a/activemodel/test/models/observers.rb b/activemodel/test/models/observers.rb deleted file mode 100644 index 3729b3435e..0000000000 --- a/activemodel/test/models/observers.rb +++ /dev/null @@ -1,27 +0,0 @@ -class ORM - include ActiveModel::Observing - - def save - notify_observers :before_save - end - - class Observer < ActiveModel::Observer - def before_save_invocations - @before_save_invocations ||= [] - end - - def before_save(record) - before_save_invocations << record - end - end -end - -class Widget < ORM; end -class Budget < ORM; end -class WidgetObserver < ORM::Observer; end -class BudgetObserver < ORM::Observer; end -class AuditTrail < ORM::Observer - observe :widget, :budget -end - -ORM.instantiate_observers diff --git a/activemodel/test/models/reply.rb b/activemodel/test/models/reply.rb index ec1efeac19..b77910e671 100644 --- a/activemodel/test/models/reply.rb +++ b/activemodel/test/models/reply.rb @@ -2,11 +2,11 @@ require 'models/topic' class Reply < Topic validate :errors_on_empty_content - validate :title_is_wrong_create, :on => :create + validate :title_is_wrong_create, on: :create validate :check_empty_title - validate :check_content_mismatch, :on => :create - validate :check_wrong_update, :on => :update + validate :check_content_mismatch, on: :create + validate :check_wrong_update, on: :update def check_empty_title errors[:title] << "is Empty" unless title && title.size > 0 diff --git a/activemodel/test/models/topic.rb b/activemodel/test/models/topic.rb index c9af78f595..1411a093e9 100644 --- a/activemodel/test/models/topic.rb +++ b/activemodel/test/models/topic.rb @@ -6,7 +6,7 @@ class Topic super | [ :message ] end - attr_accessor :title, :author_name, :content, :approved + attr_accessor :title, :author_name, :content, :approved, :created_at attr_accessor :after_validation_performed after_validation :perform_after_validation |