emery 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/jsoner_test.rb CHANGED
@@ -1,260 +1,257 @@
1
1
  require "test/unit/runner/junitxml"
2
2
 
3
- require 'emery/type'
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
- def test_deserialize_integer_fail
15
- assert_raise JsonerError do
16
- Jsoner.from_json(Integer, '"abc"')
17
- end
18
- end
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
- def test_deserialize_float
21
- data = Jsoner.from_json(Float, '1.23')
22
- T.check(Float, data)
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
- def test_deserialize_float_from_integer
27
- data = Jsoner.from_json(Float, '123')
28
- T.check(Float, data)
29
- assert_equal 123.0, data, "Float should be parsable from JSON"
30
- end
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
- def test_deserialize_float_fail
33
- assert_raise JsonerError do
34
- Jsoner.from_json(Float, '"abc"')
35
- end
36
- end
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
- def test_deserialize_boolean
39
- data = Jsoner.from_json(Boolean, 'true')
40
- T.check(Boolean, data)
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
- def test_deserialize_boolean_fail
45
- assert_raise JsonerError do
46
- Jsoner.from_json(Boolean, '1')
47
- end
48
- end
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
- def test_deserialize_string
51
- data = Jsoner.from_json(String, '"the string"')
52
- T.check(String, data)
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
- def test_deserialize_string_fail
57
- assert_raise JsonerError do
58
- Jsoner.from_json(String, '123')
59
- end
60
- end
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
- def test_deserialize_uuid
63
- data = Jsoner.from_json(UUID, '"58d5e212-165b-4ca0-909b-c86b9cee0111"')
64
- T.check(UUID, data)
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
- def test_deserialize_uuid_fail
69
- assert_raise JsonerError do
70
- Jsoner.from_json(UUID, '"abc"')
71
- end
72
- end
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
- def test_deserialize_date
75
- data = Jsoner.from_json(Date, '"2019-11-30"')
76
- T.check(Date, data)
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
- def test_deserialize_date_wrong_format
81
- assert_raise JsonerError do
82
- Jsoner.from_json(Date, '"11/30/2019"')
83
- end
84
- end
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
- def test_deserialize_datetime
87
- data = Jsoner.from_json(DateTime, '"2019-11-30T17:45:55+00:00"')
88
- T.check(DateTime, data)
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
- def test_deserialize_datetime_wrong_format
93
- assert_raise JsonerError do
94
- Jsoner.from_json(DateTime, '"2019-11-30 17:45:55"')
95
- end
96
- end
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
- def test_deserialize_nilable_nil
99
- data = Jsoner.from_json(T.nilable(String), 'null')
100
- T.check(T.nilable(String), data)
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
- def test_deserialize_nilable_string
105
- data = Jsoner.from_json(T.nilable(String), '"some string"')
106
- T.check(T.nilable(String), data)
107
- assert_equal "some string", data, "Nilable non-null value should be parsable from JSON"
108
- end
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
- def test_deserialize_non_nilable
111
- assert_raise JsonerError do
112
- Jsoner.from_json(String, 'null')
113
- end
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
- class PlainTypesSerialization < Test::Unit::TestCase
118
- def test_serialize_integer
119
- assert_equal "123", Jsoner.to_json(Integer,123), "Integer should be serializable to JSON"
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
- def test_serialize_integer_fail
123
- assert_raise JsonerError do
124
- Jsoner.to_json(Integer,123.4)
125
- end
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
- def test_serialize_float
129
- assert_equal "12.3", Jsoner.to_json(Float,12.3), "Float should be serializable to JSON"
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
- def test_serialize_float_fail
133
- assert_raise JsonerError do
134
- Jsoner.to_json(Float,123)
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
- def test_serialize_boolean
139
- assert_equal "true", Jsoner.to_json(Boolean, true), "Boolean should be serializable to JSON"
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
- def test_serialize_string
143
- assert_equal '"the string"', Jsoner.to_json(String,"the string"), "String should be serializable to JSON"
144
- end
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
- def test_serialize_date
147
- assert_equal '"2019-11-30"', Jsoner.to_json(Date, Date.new(2019, 11, 30)), "Date should be serializable to JSON"
148
- end
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
- def test_serialize_date_fail
151
- assert_raise JsonerError do
152
- Jsoner.to_json(Date, 123)
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
- def test_serialize_datetime
157
- 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"
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
- def test_serialize_nil
161
- json_str = Jsoner.to_json(T.nilable(Untyped), nil)
162
- assert_equal "null", json_str, "nil should be serializable to JSON"
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
- def test_serialize_uuid
166
- assert_equal '"58d5e212-165b-4ca0-909b-c86b9cee0111"', Jsoner.to_json(UUID, "58d5e212-165b-4ca0-909b-c86b9cee0111"), "UUID should be serializable to JSON"
167
- end
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
- class ArrayDeserialization < Test::Unit::TestCase
171
- def test_deserialize_array
172
- data = Jsoner.from_json(T.array(String), '["the string", "the other string"]')
173
- T.check(T.array(String), data)
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
- def test_deserialize_array_of_datetime
178
- data = Jsoner.from_json(T.array(DateTime), '["2019-11-30T17:45:55+00:00"]')
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
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
- class ArraySerialization < Test::Unit::TestCase
185
- def test_serialize_array
186
- assert_equal '["the string","the other string"]', Jsoner.to_json(T.array(String), ["the string", "the other string"]), "Array should be serializable to JSON"
187
- end
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
- def test_serialize_array_of_datetime
190
- 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"
191
- end
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
- class HashDeserialization < Test::Unit::TestCase
195
- def test_deserialize_hash
196
- data = Jsoner.from_json(T.hash(String, Integer), '{"one": 123, "two": 456}')
197
- T.check(T.hash(String, Integer), data)
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
- def test_deserialize_hash_string_to_datetime
202
- data = Jsoner.from_json(T.hash(String, DateTime), '{"one": "2019-11-30T17:45:55+00:00"}')
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
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
- class HashSerialization < Test::Unit::TestCase
209
- def test_serialize_hash
210
- assert_equal '{"one":123,"two":456}', Jsoner.to_json(T.hash(String, Integer), {"one" => 123, "two" => 456}), "Hash should be serializable to JSON"
211
- end
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
- def test_serialize_hash_string_to_datetime
214
- 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"
215
- end
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
- class AnyDeserialization < Test::Unit::TestCase
219
- def test_deserialize
220
- data = Jsoner.from_json(T.any(String, Integer), '"the string"')
221
- T.check(T.any(String, Integer), data)
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
- def test_deserialize_fail
226
- assert_raise JsonerError do
227
- Jsoner.from_json(T.any(Integer, Float), '"the string"')
228
- end
229
- end
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
- class AnySerialization < Test::Unit::TestCase
233
- def test_serialize
234
- data = Jsoner.to_json(T.any(String, Integer), "the string")
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
- class UnionDeserialization < Test::Unit::TestCase
241
- def test_deserialize
242
- data = Jsoner.from_json(T.union(str: String, int: Integer), '{"str":"the string"}')
243
- T.check(T.union(str: String, int: Integer), data)
244
- assert_equal "the string", data, "Should parse union type"
245
- end
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
- def test_deserialize_any_fail
248
- assert_raise JsonerError do
249
- Jsoner.from_json(T.union(str: String, int: Integer), '{"bool":true}')
250
- end
251
- end
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
- class UnionSerialization < Test::Unit::TestCase
255
- def test_serialize
256
- data = Jsoner.to_json(T.union(str: String, int: Integer), "the string")
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/type'
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
- module Emery
9
- class TypeCheckTimeOfDay < Test::Unit::TestCase
10
- def test_success
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
- class TimeOfDayDeserialization < Test::Unit::TestCase
22
- def test_deserialize
23
- data = Jsoner.from_json(TimeOfDay, '"10:40:50"')
24
- T.check(TimeOfDay, data)
25
- assert_equal TimeOfDay.parse("10:40:50"), data
26
- end
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
- def test_deserialize_fail
29
- assert_raise JsonerError do
30
- Jsoner.from_json(TimeOfDay, '"abc"')
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
- class TimeOfDaySerialization < Test::Unit::TestCase
36
- def test_serialize
37
- json_str = Jsoner.to_json(TimeOfDay, TimeOfDay.parse("10:40:50"))
38
- assert_equal '"10:40:50"', json_str
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