emery 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/emery/dataclass.rb +63 -65
- data/lib/emery/enum.rb +73 -75
- data/lib/emery/jsoner.rb +162 -164
- data/lib/emery/tod.rb +202 -204
- data/lib/emery/type.rb +149 -151
- data/lib/emery.rb +1 -2
- data/test/dataclass_test.rb +80 -83
- data/test/enum_test.rb +29 -33
- data/test/jsoner_test.rb +189 -192
- data/test/tod_test.rb +23 -28
- data/test/type_test.rb +153 -155
- metadata +1 -1
data/test/type_test.rb
CHANGED
@@ -1,220 +1,218 @@
|
|
1
1
|
require "test/unit/runner/junitxml"
|
2
2
|
require "date"
|
3
3
|
|
4
|
-
require 'emery
|
4
|
+
require 'emery'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
6
|
+
class TypeEquality < Test::Unit::TestCase
|
7
|
+
def test_plain_equals
|
8
|
+
assert_true Integer == Integer
|
9
|
+
end
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def test_plain_not_equals
|
12
|
+
assert_false Integer == String
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def test_uuid_equals
|
16
|
+
assert_true UUID == UUID
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def test_boolean_equals
|
20
|
+
assert_true Boolean == Boolean
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
def test_untyped_equals
|
24
|
+
assert_true Untyped == Untyped
|
25
|
+
end
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
def test_nilable_equals
|
28
|
+
assert_true T.nilable(Integer) == T.nilable(Integer)
|
29
|
+
end
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
def test_nilable_not_equals
|
32
|
+
assert_false T.nilable(Integer) == T.nilable(String)
|
33
|
+
end
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
def test_array_equals
|
36
|
+
assert_true T.array(Integer) == T.array(Integer)
|
37
|
+
end
|
39
38
|
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
def test_array_not_equals
|
40
|
+
assert_false T.array(Integer) == Integer
|
41
|
+
end
|
43
42
|
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
def test_array_other_item_type
|
44
|
+
assert_false T.array(Integer) == T.array(String)
|
45
|
+
end
|
47
46
|
|
48
|
-
|
49
|
-
|
50
|
-
|
47
|
+
def test_hash_equals
|
48
|
+
assert_true T.hash(String, Integer) == T.hash(String, Integer)
|
49
|
+
end
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
def test_hash_not_equals
|
52
|
+
assert_false T.hash(String, Integer) == String
|
53
|
+
end
|
55
54
|
|
56
|
-
|
57
|
-
|
58
|
-
|
55
|
+
def test_hash_other_value_type
|
56
|
+
assert_false T.hash(String, Integer) == T.hash(String, String)
|
57
|
+
end
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
59
|
+
def test_any_equals
|
60
|
+
assert_true T.any(String, Integer) == T.any(String, Integer)
|
61
|
+
end
|
63
62
|
|
64
|
-
|
65
|
-
|
66
|
-
|
63
|
+
def test_any_equals_other_order
|
64
|
+
assert_true T.any(String, Integer) == T.any(Integer, String)
|
65
|
+
end
|
67
66
|
|
68
|
-
|
69
|
-
|
70
|
-
end
|
67
|
+
def test_any_of_other_type
|
68
|
+
assert_false T.any(String, Integer) == T.any(Integer, Float)
|
71
69
|
end
|
70
|
+
end
|
72
71
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
72
|
+
class TypeToString < Test::Unit::TestCase
|
73
|
+
def test_nilable
|
74
|
+
assert_equal "Nilable[Integer]", T.nilable(Integer).to_s
|
75
|
+
end
|
77
76
|
|
78
|
-
|
79
|
-
|
80
|
-
|
77
|
+
def test_array
|
78
|
+
assert_equal "Array[Integer]", T.array(Integer).to_s
|
79
|
+
end
|
81
80
|
|
82
|
-
|
83
|
-
|
84
|
-
|
81
|
+
def test_hash
|
82
|
+
assert_equal "Hash[String, Integer]", T.hash(String, Integer).to_s
|
83
|
+
end
|
85
84
|
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
def test_any
|
86
|
+
assert_equal "Any[String, Integer]", T.any(String, Integer).to_s
|
87
|
+
end
|
89
88
|
|
90
|
-
|
91
|
-
|
92
|
-
end
|
89
|
+
def test_union
|
90
|
+
assert_equal "Union[str: String, int: Integer]", T.union(str: String, int: Integer).to_s
|
93
91
|
end
|
92
|
+
end
|
94
93
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
94
|
+
class TypeCheck < Test::Unit::TestCase
|
95
|
+
def test_nil
|
96
|
+
assert_equal nil, T.check(NilClass, nil)
|
97
|
+
end
|
99
98
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
end
|
99
|
+
def test_nil_string
|
100
|
+
assert_raise TypeError do
|
101
|
+
T.check(String, nil)
|
104
102
|
end
|
103
|
+
end
|
105
104
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
end
|
105
|
+
def test_types_mismatch
|
106
|
+
assert_raise TypeError do
|
107
|
+
T.check(String, 123)
|
110
108
|
end
|
109
|
+
end
|
111
110
|
|
112
|
-
|
113
|
-
|
114
|
-
|
111
|
+
def test_string
|
112
|
+
assert_equal "the string", T.check(String, "the string"), "Plain String type should allow String value"
|
113
|
+
end
|
115
114
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
120
|
-
assert_match "Type String does not allow nil value", err.message
|
115
|
+
def test_string_nil
|
116
|
+
err = assert_raise TypeError do
|
117
|
+
T.check(String, nil)
|
121
118
|
end
|
119
|
+
assert_match "Type String does not allow nil value", err.message
|
120
|
+
end
|
122
121
|
|
123
|
-
|
124
|
-
|
125
|
-
|
122
|
+
def test_boolean
|
123
|
+
assert_equal true, T.check(Boolean, true), "Artificial Boolean type should allow true value"
|
124
|
+
end
|
126
125
|
|
127
|
-
|
128
|
-
|
129
|
-
|
126
|
+
def test_date
|
127
|
+
assert_equal Date.new(2020, 5, 24), T.check(Date, Date.new(2020, 5, 24)), "Date type should pass validation"
|
128
|
+
end
|
130
129
|
|
131
|
-
|
132
|
-
|
133
|
-
|
130
|
+
def test_datetime
|
131
|
+
assert_equal DateTime.new(2020, 5, 24, 14, 30, 30), T.check(Date, DateTime.new(2020, 5, 24, 14, 30, 30)), "DateTime type should pass validation"
|
132
|
+
end
|
134
133
|
|
135
|
-
|
136
|
-
|
137
|
-
|
134
|
+
def test_time
|
135
|
+
assert_equal Time.new(2007,11,5,13,45,0, "-05:00"), T.check(Time, Time.new(2007, 11, 5, 13, 45, 0, "-05:00")), "Time type should pass validation"
|
136
|
+
end
|
138
137
|
|
139
|
-
|
140
|
-
|
141
|
-
|
138
|
+
def test_uuid
|
139
|
+
assert_equal "123e4567-e89b-12d3-a456-426655440000", T.check(UUID, "123e4567-e89b-12d3-a456-426655440000"), "UUID type should pass validation on correctly formatted string"
|
140
|
+
end
|
142
141
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
end
|
142
|
+
def test_uuid_fail
|
143
|
+
assert_raise TypeError do
|
144
|
+
T.check(UUID, "really not the uuid")
|
147
145
|
end
|
146
|
+
end
|
148
147
|
|
149
|
-
|
150
|
-
|
151
|
-
|
148
|
+
def test_untyped_success
|
149
|
+
assert_equal "bla", T.check(Untyped, "bla"), "Untyped should accept strings"
|
150
|
+
end
|
152
151
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
end
|
152
|
+
def test_untyped_nil
|
153
|
+
assert_raise TypeError do
|
154
|
+
T.check(Untyped, nil)
|
157
155
|
end
|
156
|
+
end
|
158
157
|
|
159
|
-
|
160
|
-
|
161
|
-
|
158
|
+
def test_nilable_nil
|
159
|
+
assert_equal nil, T.check(T.nilable(String), nil), "Nilable type should allow nil value"
|
160
|
+
end
|
162
161
|
|
163
|
-
|
164
|
-
|
165
|
-
end
|
162
|
+
def test_nilable
|
163
|
+
assert_equal "the string", T.check(T.nilable(String), "the string"), "Nilable String type should allow String value"
|
166
164
|
end
|
165
|
+
end
|
167
166
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
167
|
+
class TypeCheckArray < Test::Unit::TestCase
|
168
|
+
def test_array_string
|
169
|
+
assert_equal ["the string"], T.check(T.array(String), ["the string"]), "Array of String should allow String value"
|
170
|
+
end
|
172
171
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
end
|
172
|
+
def test_array_fail
|
173
|
+
ex = assert_raise TypeError do
|
174
|
+
T.check(T.array(String), "the string")
|
177
175
|
end
|
176
|
+
puts ex
|
177
|
+
end
|
178
178
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
end
|
179
|
+
def test_array_wrong_item_type
|
180
|
+
assert_raise TypeError do
|
181
|
+
T.check(T.array(String), ["the string", 123])
|
183
182
|
end
|
184
183
|
end
|
184
|
+
end
|
185
185
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
186
|
+
class TypeCheckHash < Test::Unit::TestCase
|
187
|
+
def test_hash_string_to_string
|
188
|
+
assert_equal({"key" => "the value"}, T.check(T.hash(String, String), {"key" => "the value"}), "Hash of String -> String should allow String -> String value")
|
189
|
+
end
|
190
190
|
|
191
|
-
|
192
|
-
|
193
|
-
end
|
191
|
+
def test_hash_string_to_untyped
|
192
|
+
assert_equal({"key" => "the value"}, T.check(T.hash(String, Untyped), {"key" => "the value"}), "Hash of String -> Untyped should allow String -> String value")
|
194
193
|
end
|
194
|
+
end
|
195
195
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
196
|
+
class TypeCheckAny < Test::Unit::TestCase
|
197
|
+
def test_success
|
198
|
+
assert_equal(123, T.check(T.any(String, Integer), 123), "Any of String, Integer should allow Integer value")
|
199
|
+
end
|
200
200
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
end
|
201
|
+
def test_fail
|
202
|
+
assert_raise TypeError do
|
203
|
+
T.check(T.any(String, Integer), true)
|
205
204
|
end
|
206
205
|
end
|
206
|
+
end
|
207
207
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
208
|
+
class TypeCheckUnion < Test::Unit::TestCase
|
209
|
+
def test_success
|
210
|
+
assert_equal(123, T.check(T.union(str: String, int: Integer), 123))
|
211
|
+
end
|
212
212
|
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
end
|
213
|
+
def test_fail
|
214
|
+
assert_raise TypeError do
|
215
|
+
T.check(T.union(str: String, int: Integer), true)
|
217
216
|
end
|
218
217
|
end
|
219
|
-
|
220
218
|
end
|