bindata 2.4.10 → 2.4.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ChangeLog.rdoc +19 -0
- data/lib/bindata/bits.rb +10 -10
- data/lib/bindata/delayed_io.rb +7 -0
- data/lib/bindata/dsl.rb +3 -3
- data/lib/bindata/int.rb +5 -7
- data/lib/bindata/params.rb +1 -1
- data/lib/bindata/struct.rb +9 -2
- data/lib/bindata/version.rb +1 -1
- data/test/alignment_test.rb +8 -8
- data/test/array_test.rb +88 -88
- 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 +22 -22
- data/test/choice_test.rb +31 -31
- data/test/count_bytes_remaining_test.rb +8 -8
- data/test/delayed_io_test.rb +75 -30
- data/test/float_test.rb +8 -8
- data/test/int_test.rb +14 -14
- data/test/io_test.rb +105 -105
- data/test/lazy_test.rb +38 -38
- data/test/offset_test.rb +8 -8
- 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 +42 -42
- data/test/rest_test.rb +5 -5
- data/test/skip_test.rb +17 -17
- data/test/string_test.rb +56 -56
- data/test/stringz_test.rb +26 -26
- data/test/struct_test.rb +85 -85
- data/test/system_test.rb +40 -40
- data/test/test_helper.rb +2 -2
- data/test/uint8_array_test.rb +6 -6
- data/test/virtual_test.rb +7 -7
- data/test/warnings_test.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 991ca0b6e07c8ef180244a7495796d7da21b1b27091d00b9ab6218c7208251d3
|
4
|
+
data.tar.gz: c72cdce5b44d3a5837d6172271d69ac1a56b1227945bfcb9edce592da9d822c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d53ac6de7495ae457d73b642190460adf0b5715c9b9538ad131ab8925f705df4cfa1ccf61c26361654998a9dabd32e0de33cb281954c346f8a724b5ee19249c8
|
7
|
+
data.tar.gz: '089fc03d8e7e8989553aec6b2878d71c41a2bb3ab98f4ba67c680176ff75c243afb48298b7e54abd439c1ff084433850373f5659b4edc058240387fc10a37fa5'
|
data/ChangeLog.rdoc
CHANGED
@@ -1,5 +1,24 @@
|
|
1
1
|
= BinData Changelog
|
2
2
|
|
3
|
+
== Version 2.4.14 (2022-10-31)
|
4
|
+
|
5
|
+
* Use Comparable#clamp instead of manual calculations.
|
6
|
+
* Update tests to new minitest requirements.
|
7
|
+
* Adjust tests due to changes in ruby 3.1
|
8
|
+
|
9
|
+
== Version 2.4.13 (2022-10-16)
|
10
|
+
|
11
|
+
* Relax over-strict parameter naming requirements. Requested by
|
12
|
+
vinayak3qilabs.
|
13
|
+
|
14
|
+
== Version 2.4.12 (2022-10-03)
|
15
|
+
|
16
|
+
* Do not include DelayedIO objects when :onlyif is false.
|
17
|
+
|
18
|
+
== Version 2.4.11 (2022-09-27)
|
19
|
+
|
20
|
+
* Make DelayedIO work with :onlyif. Reported by Spencer McIntyre.
|
21
|
+
|
3
22
|
== Version 2.4.10 (2021-05-18)
|
4
23
|
|
5
24
|
* Improve speed of dynamic object creation. Reported by Charlie Ablett.
|
data/lib/bindata/bits.rb
CHANGED
@@ -99,14 +99,14 @@ module BinData
|
|
99
99
|
|
100
100
|
def create_dynamic_clamp_code(signed)
|
101
101
|
if signed == :signed
|
102
|
-
max = "
|
103
|
-
min = "
|
102
|
+
max = "(1 << (nbits - 1)) - 1"
|
103
|
+
min = "-((#{max}) + 1)"
|
104
104
|
else
|
105
|
-
max = "
|
106
|
-
min = "
|
105
|
+
max = "(1 << nbits) - 1"
|
106
|
+
min = "0"
|
107
107
|
end
|
108
108
|
|
109
|
-
"
|
109
|
+
"val = val.clamp(#{min}, #{max})"
|
110
110
|
end
|
111
111
|
|
112
112
|
def create_fixed_clamp_code(nbits, signed)
|
@@ -115,14 +115,14 @@ module BinData
|
|
115
115
|
end
|
116
116
|
|
117
117
|
if signed == :signed
|
118
|
-
max = "
|
119
|
-
min = "
|
118
|
+
max = "(1 << (#{nbits} - 1)) - 1"
|
119
|
+
min = "-((#{max}) + 1)"
|
120
120
|
else
|
121
|
-
min = "
|
122
|
-
max = "
|
121
|
+
min = "0"
|
122
|
+
max = "(1 << #{nbits}) - 1"
|
123
123
|
end
|
124
124
|
|
125
|
-
clamp = "(
|
125
|
+
clamp = "(val = val.clamp(#{min}, #{max}))"
|
126
126
|
|
127
127
|
if nbits == 1
|
128
128
|
# allow single bits to be used as booleans
|
data/lib/bindata/delayed_io.rb
CHANGED
@@ -116,9 +116,14 @@ module BinData
|
|
116
116
|
0
|
117
117
|
end
|
118
118
|
|
119
|
+
def include_obj?
|
120
|
+
! has_parameter?(:onlyif) || eval_parameter(:onlyif)
|
121
|
+
end
|
122
|
+
|
119
123
|
# DelayedIO objects aren't read when #read is called.
|
120
124
|
# The reading is delayed until this method is called.
|
121
125
|
def read_now!
|
126
|
+
return unless include_obj?
|
122
127
|
raise IOError, "read from where?" unless @read_io
|
123
128
|
|
124
129
|
@read_io.seekbytes(abs_offset - @read_io.offset)
|
@@ -130,7 +135,9 @@ module BinData
|
|
130
135
|
# DelayedIO objects aren't written when #write is called.
|
131
136
|
# The writing is delayed until this method is called.
|
132
137
|
def write_now!
|
138
|
+
return unless include_obj?
|
133
139
|
raise IOError, "write to where?" unless @write_io
|
140
|
+
|
134
141
|
@write_io.seekbytes(abs_offset - @write_io.offset)
|
135
142
|
@type.do_write(@write_io)
|
136
143
|
end
|
data/lib/bindata/dsl.rb
CHANGED
@@ -420,7 +420,7 @@ module BinData
|
|
420
420
|
def ensure_valid_name(name)
|
421
421
|
if name && !option?(:fieldnames_are_values)
|
422
422
|
if malformed_name?(name)
|
423
|
-
raise
|
423
|
+
raise SyntaxError, "field '#{name}' is an illegal fieldname"
|
424
424
|
end
|
425
425
|
|
426
426
|
if duplicate_name?(name)
|
@@ -428,11 +428,11 @@ module BinData
|
|
428
428
|
end
|
429
429
|
|
430
430
|
if name_shadows_method?(name)
|
431
|
-
raise
|
431
|
+
raise SyntaxError, "field '#{name}' shadows an existing method"
|
432
432
|
end
|
433
433
|
|
434
434
|
if name_is_reserved?(name)
|
435
|
-
raise
|
435
|
+
raise SyntaxError, "field '#{name}' is a reserved name"
|
436
436
|
end
|
437
437
|
end
|
438
438
|
end
|
data/lib/bindata/int.rb
CHANGED
@@ -59,16 +59,14 @@ module BinData
|
|
59
59
|
|
60
60
|
def create_clamp_code(nbits, signed)
|
61
61
|
if signed == :signed
|
62
|
-
max = "
|
63
|
-
min = "
|
62
|
+
max = "(1 << (#{nbits} - 1)) - 1"
|
63
|
+
min = "-((#{max}) + 1)"
|
64
64
|
else
|
65
|
-
max = "
|
66
|
-
min = "
|
65
|
+
max = "(1 << #{nbits}) - 1"
|
66
|
+
min = "0"
|
67
67
|
end
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
"val = #{clamp}"
|
69
|
+
"val = val.clamp(#{min}, #{max})"
|
72
70
|
end
|
73
71
|
|
74
72
|
def create_read_code(nbits, endian, signed)
|
data/lib/bindata/params.rb
CHANGED
@@ -116,7 +116,7 @@ module BinData
|
|
116
116
|
|
117
117
|
def self.invalid_parameter_names
|
118
118
|
@invalid_names ||= begin
|
119
|
-
all_names = LazyEvaluator.instance_methods(true)
|
119
|
+
all_names = LazyEvaluator.instance_methods(true)
|
120
120
|
allowed_names = [:name, :type]
|
121
121
|
invalid_names = (all_names - allowed_names).uniq
|
122
122
|
|
data/lib/bindata/struct.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'bindata/base'
|
2
|
+
require 'bindata/delayed_io'
|
2
3
|
|
3
4
|
module BinData
|
4
5
|
|
@@ -136,12 +137,12 @@ module BinData
|
|
136
137
|
|
137
138
|
def do_read(io) #:nodoc:
|
138
139
|
instantiate_all_objs
|
139
|
-
@field_objs.each { |f| f.do_read(io) if
|
140
|
+
@field_objs.each { |f| f.do_read(io) if include_obj_for_io?(f) }
|
140
141
|
end
|
141
142
|
|
142
143
|
def do_write(io) #:nodoc
|
143
144
|
instantiate_all_objs
|
144
|
-
@field_objs.each { |f| f.do_write(io) if
|
145
|
+
@field_objs.each { |f| f.do_write(io) if include_obj_for_io?(f) }
|
145
146
|
end
|
146
147
|
|
147
148
|
def do_num_bytes #:nodoc:
|
@@ -263,6 +264,12 @@ module BinData
|
|
263
264
|
end
|
264
265
|
end
|
265
266
|
|
267
|
+
def include_obj_for_io?(obj)
|
268
|
+
# Used by #do_read and #do_write, to ensure the stream is passed to
|
269
|
+
# DelayedIO objects for delayed processing.
|
270
|
+
include_obj?(obj) || DelayedIO === obj
|
271
|
+
end
|
272
|
+
|
266
273
|
def include_obj?(obj)
|
267
274
|
!obj.has_parameter?(:onlyif) || obj.eval_parameter(:onlyif)
|
268
275
|
end
|
data/lib/bindata/version.rb
CHANGED
data/test/alignment_test.rb
CHANGED
@@ -14,20 +14,20 @@ describe BinData::ResumeByteAlignment do
|
|
14
14
|
it "resets read alignment" do
|
15
15
|
obj.read "\x12\x34"
|
16
16
|
|
17
|
-
obj.a.must_equal 1
|
18
|
-
obj.b.must_equal 3
|
17
|
+
_(obj.a).must_equal 1
|
18
|
+
_(obj.b).must_equal 3
|
19
19
|
end
|
20
20
|
|
21
21
|
it "resets write alignment" do
|
22
22
|
obj.assign(a: 2, b: 7)
|
23
23
|
|
24
|
-
obj.to_binary_s.must_equal_binary "\x20\x70"
|
24
|
+
_(obj.to_binary_s).must_equal_binary "\x20\x70"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
describe BinData::BitAligned do
|
29
29
|
it "does not apply to BinData::Primitives" do
|
30
|
-
|
30
|
+
_ {
|
31
31
|
class BitAlignedPrimitive < BinData::Primitive
|
32
32
|
bit_aligned
|
33
33
|
end
|
@@ -47,20 +47,20 @@ describe BinData::BitAligned do
|
|
47
47
|
let(:obj) { BitAlignedRecord.new }
|
48
48
|
|
49
49
|
it "#num_bytes as expected" do
|
50
|
-
obj.num_bytes.must_equal 3
|
50
|
+
_(obj.num_bytes).must_equal 3
|
51
51
|
end
|
52
52
|
|
53
53
|
it "has expected abs_offset" do
|
54
|
-
obj.str.abs_offset.must_equal 0
|
54
|
+
_(obj.str.abs_offset).must_equal 0
|
55
55
|
end
|
56
56
|
|
57
57
|
it "reads as expected" do
|
58
58
|
obj.read("\x56\x36\x42")
|
59
|
-
obj.snapshot.must_equal({preamble: 5, str: "cd", afterward: 2})
|
59
|
+
_(obj.snapshot).must_equal({preamble: 5, str: "cd", afterward: 2})
|
60
60
|
end
|
61
61
|
|
62
62
|
it "writes as expected" do
|
63
63
|
obj.assign(preamble: 5, str: "ab", afterward: 1)
|
64
|
-
obj.to_binary_s.must_equal_binary "\x56\x16\x21"
|
64
|
+
_(obj.to_binary_s).must_equal_binary "\x56\x16\x21"
|
65
65
|
end
|
66
66
|
end
|
data/test/array_test.rb
CHANGED
@@ -6,14 +6,14 @@ describe BinData::Array, "when instantiating" do
|
|
6
6
|
describe "with no mandatory parameters supplied" do
|
7
7
|
it "raises an error" do
|
8
8
|
args = {}
|
9
|
-
|
9
|
+
_ { BinData::Array.new(args) }.must_raise ArgumentError
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
describe "with some but not all mandatory parameters supplied" do
|
14
14
|
it "raises an error" do
|
15
15
|
args = {initial_length: 3}
|
16
|
-
|
16
|
+
_ { BinData::Array.new(args) }.must_raise ArgumentError
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -33,23 +33,23 @@ describe BinData::Array, "when instantiating" do
|
|
33
33
|
|
34
34
|
it "fails if a given type is unknown" do
|
35
35
|
args = {type: :does_not_exist, initial_length: 3}
|
36
|
-
|
36
|
+
_ { BinData::Array.new(args) }.must_raise BinData::UnRegisteredTypeError
|
37
37
|
end
|
38
38
|
|
39
39
|
it "fails if :initial_length is not an integer" do
|
40
40
|
args = {type: :uint8, initial_length: "3"}
|
41
|
-
|
41
|
+
_ { BinData::Array.new(args) }.must_raise ArgumentError
|
42
42
|
end
|
43
43
|
|
44
44
|
it "does not allow both :initial_length and :read_until" do
|
45
45
|
args = {initial_length: 3, read_until: -> { false } }
|
46
|
-
|
46
|
+
_ { BinData::Array.new(args) }.must_raise ArgumentError
|
47
47
|
end
|
48
48
|
|
49
49
|
it "accepts BinData::Base as :type" do
|
50
50
|
obj = BinData::Int8.new(initial_value: 5)
|
51
51
|
array = BinData::Array.new(type: obj, initial_length: 1)
|
52
|
-
array.must_equal [5]
|
52
|
+
_(array).must_equal [5]
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -58,18 +58,18 @@ describe BinData::Array, "with no elements" do
|
|
58
58
|
|
59
59
|
it "initial state" do
|
60
60
|
assert obj.clear?
|
61
|
-
obj.must_be_empty
|
62
|
-
obj.length.must_equal 0
|
63
|
-
obj.first.must_be_nil
|
64
|
-
obj.last.must_be_nil
|
61
|
+
_(obj).must_be_empty
|
62
|
+
_(obj.length).must_equal 0
|
63
|
+
_(obj.first).must_be_nil
|
64
|
+
_(obj.last).must_be_nil
|
65
65
|
end
|
66
66
|
|
67
67
|
it "returns [] for the first n elements" do
|
68
|
-
obj.first(3).must_equal []
|
68
|
+
_(obj.first(3)).must_equal []
|
69
69
|
end
|
70
70
|
|
71
71
|
it "returns [] for the last n elements" do
|
72
|
-
obj.last(3).must_equal []
|
72
|
+
_(obj.last(3)).must_equal []
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
@@ -81,69 +81,69 @@ describe BinData::Array, "with several elements" do
|
|
81
81
|
|
82
82
|
it "initial state" do
|
83
83
|
assert obj.clear?
|
84
|
-
obj.wont_be_empty
|
85
|
-
obj.size.must_equal 5
|
86
|
-
obj.length.must_equal 5
|
87
|
-
obj.snapshot.must_equal [1, 2, 3, 4, 5]
|
88
|
-
obj.inspect.must_equal "[1, 2, 3, 4, 5]"
|
84
|
+
_(obj).wont_be_empty
|
85
|
+
_(obj.size).must_equal 5
|
86
|
+
_(obj.length).must_equal 5
|
87
|
+
_(obj.snapshot).must_equal [1, 2, 3, 4, 5]
|
88
|
+
_(obj.inspect).must_equal "[1, 2, 3, 4, 5]"
|
89
89
|
end
|
90
90
|
|
91
91
|
it "coerces to ::Array if required" do
|
92
|
-
[0].concat(obj).must_equal [0, 1, 2, 3, 4, 5]
|
92
|
+
_([0].concat(obj)).must_equal [0, 1, 2, 3, 4, 5]
|
93
93
|
end
|
94
94
|
|
95
95
|
it "uses methods from Enumerable" do
|
96
|
-
obj.select { |x| (x % 2) == 0 }.must_equal [2, 4]
|
96
|
+
_(obj.select { |x| (x % 2) == 0 }).must_equal [2, 4]
|
97
97
|
end
|
98
98
|
|
99
99
|
it "assigns primitive values" do
|
100
100
|
obj.assign([4, 5, 6])
|
101
|
-
obj.must_equal [4, 5, 6]
|
101
|
+
_(obj).must_equal [4, 5, 6]
|
102
102
|
end
|
103
103
|
|
104
104
|
it "assigns bindata objects" do
|
105
105
|
obj.assign([BinData::Uint32le.new(4), BinData::Uint32le.new(5), BinData::Uint32le.new(6)])
|
106
|
-
obj.must_equal [4, 5, 6]
|
106
|
+
_(obj).must_equal [4, 5, 6]
|
107
107
|
end
|
108
108
|
|
109
109
|
it "assigns a bindata array" do
|
110
110
|
array = BinData::Array.new([4, 5, 6], type: :uint32le)
|
111
111
|
obj.assign(array)
|
112
|
-
obj.must_equal [4, 5, 6]
|
112
|
+
_(obj).must_equal [4, 5, 6]
|
113
113
|
end
|
114
114
|
|
115
115
|
it "returns the first element" do
|
116
|
-
obj.first.must_equal 1
|
116
|
+
_(obj.first).must_equal 1
|
117
117
|
end
|
118
118
|
|
119
119
|
it "returns the first n elements" do
|
120
|
-
obj[0...3].must_equal [1, 2, 3]
|
121
|
-
obj.first(3).must_equal [1, 2, 3]
|
122
|
-
obj.first(99).must_equal [1, 2, 3, 4, 5]
|
120
|
+
_(obj[0...3]).must_equal [1, 2, 3]
|
121
|
+
_(obj.first(3)).must_equal [1, 2, 3]
|
122
|
+
_(obj.first(99)).must_equal [1, 2, 3, 4, 5]
|
123
123
|
end
|
124
124
|
|
125
125
|
it "returns the last element" do
|
126
|
-
obj.last.must_equal 5
|
127
|
-
obj[-1].must_equal 5
|
126
|
+
_(obj.last).must_equal 5
|
127
|
+
_(obj[-1]).must_equal 5
|
128
128
|
end
|
129
129
|
|
130
130
|
it "returns the last n elements" do
|
131
|
-
obj.last(3).must_equal [3, 4, 5]
|
132
|
-
obj.last(99).must_equal [1, 2, 3, 4, 5]
|
131
|
+
_(obj.last(3)).must_equal [3, 4, 5]
|
132
|
+
_(obj.last(99)).must_equal [1, 2, 3, 4, 5]
|
133
133
|
|
134
|
-
obj[-3, 100].must_equal [3, 4, 5]
|
134
|
+
_(obj[-3, 100]).must_equal [3, 4, 5]
|
135
135
|
end
|
136
136
|
|
137
137
|
it "clears all" do
|
138
138
|
obj[1] = 8
|
139
139
|
obj.clear
|
140
|
-
obj.must_equal [1, 2, 3, 4, 5]
|
140
|
+
_(obj).must_equal [1, 2, 3, 4, 5]
|
141
141
|
end
|
142
142
|
|
143
143
|
it "clears a single element" do
|
144
144
|
obj[1] = 8
|
145
145
|
obj[1].clear
|
146
|
-
obj[1].must_equal 2
|
146
|
+
_(obj[1]).must_equal 2
|
147
147
|
end
|
148
148
|
|
149
149
|
it "is clear if all elements are clear" do
|
@@ -160,7 +160,7 @@ describe BinData::Array, "with several elements" do
|
|
160
160
|
|
161
161
|
it "directly accesses elements" do
|
162
162
|
obj[1] = 8
|
163
|
-
obj[1].must_equal 8
|
163
|
+
_(obj[1]).must_equal 8
|
164
164
|
end
|
165
165
|
|
166
166
|
it "symmetrically reads and writes" do
|
@@ -168,34 +168,34 @@ describe BinData::Array, "with several elements" do
|
|
168
168
|
str = obj.to_binary_s
|
169
169
|
|
170
170
|
obj.clear
|
171
|
-
obj[1].must_equal 2
|
171
|
+
_(obj[1]).must_equal 2
|
172
172
|
|
173
173
|
obj.read(str)
|
174
|
-
obj[1].must_equal 8
|
174
|
+
_(obj[1]).must_equal 8
|
175
175
|
end
|
176
176
|
|
177
177
|
it "identifies index of elements" do
|
178
|
-
obj.index(3).must_equal 2
|
178
|
+
_(obj.index(3)).must_equal 2
|
179
179
|
end
|
180
180
|
|
181
181
|
it "returns nil for index of non existent element" do
|
182
|
-
obj.index(42).must_be_nil
|
182
|
+
_(obj.index(42)).must_be_nil
|
183
183
|
end
|
184
184
|
|
185
185
|
it "has correct debug name" do
|
186
|
-
obj[2].debug_name.must_equal "obj[2]"
|
186
|
+
_(obj[2].debug_name).must_equal "obj[2]"
|
187
187
|
end
|
188
188
|
|
189
189
|
it "has correct offset" do
|
190
|
-
obj[2].rel_offset.must_equal 2 * 4
|
190
|
+
_(obj[2].rel_offset).must_equal 2 * 4
|
191
191
|
end
|
192
192
|
|
193
193
|
it "has correct num_bytes" do
|
194
|
-
obj.num_bytes.must_equal 5 * 4
|
194
|
+
_(obj.num_bytes).must_equal 5 * 4
|
195
195
|
end
|
196
196
|
|
197
197
|
it "has correct num_bytes for individual elements" do
|
198
|
-
obj[0].num_bytes.must_equal 4
|
198
|
+
_(obj[0].num_bytes).must_equal 4
|
199
199
|
end
|
200
200
|
end
|
201
201
|
|
@@ -209,67 +209,67 @@ describe BinData::Array, "when accessing elements" do
|
|
209
209
|
|
210
210
|
it "inserts with positive indexes" do
|
211
211
|
obj.insert(2, 30, 40)
|
212
|
-
obj.snapshot.must_equal [1, 2, 30, 40, 3, 4, 5]
|
212
|
+
_(obj.snapshot).must_equal [1, 2, 30, 40, 3, 4, 5]
|
213
213
|
end
|
214
214
|
|
215
215
|
it "inserts with negative indexes" do
|
216
216
|
obj.insert(-2, 30, 40)
|
217
|
-
obj.snapshot.must_equal [1, 2, 3, 4, 30, 40, 5]
|
217
|
+
_(obj.snapshot).must_equal [1, 2, 3, 4, 30, 40, 5]
|
218
218
|
end
|
219
219
|
|
220
220
|
it "pushes" do
|
221
221
|
obj.push(30, 40)
|
222
|
-
obj.snapshot.must_equal [1, 2, 3, 4, 5, 30, 40]
|
222
|
+
_(obj.snapshot).must_equal [1, 2, 3, 4, 5, 30, 40]
|
223
223
|
end
|
224
224
|
|
225
225
|
it "concats" do
|
226
226
|
obj.concat([30, 40])
|
227
|
-
obj.snapshot.must_equal [1, 2, 3, 4, 5, 30, 40]
|
227
|
+
_(obj.snapshot).must_equal [1, 2, 3, 4, 5, 30, 40]
|
228
228
|
end
|
229
229
|
|
230
230
|
it "unshifts" do
|
231
231
|
obj.unshift(30, 40)
|
232
|
-
obj.snapshot.must_equal [30, 40, 1, 2, 3, 4, 5]
|
232
|
+
_(obj.snapshot).must_equal [30, 40, 1, 2, 3, 4, 5]
|
233
233
|
end
|
234
234
|
|
235
235
|
it "automatically extends on [index]" do
|
236
|
-
obj[9].must_equal 10
|
237
|
-
obj.snapshot.must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
236
|
+
_(obj[9]).must_equal 10
|
237
|
+
_(obj.snapshot).must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
238
238
|
end
|
239
239
|
|
240
240
|
it "automatically extends on []=" do
|
241
241
|
obj[9] = 30
|
242
|
-
obj.snapshot.must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 30]
|
242
|
+
_(obj.snapshot).must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 30]
|
243
243
|
end
|
244
244
|
|
245
245
|
it "automatically extends on insert" do
|
246
246
|
obj.insert(7, 30, 40)
|
247
|
-
obj.snapshot.must_equal [1, 2, 3, 4, 5, 6, 7, 30, 40]
|
247
|
+
_(obj.snapshot).must_equal [1, 2, 3, 4, 5, 6, 7, 30, 40]
|
248
248
|
end
|
249
249
|
|
250
250
|
it "does not extend on at" do
|
251
|
-
obj.at(9).must_be_nil
|
252
|
-
obj.length.must_equal 5
|
251
|
+
_(obj.at(9)).must_be_nil
|
252
|
+
_(obj.length).must_equal 5
|
253
253
|
end
|
254
254
|
|
255
255
|
it "does not extend on [start, length]" do
|
256
|
-
obj[9, 2].must_be_nil
|
257
|
-
obj.length.must_equal 5
|
256
|
+
_(obj[9, 2]).must_be_nil
|
257
|
+
_(obj.length).must_equal 5
|
258
258
|
end
|
259
259
|
|
260
260
|
it "does not extend on [range]" do
|
261
|
-
obj[9 .. 10].must_be_nil
|
262
|
-
obj.length.must_equal 5
|
261
|
+
_(obj[9 .. 10]).must_be_nil
|
262
|
+
_(obj.length).must_equal 5
|
263
263
|
end
|
264
264
|
|
265
265
|
it "raises error on bad input to []" do
|
266
|
-
|
267
|
-
|
266
|
+
_ { obj["a"] }.must_raise TypeError
|
267
|
+
_ { obj[1, "a"] }.must_raise TypeError
|
268
268
|
end
|
269
269
|
|
270
270
|
it "is unaffected by self assignment" do
|
271
271
|
obj.assign(obj)
|
272
|
-
obj.snapshot.must_equal [1, 2, 3, 4, 5]
|
272
|
+
_(obj.snapshot).must_equal [1, 2, 3, 4, 5]
|
273
273
|
end
|
274
274
|
end
|
275
275
|
|
@@ -281,7 +281,7 @@ describe BinData::Array, "with :read_until" do
|
|
281
281
|
obj = BinData::Array.new(type: :int8, read_until: read_until)
|
282
282
|
|
283
283
|
obj.read "\x01\x02\x03\x04\x05\x06\x07\x08"
|
284
|
-
obj.must_equal [1, 2, 3, 4, 5]
|
284
|
+
_(obj).must_equal [1, 2, 3, 4, 5]
|
285
285
|
end
|
286
286
|
end
|
287
287
|
|
@@ -291,7 +291,7 @@ describe BinData::Array, "with :read_until" do
|
|
291
291
|
obj = BinData::Array.new(type: :int8, read_until: read_until)
|
292
292
|
|
293
293
|
obj.read "\x01\x02\x03\x04\x05\x06\x07\x08"
|
294
|
-
obj.must_equal [1, 2, 3, 4, 5, 6, 7]
|
294
|
+
_(obj).must_equal [1, 2, 3, 4, 5, 6, 7]
|
295
295
|
end
|
296
296
|
end
|
297
297
|
|
@@ -300,20 +300,20 @@ describe BinData::Array, "with :read_until" do
|
|
300
300
|
obj = BinData::Array.new(type: :int8, read_until: :eof)
|
301
301
|
|
302
302
|
obj.read "\x01\x02\x03"
|
303
|
-
obj.must_equal [1, 2, 3]
|
303
|
+
_(obj).must_equal [1, 2, 3]
|
304
304
|
end
|
305
305
|
|
306
306
|
it "reads records until eof, ignoring partial records" do
|
307
307
|
obj = BinData::Array.new(type: :int16be, read_until: :eof)
|
308
308
|
|
309
309
|
obj.read "\x00\x01\x00\x02\x03"
|
310
|
-
obj.must_equal [1, 2]
|
310
|
+
_(obj).must_equal [1, 2]
|
311
311
|
end
|
312
312
|
|
313
313
|
it "reports exceptions" do
|
314
314
|
array_type = [:string, {read_length: -> { unknown_variable }}]
|
315
315
|
obj = BinData::Array.new(type: array_type, read_until: :eof)
|
316
|
-
|
316
|
+
_ { obj.read "\x00\x01\x00\x02\x03" }.must_raise NoMethodError
|
317
317
|
end
|
318
318
|
end
|
319
319
|
end
|
@@ -327,12 +327,12 @@ describe BinData::Array, "nested within an Array" do
|
|
327
327
|
}
|
328
328
|
|
329
329
|
it "#snapshot" do
|
330
|
-
obj.snapshot.must_equal [ [0], [0, 1], [0, 1, 2] ]
|
330
|
+
_(obj.snapshot).must_equal [ [0], [0, 1], [0, 1, 2] ]
|
331
331
|
end
|
332
332
|
|
333
333
|
it "maintains structure when reading" do
|
334
334
|
obj.read "\x04\x05\x06\x07\x08\x09"
|
335
|
-
obj.must_equal [ [4], [5, 6], [7, 8, 9] ]
|
335
|
+
_(obj).must_equal [ [4], [5, 6], [7, 8, 9] ]
|
336
336
|
end
|
337
337
|
end
|
338
338
|
|
@@ -346,12 +346,12 @@ describe BinData::Array, "subclassed" do
|
|
346
346
|
|
347
347
|
it "forwards parameters" do
|
348
348
|
obj = IntArray.new(initial_length: 7)
|
349
|
-
obj.length.must_equal 7
|
349
|
+
_(obj.length).must_equal 7
|
350
350
|
end
|
351
351
|
|
352
352
|
it "overrides default parameters" do
|
353
353
|
obj = IntArray.new(initial_length: 3, initial_element_value: 5)
|
354
|
-
obj.to_binary_s.must_equal_binary "\x00\x05\x00\x05\x00\x05"
|
354
|
+
_(obj.to_binary_s).must_equal_binary "\x00\x05\x00\x05\x00\x05"
|
355
355
|
end
|
356
356
|
end
|
357
357
|
|
@@ -361,35 +361,35 @@ describe BinData::Array, "of bits" do
|
|
361
361
|
it "reads" do
|
362
362
|
str = [0b0001_0100, 0b1000_1000].pack("CC")
|
363
363
|
obj.read(str)
|
364
|
-
obj[0].must_equal 0
|
365
|
-
obj[1].must_equal 0
|
366
|
-
obj[2].must_equal 0
|
367
|
-
obj[3].must_equal 1
|
368
|
-
obj[4].must_equal 0
|
369
|
-
obj[5].must_equal 1
|
370
|
-
obj[6].must_equal 0
|
371
|
-
obj[7].must_equal 0
|
372
|
-
obj[8].must_equal 1
|
373
|
-
obj[9].must_equal 0
|
374
|
-
obj[10].must_equal 0
|
375
|
-
obj[11].must_equal 0
|
376
|
-
obj[12].must_equal 1
|
377
|
-
obj[13].must_equal 0
|
378
|
-
obj[14].must_equal 0
|
364
|
+
_(obj[0]).must_equal 0
|
365
|
+
_(obj[1]).must_equal 0
|
366
|
+
_(obj[2]).must_equal 0
|
367
|
+
_(obj[3]).must_equal 1
|
368
|
+
_(obj[4]).must_equal 0
|
369
|
+
_(obj[5]).must_equal 1
|
370
|
+
_(obj[6]).must_equal 0
|
371
|
+
_(obj[7]).must_equal 0
|
372
|
+
_(obj[8]).must_equal 1
|
373
|
+
_(obj[9]).must_equal 0
|
374
|
+
_(obj[10]).must_equal 0
|
375
|
+
_(obj[11]).must_equal 0
|
376
|
+
_(obj[12]).must_equal 1
|
377
|
+
_(obj[13]).must_equal 0
|
378
|
+
_(obj[14]).must_equal 0
|
379
379
|
end
|
380
380
|
|
381
381
|
it "writes" do
|
382
382
|
obj[3] = 1
|
383
|
-
obj.to_binary_s.must_equal_binary [0b0001_0000, 0b0000_0000].pack("CC")
|
383
|
+
_(obj.to_binary_s).must_equal_binary [0b0001_0000, 0b0000_0000].pack("CC")
|
384
384
|
end
|
385
385
|
|
386
386
|
it "returns num_bytes" do
|
387
|
-
obj.num_bytes.must_equal 2
|
387
|
+
_(obj.num_bytes).must_equal 2
|
388
388
|
end
|
389
389
|
|
390
390
|
it "has correct offset" do
|
391
|
-
obj[7].rel_offset.must_equal 0
|
392
|
-
obj[8].rel_offset.must_equal 1
|
391
|
+
_(obj[7].rel_offset).must_equal 0
|
392
|
+
_(obj[8].rel_offset).must_equal 1
|
393
393
|
end
|
394
394
|
end
|
395
395
|
|