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/struct_test.rb
CHANGED
@@ -5,14 +5,14 @@ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
|
|
5
5
|
describe BinData::Struct, "when initializing" do
|
6
6
|
it "fails on non registered types" do
|
7
7
|
params = {fields: [[:non_registered_type, :a]]}
|
8
|
-
|
8
|
+
_ {
|
9
9
|
BinData::Struct.new(params)
|
10
10
|
}.must_raise BinData::UnRegisteredTypeError
|
11
11
|
end
|
12
12
|
|
13
13
|
it "fails on duplicate names" do
|
14
14
|
params = {fields: [[:int8, :a], [:int8, :b], [:int8, :a]]}
|
15
|
-
|
15
|
+
_ {
|
16
16
|
BinData::Struct.new(params)
|
17
17
|
}.must_raise NameError
|
18
18
|
end
|
@@ -20,21 +20,21 @@ describe BinData::Struct, "when initializing" do
|
|
20
20
|
it "fails on reserved names" do
|
21
21
|
# note that #invert is from Hash.instance_methods
|
22
22
|
params = {fields: [[:int8, :a], [:int8, :invert]]}
|
23
|
-
|
23
|
+
_ {
|
24
24
|
BinData::Struct.new(params)
|
25
25
|
}.must_raise NameError
|
26
26
|
end
|
27
27
|
|
28
28
|
it "fails when field name shadows an existing method" do
|
29
29
|
params = {fields: [[:int8, :object_id]]}
|
30
|
-
|
30
|
+
_ {
|
31
31
|
BinData::Struct.new(params)
|
32
32
|
}.must_raise NameError
|
33
33
|
end
|
34
34
|
|
35
35
|
it "fails on unknown endian" do
|
36
36
|
params = {endian: 'bad value', fields: []}
|
37
|
-
|
37
|
+
_ {
|
38
38
|
BinData::Struct.new(params)
|
39
39
|
}.must_raise ArgumentError
|
40
40
|
end
|
@@ -45,24 +45,65 @@ describe BinData::Struct, "with anonymous fields" do
|
|
45
45
|
params = { fields: [
|
46
46
|
[:int8, :a, {initial_value: 5}],
|
47
47
|
[:int8, nil],
|
48
|
-
[:int8, '', {value: :a}]
|
48
|
+
[:int8, '', {value: :a}],
|
49
|
+
[:int8, :b]
|
49
50
|
] }
|
50
51
|
BinData::Struct.new(params)
|
51
52
|
}
|
52
53
|
|
53
54
|
it "only shows non anonymous fields" do
|
54
|
-
obj.field_names.must_equal [:a]
|
55
|
+
_(obj.field_names).must_equal [:a, :b]
|
55
56
|
end
|
56
57
|
|
57
58
|
it "does not include anonymous fields in snapshot" do
|
58
|
-
obj.a =
|
59
|
-
obj.snapshot.must_equal({a:
|
59
|
+
obj.a = 6
|
60
|
+
_(obj.snapshot).must_equal({a: 6, b: 0})
|
60
61
|
end
|
61
62
|
|
62
63
|
it "writes anonymous fields" do
|
63
|
-
obj.read("\001\002\003")
|
64
|
+
obj.read("\001\002\003\004")
|
64
65
|
obj.a.clear
|
65
|
-
obj.to_binary_s.must_equal_binary "\005\002\005"
|
66
|
+
_(obj.to_binary_s).must_equal_binary "\005\002\005\004"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "does not include anonymous fields in each_pair" do
|
70
|
+
_(obj.each_pair.count).must_equal 2
|
71
|
+
end
|
72
|
+
|
73
|
+
it "includes anonymous fields in each_pair when specified" do
|
74
|
+
_(obj.each_pair(true).count).must_equal 4
|
75
|
+
end
|
76
|
+
|
77
|
+
it "clears" do
|
78
|
+
obj.b = 3
|
79
|
+
refute obj.clear?
|
80
|
+
|
81
|
+
obj.clear
|
82
|
+
assert obj.clear?
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe BinData::Struct, "with onlyif fields" do
|
87
|
+
let(:obj) {
|
88
|
+
params = { fields: [
|
89
|
+
[:int8, :a, {initial_value: 2}],
|
90
|
+
[:int8, :b, {onlyif: -> { a.odd? }}]
|
91
|
+
] }
|
92
|
+
BinData::Struct.new(params)
|
93
|
+
}
|
94
|
+
|
95
|
+
it "#fieldnames includes all fields" do
|
96
|
+
_(obj.field_names).must_equal [:a, :b]
|
97
|
+
end
|
98
|
+
|
99
|
+
it "#snapshot checks for onlyif" do
|
100
|
+
_(obj.snapshot.keys).must_equal [:a]
|
101
|
+
end
|
102
|
+
|
103
|
+
it "includes fields when required" do
|
104
|
+
refute obj.b?
|
105
|
+
obj.a = 3
|
106
|
+
assert obj.b?
|
66
107
|
end
|
67
108
|
end
|
68
109
|
|
@@ -78,24 +119,24 @@ describe BinData::Struct, "with hidden fields" do
|
|
78
119
|
}
|
79
120
|
|
80
121
|
it "only shows fields that aren't hidden" do
|
81
|
-
obj.field_names.must_equal [:a, :d]
|
122
|
+
_(obj.field_names).must_equal [:a, :d]
|
82
123
|
end
|
83
124
|
|
84
125
|
it "shows all fields when requested" do
|
85
|
-
obj.field_names(true).must_equal [:a, :b, :c, :d]
|
126
|
+
_(obj.field_names(true)).must_equal [:a, :b, :c, :d]
|
86
127
|
end
|
87
128
|
|
88
129
|
it "accesses hidden fields directly" do
|
89
|
-
obj.b.must_equal 5
|
130
|
+
_(obj.b).must_equal 5
|
90
131
|
obj.c = 15
|
91
|
-
obj.c.must_equal 15
|
132
|
+
_(obj.c).must_equal 15
|
92
133
|
|
93
|
-
obj.must_respond_to :b=
|
134
|
+
_(obj).must_respond_to :b=
|
94
135
|
end
|
95
136
|
|
96
137
|
it "does not include hidden fields in snapshot" do
|
97
138
|
obj.b = 7
|
98
|
-
obj.snapshot.must_equal({a: 0, d: 7})
|
139
|
+
_(obj.snapshot).must_equal({a: 0, d: 7})
|
99
140
|
end
|
100
141
|
|
101
142
|
it "detects hidden fields with has_key?" do
|
@@ -107,19 +148,19 @@ describe BinData::Struct, "with multiple fields" do
|
|
107
148
|
let(:params) { { fields: [ [:int8, :a], [:int8, :b] ] } }
|
108
149
|
let(:obj) { BinData::Struct.new({a: 1, b: 2}, params) }
|
109
150
|
|
110
|
-
specify { obj.field_names.must_equal [:a, :b] }
|
111
|
-
specify { obj.to_binary_s.must_equal_binary "\x01\x02" }
|
151
|
+
specify { _(obj.field_names).must_equal [:a, :b] }
|
152
|
+
specify { _(obj.to_binary_s).must_equal_binary "\x01\x02" }
|
112
153
|
|
113
154
|
it "returns num_bytes" do
|
114
|
-
obj.a.num_bytes.must_equal 1
|
115
|
-
obj.b.num_bytes.must_equal 1
|
116
|
-
obj.num_bytes.must_equal 2
|
155
|
+
_(obj.a.num_bytes).must_equal 1
|
156
|
+
_(obj.b.num_bytes).must_equal 1
|
157
|
+
_(obj.num_bytes).must_equal 2
|
117
158
|
end
|
118
159
|
|
119
160
|
it "identifies accepted parameters" do
|
120
|
-
BinData::Struct.accepted_parameters.all.must_include :fields
|
121
|
-
BinData::Struct.accepted_parameters.all.must_include :hide
|
122
|
-
BinData::Struct.accepted_parameters.all.must_include :endian
|
161
|
+
_(BinData::Struct.accepted_parameters.all).must_include :fields
|
162
|
+
_(BinData::Struct.accepted_parameters.all).must_include :hide
|
163
|
+
_(BinData::Struct.accepted_parameters.all).must_include :endian
|
123
164
|
end
|
124
165
|
|
125
166
|
it "clears" do
|
@@ -137,16 +178,22 @@ describe BinData::Struct, "with multiple fields" do
|
|
137
178
|
end
|
138
179
|
|
139
180
|
it "reads elements dynamically" do
|
140
|
-
obj[:a].must_equal 1
|
181
|
+
_(obj[:a]).must_equal 1
|
182
|
+
end
|
183
|
+
|
184
|
+
it "handles nonexistent elements" do
|
185
|
+
_(obj[:does_not_exist]).must_be_nil
|
141
186
|
end
|
142
187
|
|
143
|
-
it "
|
144
|
-
obj
|
188
|
+
it "ignores setting nonexistent elements" do
|
189
|
+
snap = obj.snapshot
|
190
|
+
obj[:does_not_exist] = 3
|
191
|
+
_(obj.snapshot).must_equal snap
|
145
192
|
end
|
146
193
|
|
147
194
|
it "writes elements dynamically" do
|
148
195
|
obj[:a] = 2
|
149
|
-
obj.a.must_equal 2
|
196
|
+
_(obj.a).must_equal 2
|
150
197
|
end
|
151
198
|
|
152
199
|
it "implements has_key?" do
|
@@ -156,28 +203,28 @@ describe BinData::Struct, "with multiple fields" do
|
|
156
203
|
it "reads ordered" do
|
157
204
|
obj.read("\x03\x04")
|
158
205
|
|
159
|
-
obj.a.must_equal 3
|
160
|
-
obj.b.must_equal 4
|
206
|
+
_(obj.a).must_equal 3
|
207
|
+
_(obj.b).must_equal 4
|
161
208
|
end
|
162
209
|
|
163
210
|
it "returns a snapshot" do
|
164
211
|
snap = obj.snapshot
|
165
212
|
assert snap.respond_to?(:a)
|
166
|
-
snap.a.must_equal 1
|
167
|
-
snap.b.must_equal 2
|
168
|
-
snap.must_equal({ a: 1, b: 2 })
|
213
|
+
_(snap.a).must_equal 1
|
214
|
+
_(snap.b).must_equal 2
|
215
|
+
_(snap).must_equal({ a: 1, b: 2 })
|
169
216
|
end
|
170
217
|
|
171
218
|
it "assigns from partial hash" do
|
172
219
|
obj.assign(a: 3)
|
173
|
-
obj.a.must_equal 3
|
174
|
-
obj.b.must_equal 0
|
220
|
+
_(obj.a).must_equal 3
|
221
|
+
_(obj.b).must_equal 0
|
175
222
|
end
|
176
223
|
|
177
224
|
it "assigns from hash" do
|
178
225
|
obj.assign(a: 3, b: 4)
|
179
|
-
obj.a.must_equal 3
|
180
|
-
obj.b.must_equal 4
|
226
|
+
_(obj.a).must_equal 3
|
227
|
+
_(obj.b).must_equal 4
|
181
228
|
end
|
182
229
|
|
183
230
|
it "assigns from nil" do
|
@@ -191,8 +238,8 @@ describe BinData::Struct, "with multiple fields" do
|
|
191
238
|
src.b = 4
|
192
239
|
|
193
240
|
obj.assign(src)
|
194
|
-
obj.a.must_equal 3
|
195
|
-
obj.b.must_equal 4
|
241
|
+
_(obj.a).must_equal 3
|
242
|
+
_(obj.b).must_equal 4
|
196
243
|
end
|
197
244
|
|
198
245
|
it "assigns from snapshot" do
|
@@ -201,29 +248,43 @@ describe BinData::Struct, "with multiple fields" do
|
|
201
248
|
src.b = 4
|
202
249
|
|
203
250
|
obj.assign(src.snapshot)
|
204
|
-
obj.a.must_equal 3
|
205
|
-
obj.b.must_equal 4
|
251
|
+
_(obj.a).must_equal 3
|
252
|
+
_(obj.b).must_equal 4
|
206
253
|
end
|
207
254
|
|
208
255
|
it "fails on unknown method call" do
|
209
|
-
|
256
|
+
_ { obj.does_not_exist }.must_raise NoMethodError
|
210
257
|
end
|
211
258
|
|
212
259
|
describe "#snapshot" do
|
213
260
|
it "has ordered #keys" do
|
214
|
-
obj.snapshot.keys.must_equal [:a, :b]
|
261
|
+
_(obj.snapshot.keys).must_equal [:a, :b]
|
215
262
|
end
|
216
263
|
|
217
264
|
it "has ordered #each" do
|
218
265
|
keys = []
|
219
266
|
obj.snapshot.each { |el| keys << el[0] }
|
220
|
-
keys.must_equal [:a, :b]
|
267
|
+
_(keys).must_equal [:a, :b]
|
221
268
|
end
|
222
269
|
|
223
270
|
it "has ordered #each_pair" do
|
224
271
|
keys = []
|
225
272
|
obj.snapshot.each_pair { |k, v| keys << k }
|
226
|
-
keys.must_equal [:a, :b]
|
273
|
+
_(keys).must_equal [:a, :b]
|
274
|
+
end
|
275
|
+
|
276
|
+
it "accesses field" do
|
277
|
+
_(obj.snapshot[:a]).must_equal 1
|
278
|
+
end
|
279
|
+
|
280
|
+
it "fails on unknown method call" do
|
281
|
+
_ { obj.snapshot.does_not_exist }.must_raise NoMethodError
|
282
|
+
end
|
283
|
+
|
284
|
+
it "will not set attribute with nil value" do
|
285
|
+
refute obj.snapshot.key?(:foo)
|
286
|
+
obj.snapshot[:foo] = nil
|
287
|
+
refute obj.snapshot.key?(:foo)
|
227
288
|
end
|
228
289
|
end
|
229
290
|
end
|
@@ -243,27 +304,27 @@ describe BinData::Struct, "with nested structs" do
|
|
243
304
|
BinData::Struct.new(params)
|
244
305
|
}
|
245
306
|
|
246
|
-
specify { obj.field_names.must_equal [:a, :b, :c] }
|
307
|
+
specify { _(obj.field_names).must_equal [:a, :b, :c] }
|
247
308
|
|
248
309
|
it "returns num_bytes" do
|
249
|
-
obj.b.num_bytes.must_equal 2
|
250
|
-
obj.c.num_bytes.must_equal 2
|
251
|
-
obj.num_bytes.must_equal 5
|
310
|
+
_(obj.b.num_bytes).must_equal 2
|
311
|
+
_(obj.c.num_bytes).must_equal 2
|
312
|
+
_(obj.num_bytes).must_equal 5
|
252
313
|
end
|
253
314
|
|
254
315
|
it "accesses nested fields" do
|
255
|
-
obj.a.must_equal 6
|
256
|
-
obj.b.w.must_equal 3
|
257
|
-
obj.b.x.must_equal 6
|
258
|
-
obj.c.y.must_equal 3
|
259
|
-
obj.c.z.must_equal 0
|
316
|
+
_(obj.a).must_equal 6
|
317
|
+
_(obj.b.w).must_equal 3
|
318
|
+
_(obj.b.x).must_equal 6
|
319
|
+
_(obj.c.y).must_equal 3
|
320
|
+
_(obj.c.z).must_equal 0
|
260
321
|
end
|
261
322
|
|
262
323
|
it "returns correct abs_offset" do
|
263
|
-
obj.b.abs_offset.must_equal 1
|
264
|
-
obj.b.w.abs_offset.must_equal 1
|
265
|
-
obj.c.abs_offset.must_equal 3
|
266
|
-
obj.c.z.abs_offset.must_equal 4
|
324
|
+
_(obj.b.abs_offset).must_equal 1
|
325
|
+
_(obj.b.w.abs_offset).must_equal 1
|
326
|
+
_(obj.c.abs_offset).must_equal 3
|
327
|
+
_(obj.c.z.abs_offset).must_equal 4
|
267
328
|
end
|
268
329
|
end
|
269
330
|
|
@@ -300,7 +361,7 @@ describe BinData::Struct, "with an endian defined" do
|
|
300
361
|
|
301
362
|
expected = [1, 2.0, 3, 4, 5, 6, 7, 8].pack('veCCVvNv')
|
302
363
|
|
303
|
-
obj.to_binary_s.must_equal_binary expected
|
364
|
+
_(obj.to_binary_s).must_equal_binary expected
|
304
365
|
end
|
305
366
|
end
|
306
367
|
|
@@ -310,23 +371,23 @@ describe BinData::Struct, "with bit fields" do
|
|
310
371
|
BinData::Struct.new({a: 1, b: 2, c: 3, d: 1}, params)
|
311
372
|
}
|
312
373
|
|
313
|
-
specify { obj.num_bytes.must_equal 3 }
|
314
|
-
specify { obj.to_binary_s.must_equal_binary [0b0000_0101, 3, 1].pack("C*") }
|
374
|
+
specify { _(obj.num_bytes).must_equal 3 }
|
375
|
+
specify { _(obj.to_binary_s).must_equal_binary [0b0000_0101, 3, 1].pack("C*") }
|
315
376
|
|
316
377
|
it "reads" do
|
317
378
|
str = [0b0000_0110, 5, 0].pack("C*")
|
318
379
|
obj.read(str)
|
319
|
-
obj.a.must_equal 0
|
320
|
-
obj.b.must_equal 3
|
321
|
-
obj.c.must_equal 5
|
322
|
-
obj.d.must_equal 0
|
380
|
+
_(obj.a).must_equal 0
|
381
|
+
_(obj.b).must_equal 3
|
382
|
+
_(obj.c).must_equal 5
|
383
|
+
_(obj.d).must_equal 0
|
323
384
|
end
|
324
385
|
|
325
386
|
it "has correct offsets" do
|
326
|
-
obj.a.rel_offset.must_equal 0
|
327
|
-
obj.b.rel_offset.must_equal 0
|
328
|
-
obj.c.rel_offset.must_equal 1
|
329
|
-
obj.d.rel_offset.must_equal 2
|
387
|
+
_(obj.a.rel_offset).must_equal 0
|
388
|
+
_(obj.b.rel_offset).must_equal 0
|
389
|
+
_(obj.c.rel_offset).must_equal 1
|
390
|
+
_(obj.d.rel_offset).must_equal 2
|
330
391
|
end
|
331
392
|
end
|
332
393
|
|
@@ -341,10 +402,10 @@ describe BinData::Struct, "with nested endian" do
|
|
341
402
|
obj = BinData::Struct.new(params)
|
342
403
|
obj.read("\x00\x01\x02\x00\x03\x00\x00\x04")
|
343
404
|
|
344
|
-
obj.a.must_equal 1
|
345
|
-
obj.s.b.must_equal 2
|
346
|
-
obj.s.c.must_equal 3
|
347
|
-
obj.d.must_equal 4
|
405
|
+
_(obj.a).must_equal 1
|
406
|
+
_(obj.s.b).must_equal 2
|
407
|
+
_(obj.s.c).must_equal 3
|
408
|
+
_(obj.d).must_equal 4
|
348
409
|
end
|
349
410
|
end
|
350
411
|
|
@@ -355,46 +416,46 @@ describe BinData::Struct, "with a search_prefix" do
|
|
355
416
|
it "searches symbol prefixes" do
|
356
417
|
obj = BinData::Struct.new(search_prefix: :a,
|
357
418
|
fields: [ [:short, :f] ])
|
358
|
-
obj.f.class.name.must_equal "AShort"
|
419
|
+
_(obj.f.class.name).must_equal "AShort"
|
359
420
|
end
|
360
421
|
|
361
422
|
it "searches string prefixes" do
|
362
423
|
obj = BinData::Struct.new(search_prefix: "a",
|
363
424
|
fields: [ [:short, :f] ])
|
364
|
-
obj.f.class.name.must_equal "AShort"
|
425
|
+
_(obj.f.class.name).must_equal "AShort"
|
365
426
|
end
|
366
427
|
|
367
428
|
it "searches string prefixes with optional underscore" do
|
368
429
|
obj = BinData::Struct.new(search_prefix: "a_",
|
369
430
|
fields: [ [:short, :f] ])
|
370
|
-
obj.f.class.name.must_equal "AShort"
|
431
|
+
_(obj.f.class.name).must_equal "AShort"
|
371
432
|
end
|
372
433
|
|
373
434
|
it "searches multiple prefixes" do
|
374
435
|
obj = BinData::Struct.new(search_prefix: [:x, :a],
|
375
436
|
fields: [ [:short, :f] ])
|
376
|
-
obj.f.class.name.must_equal "AShort"
|
437
|
+
_(obj.f.class.name).must_equal "AShort"
|
377
438
|
end
|
378
439
|
|
379
440
|
it "uses parent search_prefix" do
|
380
441
|
nested_params = { fields: [[:short, :f]] }
|
381
442
|
obj = BinData::Struct.new(search_prefix: :a,
|
382
443
|
fields: [[:struct, :s, nested_params]])
|
383
|
-
obj.s.f.class.name.must_equal "AShort"
|
444
|
+
_(obj.s.f.class.name).must_equal "AShort"
|
384
445
|
end
|
385
446
|
|
386
447
|
it "searches parent search_prefix" do
|
387
448
|
nested_params = { search_prefix: :x, fields: [[:short, :f]] }
|
388
449
|
obj = BinData::Struct.new(search_prefix: :a,
|
389
450
|
fields: [[:struct, :s, nested_params]])
|
390
|
-
obj.s.f.class.name.must_equal "AShort"
|
451
|
+
_(obj.s.f.class.name).must_equal "AShort"
|
391
452
|
end
|
392
453
|
|
393
454
|
it "prioritises nested search_prefix" do
|
394
455
|
nested_params = { search_prefix: :a, fields: [[:short, :f]] }
|
395
456
|
obj = BinData::Struct.new(search_prefix: :b,
|
396
457
|
fields: [[:struct, :s, nested_params]])
|
397
|
-
obj.s.f.class.name.must_equal "AShort"
|
458
|
+
_(obj.s.f.class.name).must_equal "AShort"
|
398
459
|
end
|
399
460
|
end
|
400
461
|
|
@@ -402,30 +463,44 @@ describe BinData::Struct, "with byte_align" do
|
|
402
463
|
let(:obj) {
|
403
464
|
params = { fields: [[:int8, :a],
|
404
465
|
[:int8, :b, byte_align: 5],
|
405
|
-
[:bit2, :c],
|
466
|
+
[:bit2, :c, onlyif: -> { a == 1}],
|
406
467
|
[:int8, :d, byte_align: 3]] }
|
407
468
|
BinData::Struct.new(params)
|
408
469
|
}
|
409
470
|
|
410
471
|
it "has #num_bytes" do
|
411
|
-
obj.num_bytes.must_equal
|
472
|
+
_(obj.num_bytes).must_equal 7
|
412
473
|
end
|
413
474
|
|
414
|
-
it "
|
475
|
+
it "has #num_bytes with onlyif" do
|
476
|
+
obj.a = 1
|
477
|
+
_(obj.num_bytes).must_equal 10
|
478
|
+
end
|
479
|
+
|
480
|
+
it "reads with onlyif" do
|
415
481
|
obj.read("\x01\x00\x00\x00\x00\x02\xc0\x00\x00\x04")
|
416
|
-
obj.snapshot.must_equal({ a: 1, b: 2, c: 3, d: 4 })
|
482
|
+
_(obj.snapshot).must_equal({ a: 1, b: 2, c: 3, d: 4 })
|
483
|
+
end
|
484
|
+
|
485
|
+
it "reads without onlyif" do
|
486
|
+
obj.read("\x00\x00\x00\x00\x00\x02\x04")
|
487
|
+
_(obj.snapshot).must_equal({ a: 0, b: 2, d: 4 })
|
417
488
|
end
|
418
489
|
|
419
|
-
it "writes" do
|
490
|
+
it "writes with onlyif" do
|
420
491
|
obj.assign(a: 1, b: 2, c: 3, d: 4)
|
421
|
-
obj.to_binary_s.must_equal_binary "\x01\x00\x00\x00\x00\x02\xc0\x00\x00\x04"
|
492
|
+
_(obj.to_binary_s).must_equal_binary "\x01\x00\x00\x00\x00\x02\xc0\x00\x00\x04"
|
493
|
+
end
|
494
|
+
|
495
|
+
it "writes without onlyif" do
|
496
|
+
obj.assign(a: 0, b: 2, c: 3, d: 4)
|
497
|
+
_(obj.to_binary_s).must_equal_binary "\x00\x00\x00\x00\x00\x02\x04"
|
422
498
|
end
|
423
499
|
|
424
500
|
it "has correct offsets" do
|
425
|
-
obj.a.rel_offset.must_equal 0
|
426
|
-
obj.b.rel_offset.must_equal 5
|
427
|
-
obj.
|
428
|
-
obj.d.rel_offset.must_equal 9
|
501
|
+
_(obj.a.rel_offset).must_equal 0
|
502
|
+
_(obj.b.rel_offset).must_equal 5
|
503
|
+
_(obj.d.rel_offset).must_equal 6
|
429
504
|
end
|
430
505
|
end
|
431
506
|
|
@@ -435,6 +510,6 @@ describe BinData::Struct, "with dynamically named types" do
|
|
435
510
|
|
436
511
|
obj = BinData::Struct.new(fields: [[:my_struct, :v]])
|
437
512
|
|
438
|
-
obj.v.a.must_equal 3
|
513
|
+
_(obj.v.a).must_equal 3
|
439
514
|
end
|
440
515
|
end
|