/
byte_format_spec.cr
315 lines (263 loc) · 9.66 KB
/
byte_format_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
require "spec"
require "../src/byte_format"
require "./format_helper"
module ByteFormatTest
struct ComplexObject
@index : UInt8 = 1
@[Crystalizer::Field(bytesize: 1)]
@y = "a"
@key : String = "abc"
@value : Int8 = 2
end
struct Point
@x = 1
@y = "a"
end
enum Abc : UInt8
A
B
C
end
struct PointBytesize
@x = 1
@[Crystalizer::Field(bytesize: 1)]
@y = "a"
end
struct StringGoodSize
@[Crystalizer::Field(bytesize: 5)]
@a = "Short"
end
struct StringBadSize1
@[Crystalizer::Field(bytesize: 4)]
@a = "Short"
end
struct StringBadSize2
@[Crystalizer::Field(bytesize: 6)]
@a = "Short"
end
struct StringGoodRange1
@[Crystalizer::Field(bytesize: ..20)]
@b = "Long description"
end
struct StringGoodRange2
@[Crystalizer::Field(bytesize: 10..)]
@b = "Long description"
end
struct StringGoodRange3
@[Crystalizer::Field(bytesize: 10..20)]
@b = "Long description"
end
struct StringGoodRange4
@[Crystalizer::Field(bytesize: ...20)]
@b = "Long description"
end
struct StringGoodRange5
@[Crystalizer::Field(bytesize: 10...)]
@b = "Long description"
end
struct StringGoodRange6
@[Crystalizer::Field(bytesize: 10...20)]
@b = "Long description"
end
struct StringGoodRange7
@[Crystalizer::Field(bytesize: 16...20)]
@b = "Long description"
end
struct StringGoodRange8
@[Crystalizer::Field(bytesize: 10..16)]
@b = "Long description"
end
struct StringGoodRange9
@[Crystalizer::Field(bytesize: 10...17)]
@b = "Long description"
end
struct StringBadRange1
@[Crystalizer::Field(bytesize: 6..)]
@a = "Short"
end
struct StringBadRange2
@[Crystalizer::Field(bytesize: ..4)]
@a = "Short"
end
struct StringBadRange3
@[Crystalizer::Field(bytesize: 6..8)]
@a = "Short"
end
struct StringBadRange4
@[Crystalizer::Field(bytesize: 6..8)]
@a = "Long description"
end
struct StringBadRange5
@[Crystalizer::Field(bytesize: 6...)]
@a = "Short"
end
struct StringBadRange6
@[Crystalizer::Field(bytesize: ...4)]
@a = "Short"
end
struct StringBadRange7
@[Crystalizer::Field(bytesize: 6...8)]
@a = "Short"
end
class Obj
getter x = 1, y = "a"
def ==(other : self)
@x == other.x && @y == other.y
end
end
end
private def assert_byte_format_serialization(object : T, bytes : Bytes, line = __LINE__) forall T
it "serializes", line: line do
Crystalizer::ByteFormat.serialize(object).should eq bytes
end
it "deserializes", line: line do
Crystalizer::ByteFormat.deserialize(bytes, to: T).should eq object
end
end
describe Crystalizer::ByteFormat do
describe "struct" do
assert_byte_format_serialization(
ByteFormatTest::Point.new,
Bytes[1, 0, 0, 0, 97, 0]
)
assert_byte_format_serialization(
ByteFormatTest::ComplexObject.new,
Bytes[1, 97, 98, 99, 0, 2, 97],
)
end
describe "class" do
assert_byte_format_serialization(
ByteFormatTest::Obj.new,
Bytes[1, 0, 0, 0, 97, 0]
)
end
describe "nested class" do
assert_byte_format_serialization(
Parent.new("foo", Nested.new("bar")),
Bytes[102, 111, 111, 0, 98, 97, 114, 0]
)
end
describe Array do
assert_byte_format_serialization([1, 2], Bytes[1, 0, 0, 0, 2, 0, 0, 0])
end
describe Tuple do
assert_byte_format_serialization({"a", 2}, Bytes[97, 0, 2, 0, 0, 0])
end
describe Enum do
assert_byte_format_serialization(ByteFormatTest::Abc::B, Bytes[1])
end
describe NamedTuple do
assert_byte_format_serialization({a: "a", b: 1}, Bytes[97, 0, 97, 0, 98, 0, 1, 0, 0, 0])
end
describe Bool do
assert_byte_format_serialization(false, Bytes[0])
assert_byte_format_serialization(true, Bytes[1])
end
describe Float do
assert_byte_format_serialization(1.5_f32, Bytes[0, 0, 192, 63])
assert_byte_format_serialization(1.5_f64, Bytes[0, 0, 0, 0, 0, 0, 248, 63])
end
describe Int do
assert_byte_format_serialization(Int32::MIN, Bytes[0, 0, 0, 128])
assert_byte_format_serialization(1_u64, Bytes[1, 0, 0, 0, 0, 0, 0, 0])
end
describe Hash do
assert_byte_format_serialization({"a" => 123}, Bytes[97, 0, 123, 0, 0, 0])
end
describe String do
describe "(de)serialization" do
assert_byte_format_serialization("abc", Bytes[97, 98, 99, 0])
assert_byte_format_serialization(
ByteFormatTest::PointBytesize.new,
Bytes[1, 0, 0, 0, 97]
)
# Bytes corresponding to `@a = "Long description\0"`
bytes = Bytes[76, 111, 110, 103, 32, 100, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 0]
assert_byte_format_serialization(ByteFormatTest::StringGoodRange1.new, bytes)
assert_byte_format_serialization(ByteFormatTest::StringGoodRange2.new, bytes)
assert_byte_format_serialization(ByteFormatTest::StringGoodRange3.new, bytes)
assert_byte_format_serialization(ByteFormatTest::StringGoodRange4.new, bytes)
assert_byte_format_serialization(ByteFormatTest::StringGoodRange5.new, bytes)
assert_byte_format_serialization(ByteFormatTest::StringGoodRange6.new, bytes)
assert_byte_format_serialization(ByteFormatTest::StringGoodRange7.new, bytes)
assert_byte_format_serialization(ByteFormatTest::StringGoodRange8.new, bytes)
assert_byte_format_serialization(ByteFormatTest::StringGoodRange9.new, bytes)
# Bytes corresponding to `@a = "Short"`
bytes = Bytes[83, 104, 111, 114, 116]
assert_byte_format_serialization(ByteFormatTest::StringGoodSize.new, bytes)
assert_byte_format_serialization(ByteFormatTest::StringGoodSize.new, bytes)
end
describe "deserialization" do
bytes = Bytes[83, 104, 111, 114, 116]
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: 6..8" do
Crystalizer::ByteFormat.deserialize(bytes, to: ByteFormatTest::StringBadRange4)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: 6.." do
Crystalizer::ByteFormat.deserialize(bytes, to: ByteFormatTest::StringBadRange1)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: ..4" do
Crystalizer::ByteFormat.deserialize(bytes, to: ByteFormatTest::StringBadRange2)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: 6..8" do
Crystalizer::ByteFormat.deserialize(bytes, to: ByteFormatTest::StringBadRange3)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: 6.." do
Crystalizer::ByteFormat.deserialize(bytes, to: ByteFormatTest::StringBadRange5)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: ...4" do
Crystalizer::ByteFormat.deserialize(bytes, to: ByteFormatTest::StringBadRange6)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: 6...8" do
Crystalizer::ByteFormat.deserialize(bytes, to: ByteFormatTest::StringBadRange7)
end
end
describe "serialization" do
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: 6.. (have: 5)" do
Crystalizer::ByteFormat.serialize(ByteFormatTest::StringBadRange1.new)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: ..4 (have: 5)" do
Crystalizer::ByteFormat.serialize(ByteFormatTest::StringBadRange2.new)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: 6..8 (have: 5)" do
Crystalizer::ByteFormat.serialize(ByteFormatTest::StringBadRange3.new)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: 6... (have: 5)" do
Crystalizer::ByteFormat.serialize(ByteFormatTest::StringBadRange5.new)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: ...4 (have: 5)" do
Crystalizer::ByteFormat.serialize(ByteFormatTest::StringBadRange6.new)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not in range: 6...8 (have: 5)" do
Crystalizer::ByteFormat.serialize(ByteFormatTest::StringBadRange7.new)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not expected, expected: 4, have: 5" do
Crystalizer::ByteFormat.serialize(ByteFormatTest::StringBadSize1.new)
end
expect_raises Crystalizer::ByteFormat::Error, message: "String bytesize not expected, expected: 6, have: 5" do
Crystalizer::ByteFormat.serialize(ByteFormatTest::StringBadSize2.new)
end
end
end
describe Symbol do
it "serializes" do
Crystalizer::ByteFormat.serialize(:a).should eq Bytes[97, 0]
end
end
describe "compiles when an object has two enum ivars" do
assert_byte_format_serialization(ObjWithEnum.new, Bytes[1, 0, 0, 0, 0, 2, 0, 0, 0])
end
describe Crystalizer::Type do
it "serializes a custom type" do
Crystalizer::ByteFormat.serialize(TestCustomTypeSerialization.new 3).should eq Bytes[3, 0, 0, 0]
end
it "deserializes a custom type" do
Crystalizer::ByteFormat.deserialize(Bytes[3, 0, 0, 0], to: TestCustomTypeSerialization).should eq TestCustomTypeSerialization.new 3
end
it "serializes a type with a custom one inside" do
Crystalizer::ByteFormat.serialize(CustomSub.new).should eq Bytes[97, 98, 99, 0, 1, 0, 0, 0]
end
it "deserializes a type with a custom one inside" do
Crystalizer::ByteFormat.deserialize(Bytes[97, 98, 99, 0, 1, 0, 0, 0], to: CustomSub).should eq CustomSub.new
end
end
end