-
Notifications
You must be signed in to change notification settings - Fork 18
/
compact_serializable_spec.cr
332 lines (250 loc) · 9.06 KB
/
compact_serializable_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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
require "./spec_helper"
struct CMessagePackAttr1
include MessagePack::CompactSerializable
@a : Int32
@b : Int32
@c : Int32
def initialize(@a, @b, @c)
end
end
struct CMessagePackAttr2
include MessagePack::CompactSerializable
@[MessagePack::Field(id: 2)]
@a : Int32
@b : Int32
@[MessagePack::Field(id: 0)]
@c : Int32
def initialize(@a, @b, @c)
end
end
struct CMessagePackAttr3
include MessagePack::CompactSerializable
@[MessagePack::Field(id: 5)]
@a : Int32
@[MessagePack::Field(id: 3)]
@b : Int32
@[MessagePack::Field(id: 1)]
@c : Int32
def initialize(@a, @b, @c)
end
end
@[MessagePack::CompactSerializable::Options(variables: 8)]
struct CMessagePackAttr4
include MessagePack::CompactSerializable
@[MessagePack::Field(id: 5)]
@a : Int32
@[MessagePack::Field(id: 3)]
@b : Int32
@[MessagePack::Field(id: 1)]
@c : Int32
def initialize(@a, @b, @c)
end
end
# ============ with ignore ==============================
struct CMessagePackAttr5
include MessagePack::CompactSerializable
@a : Int32
@[MessagePack::Field(ignore: true)]
@b = 9
@c : Int32
def initialize(@a, @b, @c)
end
end
struct CMessagePackAttr6
include MessagePack::CompactSerializable
@[MessagePack::Field(id: 2)]
@a : Int32
@[MessagePack::Field(ignore: true)]
@b = 9
@[MessagePack::Field(id: 1)]
@c : Int32
def initialize(@a, @b, @c)
end
end
struct CMessagePackAttr7
include MessagePack::CompactSerializable
@[MessagePack::Field(id: 2)]
@a : Int32? = nil
@[MessagePack::Field(ignore: true)]
@b = 9
@[MessagePack::Field(id: 1)]
@c : Int32
def initialize(@a, @b, @c)
end
end
# ========== strict ===========
struct CMessagePackAttr8
include MessagePack::CompactSerializable
include MessagePack::CompactSerializable::Strict
@a : Int32
@b : Int32
@c : Int32
def initialize(@a, @b, @c)
end
end
@[MessagePack::CompactSerializable::Options(variables: 8)]
struct CMessagePackAttr9
include MessagePack::CompactSerializable
include MessagePack::CompactSerializable::Strict
@a : Int32
@b : Int32
@[MessagePack::Field(id: 7)]
@c : Int32
def initialize(@a, @b, @c)
end
end
# ========== unmapped ===========
struct CMessagePackAttr10
include MessagePack::CompactSerializable
include MessagePack::CompactSerializable::Unmapped
@a : Int32
@b : Int32
@c : Int32
def initialize(@a, @b, @c)
end
end
struct CMessagePackAttr11
include MessagePack::CompactSerializable
include MessagePack::CompactSerializable::Unmapped
@[MessagePack::Field(id: 5)]
@a : Int32
@[MessagePack::Field(id: 1)]
@b : Int32
@[MessagePack::Field(id: 3)]
@c : Int32
def initialize(@a, @b, @c)
end
end
struct CMessagePackAttr12
include MessagePack::CompactSerializable
include MessagePack::CompactSerializable::Unmapped
@[MessagePack::Field(id: 5)]
@a : Int32
@[MessagePack::Field(ignore: true)]
@b = 9
@[MessagePack::Field(id: 3)]
@c : Int32
def initialize(@a, @b, @c)
end
end
describe "MessagePack mapping" do
it "CMessagePackAttr1" do
CMessagePackAttr1.new(1, 2, 3).to_msgpack.should eq Bytes[147, 1, 2, 3]
CMessagePackAttr1.from_msgpack(Bytes[147, 1, 2, 3]).should eq CMessagePackAttr1.new(1, 2, 3)
end
it "CMessagePackAttr1 raises when data shorter" do
expect_raises(MessagePack::Error, "CompactSerializable(CMessagePackAttr1): expect array with 3 elements, but got 2 at 0") do
CMessagePackAttr1.from_msgpack(Bytes[146, 1, 2])
end
end
it "CMessagePackAttr1 raises when data not array" do
expect_raises(MessagePack::Error, "CompactSerializable(CMessagePackAttr1): unpacker expect Array type but got nil at 0") do
CMessagePackAttr1.from_msgpack(nil.to_msgpack)
end
end
it "CMessagePackAttr1 raises when got nil for not nil variable" do
expect_raises(MessagePack::Error, "unexpected token 'nil' expected INT at 3") do
CMessagePackAttr1.from_msgpack(Bytes[147, 1, 192, 3])
end
end
it "CMessagePackAttr1 allow to read after data end" do
[CMessagePackAttr1.new(1, 2, 3), CMessagePackAttr1.new(3, 4, 5)].to_msgpack.should eq Bytes[146, 147, 1, 2, 3, 147, 3, 4, 5]
Array(CMessagePackAttr1).from_msgpack(Bytes[146, 147, 1, 2, 3, 147, 3, 4, 5]).should eq [CMessagePackAttr1.new(1, 2, 3), CMessagePackAttr1.new(3, 4, 5)]
end
it "CMessagePackAttr1 allow to read after data end" do
[CMessagePackAttr1.new(1, 2, 3), CMessagePackAttr1.new(3, 4, 5)].to_msgpack.should eq Bytes[146, 147, 1, 2, 3, 147, 3, 4, 5]
Array(CMessagePackAttr1).from_msgpack(Bytes[146, 148, 1, 2, 3, 4, 148, 3, 4, 5, 6]).should eq [CMessagePackAttr1.new(1, 2, 3), CMessagePackAttr1.new(3, 4, 5)]
end
it "CMessagePackAttr2" do
CMessagePackAttr2.new(1, 2, 3).to_msgpack.should eq Bytes[147, 3, 2, 1]
CMessagePackAttr2.from_msgpack(Bytes[147, 3, 2, 1]).should eq CMessagePackAttr2.new(1, 2, 3)
end
it "CMessagePackAttr3" do
CMessagePackAttr3.new(1, 2, 3).to_msgpack.should eq Bytes[150, 192, 3, 192, 2, 192, 1]
CMessagePackAttr3.from_msgpack(Bytes[150, 192, 3, 192, 2, 192, 1]).should eq CMessagePackAttr3.new(1, 2, 3)
end
it "CMessagePackAttr3 ignore when other fields not nil" do
CMessagePackAttr3.new(1, 2, 3).to_msgpack.should eq Bytes[150, 192, 3, 192, 2, 192, 1]
CMessagePackAttr3.from_msgpack(Bytes[150, 1, 3, 1, 2, 1, 1]).should eq CMessagePackAttr3.new(1, 2, 3)
end
it "CMessagePackAttr4" do
CMessagePackAttr4.new(1, 2, 3).to_msgpack.should eq Bytes[152, 192, 3, 192, 2, 192, 1, 192, 192]
CMessagePackAttr4.from_msgpack(Bytes[152, 192, 3, 192, 2, 192, 1, 192, 192]).should eq CMessagePackAttr4.new(1, 2, 3)
end
it "CMessagePackAttr5" do
CMessagePackAttr5.new(1, 2, 3).to_msgpack.should eq Bytes[146, 1, 3]
CMessagePackAttr5.from_msgpack(Bytes[146, 1, 3]).should eq CMessagePackAttr5.new(1, 9, 3)
end
it "CMessagePackAttr6" do
CMessagePackAttr6.new(1, 2, 3).to_msgpack.should eq Bytes[147, 192, 3, 1]
CMessagePackAttr6.from_msgpack(Bytes[147, 192, 3, 1]).should eq CMessagePackAttr6.new(1, 9, 3)
end
it "CMessagePackAttr7" do
CMessagePackAttr7.new(1, 2, 3).to_msgpack.should eq Bytes[147, 192, 3, 1]
CMessagePackAttr7.from_msgpack(Bytes[147, 192, 3, 1]).should eq CMessagePackAttr7.new(1, 9, 3)
end
it "CMessagePackAttr7" do
CMessagePackAttr7.new(nil, 2, 3).to_msgpack.should eq Bytes[147, 192, 3, 192]
CMessagePackAttr7.from_msgpack(Bytes[147, 192, 3, 192]).should eq CMessagePackAttr7.new(nil, 9, 3)
end
it "CMessagePackAttr8" do
CMessagePackAttr8.new(1, 2, 3).to_msgpack.should eq Bytes[147, 1, 2, 3]
CMessagePackAttr8.from_msgpack(Bytes[147, 1, 2, 3]).should eq CMessagePackAttr8.new(1, 2, 3)
end
it "CMessagePackAttr8 raises when array is bigger" do
expect_raises(MessagePack::Error, "got array(8) bigger than expected(3) at 0") do
CMessagePackAttr8.from_msgpack(Bytes[152, 1, 2, 3, 2, 192, 1, 192, 192])
end
end
it "CMessagePackAttr9" do
CMessagePackAttr9.from_msgpack(Bytes[152, 1, 2, 3, 2, 192, 1, 192, 4]).should eq CMessagePackAttr9.new(1, 2, 4)
end
it "CMessagePackAttr9 raises when array is bigger" do
expect_raises(MessagePack::Error, "got array(9) bigger than expected(8) at 0") do
CMessagePackAttr9.from_msgpack(Bytes[153, 1, 2, 3, 2, 192, 1, 192, 4, 5])
end
end
it "CMessagePackAttr10" do
CMessagePackAttr10.new(1, 2, 3).to_msgpack.should eq Bytes[147, 1, 2, 3]
s = CMessagePackAttr10.from_msgpack(Bytes[147, 1, 2, 3])
s.should eq CMessagePackAttr10.new(1, 2, 3)
s.msgpack_unmapped.size.should eq 0
end
it "CMessagePackAttr10 extra size" do
s = CMessagePackAttr10.from_msgpack(Bytes[148, 1, 2, 3, 4])
s.@b.should eq 2
s.msgpack_unmapped.should eq({3 => 4_u8})
end
it "CMessagePackAttr11" do
CMessagePackAttr11.new(1, 2, 3).to_msgpack.should eq Bytes[150, 192, 2, 192, 3, 192, 1]
s = CMessagePackAttr11.from_msgpack(Bytes[150, 192, 2, 192, 3, 192, 1])
s.should eq CMessagePackAttr11.new(1, 2, 3)
s.msgpack_unmapped.size.should eq 0
end
it "CMessagePackAttr11 extra not nil fields" do
s = CMessagePackAttr11.from_msgpack(Bytes[150, 7, 2, 8, 3, 9, 1])
s.@b.should eq 2
s.msgpack_unmapped.should eq({0 => 7_u8, 2 => 8_u8, 4 => 9_u8})
end
it "CMessagePackAttr11 extra size" do
s = CMessagePackAttr11.from_msgpack(Bytes[152, 7, 2, 8, 3, 9, 1, 192, 10])
s.@b.should eq 2
s.msgpack_unmapped.should eq({0 => 7_u8, 2 => 8_u8, 4 => 9_u8, 7 => 10_u8})
end
it "CMessagePackAttr12" do
CMessagePackAttr12.new(1, 2, 3).to_msgpack.should eq Bytes[150, 192, 192, 192, 3, 192, 1]
s = CMessagePackAttr12.from_msgpack(Bytes[150, 192, 192, 192, 3, 192, 1])
s.@b.should eq 9
s.msgpack_unmapped.size.should eq 0
end
it "CMessagePackAttr12 extra not nil fields" do
s = CMessagePackAttr12.from_msgpack(Bytes[150, 7, 2, 8, 3, 9, 1])
s.@b.should eq 9
s.msgpack_unmapped.should eq({0 => 7_u8, 1 => 2_u8, 2 => 8_u8, 4 => 9_u8})
end
it "CMessagePackAttr12 extra size" do
s = CMessagePackAttr12.from_msgpack(Bytes[152, 7, 2, 8, 3, 9, 1, 192, 10])
s.@b.should eq 9
s.msgpack_unmapped.should eq({0 => 7_u8, 1 => 2_u8, 2 => 8_u8, 4 => 9_u8, 7 => 10_u8})
end
end