bindata 2.3.3 → 2.3.4
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of bindata might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/ChangeLog.rdoc +4 -0
- data/Rakefile +2 -2
- data/examples/gzip.rb +24 -24
- data/examples/ip_address.rb +3 -4
- data/examples/list.rb +20 -20
- data/examples/nbt.rb +14 -14
- data/examples/tcp_ip.rb +12 -14
- data/lib/bindata/alignment.rb +2 -2
- data/lib/bindata/array.rb +22 -23
- data/lib/bindata/base.rb +13 -12
- data/lib/bindata/base_primitive.rb +20 -17
- data/lib/bindata/bits.rb +4 -4
- data/lib/bindata/buffer.rb +5 -5
- data/lib/bindata/choice.rb +11 -13
- data/lib/bindata/count_bytes_remaining.rb +1 -2
- data/lib/bindata/delayed_io.rb +11 -11
- data/lib/bindata/dsl.rb +57 -64
- data/lib/bindata/float.rb +16 -13
- data/lib/bindata/int.rb +6 -6
- data/lib/bindata/io.rb +16 -16
- data/lib/bindata/lazy.rb +3 -3
- data/lib/bindata/name.rb +2 -2
- data/lib/bindata/offset.rb +3 -3
- data/lib/bindata/params.rb +13 -15
- data/lib/bindata/primitive.rb +3 -3
- data/lib/bindata/registry.rb +12 -12
- data/lib/bindata/rest.rb +1 -2
- data/lib/bindata/sanitize.rb +17 -18
- data/lib/bindata/skip.rb +7 -8
- data/lib/bindata/string.rb +6 -6
- data/lib/bindata/stringz.rb +3 -3
- data/lib/bindata/struct.rb +16 -16
- data/lib/bindata/trace.rb +9 -8
- data/lib/bindata/version.rb +1 -1
- data/lib/bindata/virtual.rb +3 -3
- data/lib/bindata/warnings.rb +6 -2
- data/test/alignment_test.rb +4 -4
- data/test/array_test.rb +29 -29
- data/test/base_primitive_test.rb +17 -17
- data/test/base_test.rb +6 -6
- data/test/bits_test.rb +1 -1
- data/test/buffer_test.rb +19 -19
- data/test/choice_test.rb +20 -20
- data/test/count_bytes_remaining_test.rb +1 -1
- data/test/delayed_io_test.rb +26 -26
- data/test/lazy_test.rb +16 -16
- data/test/offset_test.rb +8 -8
- data/test/params_test.rb +11 -11
- data/test/primitive_test.rb +11 -11
- data/test/record_test.rb +50 -50
- data/test/registry_test.rb +26 -26
- data/test/rest_test.rb +1 -1
- data/test/skip_test.rb +27 -27
- data/test/string_test.rb +24 -24
- data/test/stringz_test.rb +1 -1
- data/test/struct_test.rb +76 -75
- data/test/system_test.rb +51 -51
- data/test/virtual_test.rb +3 -3
- metadata +2 -2
data/lib/bindata/skip.rb
CHANGED
@@ -9,8 +9,8 @@ module BinData
|
|
9
9
|
# require 'bindata'
|
10
10
|
#
|
11
11
|
# class A < BinData::Record
|
12
|
-
# skip :
|
13
|
-
# string :a, :
|
12
|
+
# skip length: 5
|
13
|
+
# string :a, read_length: 5
|
14
14
|
# end
|
15
15
|
#
|
16
16
|
# obj = A.read("abcdefghij")
|
@@ -18,8 +18,8 @@ module BinData
|
|
18
18
|
#
|
19
19
|
#
|
20
20
|
# class B < BinData::Record
|
21
|
-
# skip :
|
22
|
-
# string :b, :
|
21
|
+
# skip until_valid: [:string, {read_length: 2, assert: "ef"} ]
|
22
|
+
# string :b, read_length: 5
|
23
23
|
# end
|
24
24
|
#
|
25
25
|
# obj = B.read("abcdefghij")
|
@@ -42,7 +42,6 @@ module BinData
|
|
42
42
|
# <tt>[type_symbol, hash_params]</tt>.
|
43
43
|
#
|
44
44
|
class Skip < BinData::BasePrimitive
|
45
|
-
|
46
45
|
arg_processor :skip
|
47
46
|
|
48
47
|
optional_parameters :length, :to_abs_offset, :until_valid
|
@@ -51,7 +50,7 @@ module BinData
|
|
51
50
|
def initialize_shared_instance
|
52
51
|
extend SkipLengthPlugin if has_parameter?(:length)
|
53
52
|
extend SkipToAbsOffsetPlugin if has_parameter?(:to_abs_offset)
|
54
|
-
extend SkipUntilValidPlugin
|
53
|
+
extend SkipUntilValidPlugin if has_parameter?(:until_valid)
|
55
54
|
super
|
56
55
|
end
|
57
56
|
|
@@ -84,8 +83,8 @@ module BinData
|
|
84
83
|
|
85
84
|
class SkipArgProcessor < BaseArgProcessor
|
86
85
|
def sanitize_parameters!(obj_class, params)
|
87
|
-
unless params.has_parameter?(:length)
|
88
|
-
params.has_parameter?(:to_abs_offset)
|
86
|
+
unless params.has_parameter?(:length) ||
|
87
|
+
params.has_parameter?(:to_abs_offset) ||
|
89
88
|
params.has_parameter?(:until_valid)
|
90
89
|
raise ArgumentError, "#{obj_class} requires either :length, :to_abs_offset or :until_valid"
|
91
90
|
end
|
data/lib/bindata/string.rb
CHANGED
@@ -8,11 +8,11 @@ module BinData
|
|
8
8
|
#
|
9
9
|
# data = "abcdefghij"
|
10
10
|
#
|
11
|
-
# obj = BinData::String.new(:
|
11
|
+
# obj = BinData::String.new(read_length: 5)
|
12
12
|
# obj.read(data)
|
13
13
|
# obj #=> "abcde"
|
14
14
|
#
|
15
|
-
# obj = BinData::String.new(:
|
15
|
+
# obj = BinData::String.new(length: 6)
|
16
16
|
# obj.read(data)
|
17
17
|
# obj #=> "abcdef"
|
18
18
|
# obj.assign("abcdefghij")
|
@@ -20,12 +20,12 @@ module BinData
|
|
20
20
|
# obj.assign("abcd")
|
21
21
|
# obj #=> "abcd\000\000"
|
22
22
|
#
|
23
|
-
# obj = BinData::String.new(:
|
23
|
+
# obj = BinData::String.new(length: 6, trim_padding: true)
|
24
24
|
# obj.assign("abcd")
|
25
25
|
# obj #=> "abcd"
|
26
26
|
# obj.to_binary_s #=> "abcd\000\000"
|
27
27
|
#
|
28
|
-
# obj = BinData::String.new(:
|
28
|
+
# obj = BinData::String.new(length: 6, pad_byte: 'A')
|
29
29
|
# obj.assign("abcd")
|
30
30
|
# obj #=> "abcdAA"
|
31
31
|
# obj.to_binary_s #=> "abcdAA"
|
@@ -52,13 +52,13 @@ module BinData
|
|
52
52
|
arg_processor :string
|
53
53
|
|
54
54
|
optional_parameters :read_length, :length, :trim_padding, :pad_front, :pad_left
|
55
|
-
default_parameters :
|
55
|
+
default_parameters pad_byte: "\0"
|
56
56
|
mutually_exclusive_parameters :read_length, :length
|
57
57
|
mutually_exclusive_parameters :length, :value
|
58
58
|
|
59
59
|
def initialize_shared_instance
|
60
60
|
if (has_parameter?(:value) || has_parameter?(:asserted_value)) &&
|
61
|
-
!
|
61
|
+
!has_parameter?(:read_length)
|
62
62
|
extend WarnNoReadLengthPlugin
|
63
63
|
end
|
64
64
|
super
|
data/lib/bindata/stringz.rb
CHANGED
@@ -52,7 +52,7 @@ module BinData
|
|
52
52
|
ch = nil
|
53
53
|
|
54
54
|
# read until zero byte or we have read in the max number of bytes
|
55
|
-
while ch != "\0"
|
55
|
+
while ch != "\0" && i != max_length
|
56
56
|
ch = io.readbytes(1)
|
57
57
|
str << ch
|
58
58
|
i += 1
|
@@ -81,14 +81,14 @@ module BinData
|
|
81
81
|
if max_length
|
82
82
|
max_length = 1 if max_length < 1
|
83
83
|
str.slice!(max_length)
|
84
|
-
if str.length == max_length
|
84
|
+
if str.length == max_length && str[-1, 1] != "\0"
|
85
85
|
str[-1, 1] = "\0"
|
86
86
|
end
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
90
|
def append_zero_byte_if_needed!(str)
|
91
|
-
if str.length == 0
|
91
|
+
if str.length == 0 || str[-1, 1] != "\0"
|
92
92
|
str << "\0"
|
93
93
|
end
|
94
94
|
end
|
data/lib/bindata/struct.rb
CHANGED
@@ -16,10 +16,10 @@ module BinData
|
|
16
16
|
# int8 :z
|
17
17
|
# end
|
18
18
|
#
|
19
|
-
# obj = BinData::Struct.new(:
|
20
|
-
# :
|
21
|
-
#
|
22
|
-
#
|
19
|
+
# obj = BinData::Struct.new(hide: :a,
|
20
|
+
# fields: [ [:int32le, :a],
|
21
|
+
# [:int16le, :b],
|
22
|
+
# [:tuple, :s] ])
|
23
23
|
# obj.field_names =># [:b, :s]
|
24
24
|
#
|
25
25
|
#
|
@@ -72,7 +72,7 @@ module BinData
|
|
72
72
|
%w{type initial_length read_until} +
|
73
73
|
%w{fields endian search_prefix hide only_if byte_align} +
|
74
74
|
%w{choices selection copy_on_change} +
|
75
|
-
%w{read_abs_offset struct_params}).collect
|
75
|
+
%w{read_abs_offset struct_params}).collect(&:to_sym).
|
76
76
|
uniq.collect { |key| [key, true] }.flatten
|
77
77
|
]
|
78
78
|
|
@@ -85,7 +85,7 @@ module BinData
|
|
85
85
|
end
|
86
86
|
|
87
87
|
def initialize_instance
|
88
|
-
@field_objs
|
88
|
+
@field_objs = []
|
89
89
|
end
|
90
90
|
|
91
91
|
def clear #:nodoc:
|
@@ -93,7 +93,7 @@ module BinData
|
|
93
93
|
end
|
94
94
|
|
95
95
|
def clear? #:nodoc:
|
96
|
-
@field_objs.all? { |f| f.nil?
|
96
|
+
@field_objs.all? { |f| f.nil? || f.clear? }
|
97
97
|
end
|
98
98
|
|
99
99
|
def assign(val)
|
@@ -159,7 +159,7 @@ module BinData
|
|
159
159
|
end
|
160
160
|
end
|
161
161
|
|
162
|
-
def
|
162
|
+
def key?(key)
|
163
163
|
@field_names.index(base_field_name(key))
|
164
164
|
end
|
165
165
|
|
@@ -228,7 +228,7 @@ module BinData
|
|
228
228
|
|
229
229
|
@field_names.compact.each do |name|
|
230
230
|
obj = find_obj_for_name(name)
|
231
|
-
if obj
|
231
|
+
if obj && src.key?(name)
|
232
232
|
obj.assign(src[name])
|
233
233
|
end
|
234
234
|
end
|
@@ -263,7 +263,7 @@ module BinData
|
|
263
263
|
end
|
264
264
|
|
265
265
|
def include_obj?(obj)
|
266
|
-
|
266
|
+
!obj.has_parameter?(:onlyif) || obj.eval_parameter(:onlyif)
|
267
267
|
end
|
268
268
|
|
269
269
|
# A hash that can be accessed via attributes.
|
@@ -273,7 +273,7 @@ module BinData
|
|
273
273
|
end
|
274
274
|
|
275
275
|
def respond_to?(symbol, include_private = false)
|
276
|
-
|
276
|
+
key?(symbol) || super
|
277
277
|
end
|
278
278
|
|
279
279
|
def method_missing(symbol, *args)
|
@@ -382,7 +382,7 @@ module BinData
|
|
382
382
|
end
|
383
383
|
|
384
384
|
def sanitize_hide(params)
|
385
|
-
if params.needs_sanitizing?(:hide)
|
385
|
+
if params.needs_sanitizing?(:hide) && params.has_parameter?(:fields)
|
386
386
|
field_names = sanitized_field_names(params[:fields])
|
387
387
|
hfield_names = hidden_field_names(params[:hide])
|
388
388
|
|
@@ -395,7 +395,7 @@ module BinData
|
|
395
395
|
end
|
396
396
|
|
397
397
|
def hidden_field_names(hidden)
|
398
|
-
(hidden || []).collect
|
398
|
+
(hidden || []).collect(&:to_sym)
|
399
399
|
end
|
400
400
|
|
401
401
|
def ensure_field_names_are_valid(obj_class, field_names)
|
@@ -403,15 +403,15 @@ module BinData
|
|
403
403
|
|
404
404
|
field_names.each do |name|
|
405
405
|
if obj_class.method_defined?(name)
|
406
|
-
raise NameError.new("Rename field '#{name}' in #{obj_class}, "
|
406
|
+
raise NameError.new("Rename field '#{name}' in #{obj_class}, " \
|
407
407
|
"as it shadows an existing method.", name)
|
408
408
|
end
|
409
409
|
if reserved_names.include?(name)
|
410
|
-
raise NameError.new("Rename field '#{name}' in #{obj_class}, "
|
410
|
+
raise NameError.new("Rename field '#{name}' in #{obj_class}, " \
|
411
411
|
"as it is a reserved name.", name)
|
412
412
|
end
|
413
413
|
if field_names.count(name) != 1
|
414
|
-
raise NameError.new("field '#{name}' in #{obj_class}, "
|
414
|
+
raise NameError.new("field '#{name}' in #{obj_class}, " \
|
415
415
|
"is defined multiple times.", name)
|
416
416
|
end
|
417
417
|
end
|
data/lib/bindata/trace.rb
CHANGED
@@ -13,7 +13,7 @@ module BinData
|
|
13
13
|
|
14
14
|
def trace_obj(obj_name, val)
|
15
15
|
if val.length > 30
|
16
|
-
val = val.slice(0
|
16
|
+
val = val.slice(0..30) + "..."
|
17
17
|
end
|
18
18
|
|
19
19
|
trace "#{obj_name} => #{val}"
|
@@ -23,20 +23,21 @@ module BinData
|
|
23
23
|
# Turn on trace information when reading a BinData object.
|
24
24
|
# If +block+ is given then the tracing only occurs for that block.
|
25
25
|
# This is useful for debugging a BinData declaration.
|
26
|
-
def trace_reading(io = STDERR
|
26
|
+
def trace_reading(io = STDERR)
|
27
27
|
@tracer = Tracer.new(io)
|
28
|
-
[BasePrimitive, Choice].each
|
28
|
+
[BasePrimitive, Choice].each(&:turn_on_tracing)
|
29
|
+
|
29
30
|
if block_given?
|
30
31
|
begin
|
31
|
-
|
32
|
+
yield
|
32
33
|
ensure
|
33
|
-
[BasePrimitive, Choice].each
|
34
|
+
[BasePrimitive, Choice].each(&:turn_off_tracing)
|
34
35
|
@tracer = nil
|
35
36
|
end
|
36
37
|
end
|
37
38
|
end
|
38
39
|
|
39
|
-
def trace_message
|
40
|
+
def trace_message #:nodoc:
|
40
41
|
yield @tracer if @tracer
|
41
42
|
end
|
42
43
|
|
@@ -60,7 +61,7 @@ module BinData
|
|
60
61
|
end
|
61
62
|
|
62
63
|
def trace_value
|
63
|
-
BinData
|
64
|
+
BinData.trace_message do |tracer|
|
64
65
|
value_string = _value.inspect
|
65
66
|
tracer.trace_obj(debug_name, value_string)
|
66
67
|
end
|
@@ -85,7 +86,7 @@ module BinData
|
|
85
86
|
end
|
86
87
|
|
87
88
|
def trace_selection
|
88
|
-
BinData
|
89
|
+
BinData.trace_message do |tracer|
|
89
90
|
selection_string = eval_parameter(:selection).inspect
|
90
91
|
tracer.trace_obj("#{debug_name}-selection-", selection_string)
|
91
92
|
end
|
data/lib/bindata/version.rb
CHANGED
data/lib/bindata/virtual.rb
CHANGED
@@ -8,9 +8,9 @@ module BinData
|
|
8
8
|
# require 'bindata'
|
9
9
|
#
|
10
10
|
# class A < BinData::Record
|
11
|
-
# string :a, :
|
12
|
-
# string :b, :
|
13
|
-
# virtual :c, :
|
11
|
+
# string :a, read_length: 5
|
12
|
+
# string :b, read_length: 5
|
13
|
+
# virtual :c, assert: -> { a == b }
|
14
14
|
# end
|
15
15
|
#
|
16
16
|
# obj = A.read("abcdeabcde")
|
data/lib/bindata/warnings.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module BinData
|
2
2
|
class Base
|
3
|
-
|
4
3
|
# Don't override initialize. If you are defining a new kind of datatype
|
5
4
|
# (list, array, choice etc) then put your initialization code in
|
6
5
|
# #initialize_instance. BinData objects might be initialized as prototypes
|
@@ -21,7 +20,7 @@ module BinData
|
|
21
20
|
end
|
22
21
|
initialize_without_warning(*args)
|
23
22
|
end
|
24
|
-
|
23
|
+
alias initialize initialize_with_warning
|
25
24
|
|
26
25
|
def initialize_instance(*args)
|
27
26
|
unless args.empty?
|
@@ -29,4 +28,9 @@ module BinData
|
|
29
28
|
end
|
30
29
|
end
|
31
30
|
end
|
31
|
+
|
32
|
+
class Struct
|
33
|
+
# has_key? is deprecated
|
34
|
+
alias has_key? key?
|
35
|
+
end
|
32
36
|
end
|
data/test/alignment_test.rb
CHANGED
@@ -19,7 +19,7 @@ describe BinData::ResumeByteAlignment do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
it "resets write alignment" do
|
22
|
-
obj.assign(:
|
22
|
+
obj.assign(a: 2, b: 7)
|
23
23
|
|
24
24
|
obj.to_binary_s.must_equal_binary "\x20\x70"
|
25
25
|
end
|
@@ -40,7 +40,7 @@ describe BinData::BitAligned do
|
|
40
40
|
|
41
41
|
class BitAlignedRecord < BinData::Record
|
42
42
|
bit4 :preamble
|
43
|
-
bit_string :str, :
|
43
|
+
bit_string :str, length: 2
|
44
44
|
bit4 :afterward
|
45
45
|
end
|
46
46
|
|
@@ -56,11 +56,11 @@ describe BinData::BitAligned do
|
|
56
56
|
|
57
57
|
it "reads as expected" do
|
58
58
|
obj.read("\x56\x36\x42")
|
59
|
-
obj.snapshot.must_equal({:
|
59
|
+
obj.snapshot.must_equal({preamble: 5, str: "cd", afterward: 2})
|
60
60
|
end
|
61
61
|
|
62
62
|
it "writes as expected" do
|
63
|
-
obj.assign(:
|
63
|
+
obj.assign(preamble: 5, str: "ab", afterward: 1)
|
64
64
|
obj.to_binary_s.must_equal_binary "\x56\x16\x21"
|
65
65
|
end
|
66
66
|
end
|
data/test/array_test.rb
CHANGED
@@ -12,49 +12,49 @@ describe BinData::Array, "when instantiating" do
|
|
12
12
|
|
13
13
|
describe "with some but not all mandatory parameters supplied" do
|
14
14
|
it "raises an error" do
|
15
|
-
args = {:
|
15
|
+
args = {initial_length: 3}
|
16
16
|
lambda { BinData::Array.new(args) }.must_raise ArgumentError
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
it "warns about :length" do
|
21
21
|
Kernel.must_warn ":length is not used with BinData::Array. You probably want to change this to :initial_length" do
|
22
|
-
obj = BinData::Array.new(:
|
22
|
+
obj = BinData::Array.new(type: :uint8, length: 3)
|
23
23
|
obj.read "123"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
it "warns about :read_length" do
|
28
28
|
Kernel.must_warn ":read_length is not used with BinData::Array. You probably want to change this to :initial_length" do
|
29
|
-
obj = BinData::Array.new(:
|
29
|
+
obj = BinData::Array.new(type: :uint8, read_length: 3)
|
30
30
|
obj.read "123"
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
it "fails if a given type is unknown" do
|
35
|
-
args = {:
|
35
|
+
args = {type: :does_not_exist, initial_length: 3}
|
36
36
|
lambda { 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
|
-
args = {:
|
40
|
+
args = {type: :uint8, initial_length: "3"}
|
41
41
|
lambda { 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
|
-
args = {:
|
45
|
+
args = {initial_length: 3, read_until: -> { false } }
|
46
46
|
lambda { BinData::Array.new(args) }.must_raise ArgumentError
|
47
47
|
end
|
48
48
|
|
49
49
|
it "accepts BinData::Base as :type" do
|
50
|
-
obj = BinData::Int8.new(:
|
51
|
-
array = BinData::Array.new(:
|
50
|
+
obj = BinData::Int8.new(initial_value: 5)
|
51
|
+
array = BinData::Array.new(type: obj, initial_length: 1)
|
52
52
|
array.must_equal [5]
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
describe BinData::Array, "with no elements" do
|
57
|
-
let(:obj) { BinData::Array.new(:
|
57
|
+
let(:obj) { BinData::Array.new(type: :uint32le) }
|
58
58
|
|
59
59
|
it "initial state" do
|
60
60
|
assert obj.clear?
|
@@ -75,8 +75,8 @@ end
|
|
75
75
|
|
76
76
|
describe BinData::Array, "with several elements" do
|
77
77
|
let(:obj) {
|
78
|
-
type = [:uint32le, {:
|
79
|
-
BinData::Array.new(:
|
78
|
+
type = [:uint32le, {initial_value: -> { index + 1 }}]
|
79
|
+
BinData::Array.new(type: type, initial_length: 5)
|
80
80
|
}
|
81
81
|
|
82
82
|
it "initial state" do
|
@@ -107,7 +107,7 @@ describe BinData::Array, "with several elements" do
|
|
107
107
|
end
|
108
108
|
|
109
109
|
it "assigns a bindata array" do
|
110
|
-
array = BinData::Array.new([4, 5, 6], :
|
110
|
+
array = BinData::Array.new([4, 5, 6], type: :uint32le)
|
111
111
|
obj.assign(array)
|
112
112
|
obj.must_equal [4, 5, 6]
|
113
113
|
end
|
@@ -201,8 +201,8 @@ end
|
|
201
201
|
|
202
202
|
describe BinData::Array, "when accessing elements" do
|
203
203
|
let(:obj) {
|
204
|
-
type = [:uint32le, {:
|
205
|
-
data = BinData::Array.new(:
|
204
|
+
type = [:uint32le, {initial_value: -> { index + 1 }}]
|
205
|
+
data = BinData::Array.new(type: type, initial_length: 5)
|
206
206
|
data.assign([1, 2, 3, 4, 5])
|
207
207
|
data
|
208
208
|
}
|
@@ -273,7 +273,7 @@ describe BinData::Array, "with :read_until" do
|
|
273
273
|
describe "containing +element+" do
|
274
274
|
it "reads until the sentinel is reached" do
|
275
275
|
read_until = lambda { element == 5 }
|
276
|
-
obj = BinData::Array.new(:
|
276
|
+
obj = BinData::Array.new(type: :int8, read_until: read_until)
|
277
277
|
|
278
278
|
obj.read "\x01\x02\x03\x04\x05\x06\x07\x08"
|
279
279
|
obj.must_equal [1, 2, 3, 4, 5]
|
@@ -283,7 +283,7 @@ describe BinData::Array, "with :read_until" do
|
|
283
283
|
describe "containing +array+ and +index+" do
|
284
284
|
it "reads until the sentinel is reached" do
|
285
285
|
read_until = lambda { index >= 2 and array[index - 2] == 5 }
|
286
|
-
obj = BinData::Array.new(:
|
286
|
+
obj = BinData::Array.new(type: :int8, read_until: read_until)
|
287
287
|
|
288
288
|
obj.read "\x01\x02\x03\x04\x05\x06\x07\x08"
|
289
289
|
obj.must_equal [1, 2, 3, 4, 5, 6, 7]
|
@@ -292,22 +292,22 @@ describe BinData::Array, "with :read_until" do
|
|
292
292
|
|
293
293
|
describe ":eof" do
|
294
294
|
it "reads records until eof" do
|
295
|
-
obj = BinData::Array.new(:
|
295
|
+
obj = BinData::Array.new(type: :int8, read_until: :eof)
|
296
296
|
|
297
297
|
obj.read "\x01\x02\x03"
|
298
298
|
obj.must_equal [1, 2, 3]
|
299
299
|
end
|
300
300
|
|
301
301
|
it "reads records until eof, ignoring partial records" do
|
302
|
-
obj = BinData::Array.new(:
|
302
|
+
obj = BinData::Array.new(type: :int16be, read_until: :eof)
|
303
303
|
|
304
304
|
obj.read "\x00\x01\x00\x02\x03"
|
305
305
|
obj.must_equal [1, 2]
|
306
306
|
end
|
307
307
|
|
308
308
|
it "reports exceptions" do
|
309
|
-
array_type = [:string, {:
|
310
|
-
obj = BinData::Array.new(:
|
309
|
+
array_type = [:string, {read_length: -> { unknown_variable }}]
|
310
|
+
obj = BinData::Array.new(type: array_type, read_until: :eof)
|
311
311
|
lambda { obj.read "\x00\x01\x00\x02\x03" }.must_raise NoMethodError
|
312
312
|
end
|
313
313
|
end
|
@@ -315,10 +315,10 @@ end
|
|
315
315
|
|
316
316
|
describe BinData::Array, "nested within an Array" do
|
317
317
|
let(:obj) {
|
318
|
-
nested_array_params = { :
|
319
|
-
:
|
320
|
-
BinData::Array.new(:
|
321
|
-
:
|
318
|
+
nested_array_params = { type: [:int8, { initial_value: :index }],
|
319
|
+
initial_length: -> { index + 1 } }
|
320
|
+
BinData::Array.new(type: [:array, nested_array_params],
|
321
|
+
initial_length: 3)
|
322
322
|
}
|
323
323
|
|
324
324
|
it "#snapshot" do
|
@@ -334,24 +334,24 @@ end
|
|
334
334
|
describe BinData::Array, "subclassed" do
|
335
335
|
class IntArray < BinData::Array
|
336
336
|
endian :big
|
337
|
-
default_parameter :
|
337
|
+
default_parameter initial_element_value: 0
|
338
338
|
|
339
|
-
uint16 :
|
339
|
+
uint16 initial_value: :initial_element_value
|
340
340
|
end
|
341
341
|
|
342
342
|
it "forwards parameters" do
|
343
|
-
obj = IntArray.new(:
|
343
|
+
obj = IntArray.new(initial_length: 7)
|
344
344
|
obj.length.must_equal 7
|
345
345
|
end
|
346
346
|
|
347
347
|
it "overrides default parameters" do
|
348
|
-
obj = IntArray.new(:
|
348
|
+
obj = IntArray.new(initial_length: 3, initial_element_value: 5)
|
349
349
|
obj.to_binary_s.must_equal_binary "\x00\x05\x00\x05\x00\x05"
|
350
350
|
end
|
351
351
|
end
|
352
352
|
|
353
353
|
describe BinData::Array, "of bits" do
|
354
|
-
let(:obj) { BinData::Array.new(:
|
354
|
+
let(:obj) { BinData::Array.new(type: :bit1, initial_length: 15) }
|
355
355
|
|
356
356
|
it "reads" do
|
357
357
|
str = [0b0001_0100, 0b1000_1000].pack("CC")
|