diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-12-29 02:18:38 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-29 02:18:38 -0500 |
commit | eb6a6141a66e052deb58580448d7c0fa6aa675ba (patch) | |
tree | 0017a57044ec8e466c97cc6b7879d39caa6c1ffa /activemodel/test/cases/type | |
parent | 447e1a48811fa053e8bef954376d1ad47bdb5cef (diff) | |
parent | fcec126eaa4f835c837bc75efa78008667b2ec5b (diff) | |
download | rails-eb6a6141a66e052deb58580448d7c0fa6aa675ba.tar.gz rails-eb6a6141a66e052deb58580448d7c0fa6aa675ba.tar.bz2 rails-eb6a6141a66e052deb58580448d7c0fa6aa675ba.zip |
Merge branch 'master' into clear_all_environments_log_by_default
Diffstat (limited to 'activemodel/test/cases/type')
-rw-r--r-- | activemodel/test/cases/type/big_integer_test.rb | 24 | ||||
-rw-r--r-- | activemodel/test/cases/type/binary_test.rb | 15 | ||||
-rw-r--r-- | activemodel/test/cases/type/boolean_test.rb | 39 | ||||
-rw-r--r-- | activemodel/test/cases/type/date_test.rb | 19 | ||||
-rw-r--r-- | activemodel/test/cases/type/date_time_test.rb | 38 | ||||
-rw-r--r-- | activemodel/test/cases/type/float_test.rb | 22 | ||||
-rw-r--r-- | activemodel/test/cases/type/immutable_string_test.rb | 21 | ||||
-rw-r--r-- | activemodel/test/cases/type/integer_test.rb | 11 | ||||
-rw-r--r-- | activemodel/test/cases/type/registry_test.rb | 52 | ||||
-rw-r--r-- | activemodel/test/cases/type/string_test.rb | 37 | ||||
-rw-r--r-- | activemodel/test/cases/type/time_test.rb | 21 | ||||
-rw-r--r-- | activemodel/test/cases/type/unsigned_integer_test.rb | 18 | ||||
-rw-r--r-- | activemodel/test/cases/type/value_test.rb | 14 |
13 files changed, 268 insertions, 63 deletions
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..56002b7cc6 --- /dev/null +++ b/activemodel/test/cases/type/big_integer_test.rb @@ -0,0 +1,24 @@ +require "cases/helper" +require "active_model/type" + +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..e9c2ccfca4 --- /dev/null +++ b/activemodel/test/cases/type/binary_test.rb @@ -0,0 +1,15 @@ +require "cases/helper" +require "active_model/type" + +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..92e5aebfb7 --- /dev/null +++ b/activemodel/test/cases/type/boolean_test.rb @@ -0,0 +1,39 @@ +require "cases/helper" +require "active_model/type" + +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..0cc90e99d3 --- /dev/null +++ b/activemodel/test/cases/type/date_test.rb @@ -0,0 +1,19 @@ +require "cases/helper" +require "active_model/type" + +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..75a7fc686e --- /dev/null +++ b/activemodel/test/cases/type/date_time_test.rb @@ -0,0 +1,38 @@ +require "cases/helper" +require "active_model/type" + +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/float_test.rb b/activemodel/test/cases/type/float_test.rb new file mode 100644 index 0000000000..2e34f57f7e --- /dev/null +++ b/activemodel/test/cases/type/float_test.rb @@ -0,0 +1,22 @@ +require "cases/helper" +require "active_model/type" + +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_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..23e58974fb --- /dev/null +++ b/activemodel/test/cases/type/immutable_string_test.rb @@ -0,0 +1,21 @@ +require "cases/helper" +require "active_model/type" + +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 e00246b602..2b9b03f3cf 100644 --- a/activemodel/test/cases/type/integer_test.rb +++ b/activemodel/test/cases/type/integer_test.rb @@ -1,5 +1,6 @@ require "cases/helper" require "active_model/type" +require "active_support/core_ext/numeric/time" module ActiveModel module Type @@ -19,7 +20,7 @@ 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) end @@ -32,7 +33,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,6 +42,12 @@ 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 diff --git a/activemodel/test/cases/type/registry_test.rb b/activemodel/test/cases/type/registry_test.rb index 2a48998a62..927b6d0307 100644 --- a/activemodel/test/cases/type/registry_test.rb +++ b/activemodel/test/cases/type/registry_test.rb @@ -2,38 +2,40 @@ 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) + 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) - assert_equal "", registry.lookup(:foo) - assert_equal [], registry.lookup(:bar) - end - - 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..222083817e 100644 --- a/activemodel/test/cases/type/string_test.rb +++ b/activemodel/test/cases/type/string_test.rb @@ -2,26 +2,27 @@ 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 + s = "foo" + type = Type::String.new + assert_equal false, type.cast(s).frozen? + end - 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) + 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) + 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..0cc4d33caa --- /dev/null +++ b/activemodel/test/cases/type/time_test.rb @@ -0,0 +1,21 @@ +require "cases/helper" +require "active_model/type" + +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..d8b3e7f164 --- /dev/null +++ b/activemodel/test/cases/type/value_test.rb @@ -0,0 +1,14 @@ +require "cases/helper" +require "active_model/type" + +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 |