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/jsoner_test.rb
CHANGED
@@ -1,260 +1,257 @@
|
|
1
1
|
require "test/unit/runner/junitxml"
|
2
2
|
|
3
|
-
require 'emery
|
4
|
-
require 'emery/jsoner'
|
5
|
-
|
6
|
-
module Emery
|
7
|
-
class PlainTypesDeserialization < Test::Unit::TestCase
|
8
|
-
def test_deserialize_integer
|
9
|
-
data = Jsoner.from_json(Integer, '123')
|
10
|
-
T.check(Integer, data)
|
11
|
-
assert_equal 123, data, "Integer should be parsable from JSON"
|
12
|
-
end
|
3
|
+
require 'emery'
|
13
4
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
5
|
+
class PlainTypesDeserialization < Test::Unit::TestCase
|
6
|
+
def test_deserialize_integer
|
7
|
+
data = Jsoner.from_json(Integer, '123')
|
8
|
+
T.check(Integer, data)
|
9
|
+
assert_equal 123, data, "Integer should be parsable from JSON"
|
10
|
+
end
|
19
11
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
assert_equal 1.23, data, "Float should be parsable from JSON"
|
12
|
+
def test_deserialize_integer_fail
|
13
|
+
assert_raise JsonerError do
|
14
|
+
Jsoner.from_json(Integer, '"abc"')
|
24
15
|
end
|
16
|
+
end
|
25
17
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
18
|
+
def test_deserialize_float
|
19
|
+
data = Jsoner.from_json(Float, '1.23')
|
20
|
+
T.check(Float, data)
|
21
|
+
assert_equal 1.23, data, "Float should be parsable from JSON"
|
22
|
+
end
|
31
23
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
24
|
+
def test_deserialize_float_from_integer
|
25
|
+
data = Jsoner.from_json(Float, '123')
|
26
|
+
T.check(Float, data)
|
27
|
+
assert_equal 123.0, data, "Float should be parsable from JSON"
|
28
|
+
end
|
37
29
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
assert_equal true, data, "Boolean should be parsable from JSON"
|
30
|
+
def test_deserialize_float_fail
|
31
|
+
assert_raise JsonerError do
|
32
|
+
Jsoner.from_json(Float, '"abc"')
|
42
33
|
end
|
34
|
+
end
|
43
35
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
36
|
+
def test_deserialize_boolean
|
37
|
+
data = Jsoner.from_json(Boolean, 'true')
|
38
|
+
T.check(Boolean, data)
|
39
|
+
assert_equal true, data, "Boolean should be parsable from JSON"
|
40
|
+
end
|
49
41
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
assert_equal "the string", data, "String should be parsable from JSON"
|
42
|
+
def test_deserialize_boolean_fail
|
43
|
+
assert_raise JsonerError do
|
44
|
+
Jsoner.from_json(Boolean, '1')
|
54
45
|
end
|
46
|
+
end
|
55
47
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
48
|
+
def test_deserialize_string
|
49
|
+
data = Jsoner.from_json(String, '"the string"')
|
50
|
+
T.check(String, data)
|
51
|
+
assert_equal "the string", data, "String should be parsable from JSON"
|
52
|
+
end
|
61
53
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
assert_equal "58d5e212-165b-4ca0-909b-c86b9cee0111", data, "String should be parsable from JSON"
|
54
|
+
def test_deserialize_string_fail
|
55
|
+
assert_raise JsonerError do
|
56
|
+
Jsoner.from_json(String, '123')
|
66
57
|
end
|
58
|
+
end
|
67
59
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
60
|
+
def test_deserialize_uuid
|
61
|
+
data = Jsoner.from_json(UUID, '"58d5e212-165b-4ca0-909b-c86b9cee0111"')
|
62
|
+
T.check(UUID, data)
|
63
|
+
assert_equal "58d5e212-165b-4ca0-909b-c86b9cee0111", data, "String should be parsable from JSON"
|
64
|
+
end
|
73
65
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
assert_equal Date.new(2019, 11, 30), data, "Date should be parsable from JSON"
|
66
|
+
def test_deserialize_uuid_fail
|
67
|
+
assert_raise JsonerError do
|
68
|
+
Jsoner.from_json(UUID, '"abc"')
|
78
69
|
end
|
70
|
+
end
|
79
71
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
72
|
+
def test_deserialize_date
|
73
|
+
data = Jsoner.from_json(Date, '"2019-11-30"')
|
74
|
+
T.check(Date, data)
|
75
|
+
assert_equal Date.new(2019, 11, 30), data, "Date should be parsable from JSON"
|
76
|
+
end
|
85
77
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
assert_equal DateTime.new(2019, 11, 30, 17, 45, 55), data, "DateTime should be parsable from JSON"
|
78
|
+
def test_deserialize_date_wrong_format
|
79
|
+
assert_raise JsonerError do
|
80
|
+
Jsoner.from_json(Date, '"11/30/2019"')
|
90
81
|
end
|
82
|
+
end
|
91
83
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
84
|
+
def test_deserialize_datetime
|
85
|
+
data = Jsoner.from_json(DateTime, '"2019-11-30T17:45:55+00:00"')
|
86
|
+
T.check(DateTime, data)
|
87
|
+
assert_equal DateTime.new(2019, 11, 30, 17, 45, 55), data, "DateTime should be parsable from JSON"
|
88
|
+
end
|
97
89
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
assert_equal nil, data, "Nilable null value should be parsable from JSON"
|
90
|
+
def test_deserialize_datetime_wrong_format
|
91
|
+
assert_raise JsonerError do
|
92
|
+
Jsoner.from_json(DateTime, '"2019-11-30 17:45:55"')
|
102
93
|
end
|
94
|
+
end
|
103
95
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
96
|
+
def test_deserialize_nilable_nil
|
97
|
+
data = Jsoner.from_json(T.nilable(String), 'null')
|
98
|
+
T.check(T.nilable(String), data)
|
99
|
+
assert_equal nil, data, "Nilable null value should be parsable from JSON"
|
100
|
+
end
|
109
101
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
end
|
102
|
+
def test_deserialize_nilable_string
|
103
|
+
data = Jsoner.from_json(T.nilable(String), '"some string"')
|
104
|
+
T.check(T.nilable(String), data)
|
105
|
+
assert_equal "some string", data, "Nilable non-null value should be parsable from JSON"
|
115
106
|
end
|
116
107
|
|
117
|
-
|
118
|
-
|
119
|
-
|
108
|
+
def test_deserialize_non_nilable
|
109
|
+
assert_raise JsonerError do
|
110
|
+
Jsoner.from_json(String, 'null')
|
120
111
|
end
|
112
|
+
end
|
113
|
+
end
|
121
114
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
end
|
115
|
+
class PlainTypesSerialization < Test::Unit::TestCase
|
116
|
+
def test_serialize_integer
|
117
|
+
assert_equal "123", Jsoner.to_json(Integer,123), "Integer should be serializable to JSON"
|
118
|
+
end
|
127
119
|
|
128
|
-
|
129
|
-
|
120
|
+
def test_serialize_integer_fail
|
121
|
+
assert_raise JsonerError do
|
122
|
+
Jsoner.to_json(Integer,123.4)
|
130
123
|
end
|
124
|
+
end
|
131
125
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
end
|
136
|
-
end
|
126
|
+
def test_serialize_float
|
127
|
+
assert_equal "12.3", Jsoner.to_json(Float,12.3), "Float should be serializable to JSON"
|
128
|
+
end
|
137
129
|
|
138
|
-
|
139
|
-
|
130
|
+
def test_serialize_float_fail
|
131
|
+
assert_raise JsonerError do
|
132
|
+
Jsoner.to_json(Float,123)
|
140
133
|
end
|
134
|
+
end
|
141
135
|
|
142
|
-
|
143
|
-
|
144
|
-
|
136
|
+
def test_serialize_boolean
|
137
|
+
assert_equal "true", Jsoner.to_json(Boolean, true), "Boolean should be serializable to JSON"
|
138
|
+
end
|
145
139
|
|
146
|
-
|
147
|
-
|
148
|
-
|
140
|
+
def test_serialize_string
|
141
|
+
assert_equal '"the string"', Jsoner.to_json(String,"the string"), "String should be serializable to JSON"
|
142
|
+
end
|
149
143
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
end
|
154
|
-
end
|
144
|
+
def test_serialize_date
|
145
|
+
assert_equal '"2019-11-30"', Jsoner.to_json(Date, Date.new(2019, 11, 30)), "Date should be serializable to JSON"
|
146
|
+
end
|
155
147
|
|
156
|
-
|
157
|
-
|
148
|
+
def test_serialize_date_fail
|
149
|
+
assert_raise JsonerError do
|
150
|
+
Jsoner.to_json(Date, 123)
|
158
151
|
end
|
152
|
+
end
|
159
153
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
end
|
154
|
+
def test_serialize_datetime
|
155
|
+
assert_equal '"2019-11-30T17:45:55"', Jsoner.to_json(DateTime, DateTime.new(2019, 11, 30, 17, 45, 55)), "DateTime should be serializable to JSON"
|
156
|
+
end
|
164
157
|
|
165
|
-
|
166
|
-
|
167
|
-
|
158
|
+
def test_serialize_nil
|
159
|
+
json_str = Jsoner.to_json(T.nilable(Untyped), nil)
|
160
|
+
assert_equal "null", json_str, "nil should be serializable to JSON"
|
168
161
|
end
|
169
162
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
assert_equal ["the string", "the other string"], data, "Should parse array of strings"
|
175
|
-
end
|
163
|
+
def test_serialize_uuid
|
164
|
+
assert_equal '"58d5e212-165b-4ca0-909b-c86b9cee0111"', Jsoner.to_json(UUID, "58d5e212-165b-4ca0-909b-c86b9cee0111"), "UUID should be serializable to JSON"
|
165
|
+
end
|
166
|
+
end
|
176
167
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
168
|
+
class ArrayDeserialization < Test::Unit::TestCase
|
169
|
+
def test_deserialize_array
|
170
|
+
data = Jsoner.from_json(T.array(String), '["the string", "the other string"]')
|
171
|
+
T.check(T.array(String), data)
|
172
|
+
assert_equal ["the string", "the other string"], data, "Should parse array of strings"
|
182
173
|
end
|
183
174
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
175
|
+
def test_deserialize_array_of_datetime
|
176
|
+
data = Jsoner.from_json(T.array(DateTime), '["2019-11-30T17:45:55+00:00"]')
|
177
|
+
T.check(T.array(DateTime), data)
|
178
|
+
assert_equal [DateTime.new(2019, 11, 30, 17, 45, 55)], data, "Should parse array of DateTime"
|
179
|
+
end
|
180
|
+
end
|
188
181
|
|
189
|
-
|
190
|
-
|
191
|
-
|
182
|
+
class ArraySerialization < Test::Unit::TestCase
|
183
|
+
def test_serialize_array
|
184
|
+
assert_equal '["the string","the other string"]', Jsoner.to_json(T.array(String), ["the string", "the other string"]), "Array should be serializable to JSON"
|
192
185
|
end
|
193
186
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
assert_equal ({"one" => 123, "two" => 456}), data, "Should parse hash"
|
199
|
-
end
|
187
|
+
def test_serialize_array_of_datetime
|
188
|
+
assert_equal '["2019-11-30T17:45:55"]', Jsoner.to_json(T.array(DateTime), [DateTime.new(2019, 11, 30, 17, 45, 55)]), "Array should be serializable to JSON"
|
189
|
+
end
|
190
|
+
end
|
200
191
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
192
|
+
class HashDeserialization < Test::Unit::TestCase
|
193
|
+
def test_deserialize_hash
|
194
|
+
data = Jsoner.from_json(T.hash(String, Integer), '{"one": 123, "two": 456}')
|
195
|
+
T.check(T.hash(String, Integer), data)
|
196
|
+
assert_equal ({"one" => 123, "two" => 456}), data, "Should parse hash"
|
206
197
|
end
|
207
198
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
199
|
+
def test_deserialize_hash_string_to_datetime
|
200
|
+
data = Jsoner.from_json(T.hash(String, DateTime), '{"one": "2019-11-30T17:45:55+00:00"}')
|
201
|
+
T.check(T.hash(String, DateTime), data)
|
202
|
+
assert_equal ({"one" => DateTime.new(2019, 11, 30, 17, 45, 55)}), data, "Should parse hash"
|
203
|
+
end
|
204
|
+
end
|
212
205
|
|
213
|
-
|
214
|
-
|
215
|
-
|
206
|
+
class HashSerialization < Test::Unit::TestCase
|
207
|
+
def test_serialize_hash
|
208
|
+
assert_equal '{"one":123,"two":456}', Jsoner.to_json(T.hash(String, Integer), {"one" => 123, "two" => 456}), "Hash should be serializable to JSON"
|
216
209
|
end
|
217
210
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
assert_equal "the string", data, "Should parse any type"
|
223
|
-
end
|
211
|
+
def test_serialize_hash_string_to_datetime
|
212
|
+
assert_equal '{"one":"2019-11-30T17:45:55"}', Jsoner.to_json(T.hash(String, DateTime), {"one" => DateTime.new(2019, 11, 30, 17, 45, 55)}), "Hash should be serializable to JSON"
|
213
|
+
end
|
214
|
+
end
|
224
215
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
216
|
+
class AnyDeserialization < Test::Unit::TestCase
|
217
|
+
def test_deserialize
|
218
|
+
data = Jsoner.from_json(T.any(String, Integer), '"the string"')
|
219
|
+
T.check(T.any(String, Integer), data)
|
220
|
+
assert_equal "the string", data, "Should parse any type"
|
230
221
|
end
|
231
222
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
T.check(T.any(String, Integer), data)
|
236
|
-
assert_equal '"the string"', data, "Should serialize any type"
|
223
|
+
def test_deserialize_fail
|
224
|
+
assert_raise JsonerError do
|
225
|
+
Jsoner.from_json(T.any(Integer, Float), '"the string"')
|
237
226
|
end
|
238
227
|
end
|
228
|
+
end
|
239
229
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
230
|
+
class AnySerialization < Test::Unit::TestCase
|
231
|
+
def test_serialize
|
232
|
+
data = Jsoner.to_json(T.any(String, Integer), "the string")
|
233
|
+
T.check(T.any(String, Integer), data)
|
234
|
+
assert_equal '"the string"', data, "Should serialize any type"
|
235
|
+
end
|
236
|
+
end
|
246
237
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
238
|
+
class UnionDeserialization < Test::Unit::TestCase
|
239
|
+
def test_deserialize
|
240
|
+
data = Jsoner.from_json(T.union(str: String, int: Integer), '{"str":"the string"}')
|
241
|
+
T.check(T.union(str: String, int: Integer), data)
|
242
|
+
assert_equal "the string", data, "Should parse union type"
|
252
243
|
end
|
253
244
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
assert_equal '{"str":"the string"}', data, "Should serialize union type with wrapping object"
|
245
|
+
def test_deserialize_any_fail
|
246
|
+
assert_raise JsonerError do
|
247
|
+
Jsoner.from_json(T.union(str: String, int: Integer), '{"bool":true}')
|
258
248
|
end
|
259
249
|
end
|
250
|
+
end
|
251
|
+
|
252
|
+
class UnionSerialization < Test::Unit::TestCase
|
253
|
+
def test_serialize
|
254
|
+
data = Jsoner.to_json(T.union(str: String, int: Integer), "the string")
|
255
|
+
assert_equal '{"str":"the string"}', data, "Should serialize union type with wrapping object"
|
256
|
+
end
|
260
257
|
end
|
data/test/tod_test.rb
CHANGED
@@ -1,41 +1,36 @@
|
|
1
1
|
require "test/unit/runner/junitxml"
|
2
2
|
|
3
|
-
require 'emery
|
4
|
-
require 'emery/tod'
|
5
|
-
require 'emery/jsoner'
|
3
|
+
require 'emery'
|
6
4
|
|
5
|
+
class TypeCheckTimeOfDay < Test::Unit::TestCase
|
6
|
+
def test_success
|
7
|
+
assert_equal TimeOfDay.parse("10:40:50"), T.check(TimeOfDay, TimeOfDay.parse("10:40:50"))
|
8
|
+
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
assert_equal TimeOfDay.parse("10:40:50"), T.check(TimeOfDay, TimeOfDay.parse("10:40:50"))
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_fail
|
15
|
-
assert_raise TypeError do
|
16
|
-
T.check(DateTime, TimeOfDay.parse("10:40:50"))
|
17
|
-
end
|
10
|
+
def test_fail
|
11
|
+
assert_raise TypeError do
|
12
|
+
T.check(DateTime, TimeOfDay.parse("10:40:50"))
|
18
13
|
end
|
19
14
|
end
|
15
|
+
end
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
17
|
+
class TimeOfDayDeserialization < Test::Unit::TestCase
|
18
|
+
def test_deserialize
|
19
|
+
data = Jsoner.from_json(TimeOfDay, '"10:40:50"')
|
20
|
+
T.check(TimeOfDay, data)
|
21
|
+
assert_equal TimeOfDay.parse("10:40:50"), data
|
22
|
+
end
|
27
23
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
24
|
+
def test_deserialize_fail
|
25
|
+
assert_raise JsonerError do
|
26
|
+
Jsoner.from_json(TimeOfDay, '"abc"')
|
32
27
|
end
|
33
28
|
end
|
29
|
+
end
|
34
30
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
31
|
+
class TimeOfDaySerialization < Test::Unit::TestCase
|
32
|
+
def test_serialize
|
33
|
+
json_str = Jsoner.to_json(TimeOfDay, TimeOfDay.parse("10:40:50"))
|
34
|
+
assert_equal '"10:40:50"', json_str
|
40
35
|
end
|
41
36
|
end
|