Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
894c0338
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
894c0338
编写于
10月 14, 2016
作者:
I
Iain Beeston
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactored ActiveModel::Type tests into their own files
上级
159b7748
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
266 addition
and
168 deletion
+266
-168
activemodel/test/cases/type/big_integer_test.rb
activemodel/test/cases/type/big_integer_test.rb
+24
-0
activemodel/test/cases/type/binary_test.rb
activemodel/test/cases/type/binary_test.rb
+15
-0
activemodel/test/cases/type/boolean_test.rb
activemodel/test/cases/type/boolean_test.rb
+39
-0
activemodel/test/cases/type/date_test.rb
activemodel/test/cases/type/date_test.rb
+19
-0
activemodel/test/cases/type/date_time_test.rb
activemodel/test/cases/type/date_time_test.rb
+38
-0
activemodel/test/cases/type/float_test.rb
activemodel/test/cases/type/float_test.rb
+22
-0
activemodel/test/cases/type/immutable_string_test.rb
activemodel/test/cases/type/immutable_string_test.rb
+21
-0
activemodel/test/cases/type/integer_test.rb
activemodel/test/cases/type/integer_test.rb
+7
-0
activemodel/test/cases/type/registry_test.rb
activemodel/test/cases/type/registry_test.rb
+27
-25
activemodel/test/cases/type/string_test.rb
activemodel/test/cases/type/string_test.rb
+19
-18
activemodel/test/cases/type/time_test.rb
activemodel/test/cases/type/time_test.rb
+21
-0
activemodel/test/cases/type/value_test.rb
activemodel/test/cases/type/value_test.rb
+14
-0
activemodel/test/cases/types_test.rb
activemodel/test/cases/types_test.rb
+0
-125
未找到文件。
activemodel/test/cases/type/big_integer_test.rb
0 → 100644
浏览文件 @
894c0338
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
activemodel/test/cases/type/binary_test.rb
0 → 100644
浏览文件 @
894c0338
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
activemodel/test/cases/type/boolean_test.rb
0 → 100644
浏览文件 @
894c0338
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
(
"
\u
3000
\r\n
"
)
assert
type
.
cast
(
"
\u
0000"
)
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
activemodel/test/cases/type/date_test.rb
0 → 100644
浏览文件 @
894c0338
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
activemodel/test/cases/type/date_time_test.rb
0 → 100644
浏览文件 @
894c0338
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
activemodel/test/cases/type/float_test.rb
0 → 100644
浏览文件 @
894c0338
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
activemodel/test/cases/type/immutable_string_test.rb
0 → 100644
浏览文件 @
894c0338
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
activemodel/test/cases/type/integer_test.rb
浏览文件 @
894c0338
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
...
...
activemodel/test/cases/type/registry_test.rb
浏览文件 @
894c0338
...
...
@@ -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
activemodel/test/cases/type/string_test.rb
浏览文件 @
894c0338
...
...
@@ -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
activemodel/test/cases/type/time_test.rb
0 → 100644
浏览文件 @
894c0338
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
activemodel/test/cases/type/value_test.rb
0 → 100644
浏览文件 @
894c0338
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
activemodel/test/cases/types_test.rb
已删除
100644 → 0
浏览文件 @
159b7748
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
(
"
\u
3000
\r\n
"
)
assert
type
.
cast
(
"
\u
0000"
)
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录