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