diff options
Diffstat (limited to 'activemodel/test/cases')
52 files changed, 2070 insertions, 1086 deletions
diff --git a/activemodel/test/cases/attribute_assignment_test.rb b/activemodel/test/cases/attribute_assignment_test.rb index 287bea719c..5ecf0a69c4 100644 --- a/activemodel/test/cases/attribute_assignment_test.rb +++ b/activemodel/test/cases/attribute_assignment_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "cases/helper" require "active_support/core_ext/hash/indifferent_access" require "active_support/hash_with_indifferent_access" @@ -16,9 +18,11 @@ class AttributeAssignmentTest < ActiveModel::TestCase raise ErrorFromAttributeWriter end + # TODO Change this to private once we've dropped Ruby 2.2 support. + # Workaround for Ruby 2.2 "private attribute?" warning. protected - attr_writer :metadata + attr_writer :metadata end class ErrorFromAttributeWriter < StandardError diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index e81b7ac424..d2837ec894 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -1,16 +1,18 @@ -require 'cases/helper' +# frozen_string_literal: true + +require "cases/helper" class ModelWithAttributes include ActiveModel::AttributeMethods class << self define_method(:bar) do - 'original bar' + "original bar" end end def attributes - { foo: 'value of foo', baz: 'value of baz' } + { foo: "value of foo", baz: "value of baz" } end private @@ -24,7 +26,7 @@ class ModelWithAttributes2 attr_accessor :attributes - attribute_method_suffix '_test' + attribute_method_suffix "_test" private def attribute(name) @@ -48,7 +50,7 @@ class ModelWithAttributesWithSpaces include ActiveModel::AttributeMethods def attributes - { :'foo bar' => 'value of foo bar'} + { 'foo bar': "value of foo bar" } end private @@ -62,12 +64,12 @@ class ModelWithWeirdNamesAttributes class << self define_method(:'c?d') do - 'original c?d' + "original c?d" end end def attributes - { :'a?b' => 'value of a?b' } + { 'a?b': "value of a?b" } end private @@ -80,7 +82,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 @@ -94,16 +96,16 @@ class ModelWithoutAttributesMethod end class AttributeMethodsTest < ActiveModel::TestCase - test 'method missing works correctly even if attributes method is not defined' do + test "method missing works correctly even if attributes method is not defined" do assert_raises(NoMethodError) { ModelWithoutAttributesMethod.new.foo } end - test 'unrelated classes should not share attribute method matchers' do + test "unrelated classes should not share attribute method matchers" do assert_not_equal ModelWithAttributes.send(:attribute_method_matchers), ModelWithAttributes2.send(:attribute_method_matchers) end - test '#define_attribute_method generates attribute method' do + test "#define_attribute_method generates attribute method" do begin ModelWithAttributes.define_attribute_method(:foo) @@ -114,19 +116,19 @@ class AttributeMethodsTest < ActiveModel::TestCase end end - test '#define_attribute_method does not generate attribute method if already defined in attribute module' do + test "#define_attribute_method does not generate attribute method if already defined in attribute module" do klass = Class.new(ModelWithAttributes) - klass.generated_attribute_methods.module_eval do + klass.send(:generated_attribute_methods).module_eval do def foo - '<3' + "<3" end end klass.define_attribute_method(:foo) - assert_equal '<3', klass.new.foo + assert_equal "<3", klass.new.foo end - test '#define_attribute_method generates a method that is already defined on the host' do + test "#define_attribute_method generates a method that is already defined on the host" do klass = Class.new(ModelWithAttributes) do def foo super @@ -134,21 +136,21 @@ class AttributeMethodsTest < ActiveModel::TestCase end klass.define_attribute_method(:foo) - assert_equal 'value of foo', klass.new.foo + assert_equal "value of foo", klass.new.foo end - test '#define_attribute_method generates attribute method with invalid identifier characters' do + test "#define_attribute_method generates attribute method with invalid identifier characters" do begin ModelWithWeirdNamesAttributes.define_attribute_method(:'a?b') assert_respond_to ModelWithWeirdNamesAttributes.new, :'a?b' - assert_equal "value of a?b", ModelWithWeirdNamesAttributes.new.send('a?b') + assert_equal "value of a?b", ModelWithWeirdNamesAttributes.new.send("a?b") ensure ModelWithWeirdNamesAttributes.undefine_attribute_methods end end - test '#define_attribute_methods works passing multiple arguments' do + test "#define_attribute_methods works passing multiple arguments" do begin ModelWithAttributes.define_attribute_methods(:foo, :baz) @@ -159,7 +161,7 @@ class AttributeMethodsTest < ActiveModel::TestCase end end - test '#define_attribute_methods generates attribute methods' do + test "#define_attribute_methods generates attribute methods" do begin ModelWithAttributes.define_attribute_methods(:foo) @@ -170,7 +172,7 @@ class AttributeMethodsTest < ActiveModel::TestCase end end - test '#alias_attribute generates attribute_aliases lookup hash' do + test "#alias_attribute generates attribute_aliases lookup hash" do klass = Class.new(ModelWithAttributes) do define_attribute_methods :foo alias_attribute :bar, :foo @@ -179,7 +181,7 @@ class AttributeMethodsTest < ActiveModel::TestCase assert_equal({ "bar" => "foo" }, klass.attribute_aliases) end - test '#define_attribute_methods generates attribute methods with spaces in their names' do + test "#define_attribute_methods generates attribute methods with spaces in their names" do begin ModelWithAttributesWithSpaces.define_attribute_methods(:'foo bar') @@ -190,7 +192,7 @@ class AttributeMethodsTest < ActiveModel::TestCase end end - test '#alias_attribute works with attributes with spaces in their names' do + test "#alias_attribute works with attributes with spaces in their names" do begin ModelWithAttributesWithSpaces.define_attribute_methods(:'foo bar') ModelWithAttributesWithSpaces.alias_attribute(:'foo_bar', :'foo bar') @@ -201,7 +203,7 @@ class AttributeMethodsTest < ActiveModel::TestCase end end - test '#alias_attribute works with attributes named as a ruby keyword' do + test "#alias_attribute works with attributes named as a ruby keyword" do begin ModelWithRubyKeywordNamedAttributes.define_attribute_methods([:begin, :end]) ModelWithRubyKeywordNamedAttributes.alias_attribute(:from, :begin) @@ -214,7 +216,7 @@ class AttributeMethodsTest < ActiveModel::TestCase end end - test '#undefine_attribute_methods removes attribute methods' do + test "#undefine_attribute_methods removes attribute methods" do ModelWithAttributes.define_attribute_methods(:foo) ModelWithAttributes.undefine_attribute_methods @@ -222,21 +224,21 @@ class AttributeMethodsTest < ActiveModel::TestCase assert_raises(NoMethodError) { ModelWithAttributes.new.foo } end - test 'accessing a suffixed attribute' do + test "accessing a suffixed attribute" do m = ModelWithAttributes2.new - m.attributes = { 'foo' => 'bar' } + m.attributes = { "foo" => "bar" } - assert_equal 'bar', m.foo - assert_equal 'bar', m.foo_test + assert_equal "bar", m.foo + assert_equal "bar", m.foo_test end - test 'should not interfere with method_missing if the attr has a private/protected method' do + 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' } + m.attributes = { "private_method" => "<3", "protected_method" => "O_o" } # dispatches to the *method*, not the attribute - assert_equal '<3 <3', m.send(:private_method) - assert_equal 'O_o O_o', m.send(:protected_method) + assert_equal "<3 <3", m.send(:private_method) + assert_equal "O_o O_o", m.send(:protected_method) # sees that a method is already defined, so doesn't intervene assert_raises(NoMethodError) { m.private_method } @@ -245,13 +247,13 @@ class AttributeMethodsTest < ActiveModel::TestCase class ClassWithProtected protected - def protected_method - end + def protected_method + end end - test 'should not interfere with respond_to? if the attribute has a private/protected method' do + test "should not interfere with respond_to? if the attribute has a private/protected method" do m = ModelWithAttributes2.new - m.attributes = { 'private_method' => '<3', 'protected_method' => 'O_o' } + m.attributes = { "private_method" => "<3", "protected_method" => "O_o" } assert !m.respond_to?(:private_method) assert m.respond_to?(:private_method, true) @@ -264,9 +266,9 @@ class AttributeMethodsTest < ActiveModel::TestCase assert m.respond_to?(:protected_method, true) end - test 'should use attribute_missing to dispatch a missing attribute' do + test "should use attribute_missing to dispatch a missing attribute" do m = ModelWithAttributes2.new - m.attributes = { 'foo' => 'bar' } + m.attributes = { "foo" => "bar" } def m.attribute_missing(match, *args, &block) match @@ -274,8 +276,8 @@ class AttributeMethodsTest < ActiveModel::TestCase match = m.foo_test - assert_equal 'foo', match.attr_name - assert_equal 'attribute_test', match.target - assert_equal 'foo_test', match.method_name + assert_equal "foo", match.attr_name + assert_equal "attribute_test", match.target + assert_equal "foo_test", match.method_name end end diff --git a/activemodel/test/cases/attribute_set_test.rb b/activemodel/test/cases/attribute_set_test.rb new file mode 100644 index 0000000000..6e522d6c80 --- /dev/null +++ b/activemodel/test/cases/attribute_set_test.rb @@ -0,0 +1,257 @@ +# frozen_string_literal: true + +require "cases/helper" +require "active_model/attribute_set" + +module ActiveModel + class AttributeSetTest < ActiveModel::TestCase + test "building a new set from raw attributes" do + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new) + attributes = builder.build_from_database(foo: "1.1", bar: "2.2") + + assert_equal 1, attributes[:foo].value + assert_equal 2.2, attributes[:bar].value + assert_equal :foo, attributes[:foo].name + assert_equal :bar, attributes[:bar].name + end + + test "building with custom types" do + builder = AttributeSet::Builder.new(foo: Type::Float.new) + attributes = builder.build_from_database({ foo: "3.3", bar: "4.4" }, { bar: Type::Integer.new }) + + assert_equal 3.3, attributes[:foo].value + assert_equal 4, attributes[:bar].value + end + + test "[] returns a null object" do + builder = AttributeSet::Builder.new(foo: Type::Float.new) + attributes = builder.build_from_database(foo: "3.3") + + assert_equal "3.3", attributes[:foo].value_before_type_cast + assert_nil attributes[:bar].value_before_type_cast + assert_equal :bar, attributes[:bar].name + end + + test "duping creates a new hash, but does not dup the attributes" do + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::String.new) + attributes = builder.build_from_database(foo: 1, bar: "foo") + + # Ensure the type cast value is cached + attributes[:foo].value + attributes[:bar].value + + duped = attributes.dup + duped.write_from_database(:foo, 2) + duped[:bar].value << "bar" + + assert_equal 1, attributes[:foo].value + assert_equal 2, duped[:foo].value + assert_equal "foobar", attributes[:bar].value + assert_equal "foobar", duped[:bar].value + end + + test "deep_duping creates a new hash and dups each attribute" do + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::String.new) + attributes = builder.build_from_database(foo: 1, bar: "foo") + + # Ensure the type cast value is cached + attributes[:foo].value + attributes[:bar].value + + duped = attributes.deep_dup + duped.write_from_database(:foo, 2) + duped[:bar].value << "bar" + + assert_equal 1, attributes[:foo].value + assert_equal 2, duped[:foo].value + assert_equal "foo", attributes[:bar].value + assert_equal "foobar", duped[:bar].value + end + + test "freezing cloned set does not freeze original" do + attributes = AttributeSet.new({}) + clone = attributes.clone + + clone.freeze + + assert clone.frozen? + assert_not attributes.frozen? + end + + test "to_hash returns a hash of the type cast values" do + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new) + attributes = builder.build_from_database(foo: "1.1", bar: "2.2") + + assert_equal({ foo: 1, bar: 2.2 }, attributes.to_hash) + assert_equal({ foo: 1, bar: 2.2 }, attributes.to_h) + end + + test "to_hash maintains order" do + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new) + attributes = builder.build_from_database(foo: "2.2", bar: "3.3") + + attributes[:bar] + hash = attributes.to_h + + assert_equal [[:foo, 2], [:bar, 3.3]], hash.to_a + end + + test "values_before_type_cast" do + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Integer.new) + attributes = builder.build_from_database(foo: "1.1", bar: "2.2") + + assert_equal({ foo: "1.1", bar: "2.2" }, attributes.values_before_type_cast) + end + + test "known columns are built with uninitialized attributes" do + attributes = attributes_with_uninitialized_key + assert attributes[:foo].initialized? + assert_not attributes[:bar].initialized? + end + + test "uninitialized attributes are not included in the attributes hash" do + attributes = attributes_with_uninitialized_key + assert_equal({ foo: 1 }, attributes.to_hash) + end + + test "uninitialized attributes are not included in keys" do + attributes = attributes_with_uninitialized_key + assert_equal [:foo], attributes.keys + end + + test "uninitialized attributes return false for key?" do + attributes = attributes_with_uninitialized_key + assert attributes.key?(:foo) + assert_not attributes.key?(:bar) + end + + test "unknown attributes return false for key?" do + attributes = attributes_with_uninitialized_key + assert_not attributes.key?(:wibble) + end + + test "fetch_value returns the value for the given initialized attribute" do + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new) + attributes = builder.build_from_database(foo: "1.1", bar: "2.2") + + assert_equal 1, attributes.fetch_value(:foo) + assert_equal 2.2, attributes.fetch_value(:bar) + end + + test "fetch_value returns nil for unknown attributes" do + attributes = attributes_with_uninitialized_key + assert_nil attributes.fetch_value(:wibble) { "hello" } + end + + test "fetch_value returns nil for unknown attributes when types has a default" do + types = Hash.new(Type::Value.new) + builder = AttributeSet::Builder.new(types) + attributes = builder.build_from_database + + assert_nil attributes.fetch_value(:wibble) { "hello" } + end + + test "fetch_value uses the given block for uninitialized attributes" do + attributes = attributes_with_uninitialized_key + value = attributes.fetch_value(:bar) { |n| n.to_s + "!" } + assert_equal "bar!", value + end + + test "fetch_value returns nil for uninitialized attributes if no block is given" do + attributes = attributes_with_uninitialized_key + assert_nil attributes.fetch_value(:bar) + end + + test "the primary_key is always initialized" do + defaults = { foo: Attribute.from_user(:foo, nil, nil) } + builder = AttributeSet::Builder.new({ foo: Type::Integer.new }, defaults) + attributes = builder.build_from_database + + assert attributes.key?(:foo) + assert_equal [:foo], attributes.keys + assert attributes[:foo].initialized? + end + + class MyType + def cast(value) + return if value.nil? + value + " from user" + end + + def deserialize(value) + return if value.nil? + value + " from database" + end + + def assert_valid_value(*) + end + end + + test "write_from_database sets the attribute with database typecasting" do + builder = AttributeSet::Builder.new(foo: MyType.new) + attributes = builder.build_from_database + + assert_nil attributes.fetch_value(:foo) + + attributes.write_from_database(:foo, "value") + + assert_equal "value from database", attributes.fetch_value(:foo) + end + + test "write_from_user sets the attribute with user typecasting" do + builder = AttributeSet::Builder.new(foo: MyType.new) + attributes = builder.build_from_database + + assert_nil attributes.fetch_value(:foo) + + attributes.write_from_user(:foo, "value") + + assert_equal "value from user", attributes.fetch_value(:foo) + end + + def attributes_with_uninitialized_key + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new) + builder.build_from_database(foo: "1.1") + end + + test "freezing doesn't prevent the set from materializing" do + builder = AttributeSet::Builder.new(foo: Type::String.new) + attributes = builder.build_from_database(foo: "1") + + attributes.freeze + assert_equal({ foo: "1" }, attributes.to_hash) + end + + test "#accessed_attributes returns only attributes which have been read" do + builder = AttributeSet::Builder.new(foo: Type::Value.new, bar: Type::Value.new) + attributes = builder.build_from_database(foo: "1", bar: "2") + + assert_equal [], attributes.accessed + + attributes.fetch_value(:foo) + + assert_equal [:foo], attributes.accessed + end + + test "#map returns a new attribute set with the changes applied" do + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Integer.new) + attributes = builder.build_from_database(foo: "1", bar: "2") + new_attributes = attributes.map do |attr| + attr.with_cast_value(attr.value + 1) + end + + assert_equal 2, new_attributes.fetch_value(:foo) + assert_equal 3, new_attributes.fetch_value(:bar) + end + + test "comparison for equality is correctly implemented" do + builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Integer.new) + attributes = builder.build_from_database(foo: "1", bar: "2") + attributes2 = builder.build_from_database(foo: "1", bar: "2") + attributes3 = builder.build_from_database(foo: "2", bar: "2") + + assert_equal attributes, attributes2 + assert_not_equal attributes2, attributes3 + end + end +end diff --git a/activemodel/test/cases/attribute_test.rb b/activemodel/test/cases/attribute_test.rb new file mode 100644 index 0000000000..14d86cef97 --- /dev/null +++ b/activemodel/test/cases/attribute_test.rb @@ -0,0 +1,255 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + class AttributeTest < ActiveModel::TestCase + setup do + @type = Minitest::Mock.new + end + + teardown do + assert @type.verify + end + + test "from_database + read type casts from database" do + @type.expect(:deserialize, "type cast from database", ["a value"]) + attribute = Attribute.from_database(nil, "a value", @type) + + type_cast_value = attribute.value + + assert_equal "type cast from database", type_cast_value + end + + test "from_user + read type casts from user" do + @type.expect(:cast, "type cast from user", ["a value"]) + attribute = Attribute.from_user(nil, "a value", @type) + + type_cast_value = attribute.value + + assert_equal "type cast from user", type_cast_value + end + + test "reading memoizes the value" do + @type.expect(:deserialize, "from the database", ["whatever"]) + attribute = Attribute.from_database(nil, "whatever", @type) + + type_cast_value = attribute.value + second_read = attribute.value + + assert_equal "from the database", type_cast_value + assert_same type_cast_value, second_read + end + + test "reading memoizes falsy values" do + @type.expect(:deserialize, false, ["whatever"]) + attribute = Attribute.from_database(nil, "whatever", @type) + + attribute.value + attribute.value + end + + test "read_before_typecast returns the given value" do + attribute = Attribute.from_database(nil, "raw value", @type) + + raw_value = attribute.value_before_type_cast + + assert_equal "raw value", raw_value + end + + test "from_database + read_for_database type casts to and from database" do + @type.expect(:deserialize, "read from database", ["whatever"]) + @type.expect(:serialize, "ready for database", ["read from database"]) + attribute = Attribute.from_database(nil, "whatever", @type) + + serialize = attribute.value_for_database + + assert_equal "ready for database", serialize + end + + test "from_user + read_for_database type casts from the user to the database" do + @type.expect(:cast, "read from user", ["whatever"]) + @type.expect(:serialize, "ready for database", ["read from user"]) + attribute = Attribute.from_user(nil, "whatever", @type) + + serialize = attribute.value_for_database + + assert_equal "ready for database", serialize + end + + test "duping dups the value" do + @type.expect(:deserialize, "type cast".dup, ["a value"]) + attribute = Attribute.from_database(nil, "a value", @type) + + value_from_orig = attribute.value + value_from_clone = attribute.dup.value + value_from_orig << " foo" + + assert_equal "type cast foo", value_from_orig + assert_equal "type cast", value_from_clone + end + + test "duping does not dup the value if it is not dupable" do + @type.expect(:deserialize, false, ["a value"]) + attribute = Attribute.from_database(nil, "a value", @type) + + assert_same attribute.value, attribute.dup.value + end + + test "duping does not eagerly type cast if we have not yet type cast" do + attribute = Attribute.from_database(nil, "a value", @type) + attribute.dup + end + + class MyType + def cast(value) + value + " from user" + end + + def deserialize(value) + value + " from database" + end + + def assert_valid_value(*) + end + end + + test "with_value_from_user returns a new attribute with the value from the user" do + old = Attribute.from_database(nil, "old", MyType.new) + new = old.with_value_from_user("new") + + assert_equal "old from database", old.value + assert_equal "new from user", new.value + end + + test "with_value_from_database returns a new attribute with the value from the database" do + old = Attribute.from_user(nil, "old", MyType.new) + new = old.with_value_from_database("new") + + assert_equal "old from user", old.value + assert_equal "new from database", new.value + end + + test "uninitialized attributes yield their name if a block is given to value" do + block = proc { |name| name.to_s + "!" } + foo = Attribute.uninitialized(:foo, nil) + bar = Attribute.uninitialized(:bar, nil) + + assert_equal "foo!", foo.value(&block) + assert_equal "bar!", bar.value(&block) + end + + test "uninitialized attributes have no value" do + assert_nil Attribute.uninitialized(:foo, nil).value + end + + test "attributes equal other attributes with the same constructor arguments" do + first = Attribute.from_database(:foo, 1, Type::Integer.new) + second = Attribute.from_database(:foo, 1, Type::Integer.new) + assert_equal first, second + end + + test "attributes do not equal attributes with different names" do + first = Attribute.from_database(:foo, 1, Type::Integer.new) + second = Attribute.from_database(:bar, 1, Type::Integer.new) + assert_not_equal first, second + end + + test "attributes do not equal attributes with different types" do + first = Attribute.from_database(:foo, 1, Type::Integer.new) + second = Attribute.from_database(:foo, 1, Type::Float.new) + assert_not_equal first, second + end + + test "attributes do not equal attributes with different values" do + first = Attribute.from_database(:foo, 1, Type::Integer.new) + second = Attribute.from_database(:foo, 2, Type::Integer.new) + assert_not_equal first, second + end + + test "attributes do not equal attributes of other classes" do + first = Attribute.from_database(:foo, 1, Type::Integer.new) + second = Attribute.from_user(:foo, 1, Type::Integer.new) + assert_not_equal first, second + end + + test "an attribute has not been read by default" do + attribute = Attribute.from_database(:foo, 1, Type::Value.new) + assert_not attribute.has_been_read? + end + + test "an attribute has been read when its value is calculated" do + attribute = Attribute.from_database(:foo, 1, Type::Value.new) + attribute.value + assert attribute.has_been_read? + end + + test "an attribute is not changed if it hasn't been assigned or mutated" do + attribute = Attribute.from_database(:foo, 1, Type::Value.new) + + refute attribute.changed? + end + + test "an attribute is changed if it's been assigned a new value" do + attribute = Attribute.from_database(:foo, 1, Type::Value.new) + changed = attribute.with_value_from_user(2) + + assert changed.changed? + end + + test "an attribute is not changed if it's assigned the same value" do + attribute = Attribute.from_database(:foo, 1, Type::Value.new) + unchanged = attribute.with_value_from_user(1) + + refute unchanged.changed? + end + + test "an attribute can not be mutated if it has not been read, + and skips expensive calculations" do + type_which_raises_from_all_methods = Object.new + attribute = Attribute.from_database(:foo, "bar", type_which_raises_from_all_methods) + + assert_not attribute.changed_in_place? + end + + test "an attribute is changed if it has been mutated" do + attribute = Attribute.from_database(:foo, "bar", Type::String.new) + attribute.value << "!" + + assert attribute.changed_in_place? + assert attribute.changed? + end + + test "an attribute can forget its changes" do + attribute = Attribute.from_database(:foo, "bar", Type::String.new) + changed = attribute.with_value_from_user("foo") + forgotten = changed.forgetting_assignment + + assert changed.changed? # sanity check + refute forgotten.changed? + end + + test "with_value_from_user validates the value" do + type = Type::Value.new + type.define_singleton_method(:assert_valid_value) do |value| + if value == 1 + raise ArgumentError + end + end + + attribute = Attribute.from_database(:foo, 1, type) + assert_equal 1, attribute.value + assert_equal 2, attribute.with_value_from_user(2).value + assert_raises ArgumentError do + attribute.with_value_from_user(1) + end + end + + test "with_type preserves mutations" do + attribute = Attribute.from_database(:foo, "".dup, Type::Value.new) + attribute.value << "1" + + assert_equal 1, attribute.with_type(Type::Integer.new).value + end + end +end diff --git a/activemodel/test/cases/attributes_dirty_test.rb b/activemodel/test/cases/attributes_dirty_test.rb new file mode 100644 index 0000000000..83a86371e0 --- /dev/null +++ b/activemodel/test/cases/attributes_dirty_test.rb @@ -0,0 +1,205 @@ +# frozen_string_literal: true + +require "cases/helper" + +class AttributesDirtyTest < ActiveModel::TestCase + class DirtyModel + include ActiveModel::Model + include ActiveModel::Attributes + include ActiveModel::Dirty + attribute :name, :string + attribute :color, :string + attribute :size, :integer + + def save + changes_applied + end + + def reload + clear_changes_information + end + end + + setup do + @model = DirtyModel.new + end + + test "setting attribute will result in change" do + assert !@model.changed? + assert !@model.name_changed? + @model.name = "Ringo" + assert @model.changed? + assert @model.name_changed? + end + + test "list of changed attribute keys" do + assert_equal [], @model.changed + @model.name = "Paul" + assert_equal ["name"], @model.changed + end + + test "changes to attribute values" do + assert !@model.changes["name"] + @model.name = "John" + assert_equal [nil, "John"], @model.changes["name"] + end + + test "checking if an attribute has changed to a particular value" do + @model.name = "Ringo" + assert @model.name_changed?(from: nil, to: "Ringo") + assert_not @model.name_changed?(from: "Pete", to: "Ringo") + assert @model.name_changed?(to: "Ringo") + assert_not @model.name_changed?(to: "Pete") + assert @model.name_changed?(from: nil) + assert_not @model.name_changed?(from: "Pete") + end + + test "changes accessible through both strings and symbols" do + @model.name = "David" + assert_not_nil @model.changes[:name] + assert_not_nil @model.changes["name"] + end + + test "be consistent with symbols arguments after the changes are applied" do + @model.name = "David" + assert @model.attribute_changed?(:name) + @model.save + @model.name = "Rafael" + assert @model.attribute_changed?(:name) + end + + test "attribute mutation" do + @model.name = "Yam" + @model.save + assert !@model.name_changed? + @model.name.replace("Hadad") + assert @model.name_changed? + end + + test "resetting attribute" do + @model.name = "Bob" + @model.restore_name! + assert_nil @model.name + assert !@model.name_changed? + end + + test "setting color to same value should not result in change being recorded" do + @model.color = "red" + assert @model.color_changed? + @model.save + assert !@model.color_changed? + assert !@model.changed? + @model.color = "red" + assert !@model.color_changed? + assert !@model.changed? + end + + test "saving should reset model's changed status" do + @model.name = "Alf" + assert @model.changed? + @model.save + assert !@model.changed? + assert !@model.name_changed? + end + + test "saving should preserve previous changes" do + @model.name = "Jericho Cane" + @model.save + assert_equal [nil, "Jericho Cane"], @model.previous_changes["name"] + end + + test "setting new attributes should not affect previous changes" do + @model.name = "Jericho Cane" + @model.save + @model.name = "DudeFella ManGuy" + assert_equal [nil, "Jericho Cane"], @model.name_previous_change + end + + test "saving should preserve model's previous changed status" do + @model.name = "Jericho Cane" + @model.save + assert @model.name_previously_changed? + 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 + @model.name = "DudeFella ManGuy" + @model.name = "Mr. Manfredgensonton" + assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change + assert_equal @model.name_was, "Otto" + end + + test "using attribute_will_change! with a symbol" do + @model.size = 1 + assert @model.size_changed? + end + + test "reload should reset all changes" do + @model.name = "Dmitry" + @model.name_changed? + @model.save + @model.name = "Bob" + + assert_equal [nil, "Dmitry"], @model.previous_changes["name"] + assert_equal "Dmitry", @model.changed_attributes["name"] + + @model.reload + + assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.previous_changes + assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.changed_attributes + end + + test "restore_attributes should restore all previous data" do + @model.name = "Dmitry" + @model.color = "Red" + @model.save + @model.name = "Bob" + @model.color = "White" + + @model.restore_attributes + + assert_not @model.changed? + assert_equal "Dmitry", @model.name + assert_equal "Red", @model.color + end + + test "restore_attributes can restore only some attributes" do + @model.name = "Dmitry" + @model.color = "Red" + @model.save + @model.name = "Bob" + @model.color = "White" + + @model.restore_attributes(["name"]) + + assert @model.changed? + assert_equal "Dmitry", @model.name + assert_equal "White", @model.color + end + + test "changing the attribute reports a change only when the cast value changes" do + @model.size = "2.3" + @model.save + @model.size = "2.1" + + assert_equal false, @model.changed? + + @model.size = "5.1" + + assert_equal true, @model.changed? + assert_equal true, @model.size_changed? + assert_equal({ "size" => [2, 5] }, @model.changes) + end +end diff --git a/activemodel/test/cases/attributes_test.rb b/activemodel/test/cases/attributes_test.rb new file mode 100644 index 0000000000..e43bf15335 --- /dev/null +++ b/activemodel/test/cases/attributes_test.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + class AttributesTest < ActiveModel::TestCase + class ModelForAttributesTest + include ActiveModel::Model + include ActiveModel::Attributes + + attribute :integer_field, :integer + attribute :string_field, :string + attribute :decimal_field, :decimal + attribute :string_with_default, :string, default: "default string" + attribute :date_field, :date, default: -> { Date.new(2016, 1, 1) } + attribute :boolean_field, :boolean + end + + class ChildModelForAttributesTest < ModelForAttributesTest + end + + class GrandchildModelForAttributesTest < ChildModelForAttributesTest + attribute :integer_field, :string + end + + test "properties assignment" do + data = ModelForAttributesTest.new( + integer_field: "2.3", + string_field: "Rails FTW", + decimal_field: "12.3", + boolean_field: "0" + ) + + assert_equal 2, data.integer_field + assert_equal "Rails FTW", data.string_field + assert_equal BigDecimal("12.3"), data.decimal_field + assert_equal "default string", data.string_with_default + assert_equal Date.new(2016, 1, 1), data.date_field + assert_equal false, data.boolean_field + + data.integer_field = 10 + data.string_with_default = nil + data.boolean_field = "1" + + assert_equal 10, data.integer_field + assert_nil data.string_with_default + assert_equal true, data.boolean_field + end + + test "nonexistent attribute" do + assert_raise ActiveModel::UnknownAttributeError do + ModelForAttributesTest.new(nonexistent: "nonexistent") + end + end + + test "children inherit attributes" do + data = ChildModelForAttributesTest.new(integer_field: "4.4") + + assert_equal 4, data.integer_field + end + + test "children can override parents" do + data = GrandchildModelForAttributesTest.new(integer_field: "4.4") + + assert_equal "4.4", data.integer_field + end + end +end diff --git a/activemodel/test/cases/callbacks_test.rb b/activemodel/test/cases/callbacks_test.rb index e4ecc0adb4..a5d29d0f22 100644 --- a/activemodel/test/cases/callbacks_test.rb +++ b/activemodel/test/cases/callbacks_test.rb @@ -1,7 +1,8 @@ +# frozen_string_literal: true + require "cases/helper" class CallbacksTest < ActiveModel::TestCase - class CallbackValidator def around_create(model) model.callbacks << :before_around_create @@ -28,7 +29,7 @@ class CallbacksTest < ActiveModel::TestCase false end - ActiveSupport::Deprecation.silence { after_create "@callbacks << :final_callback" } + after_create { |model| model.callbacks << :final_callback } def initialize(options = {}) @callbacks = [] @@ -64,12 +65,10 @@ class CallbacksTest < ActiveModel::TestCase assert_equal model.callbacks.last, :final_callback end - test "the callback chain is halted when a before callback returns false (deprecated)" do + test "the callback chain is not halted when a before callback returns false)" do model = ModelCallbacks.new(before_create_returns: false) - assert_deprecated do - model.create - assert_equal model.callbacks.last, :before_create - end + model.create + assert_equal model.callbacks.last, :final_callback end test "the callback chain is halted when a callback throws :abort" do @@ -110,8 +109,8 @@ class CallbacksTest < ActiveModel::TestCase end extend ActiveModel::Callbacks define_model_callbacks :create - def callback1; self.history << 'callback1'; end - def callback2; self.history << 'callback2'; end + def callback1; history << "callback1"; end + def callback2; history << "callback2"; end def create run_callbacks(:create) {} self @@ -128,8 +127,8 @@ 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 different lines" do assert_equal ["callback1", "callback2"], Violin2.new.create.history end - end diff --git a/activemodel/test/cases/conversion_test.rb b/activemodel/test/cases/conversion_test.rb index 800cad6d9a..347896ed50 100644 --- a/activemodel/test/cases/conversion_test.rb +++ b/activemodel/test/cases/conversion_test.rb @@ -1,6 +1,8 @@ -require 'cases/helper' -require 'models/contact' -require 'models/helicopter' +# frozen_string_literal: true + +require "cases/helper" +require "models/contact" +require "models/helicopter" class ConversionTest < ActiveModel::TestCase test "to_model default implementation returns self" do diff --git a/activemodel/test/cases/dirty_test.rb b/activemodel/test/cases/dirty_test.rb index d17a12ad12..dfe041ff50 100644 --- a/activemodel/test/cases/dirty_test.rb +++ b/activemodel/test/cases/dirty_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "cases/helper" class DirtyTest < ActiveModel::TestCase @@ -62,13 +64,13 @@ class DirtyTest < ActiveModel::TestCase test "list of changed attribute keys" do assert_equal [], @model.changed @model.name = "Paul" - assert_equal ['name'], @model.changed + assert_equal ["name"], @model.changed end test "changes to attribute values" do - assert !@model.changes['name'] + assert !@model.changes["name"] @model.name = "John" - assert_equal [nil, "John"], @model.changes['name'] + assert_equal [nil, "John"], @model.changes["name"] end test "checking if an attribute has changed to a particular value" do @@ -84,19 +86,19 @@ class DirtyTest < ActiveModel::TestCase test "changes accessible through both strings and symbols" do @model.name = "David" assert_not_nil @model.changes[:name] - assert_not_nil @model.changes['name'] + assert_not_nil @model.changes["name"] end test "be consistent with symbols arguments after the changes are applied" do @model.name = "David" assert @model.attribute_changed?(:name) @model.save - @model.name = 'Rafael' + @model.name = "Rafael" assert @model.attribute_changed?(:name) end test "attribute mutation" do - @model.instance_variable_set("@name", "Yam") + @model.instance_variable_set("@name", "Yam".dup) assert !@model.name_changed? @model.name.replace("Hadad") assert !@model.name_changed? @@ -134,7 +136,7 @@ class DirtyTest < ActiveModel::TestCase test "saving should preserve previous changes" do @model.name = "Jericho Cane" @model.save - assert_equal [nil, "Jericho Cane"], @model.previous_changes['name'] + assert_equal [nil, "Jericho Cane"], @model.previous_changes["name"] end test "setting new attributes should not affect previous changes" do @@ -176,13 +178,13 @@ class DirtyTest < ActiveModel::TestCase end test "reload should reset all changes" do - @model.name = 'Dmitry' + @model.name = "Dmitry" @model.name_changed? @model.save - @model.name = 'Bob' + @model.name = "Bob" - assert_equal [nil, 'Dmitry'], @model.previous_changes['name'] - assert_equal 'Dmitry', @model.changed_attributes['name'] + assert_equal [nil, "Dmitry"], @model.previous_changes["name"] + assert_equal "Dmitry", @model.changed_attributes["name"] @model.reload @@ -191,30 +193,34 @@ class DirtyTest < ActiveModel::TestCase end test "restore_attributes should restore all previous data" do - @model.name = 'Dmitry' - @model.color = 'Red' + @model.name = "Dmitry" + @model.color = "Red" @model.save - @model.name = 'Bob' - @model.color = 'White' + @model.name = "Bob" + @model.color = "White" @model.restore_attributes assert_not @model.changed? - assert_equal 'Dmitry', @model.name - assert_equal 'Red', @model.color + assert_equal "Dmitry", @model.name + assert_equal "Red", @model.color end test "restore_attributes can restore only some attributes" do - @model.name = 'Dmitry' - @model.color = 'Red' + @model.name = "Dmitry" + @model.color = "Red" @model.save - @model.name = 'Bob' - @model.color = 'White' + @model.name = "Bob" + @model.color = "White" - @model.restore_attributes(['name']) + @model.restore_attributes(["name"]) assert @model.changed? - assert_equal 'Dmitry', @model.name - assert_equal 'White', @model.color + assert_equal "Dmitry", @model.name + assert_equal "White", @model.color + end + + test "model can be dup-ed without Attributes" do + assert @model.dup end end diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 26ba6ba4fb..d5c282b620 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -1,4 +1,8 @@ +# frozen_string_literal: true + require "cases/helper" +require "active_support/core_ext/string/strip" +require "yaml" class ErrorsTest < ActiveModel::TestCase class Person @@ -11,7 +15,7 @@ class ErrorsTest < ActiveModel::TestCase attr_reader :errors def validate! - errors.add(:name, "cannot be nil") if name == nil + errors.add(:name, :blank, message: "cannot be nil") if name == nil end def read_attribute_for_validation(attr) @@ -29,45 +33,48 @@ class ErrorsTest < ActiveModel::TestCase def test_delete errors = ActiveModel::Errors.new(self) - errors[:foo] << 'omg' - errors.delete(:foo) + errors[:foo] << "omg" + errors.delete("foo") assert_empty errors[:foo] end def test_include? errors = ActiveModel::Errors.new(self) - errors[:foo] << 'omg' - assert errors.include?(:foo), 'errors should include :foo' + errors[:foo] << "omg" + assert_includes errors, :foo, "errors should include :foo" + assert_includes errors, "foo", "errors should include 'foo' as :foo" end def test_dup errors = ActiveModel::Errors.new(self) - errors[:foo] << 'bar' + errors[:foo] << "bar" errors_dup = errors.dup - errors_dup[:bar] << 'omg' + errors_dup[:bar] << "omg" assert_not_same errors_dup.messages, errors.messages end def test_has_key? errors = ActiveModel::Errors.new(self) - errors[:foo] << 'omg' - assert_equal true, errors.has_key?(:foo), 'errors should have key :foo' + errors[:foo] << "omg" + assert_equal true, errors.has_key?(:foo), "errors should have key :foo" + assert_equal true, errors.has_key?("foo"), "errors should have key 'foo' as :foo" end def test_has_no_key errors = ActiveModel::Errors.new(self) - assert_equal false, errors.has_key?(:name), 'errors should not have key :name' + assert_equal false, errors.has_key?(:name), "errors should not have key :name" end def test_key? errors = ActiveModel::Errors.new(self) - errors[:foo] << 'omg' - assert_equal true, errors.key?(:foo), 'errors should have key :foo' + errors[:foo] << "omg" + assert_equal true, errors.key?(:foo), "errors should have key :foo" + assert_equal true, errors.key?("foo"), "errors should have key 'foo' as :foo" end def test_no_key errors = ActiveModel::Errors.new(self) - assert_equal false, errors.key?(:name), 'errors should not have key :name' + assert_equal false, errors.key?(:name), "errors should not have key :name" end test "clear errors" do @@ -79,24 +86,6 @@ class ErrorsTest < ActiveModel::TestCase assert person.errors.empty? end - test "get returns the errors for the provided key" do - errors = ActiveModel::Errors.new(self) - errors[:foo] << "omg" - - assert_deprecated do - assert_equal ["omg"], errors.get(:foo) - end - end - - test "sets the error with the provided key" do - errors = ActiveModel::Errors.new(self) - assert_deprecated do - errors.set(:foo, "omg") - end - - assert_equal({ foo: "omg" }, errors.messages) - end - test "error access is indifferent" do errors = ActiveModel::Errors.new(self) errors[:foo] << "omg" @@ -112,6 +101,14 @@ class ErrorsTest < ActiveModel::TestCase assert_equal ["omg", "zomg"], errors.values end + test "values returns an empty array after try to get a message only" do + errors = ActiveModel::Errors.new(self) + errors.messages[:foo] + errors.messages[:baz] + + assert_equal [], errors.values + end + test "keys returns the error keys" do errors = ActiveModel::Errors.new(self) errors.messages[:foo] << "omg" @@ -120,12 +117,20 @@ class ErrorsTest < ActiveModel::TestCase assert_equal [:foo, :baz], errors.keys end + test "keys returns an empty array after try to get a message only" do + errors = ActiveModel::Errors.new(self) + errors.messages[:foo] + errors.messages[:baz] + + assert_equal [], errors.keys + end + test "detecting whether there are errors with empty?, blank?, include?" do person = Person.new person.errors[:foo] assert person.errors.empty? assert person.errors.blank? - assert !person.errors.include?(:foo) + assert_not_includes person.errors, :foo end test "include? does not add a key to messages hash" do @@ -142,14 +147,6 @@ class ErrorsTest < ActiveModel::TestCase assert_equal ["cannot be nil"], person.errors[:name] end - test "assign error" do - person = Person.new - assert_deprecated do - person.errors[:name] = 'should not be nil' - end - assert_equal ["should not be nil"], person.errors[:name] - end - test "add an error message on a specific attribute" do person = Person.new person.errors.add(:name, "cannot be blank") @@ -176,10 +173,11 @@ class ErrorsTest < ActiveModel::TestCase assert_equal ["cannot be blank"], person.errors[:name] end - test "added? detects if a specific error was added to the object" do + test "added? detects indifferent if a specific error was added to the object" do person = Person.new person.errors.add(:name, "cannot be blank") assert person.errors.added?(:name, "cannot be blank") + assert person.errors.added?("name", "cannot be blank") end test "added? handles symbol message" do @@ -219,6 +217,19 @@ class ErrorsTest < ActiveModel::TestCase assert !person.errors.added?(:name, "cannot be blank") end + test "added? returns false when checking for an error, but not providing message arguments" do + person = Person.new + person.errors.add(:name, "cannot be blank") + assert !person.errors.added?(:name) + end + + test "added? returns false when checking for an error by symbol and a different error with same message is present" do + I18n.backend.store_translations("en", errors: { attributes: { name: { wrong: "is wrong", used: "is wrong" } } }) + person = Person.new + person.errors.add(:name, :wrong) + assert !person.errors.added?(:name, :used) + end + test "size calculates the number of error messages" do person = Person.new person.errors.add(:name, "cannot be blank") @@ -244,6 +255,16 @@ class ErrorsTest < ActiveModel::TestCase assert_equal({ name: ["cannot be blank"] }, person.errors.to_hash) end + test "to_hash returns a hash without default proc" do + person = Person.new + assert_nil person.errors.to_hash.default_proc + end + + test "as_json returns a hash without default proc" do + person = Person.new + assert_nil person.errors.as_json.default_proc + end + test "full_messages creates a list of error messages with the attribute name included" do person = Person.new person.errors.add(:name, "cannot be blank") @@ -251,7 +272,7 @@ class ErrorsTest < ActiveModel::TestCase assert_equal ["name cannot be blank", "name cannot be nil"], person.errors.full_messages end - test "full_messages_for contains all the error messages for the given attribute" do + test "full_messages_for contains all the error messages for the given attribute indifferent" do person = Person.new person.errors.add(:name, "cannot be blank") person.errors.add(:name, "cannot be nil") @@ -263,6 +284,7 @@ class ErrorsTest < ActiveModel::TestCase person.errors.add(:name, "cannot be blank") person.errors.add(:email, "cannot be blank") assert_equal ["name cannot be blank"], person.errors.full_messages_for(:name) + assert_equal ["name cannot 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 @@ -304,72 +326,6 @@ class ErrorsTest < ActiveModel::TestCase } end - test "add_on_empty generates message" do - person = Person.new - assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do - assert_deprecated do - person.errors.add_on_empty :name - end - end - end - - test "add_on_empty generates message for multiple attributes" do - person = Person.new - expected_calls = [ [:name, :empty, {}], [:age, :empty, {}] ] - assert_called_with(person.errors, :generate_message, expected_calls) do - assert_deprecated do - person.errors.add_on_empty [:name, :age] - end - end - end - - test "add_on_empty generates message with custom default message" do - person = Person.new - assert_called_with(person.errors, :generate_message, [:name, :empty, { message: 'custom' }]) do - assert_deprecated do - person.errors.add_on_empty :name, message: 'custom' - end - end - end - - test "add_on_empty generates message with empty string value" do - person = Person.new - person.name = '' - assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do - assert_deprecated do - person.errors.add_on_empty :name - end - end - end - - test "add_on_blank generates message" do - person = Person.new - assert_called_with(person.errors, :generate_message, [:name, :blank, {}]) do - assert_deprecated do - person.errors.add_on_blank :name - end - end - end - - test "add_on_blank generates message for multiple attributes" do - person = Person.new - expected_calls = [ [:name, :blank, {}], [:age, :blank, {}] ] - assert_called_with(person.errors, :generate_message, expected_calls) do - assert_deprecated do - person.errors.add_on_blank [:name, :age] - end - end - end - - test "add_on_blank generates message with custom default message" do - person = Person.new - assert_called_with(person.errors, :generate_message, [:name, :blank, { message: 'custom' }]) do - assert_deprecated do - person.errors.add_on_blank :name, message: 'custom' - end - end - end - test "details returns added error detail" do person = Person.new person.errors.add(:name, :invalid) @@ -427,4 +383,45 @@ class ErrorsTest < ActiveModel::TestCase assert_equal [:name], person.errors.messages.keys assert_equal [:name], person.errors.details.keys end + + test "merge errors" do + errors = ActiveModel::Errors.new(Person.new) + errors.add(:name, :invalid) + + person = Person.new + person.errors.add(:name, :blank) + person.errors.merge!(errors) + + assert_equal({ name: ["can't be blank", "is invalid"] }, person.errors.messages) + assert_equal({ name: [{ error: :blank }, { error: :invalid }] }, person.errors.details) + end + + test "errors are marshalable" do + errors = ActiveModel::Errors.new(Person.new) + errors.add(:name, :invalid) + serialized = Marshal.load(Marshal.dump(errors)) + + assert_equal errors.messages, serialized.messages + assert_equal errors.details, serialized.details + end + + test "errors are backward compatible with the Rails 4.2 format" do + yaml = <<-CODE.strip_heredoc + --- !ruby/object:ActiveModel::Errors + base: &1 !ruby/object:ErrorsTest::Person + errors: !ruby/object:ActiveModel::Errors + base: *1 + messages: {} + messages: {} + CODE + + errors = YAML.load(yaml) + errors.add(:name, :invalid) + assert_equal({ name: ["is invalid"] }, errors.messages) + assert_equal({ name: [{ error: :invalid }] }, errors.details) + + errors.clear + assert_equal({}, errors.messages) + assert_equal({}, errors.details) + end end diff --git a/activemodel/test/cases/forbidden_attributes_protection_test.rb b/activemodel/test/cases/forbidden_attributes_protection_test.rb index d8d757f52a..0fd0a2f8ee 100644 --- a/activemodel/test/cases/forbidden_attributes_protection_test.rb +++ b/activemodel/test/cases/forbidden_attributes_protection_test.rb @@ -1,6 +1,8 @@ -require 'cases/helper' -require 'active_support/core_ext/hash/indifferent_access' -require 'models/account' +# frozen_string_literal: true + +require "cases/helper" +require "active_support/core_ext/hash/indifferent_access" +require "models/account" class ProtectedParams attr_accessor :permitted @@ -25,18 +27,18 @@ end class ActiveModelMassUpdateProtectionTest < ActiveSupport::TestCase test "forbidden attributes cannot be used for mass updating" do - params = ProtectedParams.new({ "a" => "b" }) + params = ProtectedParams.new("a" => "b") assert_raises(ActiveModel::ForbiddenAttributesError) do Account.new.sanitize_for_mass_assignment(params) end end test "permitted attributes can be used for mass updating" do - params = ProtectedParams.new({ "a" => "b" }).permit! + params = ProtectedParams.new("a" => "b").permit! assert_equal({ "a" => "b" }, Account.new.sanitize_for_mass_assignment(params)) end test "regular attributes should still be allowed" do - assert_equal({ a: "b" }, Account.new.sanitize_for_mass_assignment(a: "b")) + assert_equal({ a: "b" }, Account.new.sanitize_for_mass_assignment(a: "b")) end end diff --git a/activemodel/test/cases/helper.rb b/activemodel/test/cases/helper.rb index 44c26e4f10..91fb9d0a7c 100644 --- a/activemodel/test/cases/helper.rb +++ b/activemodel/test/cases/helper.rb @@ -1,5 +1,6 @@ -require 'active_model' -require 'active_support/core_ext/string/access' +# frozen_string_literal: true + +require "active_model" # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true @@ -7,18 +8,18 @@ ActiveSupport::Deprecation.debug = true # Disable available locale checks to avoid warnings running the test suite. I18n.enforce_available_locales = false -require 'active_support/testing/autorun' -require 'active_support/testing/method_call_assertions' +require "active_support/testing/autorun" +require "active_support/testing/method_call_assertions" -# Skips the current run on Rubinius using Minitest::Assertions#skip -def rubinius_skip(message = '') - skip message if RUBY_ENGINE == 'rbx' -end -# Skips the current run on JRuby using Minitest::Assertions#skip -def jruby_skip(message = '') - skip message if defined?(JRUBY_VERSION) -end - -class ActiveModel::TestCase +class ActiveModel::TestCase < ActiveSupport::TestCase include ActiveSupport::Testing::MethodCallAssertions + + # Skips the current run on Rubinius using Minitest::Assertions#skip + private def rubinius_skip(message = "") + skip message if RUBY_ENGINE == "rbx" + end + # Skips the current run on JRuby using Minitest::Assertions#skip + private def jruby_skip(message = "") + skip message if defined?(JRUBY_VERSION) + end end diff --git a/activemodel/test/cases/lint_test.rb b/activemodel/test/cases/lint_test.rb index 8faf93c056..d62c80b71a 100644 --- a/activemodel/test/cases/lint_test.rb +++ b/activemodel/test/cases/lint_test.rb @@ -1,4 +1,6 @@ -require 'cases/helper' +# frozen_string_literal: true + +require "cases/helper" class LintTest < ActiveModel::TestCase include ActiveModel::Lint::Tests diff --git a/activemodel/test/cases/model_test.rb b/activemodel/test/cases/model_test.rb index 3017f3541b..b24d7e3571 100644 --- a/activemodel/test/cases/model_test.rb +++ b/activemodel/test/cases/model_test.rb @@ -1,4 +1,6 @@ -require 'cases/helper' +# frozen_string_literal: true + +require "cases/helper" class ModelTest < ActiveModel::TestCase include ActiveModel::Lint::Tests @@ -9,7 +11,7 @@ class ModelTest < ActiveModel::TestCase end def initialize(*args) - @attr ||= 'default value' + @attr ||= "default value" super end end @@ -50,7 +52,7 @@ class ModelTest < ActiveModel::TestCase BasicModel.new() BasicModel.new(nil) BasicModel.new({}) - SimpleModel.new(attr: 'value') + SimpleModel.new(attr: "value") end end @@ -61,17 +63,17 @@ class ModelTest < ActiveModel::TestCase def test_mixin_inclusion_chain object = BasicModel.new - assert_equal 'default value', object.attr + assert_equal "default value", object.attr end def test_mixin_initializer_when_args_exist - object = BasicModel.new(hello: 'world') - assert_equal 'world', object.hello + object = BasicModel.new(hello: "world") + assert_equal "world", object.hello end def test_mixin_initializer_when_args_dont_exist assert_raises(ActiveModel::UnknownAttributeError) do - SimpleModel.new(hello: 'world') + SimpleModel.new(hello: "world") end end end diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index 7b8287edbf..009f1f47af 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -1,8 +1,10 @@ -require 'cases/helper' -require 'models/contact' -require 'models/sheep' -require 'models/track_back' -require 'models/blog_post' +# frozen_string_literal: true + +require "cases/helper" +require "models/contact" +require "models/sheep" +require "models/track_back" +require "models/blog_post" class NamingTest < ActiveModel::TestCase def setup @@ -10,31 +12,31 @@ class NamingTest < ActiveModel::TestCase end def test_singular - assert_equal 'post_track_back', @model_name.singular + assert_equal "post_track_back", @model_name.singular end def test_plural - assert_equal 'post_track_backs', @model_name.plural + assert_equal "post_track_backs", @model_name.plural end def test_element - assert_equal 'track_back', @model_name.element + assert_equal "track_back", @model_name.element end def test_collection - assert_equal 'post/track_backs', @model_name.collection + assert_equal "post/track_backs", @model_name.collection end def test_human - assert_equal 'Track back', @model_name.human + assert_equal "Track back", @model_name.human end def test_route_key - assert_equal 'post_track_backs', @model_name.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 + assert_equal "post_track_back", @model_name.param_key end def test_i18n_key @@ -48,31 +50,31 @@ class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase end def test_singular - assert_equal 'blog_post', @model_name.singular + assert_equal "blog_post", @model_name.singular end def test_plural - assert_equal 'blog_posts', @model_name.plural + assert_equal "blog_posts", @model_name.plural end def test_element - assert_equal 'post', @model_name.element + assert_equal "post", @model_name.element end def test_collection - assert_equal 'blog/posts', @model_name.collection + assert_equal "blog/posts", @model_name.collection end def test_human - assert_equal 'Post', @model_name.human + assert_equal "Post", @model_name.human end def test_route_key - assert_equal 'posts', @model_name.route_key + assert_equal "posts", @model_name.route_key end def test_param_key - assert_equal 'post', @model_name.param_key + assert_equal "post", @model_name.param_key end def test_i18n_key @@ -86,31 +88,31 @@ class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase end def test_singular - assert_equal 'blog_post', @model_name.singular + assert_equal "blog_post", @model_name.singular end def test_plural - assert_equal 'blog_posts', @model_name.plural + assert_equal "blog_posts", @model_name.plural end def test_element - assert_equal 'post', @model_name.element + assert_equal "post", @model_name.element end def test_collection - assert_equal 'blog/posts', @model_name.collection + assert_equal "blog/posts", @model_name.collection end def test_human - assert_equal 'Post', @model_name.human + assert_equal "Post", @model_name.human end def test_route_key - assert_equal 'blog_posts', @model_name.route_key + assert_equal "blog_posts", @model_name.route_key end def test_param_key - assert_equal 'blog_post', @model_name.param_key + assert_equal "blog_post", @model_name.param_key end def test_i18n_key @@ -120,35 +122,35 @@ end class NamingWithSuppliedModelNameTest < ActiveModel::TestCase def setup - @model_name = ActiveModel::Name.new(Blog::Post, nil, 'Article') + @model_name = ActiveModel::Name.new(Blog::Post, nil, "Article") end def test_singular - assert_equal 'article', @model_name.singular + assert_equal "article", @model_name.singular end def test_plural - assert_equal 'articles', @model_name.plural + assert_equal "articles", @model_name.plural end def test_element - assert_equal 'article', @model_name.element + assert_equal "article", @model_name.element end def test_collection - assert_equal 'articles', @model_name.collection + assert_equal "articles", @model_name.collection end def test_human - assert_equal 'Article', @model_name.human + assert_equal "Article", @model_name.human end def test_route_key - assert_equal 'articles', @model_name.route_key + assert_equal "articles", @model_name.route_key end def test_param_key - assert_equal 'article', @model_name.param_key + assert_equal "article", @model_name.param_key end def test_i18n_key @@ -162,31 +164,31 @@ class NamingUsingRelativeModelNameTest < ActiveModel::TestCase end def test_singular - assert_equal 'blog_post', @model_name.singular + assert_equal "blog_post", @model_name.singular end def test_plural - assert_equal 'blog_posts', @model_name.plural + assert_equal "blog_posts", @model_name.plural end def test_element - assert_equal 'post', @model_name.element + assert_equal "post", @model_name.element end def test_collection - assert_equal 'blog/posts', @model_name.collection + assert_equal "blog/posts", @model_name.collection end def test_human - assert_equal 'Post', @model_name.human + assert_equal "Post", @model_name.human end def test_route_key - assert_equal 'posts', @model_name.route_key + assert_equal "posts", @model_name.route_key end def test_param_key - assert_equal 'post', @model_name.param_key + assert_equal "post", @model_name.param_key end def test_i18n_key @@ -198,16 +200,16 @@ class NamingHelpersTest < ActiveModel::TestCase def setup @klass = Contact @record = @klass.new - @singular = 'contact' - @plural = 'contacts' + @singular = "contact" + @plural = "contacts" @uncountable = Sheep - @singular_route_key = 'contact' - @route_key = 'contacts' - @param_key = 'contact' + @singular_route_key = "contact" + @route_key = "contacts" + @param_key = "contact" end def test_to_model_called_on_record - assert_equal 'post_named_track_backs', plural(Post::TrackBack.new) + assert_equal "post_named_track_backs", plural(Post::TrackBack.new) end def test_singular diff --git a/activemodel/test/cases/railtie_test.rb b/activemodel/test/cases/railtie_test.rb index 96b3b07e50..ff5022e960 100644 --- a/activemodel/test/cases/railtie_test.rb +++ b/activemodel/test/cases/railtie_test.rb @@ -1,11 +1,13 @@ -require 'cases/helper' -require 'active_support/testing/isolation' +# frozen_string_literal: true + +require "cases/helper" +require "active_support/testing/isolation" class RailtieTest < ActiveModel::TestCase include ActiveSupport::Testing::Isolation def setup - require 'active_model/railtie' + require "active_model/railtie" # Set a fake logger to avoid creating the log directory automatically fake_logger = Logger.new(nil) @@ -16,15 +18,15 @@ class RailtieTest < ActiveModel::TestCase end end - test 'secure password min_cost is false in the development environment' do - Rails.env = 'development' + 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' + test "secure password min_cost is true in the test environment" do + Rails.env = "test" @app.initialize! assert_equal true, ActiveModel::SecurePassword.min_cost diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index 6d56c8344a..d19e81a119 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -1,6 +1,8 @@ -require 'cases/helper' -require 'models/user' -require 'models/visitor' +# frozen_string_literal: true + +require "cases/helper" +require "models/user" +require "models/visitor" class SecurePasswordTest < ActiveModel::TestCase setup do @@ -13,7 +15,7 @@ class SecurePasswordTest < ActiveModel::TestCase # Simulate loading an existing user from the DB @existing_user = User.new - @existing_user.password_digest = BCrypt::Password.create('password', cost: BCrypt::Engine::MIN_COST) + @existing_user.password_digest = BCrypt::Password.create("password", cost: BCrypt::Engine::MIN_COST) end teardown do @@ -29,157 +31,157 @@ class SecurePasswordTest < ActiveModel::TestCase end test "create a new user with validations and valid password/confirmation" do - @user.password = 'password' - @user.password_confirmation = 'password' + @user.password = "password" + @user.password_confirmation = "password" - assert @user.valid?(:create), 'user should be valid' + assert @user.valid?(:create), "user should be valid" - @user.password = 'a' * 72 - @user.password_confirmation = 'a' * 72 + @user.password = "a" * 72 + @user.password_confirmation = "a" * 72 - assert @user.valid?(:create), 'user should be valid' + assert @user.valid?(:create), "user should be valid" end test "create a new user with validation and a spaces only password" do - @user.password = ' ' * 72 - assert @user.valid?(:create), 'user should be valid' + @user.password = " " * 72 + assert @user.valid?(:create), "user should be valid" end test "create a new user with validation and a blank password" do - @user.password = '' - assert !@user.valid?(:create), 'user should be invalid' + @user.password = "" + assert !@user.valid?(:create), "user should be invalid" assert_equal 1, @user.errors.count assert_equal ["can't be blank"], @user.errors[:password] end test "create a new user with validation and a nil password" do @user.password = nil - assert !@user.valid?(:create), 'user should be invalid' + assert !@user.valid?(:create), "user should be invalid" assert_equal 1, @user.errors.count assert_equal ["can't be blank"], @user.errors[:password] end - test 'create a new user with validation and password length greater than 72' do - @user.password = 'a' * 73 - @user.password_confirmation = 'a' * 73 - assert !@user.valid?(:create), 'user should be invalid' + test "create a new user with validation and password length greater than 72" do + @user.password = "a" * 73 + @user.password_confirmation = "a" * 73 + assert !@user.valid?(:create), "user should be invalid" assert_equal 1, @user.errors.count assert_equal ["is too long (maximum is 72 characters)"], @user.errors[:password] end test "create a new user with validation and a blank password confirmation" do - @user.password = 'password' - @user.password_confirmation = '' - assert !@user.valid?(:create), 'user should be invalid' + @user.password = "password" + @user.password_confirmation = "" + assert !@user.valid?(:create), "user should be invalid" assert_equal 1, @user.errors.count assert_equal ["doesn't match Password"], @user.errors[:password_confirmation] end test "create a new user with validation and a nil password confirmation" do - @user.password = 'password' + @user.password = "password" @user.password_confirmation = nil - assert @user.valid?(:create), 'user should be valid' + assert @user.valid?(:create), "user should be valid" end test "create a new user with validation and an incorrect password confirmation" do - @user.password = 'password' - @user.password_confirmation = 'something else' - assert !@user.valid?(:create), 'user should be invalid' + @user.password = "password" + @user.password_confirmation = "something else" + assert !@user.valid?(:create), "user should be invalid" assert_equal 1, @user.errors.count assert_equal ["doesn't match Password"], @user.errors[:password_confirmation] end test "update an existing user with validation and no change in password" do - assert @existing_user.valid?(:update), 'user should be valid' + assert @existing_user.valid?(:update), "user should be valid" end test "update an existing user with validations and valid password/confirmation" do - @existing_user.password = 'password' - @existing_user.password_confirmation = 'password' + @existing_user.password = "password" + @existing_user.password_confirmation = "password" - assert @existing_user.valid?(:update), 'user should be valid' + assert @existing_user.valid?(:update), "user should be valid" - @existing_user.password = 'a' * 72 - @existing_user.password_confirmation = 'a' * 72 + @existing_user.password = "a" * 72 + @existing_user.password_confirmation = "a" * 72 - assert @existing_user.valid?(:update), 'user should be valid' + assert @existing_user.valid?(:update), "user should be valid" end test "updating an existing user with validation and a blank password" do - @existing_user.password = '' - assert @existing_user.valid?(:update), 'user should be valid' + @existing_user.password = "" + assert @existing_user.valid?(:update), "user should be valid" end test "updating an existing user with validation and a spaces only password" do - @user.password = ' ' * 72 - assert @user.valid?(:update), 'user should be valid' + @user.password = " " * 72 + assert @user.valid?(:update), "user should be valid" end test "updating an existing user with validation and a blank password and password_confirmation" do - @existing_user.password = '' - @existing_user.password_confirmation = '' - assert @existing_user.valid?(:update), 'user should be valid' + @existing_user.password = "" + @existing_user.password_confirmation = "" + assert @existing_user.valid?(:update), "user should be valid" end test "updating an existing user with validation and a nil password" do @existing_user.password = nil - assert !@existing_user.valid?(:update), 'user should be invalid' + assert !@existing_user.valid?(:update), "user should be invalid" assert_equal 1, @existing_user.errors.count assert_equal ["can't be blank"], @existing_user.errors[:password] end - test 'updating an existing user with validation and password length greater than 72' do - @existing_user.password = 'a' * 73 - @existing_user.password_confirmation = 'a' * 73 - assert !@existing_user.valid?(:update), 'user should be invalid' + test "updating an existing user with validation and password length greater than 72" do + @existing_user.password = "a" * 73 + @existing_user.password_confirmation = "a" * 73 + assert !@existing_user.valid?(:update), "user should be invalid" assert_equal 1, @existing_user.errors.count assert_equal ["is too long (maximum is 72 characters)"], @existing_user.errors[:password] end test "updating an existing user with validation and a blank password confirmation" do - @existing_user.password = 'password' - @existing_user.password_confirmation = '' - assert !@existing_user.valid?(:update), 'user should be invalid' + @existing_user.password = "password" + @existing_user.password_confirmation = "" + assert !@existing_user.valid?(:update), "user should be invalid" assert_equal 1, @existing_user.errors.count assert_equal ["doesn't match Password"], @existing_user.errors[:password_confirmation] end test "updating an existing user with validation and a nil password confirmation" do - @existing_user.password = 'password' + @existing_user.password = "password" @existing_user.password_confirmation = nil - assert @existing_user.valid?(:update), 'user should be valid' + assert @existing_user.valid?(:update), "user should be valid" end test "updating an existing user with validation and an incorrect password confirmation" do - @existing_user.password = 'password' - @existing_user.password_confirmation = 'something else' - assert !@existing_user.valid?(:update), 'user should be invalid' + @existing_user.password = "password" + @existing_user.password_confirmation = "something else" + assert !@existing_user.valid?(:update), "user should be invalid" assert_equal 1, @existing_user.errors.count assert_equal ["doesn't match Password"], @existing_user.errors[:password_confirmation] end test "updating an existing user with validation and a blank password digest" do - @existing_user.password_digest = '' - assert !@existing_user.valid?(:update), 'user should be invalid' + @existing_user.password_digest = "" + assert !@existing_user.valid?(:update), "user should be invalid" assert_equal 1, @existing_user.errors.count assert_equal ["can't be blank"], @existing_user.errors[:password] end test "updating an existing user with validation and a nil password digest" do @existing_user.password_digest = nil - assert !@existing_user.valid?(:update), 'user should be invalid' + assert !@existing_user.valid?(:update), "user should be invalid" assert_equal 1, @existing_user.errors.count assert_equal ["can't be blank"], @existing_user.errors[:password] end test "setting a blank password should not change an existing password" do - @existing_user.password = '' - assert @existing_user.password_digest == 'password' + @existing_user.password = "" + assert @existing_user.password_digest == "password" end test "setting a nil password should clear an existing password" do @existing_user.password = nil - assert_equal nil, @existing_user.password_digest + assert_nil @existing_user.password_digest end test "authenticate" do diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb index 8d3165cd78..9002982e7f 100644 --- a/activemodel/test/cases/serialization_test.rb +++ b/activemodel/test/cases/serialization_test.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require "cases/helper" -require 'active_support/core_ext/object/instance_variables' +require "active_support/core_ext/object/instance_variables" class SerializationTest < ActiveModel::TestCase class User @@ -18,14 +20,14 @@ class SerializationTest < ActiveModel::TestCase def method_missing(method_name, *args) if method_name == :bar - 'i_am_bar' + "i_am_bar" else super end end def foo - 'i_am_foo' + "i_am_foo" end end @@ -40,43 +42,43 @@ class SerializationTest < ActiveModel::TestCase end setup do - @user = User.new('David', 'david@example.com', 'male') + @user = User.new("David", "david@example.com", "male") @user.address = Address.new @user.address.street = "123 Lane" @user.address.city = "Springfield" @user.address.state = "CA" @user.address.zip = 11111 - @user.friends = [User.new('Joe', 'joe@example.com', 'male'), - User.new('Sue', 'sue@example.com', 'female')] + @user.friends = [User.new("Joe", "joe@example.com", "male"), + User.new("Sue", "sue@example.com", "female")] end def test_method_serializable_hash_should_work - expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} + expected = { "name" => "David", "gender" => "male", "email" => "david@example.com" } assert_equal expected, @user.serializable_hash end def test_method_serializable_hash_should_work_with_only_option - expected = {"name"=>"David"} + expected = { "name" => "David" } 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"} + expected = { "gender" => "male", "email" => "david@example.com" } 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", "bar"=>"i_am_bar", "email"=>"david@example.com"} + expected = { "name" => "David", "gender" => "male", "foo" => "i_am_foo", "bar" => "i_am_bar", "email" => "david@example.com" } assert_equal expected, @user.serializable_hash(methods: [:foo, :bar]) end def test_method_serializable_hash_should_work_with_only_and_methods - expected = {"foo"=>"i_am_foo", "bar"=>"i_am_bar"} + expected = { "foo" => "i_am_foo", "bar" => "i_am_bar" } assert_equal expected, @user.serializable_hash(only: [], methods: [:foo, :bar]) end def test_method_serializable_hash_should_work_with_except_and_methods - expected = {"gender"=>"male", "foo"=>"i_am_foo", "bar"=>"i_am_bar"} + expected = { "gender" => "male", "foo" => "i_am_foo", "bar" => "i_am_bar" } assert_equal expected, @user.serializable_hash(except: [:name, :email], methods: [:foo, :bar]) end @@ -94,21 +96,21 @@ class SerializationTest < ActiveModel::TestCase 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}} + 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) 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'}]} + 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) end def test_include_option_with_empty_association @user.friends = [] - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", "friends"=>[]} + expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", "friends" => [] } assert_equal expected, @user.serializable_hash(include: :friends) end @@ -124,52 +126,52 @@ class SerializationTest < ActiveModel::TestCase def test_include_option_with_ary @user.friends = FriendList.new(@user.friends) - 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'}]} + 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) end def test_multiple_includes - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - "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'}]} + expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", + "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]) end def test_include_with_options - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - "address"=>{"street"=>"123 Lane"}} + expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", + "address" => { "street" => "123 Lane" } } assert_equal expected, @user.serializable_hash(include: { address: { only: "street" } }) end def test_nested_include @user.friends.first.friends = [@user] - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - "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"=> []}]} + expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", + "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 } }) end def test_only_include - expected = {"name"=>"David", "friends" => [{"name" => "Joe"}, {"name" => "Sue"}]} + expected = { "name" => "David", "friends" => [{ "name" => "Joe" }, { "name" => "Sue" }] } 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'}]} + 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 } }) end def test_multiple_includes_with_options - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - "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]) + expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", + "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]) end end diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb index d765a47636..aae98c9fe4 100644 --- a/activemodel/test/cases/serializers/json_serialization_test.rb +++ b/activemodel/test/cases/serializers/json_serialization_test.rb @@ -1,15 +1,17 @@ -require 'cases/helper' -require 'models/contact' -require 'active_support/core_ext/object/instance_variables' +# frozen_string_literal: true + +require "cases/helper" +require "models/contact" +require "active_support/core_ext/object/instance_variables" class JsonSerializationTest < ActiveModel::TestCase def setup @contact = Contact.new - @contact.name = 'Konata Izumi' + @contact.name = "Konata Izumi" @contact.age = 16 @contact.created_at = Time.utc(2006, 8, 1) @contact.awesome = true - @contact.preferences = { 'shows' => 'anime' } + @contact.preferences = { "shows" => "anime" } end test "should not include root in json (class method)" do @@ -18,7 +20,7 @@ class JsonSerializationTest < ActiveModel::TestCase assert_no_match %r{^\{"contact":\{}, json assert_match %r{"name":"Konata Izumi"}, json assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}) assert_match %r{"awesome":true}, json assert_match %r{"preferences":\{"shows":"anime"\}}, json end @@ -32,7 +34,7 @@ class JsonSerializationTest < ActiveModel::TestCase assert_match %r{^\{"contact":\{}, json assert_match %r{"name":"Konata Izumi"}, json assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}) assert_match %r{"awesome":true}, json assert_match %r{"preferences":\{"shows":"anime"\}}, json ensure @@ -53,12 +55,12 @@ class JsonSerializationTest < ActiveModel::TestCase end test "should include custom root in json" do - json = @contact.to_json(root: 'json_contact') + json = @contact.to_json(root: "json_contact") assert_match %r{^\{"json_contact":\{}, json assert_match %r{"name":"Konata Izumi"}, json assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}) assert_match %r{"awesome":true}, json assert_match %r{"preferences":\{"shows":"anime"\}}, json end @@ -68,7 +70,7 @@ class JsonSerializationTest < ActiveModel::TestCase assert_match %r{"name":"Konata Izumi"}, json assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}) assert_match %r{"awesome":true}, json assert_match %r{"preferences":\{"shows":"anime"\}}, json end @@ -79,7 +81,7 @@ class JsonSerializationTest < ActiveModel::TestCase assert_match %r{"name":"Konata Izumi"}, json assert_match %r{"age":16}, json assert_no_match %r{"awesome":true}, json - assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_not_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}) assert_no_match %r{"preferences":\{"shows":"anime"\}}, json end @@ -89,7 +91,7 @@ class JsonSerializationTest < ActiveModel::TestCase assert_no_match %r{"name":"Konata Izumi"}, json assert_no_match %r{"age":16}, json assert_match %r{"awesome":true}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_includes json, %("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}) assert_match %r{"preferences":\{"shows":"anime"\}}, json end @@ -134,9 +136,9 @@ class JsonSerializationTest < ActiveModel::TestCase json = @contact.as_json assert_kind_of Hash, json - assert_kind_of Hash, json['contact'] + assert_kind_of Hash, json["contact"] %w(name age created_at awesome preferences).each do |field| - assert_equal @contact.send(field), json['contact'][field] + assert_equal @contact.send(field), json["contact"][field] end ensure Contact.include_root_in_json = original_include_root_in_json diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb index 2c89388f14..cd75afec9e 100644 --- a/activemodel/test/cases/translation_test.rb +++ b/activemodel/test/cases/translation_test.rb @@ -1,8 +1,9 @@ -require 'cases/helper' -require 'models/person' +# frozen_string_literal: true -class ActiveModelI18nTests < ActiveModel::TestCase +require "cases/helper" +require "models/person" +class ActiveModelI18nTests < ActiveModel::TestCase def setup I18n.backend = I18n::Backend::Simple.new end @@ -12,102 +13,101 @@ class ActiveModelI18nTests < ActiveModel::TestCase end def test_translated_model_attributes - I18n.backend.store_translations 'en', activemodel: { attributes: { person: { name: 'person name attribute' } } } - assert_equal 'person name attribute', Person.human_attribute_name('name') + 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' } - assert_equal 'name default attribute', Person.human_attribute_name('name') + 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 - assert_equal 'Name', Person.human_attribute_name('name') + assert_equal "Name", Person.human_attribute_name("name") 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'} } } - assert_equal 'person name attribute', Person.human_attribute_name(:name) + 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'} } } - assert_equal 'child name attribute', Child.human_attribute_name('name') + 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'} } } - assert_equal 'person name attribute', Child.human_attribute_name('name') + 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') + 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' } } } - assert_equal 'Deeply Nested Address Street', Person.human_attribute_name('contacts.addresses.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' } } } - assert_equal 'Person Address Street', Person.human_attribute_name('addresses.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' } } } - assert_equal 'Cool Address Street', Person.human_attribute_name('addresses.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' } } - assert_equal 'person model', Person.model_name.human + 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' } } - assert_equal 'child model', Child.model_name.human + I18n.backend.store_translations "en", activemodel: { models: { child: "child model" } } + assert_equal "child model", Child.model_name.human end def test_translated_model_with_namespace - I18n.backend.store_translations 'en', activemodel: { models: { 'person/gender': 'gender model' } } - assert_equal 'gender model', Person::Gender.model_name.human + I18n.backend.store_translations "en", activemodel: { models: { 'person/gender': "gender model" } } + assert_equal "gender model", Person::Gender.model_name.human end def test_translated_model_names_with_ancestors_fallback - I18n.backend.store_translations 'en', activemodel: { models: { person: 'person model' } } - assert_equal 'person model', Child.model_name.human + 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' } - Person.human_attribute_name('gender', options) - assert_equal({ default: 'Cool gender' }, options) + options = { default: "Cool gender" } + Person.human_attribute_name("gender", options) + assert_equal({ default: "Cool gender" }, options) end end - diff --git a/activemodel/test/cases/type/big_integer_test.rb b/activemodel/test/cases/type/big_integer_test.rb new file mode 100644 index 0000000000..0fa0200df4 --- /dev/null +++ b/activemodel/test/cases/type/big_integer_test.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + module Type + class BigIntegerTest < ActiveModel::TestCase + def test_type_cast_big_integer + type = Type::BigInteger.new + assert_equal 1, type.cast(1) + assert_equal 1, type.cast("1") + end + + def test_small_values + type = Type::BigInteger.new + assert_equal(-9999999999999999999999999999999, type.serialize(-9999999999999999999999999999999)) + end + + def test_large_values + type = Type::BigInteger.new + assert_equal 9999999999999999999999999999999, type.serialize(9999999999999999999999999999999) + end + end + end +end diff --git a/activemodel/test/cases/type/binary_test.rb b/activemodel/test/cases/type/binary_test.rb new file mode 100644 index 0000000000..3221a73e49 --- /dev/null +++ b/activemodel/test/cases/type/binary_test.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + module Type + class BinaryTest < ActiveModel::TestCase + def test_type_cast_binary + type = Type::Binary.new + assert_nil type.cast(nil) + assert_equal "1", type.cast("1") + assert_equal 1, type.cast(1) + end + end + end +end diff --git a/activemodel/test/cases/type/boolean_test.rb b/activemodel/test/cases/type/boolean_test.rb new file mode 100644 index 0000000000..2d33579595 --- /dev/null +++ b/activemodel/test/cases/type/boolean_test.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + module Type + class BooleanTest < ActiveModel::TestCase + def test_type_cast_boolean + type = Type::Boolean.new + assert type.cast("").nil? + assert type.cast(nil).nil? + + assert type.cast(true) + assert type.cast(1) + assert type.cast("1") + assert type.cast("t") + assert type.cast("T") + assert type.cast("true") + assert type.cast("TRUE") + assert type.cast("on") + assert type.cast("ON") + assert type.cast(" ") + assert type.cast("\u3000\r\n") + assert type.cast("\u0000") + assert type.cast("SOMETHING RANDOM") + + # explicitly check for false vs nil + assert_equal false, type.cast(false) + assert_equal false, type.cast(0) + assert_equal false, type.cast("0") + assert_equal false, type.cast("f") + assert_equal false, type.cast("F") + assert_equal false, type.cast("false") + assert_equal false, type.cast("FALSE") + assert_equal false, type.cast("off") + assert_equal false, type.cast("OFF") + end + end + end +end diff --git a/activemodel/test/cases/type/date_test.rb b/activemodel/test/cases/type/date_test.rb new file mode 100644 index 0000000000..e8cf178612 --- /dev/null +++ b/activemodel/test/cases/type/date_test.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + module Type + class DateTest < ActiveModel::TestCase + def test_type_cast_date + type = Type::Date.new + assert_nil type.cast(nil) + assert_nil type.cast("") + assert_nil type.cast(" ") + assert_nil type.cast("ABC") + + date_string = ::Time.now.utc.strftime("%F") + assert_equal date_string, type.cast(date_string).strftime("%F") + end + end + end +end diff --git a/activemodel/test/cases/type/date_time_test.rb b/activemodel/test/cases/type/date_time_test.rb new file mode 100644 index 0000000000..60f62becc2 --- /dev/null +++ b/activemodel/test/cases/type/date_time_test.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + module Type + class DateTimeTest < ActiveModel::TestCase + def test_type_cast_datetime_and_timestamp + type = Type::DateTime.new + assert_nil type.cast(nil) + assert_nil type.cast("") + assert_nil type.cast(" ") + assert_nil type.cast("ABC") + + datetime_string = ::Time.now.utc.strftime("%FT%T") + assert_equal datetime_string, type.cast(datetime_string).strftime("%FT%T") + end + + def test_string_to_time_with_timezone + ["UTC", "US/Eastern"].each do |zone| + with_timezone_config default: zone do + type = Type::DateTime.new + assert_equal ::Time.utc(2013, 9, 4, 0, 0, 0), type.cast("Wed, 04 Sep 2013 03:00:00 EAT") + end + end + end + + private + + def with_timezone_config(default:) + old_zone_default = ::Time.zone_default + ::Time.zone_default = ::Time.find_zone(default) + yield + ensure + ::Time.zone_default = old_zone_default + end + end + end +end diff --git a/activemodel/test/cases/type/decimal_test.rb b/activemodel/test/cases/type/decimal_test.rb index 1950566c0e..c0cf6ce590 100644 --- a/activemodel/test/cases/type/decimal_test.rb +++ b/activemodel/test/cases/type/decimal_test.rb @@ -1,19 +1,28 @@ +# frozen_string_literal: true + require "cases/helper" -require "active_model/type" module ActiveModel module Type class DecimalTest < ActiveModel::TestCase def test_type_cast_decimal type = Decimal.new - assert_equal BigDecimal.new("0"), type.cast(BigDecimal.new("0")) - assert_equal BigDecimal.new("123"), type.cast(123.0) - assert_equal BigDecimal.new("1"), type.cast(:"1") + assert_equal BigDecimal("0"), type.cast(BigDecimal("0")) + assert_equal BigDecimal("123"), type.cast(123.0) + assert_equal BigDecimal("1"), type.cast(:"1") + end + + def test_type_cast_decimal_from_invalid_string + type = Decimal.new + assert_nil type.cast("") + assert_equal BigDecimal("1"), type.cast("1ignore") + assert_equal BigDecimal("0"), type.cast("bad1") + assert_equal BigDecimal("0"), type.cast("bad") end def test_type_cast_decimal_from_float_with_large_precision type = Decimal.new(precision: ::Float::DIG + 2) - assert_equal BigDecimal.new("123.0"), type.cast(123.0) + assert_equal BigDecimal("123.0"), type.cast(123.0) end def test_type_cast_from_float_with_unspecified_precision @@ -39,7 +48,7 @@ module ActiveModel def test_type_cast_decimal_from_object_responding_to_d value = Object.new def value.to_d - BigDecimal.new("1") + BigDecimal("1") end type = Decimal.new assert_equal BigDecimal("1"), type.cast(value) @@ -48,9 +57,9 @@ module ActiveModel def test_changed? type = Decimal.new - assert type.changed?(5.0, 5.0, '5.0wibble') - assert_not type.changed?(5.0, 5.0, '5.0') - assert_not type.changed?(-5.0, -5.0, '-5.0') + assert type.changed?(5.0, 5.0, "5.0wibble") + assert_not type.changed?(5.0, 5.0, "5.0") + assert_not type.changed?(-5.0, -5.0, "-5.0") end def test_scale_is_applied_before_precision_to_prevent_rounding_errors diff --git a/activemodel/test/cases/type/float_test.rb b/activemodel/test/cases/type/float_test.rb new file mode 100644 index 0000000000..28318e06f8 --- /dev/null +++ b/activemodel/test/cases/type/float_test.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + module Type + class FloatTest < ActiveModel::TestCase + def test_type_cast_float + type = Type::Float.new + assert_equal 1.0, type.cast("1") + end + + def test_type_cast_float_from_invalid_string + type = Type::Float.new + assert_nil type.cast("") + assert_equal 1.0, type.cast("1ignore") + assert_equal 0.0, type.cast("bad1") + assert_equal 0.0, type.cast("bad") + end + + def test_changing_float + type = Type::Float.new + + assert type.changed?(5.0, 5.0, "5wibble") + assert_not type.changed?(5.0, 5.0, "5") + assert_not type.changed?(5.0, 5.0, "5.0") + assert_not type.changed?(nil, nil, nil) + end + end + end +end diff --git a/activemodel/test/cases/type/immutable_string_test.rb b/activemodel/test/cases/type/immutable_string_test.rb new file mode 100644 index 0000000000..751f753ddb --- /dev/null +++ b/activemodel/test/cases/type/immutable_string_test.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + module Type + class ImmutableStringTest < ActiveModel::TestCase + test "cast strings are frozen" do + s = "foo" + type = Type::ImmutableString.new + assert_equal true, type.cast(s).frozen? + end + + test "immutable strings are not duped coming out" do + s = "foo" + type = Type::ImmutableString.new + assert_same s, type.cast(s) + assert_same s, type.deserialize(s) + end + end + end +end diff --git a/activemodel/test/cases/type/integer_test.rb b/activemodel/test/cases/type/integer_test.rb index 6603f25c9a..8c5d18c9b3 100644 --- a/activemodel/test/cases/type/integer_test.rb +++ b/activemodel/test/cases/type/integer_test.rb @@ -1,16 +1,19 @@ +# frozen_string_literal: true + require "cases/helper" -require "active_model/type" +require "active_support/core_ext/numeric/time" module ActiveModel module Type class IntegerTest < ActiveModel::TestCase test "simple values" do type = Type::Integer.new + assert_nil type.cast("") assert_equal 1, type.cast(1) - assert_equal 1, type.cast('1') - assert_equal 1, type.cast('1ignore') - assert_equal 0, type.cast('bad1') - assert_equal 0, type.cast('bad') + assert_equal 1, type.cast("1") + assert_equal 1, type.cast("1ignore") + assert_equal 0, type.cast("bad1") + assert_equal 0, type.cast("bad") assert_equal 1, type.cast(1.7) assert_equal 0, type.cast(false) assert_equal 1, type.cast(true) @@ -19,8 +22,8 @@ module ActiveModel test "random objects cast to nil" do type = Type::Integer.new - assert_nil type.cast([1,2]) - assert_nil type.cast({1 => 2}) + assert_nil type.cast([1, 2]) + assert_nil type.cast(1 => 2) assert_nil type.cast(1..2) end @@ -32,7 +35,7 @@ module ActiveModel test "casting nan and infinity" do type = Type::Integer.new assert_nil type.cast(::Float::NAN) - assert_nil type.cast(1.0/0.0) + assert_nil type.cast(1.0 / 0.0) end test "casting booleans for database" do @@ -41,14 +44,20 @@ module ActiveModel assert_equal 0, type.serialize(false) end + test "casting duration" do + type = Type::Integer.new + assert_equal 1800, type.cast(30.minutes) + assert_equal 7200, type.cast(2.hours) + end + test "changed?" do type = Type::Integer.new - assert type.changed?(5, 5, '5wibble') - assert_not type.changed?(5, 5, '5') - assert_not type.changed?(5, 5, '5.0') - assert_not type.changed?(-5, -5, '-5') - assert_not type.changed?(-5, -5, '-5.0') + assert type.changed?(5, 5, "5wibble") + assert_not type.changed?(5, 5, "5") + assert_not type.changed?(5, 5, "5.0") + assert_not type.changed?(-5, -5, "-5") + assert_not type.changed?(-5, -5, "-5.0") assert_not type.changed?(nil, nil, nil) end diff --git a/activemodel/test/cases/type/registry_test.rb b/activemodel/test/cases/type/registry_test.rb index 2a48998a62..0633ea2538 100644 --- a/activemodel/test/cases/type/registry_test.rb +++ b/activemodel/test/cases/type/registry_test.rb @@ -1,39 +1,42 @@ +# frozen_string_literal: true + require "cases/helper" -require "active_model/type" module ActiveModel - class RegistryTest < ActiveModel::TestCase - test "a class can be registered for a symbol" do - registry = Type::Registry.new - registry.register(:foo, ::String) - registry.register(:bar, ::Array) - - assert_equal "", registry.lookup(:foo) - assert_equal [], registry.lookup(:bar) - end + module Type + class RegistryTest < ActiveModel::TestCase + test "a class can be registered for a symbol" do + registry = Type::Registry.new + registry.register(:foo, ::String) + registry.register(:bar, ::Array) - test "a block can be registered" do - registry = Type::Registry.new - registry.register(:foo) do |*args| - [*args, "block for foo"] + assert_equal "", registry.lookup(:foo) + assert_equal [], registry.lookup(:bar) end - registry.register(:bar) do |*args| - [*args, "block for bar"] + + test "a block can be registered" do + registry = Type::Registry.new + registry.register(:foo) do |*args| + [*args, "block for foo"] + end + registry.register(:bar) do |*args| + [*args, "block for bar"] + end + + assert_equal [:foo, 1, "block for foo"], registry.lookup(:foo, 1) + assert_equal [:foo, 2, "block for foo"], registry.lookup(:foo, 2) + assert_equal [:bar, 1, 2, 3, "block for bar"], registry.lookup(:bar, 1, 2, 3) end - assert_equal [:foo, 1, "block for foo"], registry.lookup(:foo, 1) - assert_equal [:foo, 2, "block for foo"], registry.lookup(:foo, 2) - assert_equal [:bar, 1, 2, 3, "block for bar"], registry.lookup(:bar, 1, 2, 3) - end + test "a reasonable error is given when no type is found" do + registry = Type::Registry.new - test "a reasonable error is given when no type is found" do - registry = Type::Registry.new + e = assert_raises(ArgumentError) do + registry.lookup(:foo) + end - e = assert_raises(ArgumentError) do - registry.lookup(:foo) + assert_equal "Unknown type :foo", e.message end - - assert_equal "Unknown type :foo", e.message end end end diff --git a/activemodel/test/cases/type/string_test.rb b/activemodel/test/cases/type/string_test.rb index 7b25a1ef74..825c8bb246 100644 --- a/activemodel/test/cases/type/string_test.rb +++ b/activemodel/test/cases/type/string_test.rb @@ -1,27 +1,38 @@ +# frozen_string_literal: true + require "cases/helper" -require "active_model/type" module ActiveModel - class StringTypeTest < ActiveModel::TestCase - test "type casting" do - type = Type::String.new - assert_equal "t", type.cast(true) - assert_equal "f", type.cast(false) - assert_equal "123", type.cast(123) - end + module Type + class StringTest < ActiveModel::TestCase + test "type casting" do + type = Type::String.new + assert_equal "t", type.cast(true) + assert_equal "f", type.cast(false) + assert_equal "123", type.cast(123) + end - test "immutable strings are not duped coming out" do - s = "foo" - type = Type::ImmutableString.new - assert_same s, type.cast(s) - assert_same s, type.deserialize(s) - end + test "cast strings are mutable" do + type = Type::String.new + + s = "foo".dup + assert_equal false, type.cast(s).frozen? + assert_equal false, s.frozen? + + f = "foo".freeze + assert_equal false, type.cast(f).frozen? + assert_equal true, f.frozen? + end + + test "values are duped coming out" do + type = Type::String.new - test "values are duped coming out" do - s = "foo" - type = Type::String.new - assert_not_same s, type.cast(s) - assert_not_same s, type.deserialize(s) + s = "foo" + assert_not_same s, type.cast(s) + assert_equal s, type.cast(s) + assert_not_same s, type.deserialize(s) + assert_equal s, type.deserialize(s) + end end end end diff --git a/activemodel/test/cases/type/time_test.rb b/activemodel/test/cases/type/time_test.rb new file mode 100644 index 0000000000..f7102d1e97 --- /dev/null +++ b/activemodel/test/cases/type/time_test.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + module Type + class TimeTest < ActiveModel::TestCase + def test_type_cast_time + type = Type::Time.new + assert_nil type.cast(nil) + assert_nil type.cast("") + assert_nil type.cast("ABC") + + time_string = ::Time.now.utc.strftime("%T") + assert_equal time_string, type.cast(time_string).strftime("%T") + + assert_equal ::Time.utc(2000, 1, 1, 16, 45, 54), type.cast("2015-06-13T19:45:54+03:00") + assert_equal ::Time.utc(1999, 12, 31, 21, 7, 8), type.cast("06:07:08+09:00") + end + end + end +end diff --git a/activemodel/test/cases/type/unsigned_integer_test.rb b/activemodel/test/cases/type/unsigned_integer_test.rb deleted file mode 100644 index 026cb08a06..0000000000 --- a/activemodel/test/cases/type/unsigned_integer_test.rb +++ /dev/null @@ -1,18 +0,0 @@ -require "cases/helper" -require "active_model/type" - -module ActiveModel - module Type - class UnsignedIntegerTest < ActiveModel::TestCase - test "unsigned int max value is in range" do - assert_equal(4294967295, UnsignedInteger.new.serialize(4294967295)) - end - - test "minus value is out of range" do - assert_raises(ActiveModel::RangeError) do - UnsignedInteger.new.serialize(-1) - end - end - end - end -end diff --git a/activemodel/test/cases/type/value_test.rb b/activemodel/test/cases/type/value_test.rb new file mode 100644 index 0000000000..55b5d9d584 --- /dev/null +++ b/activemodel/test/cases/type/value_test.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveModel + module Type + class ValueTest < ActiveModel::TestCase + def test_type_equality + assert_equal Type::Value.new, Type::Value.new + assert_not_equal Type::Value.new, Type::Integer.new + assert_not_equal Type::Value.new(precision: 1), Type::Value.new(precision: 2) + end + end + end +end diff --git a/activemodel/test/cases/types_test.rb b/activemodel/test/cases/types_test.rb deleted file mode 100644 index 558c56f157..0000000000 --- a/activemodel/test/cases/types_test.rb +++ /dev/null @@ -1,125 +0,0 @@ -require "cases/helper" -require "active_model/type" -require "active_support/core_ext/numeric/time" - -module ActiveModel - class TypesTest < ActiveModel::TestCase - def test_type_cast_boolean - type = Type::Boolean.new - assert type.cast('').nil? - assert type.cast(nil).nil? - - assert type.cast(true) - assert type.cast(1) - assert type.cast('1') - assert type.cast('t') - assert type.cast('T') - assert type.cast('true') - assert type.cast('TRUE') - assert type.cast('on') - assert type.cast('ON') - assert type.cast(' ') - assert type.cast("\u3000\r\n") - assert type.cast("\u0000") - assert type.cast('SOMETHING RANDOM') - - # explicitly check for false vs nil - assert_equal false, type.cast(false) - assert_equal false, type.cast(0) - assert_equal false, type.cast('0') - assert_equal false, type.cast('f') - assert_equal false, type.cast('F') - assert_equal false, type.cast('false') - assert_equal false, type.cast('FALSE') - assert_equal false, type.cast('off') - assert_equal false, type.cast('OFF') - end - - def test_type_cast_float - type = Type::Float.new - assert_equal 1.0, type.cast("1") - end - - def test_changing_float - type = Type::Float.new - - assert type.changed?(5.0, 5.0, '5wibble') - assert_not type.changed?(5.0, 5.0, '5') - assert_not type.changed?(5.0, 5.0, '5.0') - assert_not type.changed?(nil, nil, nil) - end - - def test_type_cast_binary - type = Type::Binary.new - assert_equal nil, type.cast(nil) - assert_equal "1", type.cast("1") - assert_equal 1, type.cast(1) - end - - def test_type_cast_time - type = Type::Time.new - assert_equal nil, type.cast(nil) - assert_equal nil, type.cast('') - assert_equal nil, type.cast('ABC') - - time_string = Time.now.utc.strftime("%T") - assert_equal time_string, type.cast(time_string).strftime("%T") - - assert_equal ::Time.utc(2000, 1, 1, 16, 45, 54), type.cast('2015-06-13T19:45:54+03:00') - assert_equal ::Time.utc(1999, 12, 31, 21, 7, 8), type.cast('06:07:08+09:00') - end - - def test_type_cast_datetime_and_timestamp - type = Type::DateTime.new - assert_equal nil, type.cast(nil) - assert_equal nil, type.cast('') - assert_equal nil, type.cast(' ') - assert_equal nil, type.cast('ABC') - - datetime_string = Time.now.utc.strftime("%FT%T") - assert_equal datetime_string, type.cast(datetime_string).strftime("%FT%T") - end - - def test_type_cast_date - type = Type::Date.new - assert_equal nil, type.cast(nil) - assert_equal nil, type.cast('') - assert_equal nil, type.cast(' ') - assert_equal nil, type.cast('ABC') - - date_string = Time.now.utc.strftime("%F") - assert_equal date_string, type.cast(date_string).strftime("%F") - end - - def test_type_cast_duration_to_integer - type = Type::Integer.new - assert_equal 1800, type.cast(30.minutes) - assert_equal 7200, type.cast(2.hours) - end - - def test_string_to_time_with_timezone - ["UTC", "US/Eastern"].each do |zone| - with_timezone_config default: zone do - type = Type::DateTime.new - assert_equal Time.utc(2013, 9, 4, 0, 0, 0), type.cast("Wed, 04 Sep 2013 03:00:00 EAT") - end - end - end - - def test_type_equality - assert_equal Type::Value.new, Type::Value.new - assert_not_equal Type::Value.new, Type::Integer.new - assert_not_equal Type::Value.new(precision: 1), Type::Value.new(precision: 2) - end - - private - - def with_timezone_config(default:) - old_zone_default = ::Time.zone_default - ::Time.zone_default = ::Time.find_zone(default) - yield - ensure - ::Time.zone_default = old_zone_default - end - end -end diff --git a/activemodel/test/cases/validations/absence_validation_test.rb b/activemodel/test/cases/validations/absence_validation_test.rb index 9cbc77dfb5..801577474a 100644 --- a/activemodel/test/cases/validations/absence_validation_test.rb +++ b/activemodel/test/cases/validations/absence_validation_test.rb @@ -1,7 +1,9 @@ -require 'cases/helper' -require 'models/topic' -require 'models/person' -require 'models/custom_reader' +# frozen_string_literal: true + +require "cases/helper" +require "models/topic" +require "models/person" +require "models/custom_reader" class AbsenceValidationTest < ActiveModel::TestCase teardown do @@ -19,7 +21,7 @@ class AbsenceValidationTest < ActiveModel::TestCase assert_equal ["must be blank"], t.errors[:title] assert_equal ["must be blank"], t.errors[:content] t.title = "" - t.content = "something" + t.content = "something" assert t.invalid? assert_equal ["must be blank"], t.errors[:content] assert_equal [], t.errors[:title] diff --git a/activemodel/test/cases/validations/acceptance_validation_test.rb b/activemodel/test/cases/validations/acceptance_validation_test.rb index d3995ad5af..c5f54b1868 100644 --- a/activemodel/test/cases/validations/acceptance_validation_test.rb +++ b/activemodel/test/cases/validations/acceptance_validation_test.rb @@ -1,11 +1,12 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' -require 'models/reply' -require 'models/person' +require "cases/helper" -class AcceptanceValidationTest < ActiveModel::TestCase +require "models/topic" +require "models/reply" +require "models/person" +class AcceptanceValidationTest < ActiveModel::TestCase def teardown Topic.clear_validators! end @@ -20,7 +21,7 @@ class AcceptanceValidationTest < ActiveModel::TestCase def test_terms_of_service_agreement Topic.validates_acceptance_of(:terms_of_service) - t = Topic.new("title" => "We should be confirmed","terms_of_service" => "") + t = Topic.new("title" => "We should be confirmed", "terms_of_service" => "") assert t.invalid? assert_equal ["must be accepted"], t.errors[:terms_of_service] @@ -31,7 +32,7 @@ class AcceptanceValidationTest < ActiveModel::TestCase def test_eula Topic.validates_acceptance_of(:eula, message: "must be abided") - t = Topic.new("title" => "We should be confirmed","eula" => "") + t = Topic.new("title" => "We should be confirmed", "eula" => "") assert t.invalid? assert_equal ["must be abided"], t.errors[:eula] diff --git a/activemodel/test/cases/validations/callbacks_test.rb b/activemodel/test/cases/validations/callbacks_test.rb index 75eb18e795..ff3cf61746 100644 --- a/activemodel/test/cases/validations/callbacks_test.rb +++ b/activemodel/test/cases/validations/callbacks_test.rb @@ -1,4 +1,6 @@ -require 'cases/helper' +# frozen_string_literal: true + +require "cases/helper" class Dog include ActiveModel::Validations @@ -15,37 +17,37 @@ class DogWithMethodCallbacks < Dog before_validation :set_before_validation_marker after_validation :set_after_validation_marker - def set_before_validation_marker; self.history << 'before_validation_marker'; end - def set_after_validation_marker; self.history << 'after_validation_marker' ; end + def set_before_validation_marker; history << "before_validation_marker"; end + def set_after_validation_marker; history << "after_validation_marker" ; end end class DogValidatorsAreProc < Dog - before_validation { self.history << 'before_validation_marker' } - after_validation { self.history << 'after_validation_marker' } + before_validation { history << "before_validation_marker" } + after_validation { history << "after_validation_marker" } end class DogWithTwoValidators < Dog - before_validation { self.history << 'before_validation_marker1' } - before_validation { self.history << 'before_validation_marker2' } + before_validation { history << "before_validation_marker1" } + before_validation { history << "before_validation_marker2" } end -class DogDeprecatedBeforeValidatorReturningFalse < Dog +class DogBeforeValidatorReturningFalse < Dog before_validation { false } - before_validation { self.history << 'before_validation_marker2' } + before_validation { history << "before_validation_marker2" } end class DogBeforeValidatorThrowingAbort < Dog before_validation { throw :abort } - before_validation { self.history << 'before_validation_marker2' } + before_validation { history << "before_validation_marker2" } end class DogAfterValidatorReturningFalse < Dog after_validation { false } - after_validation { self.history << 'after_validation_marker' } + after_validation { history << "after_validation_marker" } end class DogWithMissingName < Dog - before_validation { self.history << 'before_validation_marker' } + before_validation { history << "before_validation_marker" } validates_presence_of :name end @@ -53,8 +55,20 @@ class DogValidatorWithOnCondition < Dog before_validation :set_before_validation_marker, on: :create after_validation :set_after_validation_marker, on: :create - def set_before_validation_marker; self.history << 'before_validation_marker'; end - def set_after_validation_marker; self.history << 'after_validation_marker' ; end + def set_before_validation_marker; history << "before_validation_marker"; end + def set_after_validation_marker; history << "after_validation_marker" ; end +end + +class DogValidatorWithOnMultipleCondition < Dog + before_validation :set_before_validation_marker_on_context_a, on: :context_a + before_validation :set_before_validation_marker_on_context_b, on: :context_b + after_validation :set_after_validation_marker_on_context_a, on: :context_a + after_validation :set_after_validation_marker_on_context_b, on: :context_b + + def set_before_validation_marker_on_context_a; history << "before_validation_marker on context_a"; end + def set_before_validation_marker_on_context_b; history << "before_validation_marker on context_b"; end + def set_after_validation_marker_on_context_a; history << "after_validation_marker on context_a" ; end + def set_after_validation_marker_on_context_b; history << "after_validation_marker on context_b" ; end end class DogValidatorWithIfCondition < Dog @@ -64,16 +78,14 @@ class DogValidatorWithIfCondition < Dog 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_before_validation_marker1; history << "before_validation_marker1"; end + def set_before_validation_marker2; 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 + def set_after_validation_marker1; history << "after_validation_marker1"; end + def set_after_validation_marker2; history << "after_validation_marker2" ; end end - class CallbacksWithMethodNamesShouldBeCalled < ActiveModel::TestCase - def test_if_condition_is_respected_for_before_validation d = DogValidatorWithIfCondition.new d.valid? @@ -98,22 +110,53 @@ class CallbacksWithMethodNamesShouldBeCalled < ActiveModel::TestCase assert_equal [], d.history end + def test_on_multiple_condition_is_respected_for_validation_with_matching_context + d = DogValidatorWithOnMultipleCondition.new + d.valid?(:context_a) + assert_equal ["before_validation_marker on context_a", "after_validation_marker on context_a"], d.history + + d = DogValidatorWithOnMultipleCondition.new + d.valid?(:context_b) + assert_equal ["before_validation_marker on context_b", "after_validation_marker on context_b"], d.history + + d = DogValidatorWithOnMultipleCondition.new + d.valid?([:context_a, :context_b]) + assert_equal([ + "before_validation_marker on context_a", + "before_validation_marker on context_b", + "after_validation_marker on context_a", + "after_validation_marker on context_b" + ], d.history) + end + + def test_on_multiple_condition_is_respected_for_validation_without_matching_context + d = DogValidatorWithOnMultipleCondition.new + d.valid?(:save) + assert_equal [], d.history + end + + def test_on_multiple_condition_is_respected_for_validation_without_context + d = DogValidatorWithOnMultipleCondition.new + d.valid? + assert_equal [], d.history + end + def test_before_validation_and_after_validation_callbacks_should_be_called d = DogWithMethodCallbacks.new d.valid? - assert_equal ['before_validation_marker', 'after_validation_marker'], d.history + assert_equal ["before_validation_marker", "after_validation_marker"], d.history end def test_before_validation_and_after_validation_callbacks_should_be_called_with_proc d = DogValidatorsAreProc.new d.valid? - assert_equal ['before_validation_marker', 'after_validation_marker'], d.history + assert_equal ["before_validation_marker", "after_validation_marker"], d.history end def test_before_validation_and_after_validation_callbacks_should_be_called_in_declared_order d = DogWithTwoValidators.new d.valid? - assert_equal ['before_validation_marker1', 'before_validation_marker2'], d.history + assert_equal ["before_validation_marker1", "before_validation_marker2"], d.history end def test_further_callbacks_should_not_be_called_if_before_validation_throws_abort @@ -123,26 +166,23 @@ class CallbacksWithMethodNamesShouldBeCalled < ActiveModel::TestCase assert_equal false, output end - def test_deprecated_further_callbacks_should_not_be_called_if_before_validation_returns_false - d = DogDeprecatedBeforeValidatorReturningFalse.new - assert_deprecated do - output = d.valid? - assert_equal [], d.history - assert_equal false, output - end + def test_further_callbacks_should_be_called_if_before_validation_returns_false + d = DogBeforeValidatorReturningFalse.new + output = d.valid? + assert_equal ["before_validation_marker2"], d.history + assert_equal true, output end def test_further_callbacks_should_be_called_if_after_validation_returns_false d = DogAfterValidatorReturningFalse.new d.valid? - assert_equal ['after_validation_marker'], d.history + assert_equal ["after_validation_marker"], d.history end def test_validation_test_should_be_done d = DogWithMissingName.new output = d.valid? - assert_equal ['before_validation_marker'], d.history + assert_equal ["before_validation_marker"], d.history assert_equal false, output end - end diff --git a/activemodel/test/cases/validations/conditional_validation_test.rb b/activemodel/test/cases/validations/conditional_validation_test.rb index 296d3b4407..caea8b65ef 100644 --- a/activemodel/test/cases/validations/conditional_validation_test.rb +++ b/activemodel/test/cases/validations/conditional_validation_test.rb @@ -1,9 +1,10 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' +require "cases/helper" -class ConditionalValidationTest < ActiveModel::TestCase +require "models/topic" +class ConditionalValidationTest < ActiveModel::TestCase def teardown Topic.clear_validators! end @@ -17,6 +18,22 @@ class ConditionalValidationTest < ActiveModel::TestCase assert_equal ["hoo 5"], t.errors["title"] end + def test_if_validation_using_array_of_true_methods + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: [:condition_is_true, :condition_is_true]) + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? + assert t.errors[:title].any? + assert_equal ["hoo 5"], t.errors["title"] + end + + def test_unless_validation_using_array_of_false_methods + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: [:condition_is_false, :condition_is_false]) + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? + assert t.errors[:title].any? + assert_equal ["hoo 5"], t.errors["title"] + end + 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) @@ -25,51 +42,31 @@ class ConditionalValidationTest < ActiveModel::TestCase 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) + def test_if_validation_using_array_of_true_and_false_methods + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: [:condition_is_true, :condition_is_false]) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? 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) - t = Topic.new("title" => "uhohuhoh", "content" => "whatever") - assert t.invalid? - assert t.errors[:title].any? - assert_equal ["hoo 5"], t.errors["title"] - end - - def test_if_validation_using_string_true - # When the evaluated string returns true - Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: "a = 1; a == 1") - t = Topic.new("title" => "uhohuhoh", "content" => "whatever") - assert t.invalid? - assert t.errors[:title].any? - assert_equal ["hoo 5"], t.errors["title"] - end - - 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") + def test_unless_validation_using_array_of_true_and_felse_methods + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", unless: [:condition_is_true, :condition_is_false]) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? 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") + 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_false) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? 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") + 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_false) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? @@ -98,7 +95,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_if_validation_using_block_false # When the block returns false Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", - if: Proc.new { |r| r.title != "uhohuhoh"}) + if: Proc.new { |r| r.title != "uhohuhoh" }) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert_empty t.errors[:title] @@ -107,32 +104,25 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_block_false # When the block returns false Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", - unless: Proc.new { |r| r.title != "uhohuhoh"} ) + unless: Proc.new { |r| r.title != "uhohuhoh" }) t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 5"], t.errors["title"] end - # previous implementation of validates_presence_of eval'd the - # string with the wrong binding, this regression test is to - # 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')") - - t = Topic.new - assert t.invalid?, "A topic without a title should not be valid" - 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" - - t.title = "A very important title" - assert t.invalid?, "A topic with an important title, but without an author, should not be valid" - assert t.errors[:author_name].any?, "A topic with an 'important' title should require an author" + def test_validation_using_conbining_if_true_and_unless_true_conditions + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true, unless: :condition_is_true) + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.valid? + assert_empty t.errors[:title] + end - t.author_name = "Hubert J. Farnsworth" - assert t.valid?, "A topic with an important title and author should be valid" + def test_validation_using_conbining_if_true_and_unless_false_conditions + Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}", if: :condition_is_true, unless: :condition_is_false) + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? + assert t.errors[:title].any? + assert_equal ["hoo 5"], t.errors["title"] end end diff --git a/activemodel/test/cases/validations/confirmation_validation_test.rb b/activemodel/test/cases/validations/confirmation_validation_test.rb index c56bf1c0ad..8b2c65289b 100644 --- a/activemodel/test/cases/validations/confirmation_validation_test.rb +++ b/activemodel/test/cases/validations/confirmation_validation_test.rb @@ -1,10 +1,11 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' -require 'models/person' +require "cases/helper" -class ConfirmationValidationTest < ActiveModel::TestCase +require "models/topic" +require "models/person" +class ConfirmationValidationTest < ActiveModel::TestCase def teardown Topic.clear_validators! end @@ -29,13 +30,26 @@ class ConfirmationValidationTest < ActiveModel::TestCase def test_title_confirmation Topic.validates_confirmation_of(:title) - t = Topic.new("title" => "We should be confirmed","title_confirmation" => "") + t = Topic.new("title" => "We should be confirmed", "title_confirmation" => "") assert t.invalid? t.title_confirmation = "We should be confirmed" assert t.valid? end + def test_validates_confirmation_of_with_boolean_attribute + Topic.validates_confirmation_of(:approved) + + t = Topic.new(approved: true, approved_confirmation: nil) + assert t.valid? + + t.approved_confirmation = false + assert t.invalid? + + t.approved_confirmation = true + assert t.valid? + end + def test_validates_confirmation_of_for_ruby_class Person.validates_confirmation_of :karma @@ -56,14 +70,13 @@ class ConfirmationValidationTest < 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', { + I18n.backend.store_translations("en", errors: { messages: { confirmation: "doesn't match %{attribute}" } }, - activemodel: { attributes: { topic: { title: 'Test Title'} } } - }) + activemodel: { attributes: { topic: { title: "Test Title" } } }) Topic.validates_confirmation_of(:title) - t = Topic.new("title" => "We should be confirmed","title_confirmation" => "") + t = Topic.new("title" => "We should be confirmed", "title_confirmation" => "") assert t.invalid? assert_equal ["doesn't match Test Title"], t.errors[:title_confirmation] ensure diff --git a/activemodel/test/cases/validations/exclusion_validation_test.rb b/activemodel/test/cases/validations/exclusion_validation_test.rb index 005bc15df5..68d611e904 100644 --- a/activemodel/test/cases/validations/exclusion_validation_test.rb +++ b/activemodel/test/cases/validations/exclusion_validation_test.rb @@ -1,11 +1,12 @@ -require 'cases/helper' -require 'active_support/core_ext/numeric/time' +# frozen_string_literal: true -require 'models/topic' -require 'models/person' +require "cases/helper" +require "active_support/core_ext/numeric/time" -class ExclusionValidationTest < ActiveModel::TestCase +require "models/topic" +require "models/person" +class ExclusionValidationTest < ActiveModel::TestCase def teardown Topic.clear_validators! end @@ -68,8 +69,8 @@ class ExclusionValidationTest < ActiveModel::TestCase def test_validates_exclusion_of_with_range Topic.validates_exclusion_of :content, in: ("a".."g") - assert Topic.new(content: 'g').invalid? - assert Topic.new(content: 'h').valid? + assert Topic.new(content: "g").invalid? + assert Topic.new(content: "h").valid? end def test_validates_exclusion_of_with_time_range diff --git a/activemodel/test/cases/validations/format_validation_test.rb b/activemodel/test/cases/validations/format_validation_test.rb index ea4c2ee7df..3ddda2154a 100644 --- a/activemodel/test/cases/validations/format_validation_test.rb +++ b/activemodel/test/cases/validations/format_validation_test.rb @@ -1,10 +1,11 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' -require 'models/person' +require "cases/helper" -class PresenceValidationTest < ActiveModel::TestCase +require "models/topic" +require "models/person" +class PresenceValidationTest < ActiveModel::TestCase def teardown Topic.clear_validators! end @@ -63,7 +64,7 @@ class PresenceValidationTest < ActiveModel::TestCase 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') + t = Topic.new(title: "Invalid title") assert t.invalid? assert_equal ["can't be Invalid title"], t.errors[:title] end @@ -108,7 +109,7 @@ class PresenceValidationTest < ActiveModel::TestCase 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" @@ -120,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" @@ -132,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 da63df9152..d3e44945db 100644 --- a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + require "cases/helper" -require 'models/person' +require "models/person" class I18nGenerateMessageValidationTest < ActiveModel::TestCase def setup @@ -10,29 +12,29 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase # validates_inclusion_of: generate_message(attr_name, :inclusion, message: custom_message, value: value) def test_generate_message_inclusion_with_default_message - assert_equal 'is not included in the list', @person.errors.generate_message(:title, :inclusion, value: 'title') + assert_equal "is not included in the list", @person.errors.generate_message(:title, :inclusion, value: "title") end def test_generate_message_inclusion_with_custom_message - assert_equal 'custom message title', @person.errors.generate_message(:title, :inclusion, message: 'custom message %{value}', value: 'title') + assert_equal "custom message title", @person.errors.generate_message(:title, :inclusion, message: "custom message %{value}", value: "title") end # validates_exclusion_of: generate_message(attr_name, :exclusion, message: custom_message, value: value) def test_generate_message_exclusion_with_default_message - assert_equal 'is reserved', @person.errors.generate_message(:title, :exclusion, value: 'title') + assert_equal "is reserved", @person.errors.generate_message(:title, :exclusion, value: "title") end def test_generate_message_exclusion_with_custom_message - assert_equal 'custom message title', @person.errors.generate_message(:title, :exclusion, message: 'custom message %{value}', value: 'title') + assert_equal "custom message title", @person.errors.generate_message(:title, :exclusion, message: "custom message %{value}", value: "title") end # validates_format_of: generate_message(attr_name, :invalid, message: custom_message, value: value) def test_generate_message_invalid_with_default_message - assert_equal 'is invalid', @person.errors.generate_message(:title, :invalid, value: 'title') + assert_equal "is invalid", @person.errors.generate_message(:title, :invalid, value: "title") end def test_generate_message_invalid_with_custom_message - assert_equal 'custom message title', @person.errors.generate_message(:title, :invalid, message: 'custom message %{value}', value: 'title') + assert_equal "custom message title", @person.errors.generate_message(:title, :invalid, message: "custom message %{value}", value: "title") end # validates_confirmation_of: generate_message(attr_name, :confirmation, message: custom_message) @@ -41,7 +43,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase end def test_generate_message_confirmation_with_custom_message - assert_equal 'custom message', @person.errors.generate_message(:title, :confirmation, message: 'custom message') + assert_equal "custom message", @person.errors.generate_message(:title, :confirmation, message: "custom message") end # validates_acceptance_of: generate_message(attr_name, :accepted, message: custom_message) @@ -50,7 +52,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase end def test_generate_message_accepted_with_custom_message - assert_equal 'custom message', @person.errors.generate_message(:title, :accepted, message: 'custom message') + assert_equal "custom message", @person.errors.generate_message(:title, :accepted, message: "custom message") end # add_on_empty: generate_message(attr, :empty, message: custom_message) @@ -59,7 +61,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase end def test_generate_message_empty_with_custom_message - assert_equal 'custom message', @person.errors.generate_message(:title, :empty, message: 'custom message') + assert_equal "custom message", @person.errors.generate_message(:title, :empty, message: "custom message") end # validates_presence_of: generate_message(attr, :blank, message: custom_message) @@ -68,7 +70,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase end def test_generate_message_blank_with_custom_message - assert_equal 'custom message', @person.errors.generate_message(:title, :blank, message: 'custom message') + assert_equal "custom message", @person.errors.generate_message(:title, :blank, message: "custom message") end # validates_length_of: generate_message(attr, :too_long, message: custom_message, count: option_value.end) @@ -81,7 +83,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase 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) @@ -94,7 +96,7 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase 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) @@ -107,44 +109,44 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase end def test_generate_message_wrong_length_with_custom_message - assert_equal 'custom message 10', @person.errors.generate_message(:title, :wrong_length, message: 'custom message %{count}', count: 10) + assert_equal "custom message 10", @person.errors.generate_message(:title, :wrong_length, message: "custom message %{count}", count: 10) end # validates_numericality_of: generate_message(attr_name, :not_a_number, value: raw_value, message: custom_message) def test_generate_message_not_a_number_with_default_message - assert_equal "is not a number", @person.errors.generate_message(:title, :not_a_number, value: 'title') + assert_equal "is not a number", @person.errors.generate_message(:title, :not_a_number, value: "title") end def test_generate_message_not_a_number_with_custom_message - assert_equal 'custom message title', @person.errors.generate_message(:title, :not_a_number, message: 'custom message %{value}', value: 'title') + assert_equal "custom message title", @person.errors.generate_message(:title, :not_a_number, message: "custom message %{value}", value: "title") end # validates_numericality_of: generate_message(attr_name, option, value: raw_value, default: custom_message) def test_generate_message_greater_than_with_default_message - assert_equal "must be greater than 10", @person.errors.generate_message(:title, :greater_than, value: 'title', count: 10) + assert_equal "must be greater than 10", @person.errors.generate_message(:title, :greater_than, value: "title", count: 10) end def test_generate_message_greater_than_or_equal_to_with_default_message - assert_equal "must be greater than or equal to 10", @person.errors.generate_message(:title, :greater_than_or_equal_to, value: 'title', count: 10) + assert_equal "must be greater than or equal to 10", @person.errors.generate_message(:title, :greater_than_or_equal_to, value: "title", count: 10) end def test_generate_message_equal_to_with_default_message - assert_equal "must be equal to 10", @person.errors.generate_message(:title, :equal_to, value: 'title', count: 10) + assert_equal "must be equal to 10", @person.errors.generate_message(:title, :equal_to, value: "title", count: 10) end def test_generate_message_less_than_with_default_message - assert_equal "must be less than 10", @person.errors.generate_message(:title, :less_than, value: 'title', count: 10) + assert_equal "must be less than 10", @person.errors.generate_message(:title, :less_than, value: "title", count: 10) end def test_generate_message_less_than_or_equal_to_with_default_message - assert_equal "must be less than or equal to 10", @person.errors.generate_message(:title, :less_than_or_equal_to, value: 'title', count: 10) + assert_equal "must be less than or equal to 10", @person.errors.generate_message(:title, :less_than_or_equal_to, value: "title", count: 10) end def test_generate_message_odd_with_default_message - assert_equal "must be odd", @person.errors.generate_message(:title, :odd, value: 'title', count: 10) + assert_equal "must be odd", @person.errors.generate_message(:title, :odd, value: "title", count: 10) end def test_generate_message_even_with_default_message - assert_equal "must be even", @person.errors.generate_message(:title, :even, value: 'title', count: 10) + assert_equal "must be even", @person.errors.generate_message(:title, :even, value: "title", count: 10) end end diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index 09d7226b5a..9cfe189d0e 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + require "cases/helper" -require 'models/person' +require "models/person" class I18nValidationTest < ActiveModel::TestCase - def setup Person.clear_validators! @person = Person.new @@ -10,7 +11,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,23 +22,23 @@ class I18nValidationTest < ActiveModel::TestCase end def test_full_message_encoding - I18n.backend.store_translations('en', errors: { - messages: { too_short: '猫舌' } }) + 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 + 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') - assert_called_with(Person, :human_attribute_name, [:name, default: 'Name'], returns: "Person's name") do + @person.errors.add(:name, "not found") + assert_called_with(Person, :human_attribute_name, [:name, default: "Name"], returns: "Person's name") do assert_equal ["Person's name not found"], @person.errors.full_messages end end def test_errors_full_messages_uses_format - I18n.backend.store_translations('en', errors: { format: "Field %{attribute} %{message}" }) - @person.errors.add('name', 'empty') + 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 @@ -47,19 +48,19 @@ class I18nValidationTest < ActiveModel::TestCase # are used to generate tests to keep things DRY # COMMON_CASES = [ - # [ case, validation_options, generate_message_options] + # [ 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 if condition", { if: lambda { true } }, {}], + [ "given unless condition", { unless: lambda { false } }, {}], [ "given option that is not reserved", { format: "jpg" }, { format: "jpg" }] ] COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_confirmation_of on generated message #{name}" do Person.validates_confirmation_of :title, validation_options - @person.title_confirmation = 'foo' - call = [:title_confirmation, :confirmation, generate_message_options.merge(attribute: 'Title')] + @person.title_confirmation = "foo" + call = [:title_confirmation, :confirmation, generate_message_options.merge(attribute: "Title")] assert_called_with(@person.errors, :generate_message, call) do @person.valid? end @@ -99,7 +100,7 @@ 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.title = 'this title is too long' + @person.title = "this title is too long" call = [:title, :too_long, generate_message_options.merge(count: 5)] assert_called_with(@person.errors, :generate_message, call) do @person.valid? @@ -120,8 +121,8 @@ 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.title = '72x' - call = [:title, :invalid, generate_message_options.merge(value: '72x')] + @person.title = "72x" + call = [:title, :invalid, generate_message_options.merge(value: "72x")] assert_called_with(@person.errors, :generate_message, call) do @person.valid? end @@ -131,8 +132,8 @@ 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.title = 'z' - call = [:title, :inclusion, generate_message_options.merge(value: 'z')] + @person.title = "z" + call = [:title, :inclusion, generate_message_options.merge(value: "z")] assert_called_with(@person.errors, :generate_message, call) do @person.valid? end @@ -142,8 +143,8 @@ 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.title = 'z' - call = [:title, :inclusion, generate_message_options.merge(value: 'z')] + @person.title = "z" + call = [:title, :inclusion, generate_message_options.merge(value: "z")] assert_called_with(@person.errors, :generate_message, call) do @person.valid? end @@ -153,8 +154,8 @@ 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.title = 'a' - call = [:title, :exclusion, generate_message_options.merge(value: 'a')] + @person.title = "a" + call = [:title, :exclusion, generate_message_options.merge(value: "a")] assert_called_with(@person.errors, :generate_message, call) do @person.valid? end @@ -164,8 +165,8 @@ 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.title = 'a' - call = [:title, :exclusion, generate_message_options.merge(value: 'a')] + @person.title = "a" + call = [:title, :exclusion, generate_message_options.merge(value: "a")] assert_called_with(@person.errors, :generate_message, call) do @person.valid? end @@ -175,8 +176,8 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_numericality_of generated message #{name}" do Person.validates_numericality_of :title, validation_options - @person.title = 'a' - call = [:title, :not_a_number, generate_message_options.merge(value: 'a')] + @person.title = "a" + call = [:title, :not_a_number, generate_message_options.merge(value: "a")] assert_called_with(@person.errors, :generate_message, call) do @person.valid? end @@ -186,8 +187,8 @@ 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.title = '0.0' - call = [:title, :not_an_integer, generate_message_options.merge(value: '0.0')] + @person.title = "0.0" + call = [:title, :not_an_integer, generate_message_options.merge(value: "0.0")] assert_called_with(@person.errors, :generate_message, call) do @person.valid? end @@ -225,35 +226,35 @@ class I18nValidationTest < ActiveModel::TestCase end 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? - assert_equal ['custom message'], @person.errors[attribute] + assert_equal ["custom message"], @person.errors[attribute] end 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" }) @person.valid? - assert_equal ['custom message with extra information'], @person.errors[attribute] + assert_equal ["custom message with extra information"], @person.errors[attribute] end 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? - assert_equal ['global message'], @person.errors[attribute] + assert_equal ["global message"], @person.errors[attribute] end end set_expectations_for_validation "validates_confirmation_of", :confirmation do |person, options_to_merge| Person.validates_confirmation_of :title, options_to_merge - person.title_confirmation = 'foo' + person.title_confirmation = "foo" end set_expectations_for_validation "validates_acceptance_of", :accepted do |person, options_to_merge| @@ -287,17 +288,17 @@ class I18nValidationTest < ActiveModel::TestCase 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.title = 'a' + person.title = "a" end set_expectations_for_validation "validates_numericality_of", :not_a_number do |person, options_to_merge| Person.validates_numericality_of :title, options_to_merge - person.title = 'a' + person.title = "a" end 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.title = '1.0' + person.title = "1.0" end set_expectations_for_validation "validates_numericality_of", :odd do |person, options_to_merge| @@ -311,7 +312,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validations_with_message_symbol_must_translate - I18n.backend.store_translations 'en', errors: { messages: { custom_error: "I am a 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? @@ -319,7 +320,7 @@ class I18nValidationTest < ActiveModel::TestCase 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" } } } } } } + 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? @@ -327,7 +328,7 @@ class I18nValidationTest < ActiveModel::TestCase 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" } } } } + 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? diff --git a/activemodel/test/cases/validations/inclusion_validation_test.rb b/activemodel/test/cases/validations/inclusion_validation_test.rb index 9bd44175a6..94df0649a9 100644 --- a/activemodel/test/cases/validations/inclusion_validation_test.rb +++ b/activemodel/test/cases/validations/inclusion_validation_test.rb @@ -1,17 +1,18 @@ -require 'cases/helper' -require 'active_support/all' +# frozen_string_literal: true -require 'models/topic' -require 'models/person' +require "cases/helper" +require "active_support/all" -class InclusionValidationTest < ActiveModel::TestCase +require "models/topic" +require "models/person" +class InclusionValidationTest < ActiveModel::TestCase def teardown Topic.clear_validators! 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? @@ -24,35 +25,35 @@ class InclusionValidationTest < ActiveModel::TestCase range_begin = 1.year.ago range_end = Time.now Topic.validates_inclusion_of(:created_at, in: range_begin..range_end) - 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? - assert Topic.new(title: 'aaa', created_at: range_begin).valid? - assert Topic.new(title: 'aaa', created_at: range_end).valid? + 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? + assert Topic.new(title: "aaa", created_at: range_begin).valid? + assert Topic.new(title: "aaa", created_at: range_end).valid? end def test_validates_inclusion_of_date_range range_begin = 1.year.until(Date.today) range_end = Date.today Topic.validates_inclusion_of(:created_at, in: range_begin..range_end) - 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? - assert Topic.new(title: 'aaa', created_at: 1.year.until(Date.today)).valid? - assert Topic.new(title: 'aaa', created_at: Date.today).valid? - assert Topic.new(title: 'aaa', created_at: range_begin).valid? - assert Topic.new(title: 'aaa', created_at: range_end).valid? + 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? + assert Topic.new(title: "aaa", created_at: 1.year.until(Date.today)).valid? + assert Topic.new(title: "aaa", created_at: Date.today).valid? + assert Topic.new(title: "aaa", created_at: range_begin).valid? + assert Topic.new(title: "aaa", created_at: range_end).valid? end def test_validates_inclusion_of_date_time_range range_begin = 1.year.until(DateTime.current) range_end = DateTime.current Topic.validates_inclusion_of(:created_at, in: range_begin..range_end) - 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? - assert Topic.new(title: 'aaa', created_at: range_begin).valid? - assert Topic.new(title: 'aaa', created_at: range_end).valid? + 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? + assert Topic.new(title: "aaa", created_at: range_begin).valid? + assert Topic.new(title: "aaa", created_at: range_end).valid? end def test_validates_inclusion_of @@ -122,7 +123,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" diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index ee901b75fb..42f76f3e3c 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -1,7 +1,9 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' -require 'models/person' +require "cases/helper" + +require "models/topic" +require "models/person" class LengthValidationTest < ActiveModel::TestCase def teardown @@ -9,7 +11,7 @@ class LengthValidationTest < ActiveModel::TestCase 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? @@ -18,7 +20,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? @@ -104,7 +106,7 @@ class LengthValidationTest < ActiveModel::TestCase assert_equal ["is too short (minimum is 3 characters)"], t.errors[:content] t.title = "abe" - t.content = "mad" + t.content = "mad" assert t.valid? end @@ -125,7 +127,7 @@ class LengthValidationTest < ActiveModel::TestCase def test_optionally_validates_length_of_using_within Topic.validates_length_of :title, :content, within: 3..5, allow_nil: true - t = Topic.new('title' => 'abc', 'content' => 'abcd') + t = Topic.new("title" => "abc", "content" => "abcd") assert t.valid? t.title = nil @@ -161,8 +163,8 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_using_bignum - bigmin = 2 ** 30 - bigmax = 2 ** 32 + bigmin = 2**30 + bigmax = 2**32 bigrange = bigmin...bigmax assert_nothing_raised do Topic.validates_length_of :title, is: bigmin + 5 @@ -183,7 +185,7 @@ class LengthValidationTest < ActiveModel::TestCase 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? @@ -191,7 +193,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? @@ -199,7 +201,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? @@ -220,7 +222,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? @@ -228,21 +230,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'] + 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'] + 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? @@ -250,7 +252,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? @@ -289,7 +291,7 @@ class LengthValidationTest < ActiveModel::TestCase assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title] assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content] t.title = "一二三" - t.content = "12三" + t.content = "12三" assert t.valid? end @@ -318,44 +320,7 @@ class LengthValidationTest < ActiveModel::TestCase assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"] end - def test_validates_length_of_with_block - assert_deprecated do - Topic.validates_length_of( - :content, - minimum: 5, - too_short: "Your essay must be at least %{count} words.", - tokenizer: lambda {|str| str.scan(/\w+/) }, - ) - end - t = Topic.new(content: "this content should be long enough") - assert t.valid? - - t.content = "not long enough" - assert t.invalid? - assert t.errors[:content].any? - assert_equal ["Your essay must be at least 5 words."], t.errors[:content] - end - - - def test_validates_length_of_with_symbol - assert_deprecated do - Topic.validates_length_of( - :content, - minimum: 5, - too_short: "Your essay must be at least %{count} words.", - tokenizer: :my_word_tokenizer, - ) - end - t = Topic.new(content: "this content should be long enough") - assert t.valid? - - t.content = "not long enough" - assert t.invalid? - assert t.errors[:content].any? - assert_equal ["Your essay must be at least 5 words."], t.errors[:content] - end - - def test_validates_length_of_for_fixnum + def test_validates_length_of_for_integer Topic.validates_length_of(:approved, is: 4) t = Topic.new("title" => "uhohuhoh", "content" => "whatever", approved: 1) @@ -440,9 +405,40 @@ class LengthValidationTest < ActiveModel::TestCase 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 } ) + Topic.validates_length_of(:title, is: 5, if: Proc.new { false }) assert Topic.new("title" => "david").valid? assert Topic.new("title" => "david2").invalid? end + + def test_validates_length_of_using_proc_as_maximum + Topic.validates_length_of :title, maximum: ->(model) { 5 } + + t = Topic.new("title" => "valid", "content" => "whatever") + assert t.valid? + + t.title = "notvalid" + assert t.invalid? + assert t.errors[:title].any? + assert_equal ["is too long (maximum is 5 characters)"], t.errors[:title] + + t.title = "" + assert t.valid? + end + + def test_validates_length_of_using_proc_as_maximum_with_model_method + Topic.send(:define_method, :max_title_length, lambda { 5 }) + Topic.validates_length_of :title, maximum: Proc.new(&:max_title_length) + + t = Topic.new("title" => "valid", "content" => "whatever") + assert t.valid? + + t.title = "notvalid" + assert t.invalid? + assert t.errors[:title].any? + assert_equal ["is too long (maximum is 5 characters)"], t.errors[:title] + + t.title = "" + assert t.valid? + end end diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index 74a048537d..5413255e6b 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -1,13 +1,14 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' -require 'models/person' +require "cases/helper" -require 'bigdecimal' -require 'active_support/core_ext/big_decimal' +require "models/topic" +require "models/person" -class NumericalityValidationTest < ActiveModel::TestCase +require "bigdecimal" +require "active_support/core_ext/big_decimal" +class NumericalityValidationTest < ActiveModel::TestCase def teardown Topic.clear_validators! end @@ -19,9 +20,9 @@ class NumericalityValidationTest < ActiveModel::TestCase INTEGER_STRINGS = %w(0 +0 -0 10 +10 -10 0090 -090) FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS INTEGERS = [0, 10, -10] + INTEGER_STRINGS - BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) } + BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal(bd) } JUNK = ["not a number", "42 not a number", "0xdeadbeef", "0xinvalidhex", "0Xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"] - INFINITY = [1.0/0.0] + INFINITY = [1.0 / 0.0] def test_default_validates_numericality_of Topic.validates_numericality_of :approved @@ -36,6 +37,13 @@ class NumericalityValidationTest < ActiveModel::TestCase valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY) end + def test_validates_numericality_of_with_blank_allowed + Topic.validates_numericality_of :approved, allow_blank: true + + invalid!(JUNK) + valid!(NIL + BLANK + FLOATS + INTEGERS + BIGDECIMAL + INFINITY) + end + def test_validates_numericality_of_with_integer_only Topic.validates_numericality_of :approved, only_integer: true @@ -51,7 +59,7 @@ class NumericalityValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_with_integer_only_and_symbol_as_value - Topic.validates_numericality_of :approved, only_integer: :condition_is_true_but_its_not + Topic.validates_numericality_of :approved, only_integer: :condition_is_false invalid!(NIL + BLANK + JUNK) valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY) @@ -68,119 +76,119 @@ class NumericalityValidationTest < ActiveModel::TestCase def test_validates_numericality_with_greater_than Topic.validates_numericality_of :approved, greater_than: 10 - invalid!([-10, 10], 'must be greater than 10') + invalid!([-10, 10], "must be greater than 10") valid!([11]) end def test_validates_numericality_with_greater_than_using_differing_numeric_types - Topic.validates_numericality_of :approved, greater_than: BigDecimal.new('97.18') + Topic.validates_numericality_of :approved, greater_than: BigDecimal("97.18") - invalid!([-97.18, BigDecimal.new('97.18'), BigDecimal('-97.18')], 'must be greater than 97.18') - valid!([97.18, 98, BigDecimal.new('98')]) # Notice the 97.18 as a float is greater than 97.18 as a BigDecimal due to floating point precision + invalid!([-97.18, BigDecimal("97.18"), BigDecimal("-97.18")], "must be greater than 97.18") + valid!([97.19, 98, BigDecimal("98"), BigDecimal("97.19")]) end def test_validates_numericality_with_greater_than_using_string_value Topic.validates_numericality_of :approved, greater_than: 10 - invalid!(['-10', '9', '9.9', '10'], 'must be greater than 10') - valid!(['10.1', '11']) + invalid!(["-10", "9", "9.9", "10"], "must be greater than 10") + valid!(["10.1", "11"]) end def test_validates_numericality_with_greater_than_or_equal Topic.validates_numericality_of :approved, greater_than_or_equal_to: 10 - invalid!([-9, 9], 'must be 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_greater_than_or_equal_using_differing_numeric_types - Topic.validates_numericality_of :approved, greater_than_or_equal_to: BigDecimal.new('97.18') + Topic.validates_numericality_of :approved, greater_than_or_equal_to: BigDecimal("97.18") - invalid!([-97.18, 97.17, 97, BigDecimal.new('97.17'), BigDecimal.new('-97.18')], 'must be greater than or equal to 97.18') - valid!([97.18, 98, BigDecimal.new('97.19')]) + invalid!([-97.18, 97.17, 97, BigDecimal("97.17"), BigDecimal("-97.18")], "must be greater than or equal to 97.18") + valid!([97.18, 98, BigDecimal("97.19")]) end def test_validates_numericality_with_greater_than_or_equal_using_string_value Topic.validates_numericality_of :approved, greater_than_or_equal_to: 10 - invalid!(['-10', '9', '9.9'], 'must be greater than or equal to 10') - valid!(['10', '10.1', '11']) + invalid!(["-10", "9", "9.9"], "must be greater than or equal to 10") + valid!(["10", "10.1", "11"]) end def test_validates_numericality_with_equal_to Topic.validates_numericality_of :approved, equal_to: 10 - invalid!([-10, 11] + INFINITY, 'must be equal to 10') + invalid!([-10, 11] + INFINITY, "must be equal to 10") valid!([10]) end def test_validates_numericality_with_equal_to_using_differing_numeric_types - Topic.validates_numericality_of :approved, equal_to: BigDecimal.new('97.18') + Topic.validates_numericality_of :approved, equal_to: BigDecimal("97.18") - invalid!([-97.18, 97.18], 'must be equal to 97.18') - valid!([BigDecimal.new('97.18')]) + invalid!([-97.18], "must be equal to 97.18") + valid!([BigDecimal("97.18")]) end def test_validates_numericality_with_equal_to_using_string_value Topic.validates_numericality_of :approved, equal_to: 10 - invalid!(['-10', '9', '9.9', '10.1', '11'], 'must be equal to 10') - valid!(['10']) + invalid!(["-10", "9", "9.9", "10.1", "11"], "must be equal to 10") + valid!(["10"]) end def test_validates_numericality_with_less_than Topic.validates_numericality_of :approved, less_than: 10 - invalid!([10], 'must be less than 10') + invalid!([10], "must be less than 10") valid!([-9, 9]) end def test_validates_numericality_with_less_than_using_differing_numeric_types - Topic.validates_numericality_of :approved, less_than: BigDecimal.new('97.18') + Topic.validates_numericality_of :approved, less_than: BigDecimal("97.18") - invalid!([97.18, BigDecimal.new('97.18')], 'must be less than 97.18') - valid!([-97.0, 97.0, -97, 97, BigDecimal.new('-97'), BigDecimal.new('97')]) + invalid!([97.18, BigDecimal("97.18")], "must be less than 97.18") + valid!([-97.0, 97.0, -97, 97, BigDecimal("-97"), BigDecimal("97")]) end def test_validates_numericality_with_less_than_using_string_value Topic.validates_numericality_of :approved, less_than: 10 - invalid!(['10', '10.1', '11'], 'must be less than 10') - valid!(['-10', '9', '9.9']) + invalid!(["10", "10.1", "11"], "must be less than 10") + valid!(["-10", "9", "9.9"]) end def test_validates_numericality_with_less_than_or_equal_to Topic.validates_numericality_of :approved, less_than_or_equal_to: 10 - invalid!([11], 'must be 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_less_than_or_equal_to_using_differing_numeric_types - Topic.validates_numericality_of :approved, less_than_or_equal_to: BigDecimal.new('97.18') + Topic.validates_numericality_of :approved, less_than_or_equal_to: BigDecimal("97.18") - invalid!([97.18, 98], 'must be less than or equal to 97.18') - valid!([-97.18, BigDecimal.new('-97.18'), BigDecimal.new('97.18')]) + invalid!([97.19, 98], "must be less than or equal to 97.18") + valid!([-97.18, BigDecimal("-97.18"), BigDecimal("97.18")]) end def test_validates_numericality_with_less_than_or_equal_using_string_value Topic.validates_numericality_of :approved, less_than_or_equal_to: 10 - invalid!(['10.1', '11'], 'must be less than or equal to 10') - valid!(['-10', '9', '9.9', '10']) + invalid!(["10.1", "11"], "must be less than or equal to 10") + valid!(["-10", "9", "9.9", "10"]) end def test_validates_numericality_with_odd Topic.validates_numericality_of :approved, odd: true - invalid!([-2, 2], 'must be odd') + invalid!([-2, 2], "must be odd") valid!([-1, 1]) end def test_validates_numericality_with_even Topic.validates_numericality_of :approved, even: true - invalid!([-1, 1], 'must be even') + invalid!([-1, 1], "must be even") valid!([-2, 2]) end @@ -201,8 +209,8 @@ class NumericalityValidationTest < ActiveModel::TestCase def test_validates_numericality_with_other_than_using_string_value Topic.validates_numericality_of :approved, other_than: 0 - invalid!(['0', '0.0']) - valid!(['-1', '1.1', '42']) + invalid!(["0", "0.0"]) + valid!(["-1", "1.1", "42"]) end def test_validates_numericality_with_proc @@ -254,35 +262,44 @@ class NumericalityValidationTest < ActiveModel::TestCase Person.clear_validators! end + def test_validates_numericality_with_exponent_number + base = 10_000_000_000_000_000 + Topic.validates_numericality_of :approved, less_than_or_equal_to: base + topic = Topic.new + topic.approved = (base + 1).to_s + + assert topic.invalid? + 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 - def invalid!(values, error = nil) - with_each_topic_approved_value(values) do |topic, value| - assert topic.invalid?, "#{value.inspect} not rejected as a number" - assert topic.errors[:approved].any?, "FAILED for #{value.inspect}" - assert_equal error, topic.errors[:approved].first if error + def invalid!(values, error = nil) + with_each_topic_approved_value(values) do |topic, value| + assert topic.invalid?, "#{value.inspect} not rejected as a number" + assert topic.errors[:approved].any?, "FAILED for #{value.inspect}" + assert_equal error, topic.errors[:approved].first if error + end end - end - def valid!(values) - with_each_topic_approved_value(values) do |topic, value| - assert topic.valid?, "#{value.inspect} not accepted as a number with validation error: #{topic.errors[:approved].first}" + def valid!(values) + with_each_topic_approved_value(values) do |topic, value| + assert topic.valid?, "#{value.inspect} not accepted as a number with validation error: #{topic.errors[:approved].first}" + end end - end - def with_each_topic_approved_value(values) - topic = Topic.new(title: "numeric test", content: "whatever") - values.each do |value| - topic.approved = value - yield topic, value + def with_each_topic_approved_value(values) + topic = Topic.new(title: "numeric test", content: "whatever") + values.each do |value| + topic.approved = value + yield topic, value + end end - end end diff --git a/activemodel/test/cases/validations/presence_validation_test.rb b/activemodel/test/cases/validations/presence_validation_test.rb index 59b9db0795..22c2f0af87 100644 --- a/activemodel/test/cases/validations/presence_validation_test.rb +++ b/activemodel/test/cases/validations/presence_validation_test.rb @@ -1,11 +1,12 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' -require 'models/person' -require 'models/custom_reader' +require "cases/helper" -class PresenceValidationTest < ActiveModel::TestCase +require "models/topic" +require "models/person" +require "models/custom_reader" +class PresenceValidationTest < ActiveModel::TestCase teardown do Topic.clear_validators! Person.clear_validators! @@ -21,7 +22,7 @@ class PresenceValidationTest < ActiveModel::TestCase assert_equal ["can't be blank"], t.errors[:content] t.title = "something" - t.content = " " + t.content = " " assert t.invalid? assert_equal ["can't be blank"], t.errors[:content] diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index 04101f3545..7f32f5dc74 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -1,8 +1,10 @@ -require 'cases/helper' -require 'models/person' -require 'models/topic' -require 'models/person_with_validator' -require 'validators/namespace/email_validator' +# frozen_string_literal: true + +require "cases/helper" +require "models/person" +require "models/topic" +require "models/person_with_validator" +require "validators/namespace/email_validator" class ValidatesTest < ActiveModel::TestCase setup :reset_callbacks @@ -17,21 +19,21 @@ class ValidatesTest < ActiveModel::TestCase def test_validates_with_messages_empty Person.validates :title, presence: { message: "" } person = Person.new - assert !person.valid?, 'person should not be valid.' + assert !person.valid?, "person should not be valid." end def test_validates_with_built_in_validation Person.validates :title, numericality: true person = Person.new person.valid? - assert_equal ['is not a number'], person.errors[:title] + assert_equal ["is not a number"], person.errors[:title] end def test_validates_with_attribute_specified_as_string Person.validates "title", numericality: true person = Person.new person.valid? - assert_equal ['is not a number'], person.errors[:title] + assert_equal ["is not a number"], person.errors[:title] person = Person.new person.title = 123 @@ -39,38 +41,44 @@ 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] + assert_equal ["my custom message"], person.errors[:salary] end def test_validates_with_validator_class Person.validates :karma, email: true person = Person.new person.valid? - assert_equal ['is not an email'], person.errors[:karma] + assert_equal ["is not an email"], person.errors[:karma] end def test_validates_with_namespaced_validator_class - Person.validates :karma, :'namespace/email' => true + Person.validates :karma, 'namespace/email': true person = Person.new person.valid? - assert_equal ['is not an email'], person.errors[:karma] + assert_equal ["is not an email"], person.errors[:karma] 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: { if: :condition_is_false } 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_false + person = Person.new + assert person.valid? + end + + def test_validates_with_unless_as_local_conditions + Person.validates :karma, presence: true, email: { unless: :condition_is_true } person = Person.new person.valid? - assert_equal ["can't be blank", "is not an email"], person.errors[:karma].sort + assert_equal ["can't be blank"], person.errors[:karma] end def test_validates_with_unless_shared_conditions @@ -89,7 +97,7 @@ class ValidatesTest < ActiveModel::TestCase Person.validates :karma, format: /positive|negative/ person = Person.new assert person.invalid? - assert_equal ['is invalid'], person.errors[:karma] + assert_equal ["is invalid"], person.errors[:karma] person.karma = "positive" assert person.valid? end @@ -98,7 +106,7 @@ class ValidatesTest < ActiveModel::TestCase Person.validates :gender, inclusion: %w(m f) person = Person.new assert person.invalid? - assert_equal ['is not included in the list'], person.errors[:gender] + assert_equal ["is not included in the list"], person.errors[:gender] person.gender = "m" assert person.valid? end @@ -107,16 +115,16 @@ class ValidatesTest < ActiveModel::TestCase Person.validates :karma, length: 6..20 person = Person.new assert person.invalid? - assert_equal ['is too short (minimum is 6 characters)'], person.errors[:karma] - person.karma = 'something' + assert_equal ["is too short (minimum is 6 characters)"], person.errors[:karma] + person.karma = "something" assert person.valid? 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] + assert_equal ["my custom message"], person.errors[:karma] end def test_validates_with_unknown_validator @@ -127,14 +135,14 @@ class ValidatesTest < ActiveModel::TestCase PersonWithValidator.validates :title, presence: true person = PersonWithValidator.new person.valid? - assert_equal ['Local validator'], person.errors[:title] + 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] + assert_equal ["Local validator please"], person.errors[:title] end def test_validates_with_included_validator_and_wildcard_shortcut @@ -143,15 +151,15 @@ class ValidatesTest < ActiveModel::TestCase person = PersonWithValidator.new person.title = "Ms. Pacman" person.valid? - assert_equal ['does not appear to be like Mr.'], person.errors[:title] + assert_equal ["does not appear to be like Mr."], person.errors[:title] 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" assert !topic.valid? - assert_equal ['Y U NO CONFIRM'], topic.errors[:title_confirmation] + assert_equal ["Y U NO CONFIRM"], topic.errors[:title_confirmation] end end diff --git a/activemodel/test/cases/validations/validations_context_test.rb b/activemodel/test/cases/validations/validations_context_test.rb index b901a1523e..024eb1882f 100644 --- a/activemodel/test/cases/validations/validations_context_test.rb +++ b/activemodel/test/cases/validations/validations_context_test.rb @@ -1,6 +1,8 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' +require "cases/helper" + +require "models/topic" class ValidationsContextTest < ActiveModel::TestCase def teardown @@ -38,7 +40,7 @@ class ValidationsContextTest < ActiveModel::TestCase 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) + assert_includes topic.errors[:base], ERROR_MESSAGE end test "with a class that adds errors on multiple contexts and validating a new model" do @@ -48,10 +50,10 @@ class ValidationsContextTest < ActiveModel::TestCase assert topic.valid?, "Validation ran with no context given when 'on' is set to context1 and context2" assert topic.invalid?(:context1), "Validation did not run on context1 when 'on' is set to context1 and context2" - assert topic.errors[:base].include?(ERROR_MESSAGE) + assert_includes topic.errors[:base], ERROR_MESSAGE assert topic.invalid?(:context2), "Validation did not run on context2 when 'on' is set to context1 and context2" - assert topic.errors[:base].include?(ERROR_MESSAGE) + assert_includes topic.errors[:base], ERROR_MESSAGE end test "with a class that validating a model for a multiple contexts" do @@ -62,7 +64,7 @@ class ValidationsContextTest < ActiveModel::TestCase assert topic.valid?, "Validation ran with no context given when 'on' is set to context1 and context2" assert topic.invalid?([:context1, :context2]), "Validation did not run on context1 when 'on' is set to context1 and context2" - assert topic.errors[:base].include?(ERROR_MESSAGE) - assert topic.errors[:base].include?(ANOTHER_ERROR_MESSAGE) + assert_includes topic.errors[:base], ERROR_MESSAGE + assert_includes topic.errors[:base], ANOTHER_ERROR_MESSAGE end end diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index c73580138d..13ef5e6a31 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -1,9 +1,10 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' +require "cases/helper" -class ValidatesWithTest < ActiveModel::TestCase +require "models/topic" +class ValidatesWithTest < ActiveModel::TestCase def teardown Topic.clear_validators! end @@ -52,7 +53,7 @@ class ValidatesWithTest < ActiveModel::TestCase Topic.validates_with(ValidatorThatAddsErrors) topic = Topic.new assert topic.invalid?, "A class that adds errors causes the record to be invalid" - assert topic.errors[:base].include?(ERROR_MESSAGE) + assert_includes topic.errors[:base], ERROR_MESSAGE end test "with a class that returns valid" do @@ -65,45 +66,19 @@ class ValidatesWithTest < ActiveModel::TestCase Topic.validates_with(ValidatorThatAddsErrors, OtherValidatorThatAddsErrors) topic = Topic.new assert topic.invalid? - assert topic.errors[:base].include?(ERROR_MESSAGE) - assert topic.errors[:base].include?(OTHER_ERROR_MESSAGE) - end - - test "with if statements that return false" do - 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 = 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 = Topic.new - assert topic.valid? - end - - test "with unless statements that returns false" do - Topic.validates_with(ValidatorThatAddsErrors, unless: "1 == 2") - topic = Topic.new - assert topic.invalid? - assert topic.errors[:base].include?(ERROR_MESSAGE) + assert_includes topic.errors[:base], ERROR_MESSAGE + assert_includes topic.errors[:base], OTHER_ERROR_MESSAGE end test "passes all configuration options to the validator class" do topic = Topic.new validator = Minitest::Mock.new - validator.expect(:new, validator, [{foo: :bar, if: "1 == 1", class: Topic}]) + validator.expect(:new, validator, [{ foo: :bar, if: :condition_is_true, class: Topic }]) validator.expect(:validate, nil, [topic]) validator.expect(:is_a?, false, [Symbol]) validator.expect(:is_a?, false, [String]) - Topic.validates_with(validator, if: "1 == 1", foo: :bar) + Topic.validates_with(validator, if: :condition_is_true, foo: :bar) assert topic.valid? validator.verify end @@ -112,7 +87,7 @@ class ValidatesWithTest < ActiveModel::TestCase Topic.validates_with(ValidatorThatValidatesOptions, field: :first_name) topic = Topic.new assert topic.invalid? - assert topic.errors[:base].include?(ERROR_MESSAGE) + assert_includes topic.errors[:base], ERROR_MESSAGE end test "validates_with each validator" do @@ -160,7 +135,7 @@ class ValidatesWithTest < ActiveModel::TestCase topic = Topic.new assert !topic.valid? - assert_equal ['is missing'], topic.errors[:title] + assert_equal ["is missing"], topic.errors[:title] end test "optionally pass in the attribute being validated when validating with an instance method" do @@ -169,6 +144,6 @@ class ValidatesWithTest < ActiveModel::TestCase topic = Topic.new title: "foo" assert !topic.valid? assert topic.errors[:title].empty? - assert_equal ['is missing'], topic.errors[:content] + assert_equal ["is missing"], topic.errors[:content] end end diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 2a4e9f842f..ab8c41bbd0 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -1,11 +1,13 @@ -require 'cases/helper' +# frozen_string_literal: true -require 'models/topic' -require 'models/reply' -require 'models/custom_reader' +require "cases/helper" -require 'active_support/json' -require 'active_support/xml_mini' +require "models/topic" +require "models/reply" +require "models/custom_reader" + +require "active_support/json" +require "active_support/xml_mini" class ValidationsTest < ActiveModel::TestCase class CustomStrictValidationException < StandardError; end @@ -51,10 +53,10 @@ class ValidationsTest < ActiveModel::TestCase r = Reply.new r.valid? - errors = r.errors.collect {|attr, messages| [attr.to_s, messages]} + errors = r.errors.collect { |attr, messages| [attr.to_s, messages] } - assert errors.include?(["title", "is Empty"]) - assert errors.include?(["content", "is Empty"]) + assert_includes errors, ["title", "is Empty"] + assert_includes errors, ["content", "is Empty"] end def test_multiple_errors_per_attr_iteration_with_full_error_composition @@ -86,8 +88,8 @@ class ValidationsTest < ActiveModel::TestCase assert_equal ["Reply is not dignifying"], r.errors[:base] - assert errors.include?("Title is Empty") - assert errors.include?("Reply is not dignifying") + assert_includes errors, "Title is Empty" + assert_includes errors, "Reply is not dignifying" assert_equal 2, r.errors.count end @@ -101,8 +103,8 @@ class ValidationsTest < ActiveModel::TestCase assert_equal ["is invalid"], r.errors[:base] - assert errors.include?("Title is Empty") - assert errors.include?("is invalid") + assert_includes errors, "Title is Empty" + assert_includes errors, "is invalid" assert_equal 2, r.errors.count end @@ -116,7 +118,7 @@ class ValidationsTest < ActiveModel::TestCase def test_validates_each hits = 0 Topic.validates_each(:title, :content, [:title, :content]) do |record, attr| - record.errors.add attr, 'gotcha' + record.errors.add attr, "gotcha" hits += 1 end t = Topic.new("title" => "valid", "content" => "whatever") @@ -129,7 +131,7 @@ class ValidationsTest < ActiveModel::TestCase def test_validates_each_custom_reader hits = 0 CustomReader.validates_each(:title, :content, [:title, :content]) do |record, attr| - record.errors.add attr, 'gotcha' + record.errors.add attr, "gotcha" hits += 1 end t = CustomReader.new("title" => "valid", "content" => "whatever") @@ -170,7 +172,7 @@ class ValidationsTest < ActiveModel::TestCase # A common mistake -- we meant to call 'validates' Topic.validate :title, presence: true end - message = 'Unknown key: :presence. Valid keys are: :on, :if, :unless, :prepend. Perhaps you meant to call `validates` instead of `validate`?' + message = "Unknown key: :presence. Valid keys are: :on, :if, :unless, :prepend. Perhaps you meant to call `validates` instead of `validate`?" assert_equal message, error.message end @@ -198,9 +200,9 @@ class ValidationsTest < ActiveModel::TestCase end assert_nothing_raised do - klass.validate :validator_a, if: ->{ true } + klass.validate :validator_a, if: -> { true } klass.validate :validator_b, prepend: true - klass.validate :validator_c, unless: ->{ true } + klass.validate :validator_c, unless: -> { true } end t = klass.new @@ -233,25 +235,25 @@ class ValidationsTest < ActiveModel::TestCase 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.validate { errors.add("author_email_address", "will never be valid") } 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] assert_equal "can't be blank", t.errors[key][0] - assert_equal 'is too short (minimum is 2 characters)', t.errors[key][1] + assert_equal "is too short (minimum is 2 characters)", t.errors[key][1] assert_equal :author_name, key = t.errors.keys[1] assert_equal "can't be blank", t.errors[key][0] assert_equal :author_email_address, key = t.errors.keys[2] - assert_equal 'will never be valid', t.errors[key][0] + assert_equal "will never be valid", t.errors[key][0] assert_equal :content, key = t.errors.keys[3] - assert_equal 'is too short (minimum is 2 characters)', t.errors[key][0] + assert_equal "is too short (minimum is 2 characters)", t.errors[key][0] end def test_validation_with_if_and_on - Topic.validates_presence_of :title, if: Proc.new{|x| x.author_name = "bad"; true }, on: :update + Topic.validates_presence_of :title, if: Proc.new { |x| x.author_name = "bad"; true }, on: :update t = Topic.new(title: "") @@ -271,7 +273,7 @@ class ValidationsTest < ActiveModel::TestCase assert t.invalid? assert t.errors[:title].any? - t.title = 'Things are going to change' + t.title = "Things are going to change" assert !t.invalid? end @@ -332,9 +334,9 @@ class ValidationsTest < ActiveModel::TestCase assert topic.invalid? assert_equal 3, topic.errors.size - topic.title = 'Some Title' - topic.author_name = 'Some Author' - topic.content = 'Some Content Whose Length is more than 10.' + topic.title = "Some Title" + topic.author_name = "Some Author" + topic.content = "Some Content Whose Length is more than 10." assert topic.valid? end @@ -440,7 +442,7 @@ class ValidationsTest < ActiveModel::TestCase assert duped.invalid? topic.title = nil - duped.title = 'Mathematics' + duped.title = "Mathematics" assert topic.invalid? assert duped.valid? end @@ -448,7 +450,7 @@ class ValidationsTest < ActiveModel::TestCase def test_validation_with_message_as_proc_that_takes_a_record_as_a_parameter Topic.validates_presence_of(:title, message: proc { |record| "You have failed me for the last time, #{record.author_name}." }) - t = Topic.new(author_name: 'Admiral') + t = Topic.new(author_name: "Admiral") assert t.invalid? assert_equal ["You have failed me for the last time, Admiral."], t.errors[:title] end @@ -456,7 +458,7 @@ class ValidationsTest < ActiveModel::TestCase def test_validation_with_message_as_proc_that_takes_record_and_data_as_a_parameters Topic.validates_presence_of(:title, message: proc { |record, data| "#{data[:attribute]} is missing. You have failed me for the last time, #{record.author_name}." }) - t = Topic.new(author_name: 'Admiral') + t = Topic.new(author_name: "Admiral") assert t.invalid? assert_equal ["Title is missing. You have failed me for the last time, Admiral."], t.errors[:title] end |