bindata 2.4.10 → 2.5.0
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.
- checksums.yaml +4 -4
- data/ChangeLog.rdoc +39 -0
- data/LICENSE +25 -0
- data/NEWS.rdoc +5 -0
- data/README.md +6 -9
- data/bindata.gemspec +9 -4
- data/examples/NBT.txt +1 -1
- data/examples/list.rb +1 -1
- data/lib/bindata/alignment.rb +15 -7
- data/lib/bindata/array.rb +54 -54
- data/lib/bindata/base.rb +14 -25
- data/lib/bindata/base_primitive.rb +24 -20
- data/lib/bindata/bits.rb +15 -15
- data/lib/bindata/buffer.rb +89 -11
- data/lib/bindata/choice.rb +9 -6
- data/lib/bindata/count_bytes_remaining.rb +1 -1
- data/lib/bindata/delayed_io.rb +18 -10
- data/lib/bindata/dsl.rb +37 -35
- data/lib/bindata/float.rb +3 -3
- data/lib/bindata/framework.rb +8 -10
- data/lib/bindata/int.rb +14 -16
- data/lib/bindata/io.rb +276 -253
- data/lib/bindata/name.rb +1 -1
- data/lib/bindata/params.rb +9 -7
- data/lib/bindata/primitive.rb +3 -3
- data/lib/bindata/registry.rb +18 -18
- data/lib/bindata/rest.rb +1 -1
- data/lib/bindata/sanitize.rb +9 -16
- data/lib/bindata/section.rb +97 -0
- data/lib/bindata/skip.rb +140 -51
- data/lib/bindata/string.rb +9 -9
- data/lib/bindata/stringz.rb +12 -10
- data/lib/bindata/struct.rb +92 -68
- data/lib/bindata/trace.rb +35 -42
- data/lib/bindata/transform/brotli.rb +35 -0
- data/lib/bindata/transform/lz4.rb +35 -0
- data/lib/bindata/transform/lzma.rb +35 -0
- data/lib/bindata/transform/xor.rb +19 -0
- data/lib/bindata/transform/xz.rb +35 -0
- data/lib/bindata/transform/zlib.rb +33 -0
- data/lib/bindata/transform/zstd.rb +35 -0
- data/lib/bindata/uint8_array.rb +2 -2
- data/lib/bindata/version.rb +1 -1
- data/lib/bindata/virtual.rb +4 -7
- data/lib/bindata/warnings.rb +1 -1
- data/lib/bindata.rb +1 -0
- data/test/alignment_test.rb +8 -8
- data/test/array_test.rb +98 -96
- data/test/base_primitive_test.rb +47 -47
- data/test/base_test.rb +24 -24
- data/test/bits_test.rb +15 -15
- data/test/buffer_test.rb +31 -22
- data/test/choice_test.rb +32 -32
- data/test/count_bytes_remaining_test.rb +8 -8
- data/test/delayed_io_test.rb +91 -30
- data/test/float_test.rb +8 -8
- data/test/int_test.rb +14 -14
- data/test/io_test.rb +110 -302
- data/test/lazy_test.rb +38 -38
- data/test/params_test.rb +19 -19
- data/test/primitive_test.rb +26 -26
- data/test/record_test.rb +99 -99
- data/test/registry_test.rb +43 -43
- data/test/rest_test.rb +5 -5
- data/test/section_test.rb +111 -0
- data/test/skip_test.rb +71 -26
- data/test/string_test.rb +60 -60
- data/test/stringz_test.rb +34 -26
- data/test/struct_test.rb +167 -92
- data/test/system_test.rb +159 -41
- data/test/test_helper.rb +24 -13
- data/test/uint8_array_test.rb +6 -6
- data/test/virtual_test.rb +7 -7
- data/test/warnings_test.rb +14 -2
- metadata +19 -22
- data/.gitignore +0 -2
- data/.travis.yml +0 -15
- data/BSDL +0 -22
- data/COPYING +0 -52
- data/INSTALL +0 -12
- data/lib/bindata/offset.rb +0 -94
- data/test/offset_test.rb +0 -100
data/test/system_test.rb
CHANGED
@@ -11,18 +11,18 @@ describe "lambdas with index" do
|
|
11
11
|
arr = BinData::Array.new(type:
|
12
12
|
[:uint8, { value: -> { index * 10 } }],
|
13
13
|
initial_length: 3)
|
14
|
-
arr.snapshot.must_equal [0, 10, 20]
|
14
|
+
_(arr.snapshot).must_equal [0, 10, 20]
|
15
15
|
end
|
16
16
|
|
17
17
|
it "uses index of nearest containing array" do
|
18
18
|
arr = BinData::Array.new(type: :nested_lambda_with_index,
|
19
19
|
initial_length: 3)
|
20
|
-
arr.snapshot.must_equal [{a: 0}, {a: 10}, {a: 20}]
|
20
|
+
_(arr.snapshot).must_equal [{a: 0}, {a: 10}, {a: 20}]
|
21
21
|
end
|
22
22
|
|
23
23
|
it "fails if there is no containing array" do
|
24
24
|
obj = NestedLambdaWithIndex.new
|
25
|
-
|
25
|
+
_ { obj.a.to_s }.must_raise NoMethodError
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -39,7 +39,7 @@ describe "lambdas with parent" do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
obj = TestLambdaWithoutParent.new
|
42
|
-
obj.x.b.must_equal 5
|
42
|
+
_(obj.x.b).must_equal 5
|
43
43
|
end
|
44
44
|
|
45
45
|
it "accesses parent's parent when parent is specified" do
|
@@ -54,7 +54,7 @@ describe "lambdas with parent" do
|
|
54
54
|
end
|
55
55
|
|
56
56
|
obj = TestLambdaWithParent.new
|
57
|
-
obj.x.b.must_equal 3
|
57
|
+
_(obj.x.b).must_equal 3
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -82,18 +82,18 @@ describe BinData::Record, "with choice field" do
|
|
82
82
|
it "treats choice object transparently " do
|
83
83
|
obj = RecordWithChoiceField.new
|
84
84
|
|
85
|
-
obj.x.a.must_equal 3
|
85
|
+
_(obj.x.a).must_equal 3
|
86
86
|
end
|
87
87
|
|
88
88
|
it "treats nested choice object transparently " do
|
89
89
|
obj = RecordWithNestedChoiceField.new
|
90
90
|
|
91
|
-
obj.x.a.must_equal 3
|
91
|
+
_(obj.x.a).must_equal 3
|
92
92
|
end
|
93
93
|
|
94
94
|
it "has correct offset" do
|
95
95
|
obj = RecordWithNestedChoiceField.new
|
96
|
-
obj.x.b.abs_offset.must_equal 2
|
96
|
+
_(obj.x.b.abs_offset).must_equal 2
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -132,17 +132,17 @@ describe BinData::Record, "containing bitfields" do
|
|
132
132
|
let(:obj) { BitfieldRecord.new }
|
133
133
|
|
134
134
|
it "has correct num_bytes" do
|
135
|
-
obj.num_bytes.must_equal 5
|
135
|
+
_(obj.num_bytes).must_equal 5
|
136
136
|
end
|
137
137
|
|
138
138
|
it "reads across bitfield boundaries" do
|
139
139
|
obj.read [0b0111_0010, 0b0110_0101, 0b0010_1010, 0b1000_0101, 0b1000_0000].pack("CCCCC")
|
140
140
|
|
141
|
-
obj.a.w.must_equal 7
|
142
|
-
obj.b.must_equal [0, 0, 1, 0, 0, 1, 1, 0, 0]
|
143
|
-
obj.c.x.must_equal 2
|
144
|
-
obj.d.must_equal 954
|
145
|
-
obj.e.must_equal 11
|
141
|
+
_(obj.a.w).must_equal 7
|
142
|
+
_(obj.b).must_equal [0, 0, 1, 0, 0, 1, 1, 0, 0]
|
143
|
+
_(obj.c.x).must_equal 2
|
144
|
+
_(obj.d).must_equal 954
|
145
|
+
_(obj.e).must_equal 11
|
146
146
|
end
|
147
147
|
|
148
148
|
it "writes across bitfield boundaries" do
|
@@ -152,37 +152,37 @@ describe BinData::Record, "containing bitfields" do
|
|
152
152
|
obj.c.x = 1
|
153
153
|
obj.d = 850
|
154
154
|
obj.e = 35
|
155
|
-
obj.to_binary_s.must_equal_binary [0b0011_0010, 0b0100_0011, 0b0000_1010, 0b0001_0001, 0b1000_0000].pack("CCCCC")
|
155
|
+
_(obj.to_binary_s).must_equal_binary [0b0011_0010, 0b0100_0011, 0b0000_1010, 0b0001_0001, 0b1000_0000].pack("CCCCC")
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
159
159
|
describe "Objects with debug_name" do
|
160
160
|
it "haves default name of obj" do
|
161
161
|
el = BinData::Uint8.new
|
162
|
-
el.debug_name.must_equal "obj"
|
162
|
+
_(el.debug_name).must_equal "obj"
|
163
163
|
end
|
164
164
|
|
165
165
|
it "includes array index" do
|
166
166
|
arr = BinData::Array.new(type: :uint8, initial_length: 2)
|
167
|
-
arr[2].debug_name.must_equal "obj[2]"
|
167
|
+
_(arr[2].debug_name).must_equal "obj[2]"
|
168
168
|
end
|
169
169
|
|
170
170
|
it "includes field name" do
|
171
171
|
s = BinData::Struct.new(fields: [[:uint8, :a]])
|
172
|
-
s.a.debug_name.must_equal "obj.a"
|
172
|
+
_(s.a.debug_name).must_equal "obj.a"
|
173
173
|
end
|
174
174
|
|
175
175
|
it "delegates to choice" do
|
176
176
|
choice_params = {choices: [:uint8], selection: 0}
|
177
177
|
s = BinData::Struct.new(fields: [[:choice, :a, choice_params]])
|
178
|
-
s.a.debug_name.must_equal "obj.a"
|
178
|
+
_(s.a.debug_name).must_equal "obj.a"
|
179
179
|
end
|
180
180
|
|
181
181
|
it "nests" do
|
182
182
|
nested_struct_params = {fields: [[:uint8, :c]]}
|
183
183
|
struct_params = {fields: [[:struct, :b, nested_struct_params]]}
|
184
184
|
s = BinData::Struct.new(fields: [[:struct, :a, struct_params]])
|
185
|
-
s.a.b.c.debug_name.must_equal "obj.a.b.c"
|
185
|
+
_(s.a.b.c.debug_name).must_equal "obj.a.b.c"
|
186
186
|
end
|
187
187
|
end
|
188
188
|
|
@@ -194,7 +194,7 @@ describe "Tracing" do
|
|
194
194
|
BinData::trace_reading(io) { arr.read("\x01\x02\x03\x04\x05") }
|
195
195
|
|
196
196
|
expected = (0..4).collect { |i| "obj[#{i}] => #{i + 1}\n" }.join("")
|
197
|
-
io.value.must_equal expected
|
197
|
+
_(io.value).must_equal expected
|
198
198
|
end
|
199
199
|
|
200
200
|
it "traces custom single values" do
|
@@ -209,7 +209,7 @@ describe "Tracing" do
|
|
209
209
|
io = StringIO.new
|
210
210
|
BinData::trace_reading(io) { obj.read("\x01") }
|
211
211
|
|
212
|
-
io.value.must_equal ["obj-internal-.ex => 1\n", "obj => 1\n"].join("")
|
212
|
+
_(io.value).must_equal ["obj-internal-.ex => 1\n", "obj => 1\n"].join("")
|
213
213
|
end
|
214
214
|
|
215
215
|
it "traces choice selection" do
|
@@ -218,7 +218,7 @@ describe "Tracing" do
|
|
218
218
|
io = StringIO.new
|
219
219
|
BinData::trace_reading(io) { obj.read("\x01") }
|
220
220
|
|
221
|
-
io.value.must_equal ["obj-selection- => 0\n", "obj => 1\n"].join("")
|
221
|
+
_(io.value).must_equal ["obj-selection- => 0\n", "obj => 1\n"].join("")
|
222
222
|
end
|
223
223
|
|
224
224
|
it "trims long trace values" do
|
@@ -227,7 +227,20 @@ describe "Tracing" do
|
|
227
227
|
io = StringIO.new
|
228
228
|
BinData::trace_reading(io) { obj.read("0000000000111111111122222222223333333333") }
|
229
229
|
|
230
|
-
io.value.must_equal "obj => \"000000000011111111112222222222...\n"
|
230
|
+
_(io.value).must_equal "obj => \"000000000011111111112222222222...\n"
|
231
|
+
end
|
232
|
+
|
233
|
+
it "can be nested" do
|
234
|
+
obj = BinData::String.new(read_length: 5)
|
235
|
+
|
236
|
+
io = StringIO.new
|
237
|
+
BinData::trace_reading(io) {
|
238
|
+
BinData::trace_reading(io) {
|
239
|
+
obj.read("12345")
|
240
|
+
}
|
241
|
+
}
|
242
|
+
|
243
|
+
_(io.value).must_equal "obj => \"12345\"\n"
|
231
244
|
end
|
232
245
|
end
|
233
246
|
|
@@ -240,17 +253,17 @@ describe "Forward referencing with Primitive" do
|
|
240
253
|
let(:obj) { FRPrimitive.new }
|
241
254
|
|
242
255
|
it "initialises" do
|
243
|
-
obj.snapshot.must_equal({len: 0, data: ""})
|
256
|
+
_(obj.snapshot).must_equal({len: 0, data: ""})
|
244
257
|
end
|
245
258
|
|
246
259
|
it "reads" do
|
247
260
|
obj.read("\x04test")
|
248
|
-
obj.snapshot.must_equal({len: 4, data: "test"})
|
261
|
+
_(obj.snapshot).must_equal({len: 4, data: "test"})
|
249
262
|
end
|
250
263
|
|
251
264
|
it "sets value" do
|
252
265
|
obj.data = "hello"
|
253
|
-
obj.snapshot.must_equal({len: 5, data: "hello"})
|
266
|
+
_(obj.snapshot).must_equal({len: 5, data: "hello"})
|
254
267
|
end
|
255
268
|
end
|
256
269
|
|
@@ -263,17 +276,17 @@ describe "Forward referencing with Array" do
|
|
263
276
|
let(:obj) { FRArray.new }
|
264
277
|
|
265
278
|
it "initialises" do
|
266
|
-
obj.snapshot.must_equal({len: 0, data: []})
|
279
|
+
_(obj.snapshot).must_equal({len: 0, data: []})
|
267
280
|
end
|
268
281
|
|
269
282
|
it "reads" do
|
270
283
|
obj.read("\x04\x01\x02\x03\x04")
|
271
|
-
obj.snapshot.must_equal({len: 4, data: [1, 2, 3, 4]})
|
284
|
+
_(obj.snapshot).must_equal({len: 4, data: [1, 2, 3, 4]})
|
272
285
|
end
|
273
286
|
|
274
287
|
it "sets value" do
|
275
288
|
obj.data = [1, 2, 3]
|
276
|
-
obj.snapshot.must_equal({len: 3, data: [1, 2, 3]})
|
289
|
+
_(obj.snapshot).must_equal({len: 3, data: [1, 2, 3]})
|
277
290
|
end
|
278
291
|
end
|
279
292
|
|
@@ -288,7 +301,7 @@ describe "Evaluating custom parameters" do
|
|
288
301
|
|
289
302
|
it "recursively evaluates parameter" do
|
290
303
|
obj = CustomParameterRecord.new(zz: 5)
|
291
|
-
obj.c.eval_parameter(:custom).must_equal 5
|
304
|
+
_(obj.c.eval_parameter(:custom)).must_equal 5
|
292
305
|
end
|
293
306
|
end
|
294
307
|
|
@@ -299,7 +312,7 @@ describe BinData::Record, "with custom sized integers" do
|
|
299
312
|
|
300
313
|
it "reads as expected" do
|
301
314
|
str = "\x00\x00\x00\x00\x05"
|
302
|
-
CustomIntRecord.read(str).snapshot.must_equal({a: 5})
|
315
|
+
_(CustomIntRecord.read(str).snapshot).must_equal({a: 5})
|
303
316
|
end
|
304
317
|
end
|
305
318
|
|
@@ -330,11 +343,11 @@ describe BinData::Primitive, "representing a string" do
|
|
330
343
|
let(:obj) { PascalStringPrimitive.new("testing") }
|
331
344
|
|
332
345
|
it "compares to regexp" do
|
333
|
-
(obj =~ /es/).must_equal 1
|
346
|
+
_((obj =~ /es/)).must_equal 1
|
334
347
|
end
|
335
348
|
|
336
349
|
it "compares to regexp" do
|
337
|
-
(/es/ =~ obj).must_equal 1
|
350
|
+
_((/es/ =~ obj)).must_equal 1
|
338
351
|
end
|
339
352
|
end
|
340
353
|
|
@@ -347,17 +360,17 @@ describe BinData::Record, "with boolean parameters" do
|
|
347
360
|
|
348
361
|
it "uses default parameter" do
|
349
362
|
obj = BooleanParameterRecord.new
|
350
|
-
obj.a.must_equal 2
|
363
|
+
_(obj.a).must_equal 2
|
351
364
|
end
|
352
365
|
|
353
366
|
it "overrides parameter" do
|
354
367
|
obj = BooleanParameterRecord.new(flag: false)
|
355
|
-
obj.a.must_equal 3
|
368
|
+
_(obj.a).must_equal 3
|
356
369
|
end
|
357
370
|
|
358
371
|
it "overrides parameter with same value" do
|
359
372
|
obj = BooleanParameterRecord.new(flag: true)
|
360
|
-
obj.a.must_equal 2
|
373
|
+
_(obj.a).must_equal 2
|
361
374
|
end
|
362
375
|
end
|
363
376
|
|
@@ -372,25 +385,130 @@ describe BinData::Record, "encoding" do
|
|
372
385
|
|
373
386
|
it "returns binary encoded data" do
|
374
387
|
obj = EncodingTestBufferRecord.new(num: 3)
|
375
|
-
obj.to_binary_s.encoding.must_equal Encoding::ASCII_8BIT
|
388
|
+
_(obj.to_binary_s.encoding).must_equal Encoding::ASCII_8BIT
|
376
389
|
end
|
377
390
|
|
378
391
|
it "returns binary encoded data with utf-8 string" do
|
379
392
|
obj = EncodingTestBufferRecord.new(num: 3, str: "日本語")
|
380
|
-
obj.to_binary_s.encoding.must_equal Encoding::ASCII_8BIT
|
393
|
+
_(obj.to_binary_s.encoding).must_equal Encoding::ASCII_8BIT
|
381
394
|
end
|
382
395
|
|
383
396
|
it "returns binary encoded data despite Encoding.default_internal" do
|
384
|
-
w, $-w = $-w,
|
397
|
+
w, $-w = $-w, nil
|
385
398
|
before_enc = Encoding.default_internal
|
386
399
|
|
387
400
|
begin
|
388
401
|
Encoding.default_internal = Encoding::UTF_8
|
389
402
|
obj = EncodingTestBufferRecord.new(num: 3, str: "日本語")
|
390
|
-
obj.to_binary_s.encoding.must_equal Encoding::ASCII_8BIT
|
403
|
+
_(obj.to_binary_s.encoding).must_equal Encoding::ASCII_8BIT
|
391
404
|
ensure
|
392
405
|
Encoding.default_internal = before_enc
|
393
406
|
$-w = w
|
394
407
|
end
|
395
408
|
end
|
396
409
|
end
|
410
|
+
|
411
|
+
describe BinData::Record, "buffer num_bytes" do
|
412
|
+
class BufferNumBytesRecord < BinData::Record
|
413
|
+
buffer :b, length: 10 do
|
414
|
+
int8 :a
|
415
|
+
count_bytes_remaining :nbytes
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
it "counts bytes remaining in the buffer" do
|
420
|
+
obj = BufferNumBytesRecord.read "12345678901234567890"
|
421
|
+
_(obj.b.nbytes).must_equal 9
|
422
|
+
end
|
423
|
+
|
424
|
+
it "counts bytes remaining in the buffer with short streams" do
|
425
|
+
obj = BufferNumBytesRecord.read "12345"
|
426
|
+
_(obj.b.nbytes).must_equal 4
|
427
|
+
end
|
428
|
+
|
429
|
+
it "assumes buffer is full with non-seekable short streams" do
|
430
|
+
rd, wr = IO::pipe
|
431
|
+
io = BinData::IO::Read.new(rd)
|
432
|
+
wr.write "12345"
|
433
|
+
wr.close
|
434
|
+
|
435
|
+
obj = BufferNumBytesRecord.read(io)
|
436
|
+
_(obj.b.nbytes).must_equal 9
|
437
|
+
rd.close
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
describe BinData::Buffer, "with seek_abs" do
|
442
|
+
class BufferSkipRecord < BinData::Record
|
443
|
+
endian :little
|
444
|
+
mandatory_parameter :seek_offset
|
445
|
+
|
446
|
+
uint8
|
447
|
+
buffer :buf, length: 5 do
|
448
|
+
uint8
|
449
|
+
uint8
|
450
|
+
skip to_abs_offset: :seek_offset
|
451
|
+
uint8 :a
|
452
|
+
end
|
453
|
+
uint8
|
454
|
+
end
|
455
|
+
|
456
|
+
let(:str) { "\001\002\003\004\005\006\007" }
|
457
|
+
|
458
|
+
## TODO: enable this if we decide to allow backwards seeking
|
459
|
+
#backwards_seeking = false
|
460
|
+
#
|
461
|
+
#it "won't seek backwards before buffer" do
|
462
|
+
# skip unless backwards_seeking
|
463
|
+
# _ { BufferSkipRecord.new(seek_offset: 0).read(str) }.must_raise(IOError)
|
464
|
+
#end
|
465
|
+
#
|
466
|
+
#it "seeks backwards to start of buffer" do
|
467
|
+
# skip unless backwards_seeking
|
468
|
+
# obj = BufferSkipRecord.new(seek_offset: 1).read(str)
|
469
|
+
# _(obj.buf.a).must_equal 2
|
470
|
+
#end
|
471
|
+
#
|
472
|
+
#it "seeks backwards inside buffer" do
|
473
|
+
# skip unless backwards_seeking
|
474
|
+
# obj = BufferSkipRecord.new(seek_offset: 2).read(str)
|
475
|
+
# _(obj.buf.a).must_equal 3
|
476
|
+
#end
|
477
|
+
|
478
|
+
it "seeks forwards inside buffer" do
|
479
|
+
obj = BufferSkipRecord.new(seek_offset: 4).read(str)
|
480
|
+
_(obj.buf.a).must_equal 5
|
481
|
+
end
|
482
|
+
|
483
|
+
it "seeks to end of buffer" do
|
484
|
+
obj = BufferSkipRecord.new(seek_offset: 5).read(str)
|
485
|
+
_(obj.buf.a).must_equal 6
|
486
|
+
end
|
487
|
+
|
488
|
+
it "won't seek after buffer" do
|
489
|
+
_ { BufferSkipRecord.new(seek_offset: 6).read(str) }.must_raise(IOError)
|
490
|
+
end
|
491
|
+
end
|
492
|
+
|
493
|
+
|
494
|
+
describe BinData::Record, "buffered readahead" do
|
495
|
+
class BufferedReadaheadRecord < BinData::Record
|
496
|
+
buffer :a, length: 5 do
|
497
|
+
skip do
|
498
|
+
string read_length: 1, assert: "X"
|
499
|
+
end
|
500
|
+
string :b, read_length: 1
|
501
|
+
end
|
502
|
+
string :c, read_length: 1
|
503
|
+
end
|
504
|
+
|
505
|
+
it "reads ahead inside the buffer" do
|
506
|
+
obj = BufferedReadaheadRecord.read "12X4567890"
|
507
|
+
_(obj.a.b).must_equal "X"
|
508
|
+
_(obj.c).must_equal "6"
|
509
|
+
end
|
510
|
+
|
511
|
+
it "doesn't readahead outside the buffer" do
|
512
|
+
_ { BufferedReadaheadRecord.read "123456X890" }.must_raise IOError
|
513
|
+
end
|
514
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start do
|
5
|
+
enable_coverage :branch
|
6
|
+
end
|
5
7
|
|
6
8
|
require 'minitest/autorun'
|
7
9
|
require 'stringio'
|
@@ -30,32 +32,41 @@ module Kernel
|
|
30
32
|
def value_read_from_written
|
31
33
|
self.class.read(self.to_binary_s)
|
32
34
|
end
|
35
|
+
end
|
33
36
|
|
34
|
-
|
35
|
-
|
37
|
+
module Minitest::Assertions
|
38
|
+
def assert_equals_binary(expected, actual)
|
39
|
+
assert_equal expected.dup.force_encoding(Encoding::BINARY), actual
|
36
40
|
end
|
37
41
|
|
38
|
-
def
|
39
|
-
ex =
|
40
|
-
ex.message
|
42
|
+
def assert_raises_on_line(exp, line, msg = nil, &block)
|
43
|
+
ex = assert_raises(exp, &block)
|
44
|
+
assert_equal(msg, ex.message) if msg
|
41
45
|
|
42
|
-
idx = ex.backtrace.find_index { |bt| /:in `
|
46
|
+
idx = ex.backtrace.find_index { |bt| /:in `assert_raises_on_line'$/ =~ bt }
|
43
47
|
|
44
48
|
line_num_regex = /.*:(\d+)(:.*|$)/
|
45
49
|
err_line = line_num_regex.match(ex.backtrace[0])[1].to_i
|
46
|
-
ref_line = line_num_regex.match(ex.backtrace[idx +
|
50
|
+
ref_line = line_num_regex.match(ex.backtrace[idx + 2])[1].to_i
|
47
51
|
|
48
|
-
(err_line - ref_line)
|
52
|
+
assert_equal((err_line - ref_line), line)
|
49
53
|
end
|
50
54
|
|
51
|
-
def
|
55
|
+
def assert_warns(msg, &block)
|
52
56
|
result = ""
|
53
57
|
callable = proc { |str|
|
54
58
|
result = str
|
55
59
|
}
|
56
|
-
|
60
|
+
Kernel.stub(:warn, callable) do
|
57
61
|
block.call
|
58
62
|
end
|
59
|
-
|
63
|
+
|
64
|
+
assert_equal msg, result
|
60
65
|
end
|
61
66
|
end
|
67
|
+
|
68
|
+
module Minitest::Expectations
|
69
|
+
infect_an_assertion :assert_equals_binary, :must_equal_binary
|
70
|
+
infect_an_assertion :assert_raises_on_line, :must_raise_on_line, :block
|
71
|
+
infect_an_assertion :assert_warns, :must_warn, :block
|
72
|
+
end
|
data/test/uint8_array_test.rb
CHANGED
@@ -5,17 +5,17 @@ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
|
|
5
5
|
describe BinData::Uint8Array, "when initialising" do
|
6
6
|
it "with mutually exclusive parameters :initial_length and :read_until" do
|
7
7
|
params = {initial_length: 5, read_until: :eof}
|
8
|
-
|
8
|
+
_ { BinData::Uint8Array.new(params) }.must_raise ArgumentError
|
9
9
|
end
|
10
10
|
|
11
11
|
it "with :read_until" do
|
12
12
|
params = {read_until: :not_eof}
|
13
|
-
|
13
|
+
_ { BinData::Uint8Array.new(params) }.must_raise ArgumentError
|
14
14
|
end
|
15
15
|
|
16
16
|
it "with no parameters" do
|
17
17
|
arr = BinData::Uint8Array.new
|
18
|
-
arr.num_bytes.must_equal 0
|
18
|
+
_(arr.num_bytes).must_equal 0
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -23,7 +23,7 @@ describe BinData::Uint8Array, "with :read_until" do
|
|
23
23
|
it "reads until :eof" do
|
24
24
|
arr = BinData::Uint8Array.new(read_until: :eof)
|
25
25
|
arr.read("\xff\xfe\xfd\xfc")
|
26
|
-
arr.must_equal([255, 254, 253, 252])
|
26
|
+
_(arr).must_equal([255, 254, 253, 252])
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -31,7 +31,7 @@ describe BinData::Uint8Array, "with :initial_length" do
|
|
31
31
|
it "reads" do
|
32
32
|
arr = BinData::Uint8Array.new(initial_length: 3)
|
33
33
|
arr.read("\xff\xfe\xfd\xfc")
|
34
|
-
arr.must_equal([255, 254, 253])
|
34
|
+
_(arr).must_equal([255, 254, 253])
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -39,6 +39,6 @@ describe BinData::Uint8Array, "when assigning" do
|
|
39
39
|
it "copies data" do
|
40
40
|
arr = BinData::Uint8Array.new
|
41
41
|
arr.assign([1, 2, 3])
|
42
|
-
arr.must_equal([1, 2, 3])
|
42
|
+
_(arr).must_equal([1, 2, 3])
|
43
43
|
end
|
44
44
|
end
|
data/test/virtual_test.rb
CHANGED
@@ -7,17 +7,17 @@ describe BinData::Virtual do
|
|
7
7
|
|
8
8
|
it "must not read from any stream" do
|
9
9
|
BinData::Virtual.read(stream)
|
10
|
-
stream.pos.must_equal 0
|
10
|
+
_(stream.pos).must_equal 0
|
11
11
|
end
|
12
12
|
|
13
13
|
it "must not write to a stream" do
|
14
14
|
obj = BinData::Virtual.new
|
15
|
-
obj.to_binary_s.must_equal_binary ""
|
15
|
+
_(obj.to_binary_s).must_equal_binary ""
|
16
16
|
end
|
17
17
|
|
18
18
|
it "occupies no space" do
|
19
19
|
obj = BinData::Virtual.new
|
20
|
-
obj.num_bytes.must_equal 0
|
20
|
+
_(obj.num_bytes).must_equal 0
|
21
21
|
end
|
22
22
|
|
23
23
|
it "asserts on #read" do
|
@@ -25,7 +25,7 @@ describe BinData::Virtual do
|
|
25
25
|
obj = BinData::Virtual.new(assert: -> { data << 1; true })
|
26
26
|
|
27
27
|
obj.read ""
|
28
|
-
data.must_equal [1]
|
28
|
+
_(data).must_equal [1]
|
29
29
|
end
|
30
30
|
|
31
31
|
it "asserts on #assign" do
|
@@ -33,16 +33,16 @@ describe BinData::Virtual do
|
|
33
33
|
obj = BinData::Virtual.new(assert: -> { data << 1; true })
|
34
34
|
|
35
35
|
obj.assign("foo")
|
36
|
-
data.must_equal [1]
|
36
|
+
_(data).must_equal [1]
|
37
37
|
end
|
38
38
|
|
39
39
|
it "assigns a value" do
|
40
40
|
obj = BinData::Virtual.new(3)
|
41
|
-
obj.must_equal 3
|
41
|
+
_(obj).must_equal 3
|
42
42
|
end
|
43
43
|
|
44
44
|
it "accepts the :value parameter" do
|
45
45
|
obj = BinData::Virtual.new(value: 3)
|
46
|
-
obj.must_equal 3
|
46
|
+
_(obj).must_equal 3
|
47
47
|
end
|
48
48
|
end
|
data/test/warnings_test.rb
CHANGED
@@ -10,11 +10,23 @@ describe BinData::Base, "when defining" do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
_ {
|
14
14
|
BaseWithInitialize.new
|
15
15
|
}.must_raise RuntimeError
|
16
16
|
end
|
17
17
|
|
18
|
+
it "fails if #initialize is overridden on BasePrimitive" do
|
19
|
+
class BasePrimitiveWithInitialize < BinData::String
|
20
|
+
def initialize(params = {}, parent = nil)
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
_ {
|
26
|
+
BasePrimitiveWithInitialize.new
|
27
|
+
}.must_raise RuntimeError
|
28
|
+
end
|
29
|
+
|
18
30
|
it "handles if #initialize is naively renamed to #initialize_instance" do
|
19
31
|
class BaseWithInitializeInstance < BinData::Base
|
20
32
|
def initialize_instance(params = {}, parent = nil)
|
@@ -22,7 +34,7 @@ describe BinData::Base, "when defining" do
|
|
22
34
|
end
|
23
35
|
end
|
24
36
|
|
25
|
-
|
37
|
+
_ {
|
26
38
|
BaseWithInitializeInstance.new
|
27
39
|
}.must_raise RuntimeError
|
28
40
|
end
|