提交 894c0338 编写于 作者: I Iain Beeston

Refactored ActiveModel::Type tests into their own files

上级 159b7748
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
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_equal nil, type.cast(nil)
assert_equal "1", type.cast("1")
assert_equal 1, type.cast(1)
end
end
end
end
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
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_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
end
end
end
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_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_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
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
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
require "cases/helper"
require "active_model/type"
require "active_support/core_ext/numeric/time"
module ActiveModel
module Type
......@@ -41,6 +42,12 @@ class IntegerTest < ActiveModel::TestCase
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
......
......@@ -2,38 +2,40 @@
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
......@@ -2,26 +2,27 @@
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
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_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
end
end
end
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
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册