bindata 2.1.0 → 2.2.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.

Potentially problematic release.


This version of bindata might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::ResumeByteAlignment do
6
6
  class ResumeAlignmentRecord < BinData::Record
@@ -21,7 +21,7 @@ describe BinData::ResumeByteAlignment do
21
21
  it "resets write alignment" do
22
22
  obj.assign(:a => 2, :b => 7)
23
23
 
24
- obj.to_binary_s.must_equal "\x20\x70"
24
+ obj.to_binary_s.must_equal_binary "\x20\x70"
25
25
  end
26
26
  end
27
27
 
@@ -57,6 +57,6 @@ describe BinData::BitAligned do
57
57
 
58
58
  it "writes as expected" do
59
59
  obj.assign(:preamble => 5, :str => "ab", :afterward => 1)
60
- obj.to_binary_s.must_equal "\x56\x16\x21"
60
+ obj.to_binary_s.must_equal_binary "\x56\x16\x21"
61
61
  end
62
62
  end
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::Array, "when instantiating" do
6
6
  describe "with no mandatory parameters supplied" do
@@ -35,7 +35,7 @@ describe BinData::Array, "when instantiating" do
35
35
  end
36
36
 
37
37
  describe BinData::Array, "with no elements" do
38
- let(:obj) { BinData::Array.new(:type => :example_single) }
38
+ let(:obj) { BinData::Array.new(:type => :uint32le) }
39
39
 
40
40
  it "initial state" do
41
41
  assert obj.clear?
@@ -56,7 +56,7 @@ end
56
56
 
57
57
  describe BinData::Array, "with several elements" do
58
58
  let(:obj) {
59
- type = [:example_single, {:initial_value => lambda { index + 1 }}]
59
+ type = [:uint32le, {:initial_value => lambda { index + 1 }}]
60
60
  BinData::Array.new(:type => type, :initial_length => 5)
61
61
  }
62
62
 
@@ -83,12 +83,12 @@ describe BinData::Array, "with several elements" do
83
83
  end
84
84
 
85
85
  it "assigns bindata objects" do
86
- obj.assign([ExampleSingle.new(4), ExampleSingle.new(5), ExampleSingle.new(6)])
86
+ obj.assign([BinData::Uint32le.new(4), BinData::Uint32le.new(5), BinData::Uint32le.new(6)])
87
87
  obj.must_equal [4, 5, 6]
88
88
  end
89
89
 
90
90
  it "assigns a bindata array" do
91
- array = BinData::Array.new([4, 5, 6], :type => :example_single)
91
+ array = BinData::Array.new([4, 5, 6], :type => :uint32le)
92
92
  obj.assign(array)
93
93
  obj.must_equal [4, 5, 6]
94
94
  end
@@ -168,21 +168,21 @@ describe BinData::Array, "with several elements" do
168
168
  end
169
169
 
170
170
  it "has correct offset" do
171
- obj[2].rel_offset.must_equal ExampleSingle.new.num_bytes * 2
171
+ obj[2].rel_offset.must_equal 2 * 4
172
172
  end
173
173
 
174
174
  it "has correct num_bytes" do
175
- obj.num_bytes.must_equal 5 * ExampleSingle.new.num_bytes
175
+ obj.num_bytes.must_equal 5 * 4
176
176
  end
177
177
 
178
178
  it "has correct num_bytes for individual elements" do
179
- obj[0].num_bytes.must_equal ExampleSingle.new.num_bytes
179
+ obj[0].num_bytes.must_equal 4
180
180
  end
181
181
  end
182
182
 
183
183
  describe BinData::Array, "when accessing elements" do
184
184
  let(:obj) {
185
- type = [:example_single, {:initial_value => lambda { index + 1 }}]
185
+ type = [:uint32le, {:initial_value => lambda { index + 1 }}]
186
186
  data = BinData::Array.new(:type => type, :initial_length => 5)
187
187
  data.assign([1, 2, 3, 4, 5])
188
188
  data
@@ -327,7 +327,7 @@ describe BinData::Array, "subclassed" do
327
327
 
328
328
  it "overrides default parameters" do
329
329
  obj = IntArray.new(:initial_length => 3, :initial_element_value => 5)
330
- obj.to_binary_s.must_equal "\x00\x05\x00\x05\x00\x05"
330
+ obj.to_binary_s.must_equal_binary "\x00\x05\x00\x05\x00\x05"
331
331
  end
332
332
  end
333
333
 
@@ -356,7 +356,7 @@ describe BinData::Array, "of bits" do
356
356
 
357
357
  it "writes" do
358
358
  obj[3] = 1
359
- obj.to_binary_s.must_equal [0b0001_0000, 0b0000_0000].pack("CC")
359
+ obj.to_binary_s.must_equal_binary [0b0001_0000, 0b0000_0000].pack("CC")
360
360
  end
361
361
 
362
362
  it "returns num_bytes" do
@@ -1,6 +1,26 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
+
5
+ class ExampleSingle < BinData::BasePrimitive
6
+ def self.io_with_value(val)
7
+ StringIO.new([val].pack("V"))
8
+ end
9
+
10
+ private
11
+
12
+ def value_to_binary_string(val)
13
+ [val].pack("V")
14
+ end
15
+
16
+ def read_and_return_value(io)
17
+ io.readbytes(4).unpack("V").at(0)
18
+ end
19
+
20
+ def sensible_default
21
+ 0
22
+ end
23
+ end
4
24
 
5
25
  describe BinData::BasePrimitive do
6
26
  it "is not registered" do
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe "BinData::Base", "framework" do
6
6
  class FrameworkBase < BinData::Base
@@ -108,6 +108,12 @@ describe BinData::Base do
108
108
  obj.write("").must_equal obj
109
109
  end
110
110
 
111
+ it "#to_hex uses #to_binary_s representation" do
112
+ obj.stub :to_binary_s, "\x01\xab\xCD" do
113
+ obj.to_hex.must_equal "01abcd"
114
+ end
115
+ end
116
+
111
117
  it "#inspect is forwarded to snapshot" do
112
118
  obj.stub :snapshot, [1, 2, 3] do
113
119
  obj.inspect.must_equal obj.snapshot.inspect
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  module AllBitfields
6
6
 
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::Buffer, "when instantiating" do
6
6
  describe "with no mandatory parameters supplied" do
@@ -49,7 +49,7 @@ describe BinData::Buffer, "subclassed with a single type" do
49
49
 
50
50
  it "writes data" do
51
51
  obj = IntBuffer.new(3)
52
- obj.to_binary_s.must_equal "\000\003\000\000\000"
52
+ obj.to_binary_s.must_equal_binary "\000\003\000\000\000"
53
53
  end
54
54
 
55
55
  it "has total num_bytes" do
@@ -86,7 +86,7 @@ describe BinData::Buffer, "subclassed with multiple types" do
86
86
 
87
87
  it "writes data" do
88
88
  obj = TupleBuffer.new(:a => 1, :b => 2)
89
- obj.to_binary_s.must_equal "\000\001\000\002\000"
89
+ obj.to_binary_s.must_equal_binary "\000\001\000\002\000"
90
90
  end
91
91
  end
92
92
 
@@ -98,7 +98,7 @@ describe BinData::Buffer, "inside a Record" do
98
98
  buffer :list, :length => :buffer_length do
99
99
  array :type => :int16, :read_until => :eof
100
100
  end
101
- string :footer, :length => 2, :asserted_value => "ZZ"
101
+ string :footer, :read_length => 2, :asserted_value => "ZZ"
102
102
  end
103
103
 
104
104
  it "reads" do
@@ -108,7 +108,7 @@ describe BinData::Buffer, "inside a Record" do
108
108
 
109
109
  it "writes" do
110
110
  obj = BufferRecord.new(:list => [1, 2, 3, 4, 5])
111
- obj.to_binary_s.must_equal "\013\000\001\000\002\000\003\000\004\000\005\000\000ZZ"
111
+ obj.to_binary_s.must_equal_binary "\013\000\001\000\002\000\003\000\004\000\005\000\000ZZ"
112
112
  end
113
113
  end
114
114
 
@@ -138,7 +138,7 @@ describe BinData::Buffer, "nested buffers" do
138
138
  obj.a.bb = "ABCDEFGHIJ"
139
139
  obj.b = "12345"
140
140
 
141
- obj.to_binary_s.must_equal "abcdeABCDE12345"
141
+ obj.to_binary_s.must_equal_binary "abcdeABCDE12345"
142
142
  end
143
143
  end
144
144
 
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  class Chooser
6
6
  attr_accessor :choice
@@ -46,12 +46,12 @@ describe BinData::Choice, "when instantiating" do
46
46
  end
47
47
 
48
48
  it "fails when :choices Hash has a symbol as key" do
49
- args = {:choices => {:a => :example_single}, :selection => 0}
49
+ args = {:choices => {:a => :uint8}, :selection => 0}
50
50
  lambda { BinData::Choice.new(args) }.must_raise ArgumentError
51
51
  end
52
52
 
53
53
  it "fails when :choices Hash has a nil key" do
54
- args = {:choices => {nil => :example_single}, :selection => 0}
54
+ args = {:choices => {nil => :uint8}, :selection => 0}
55
55
  lambda { BinData::Choice.new(args) }.must_raise ArgumentError
56
56
  end
57
57
  end
@@ -103,7 +103,7 @@ module ChoiceInitializedWithArrayOrHash
103
103
 
104
104
  def test_delegates_methods_to_the_selected_single_choice
105
105
  obj.choice = 5
106
- obj.num_bytes.must_equal ExampleSingle.new.num_bytes
106
+ obj.num_bytes.must_equal 1
107
107
  end
108
108
  end
109
109
 
@@ -112,9 +112,9 @@ describe BinData::Choice, "with sparse choices array" do
112
112
 
113
113
  let(:obj) {
114
114
  choices = [nil, nil, nil,
115
- [:example_single, {:value => 30}], nil,
116
- [:example_single, {:value => 50}], nil,
117
- [:example_single, {:value => 70}]]
115
+ [:uint8, {:value => 30}], nil,
116
+ [:uint8, {:value => 50}], nil,
117
+ [:uint8, {:value => 70}]]
118
118
  create_choice(choices)
119
119
  }
120
120
  end
@@ -123,19 +123,16 @@ describe BinData::Choice, "with choices hash" do
123
123
  include ChoiceInitializedWithArrayOrHash
124
124
 
125
125
  let(:obj) {
126
- choices = {3 => [:example_single, {:value => 30}],
127
- 5 => [:example_single, {:value => 50}],
128
- 7 => [:example_single, {:value => 70}]}
126
+ choices = {3 => [:uint8, {:value => 30}],
127
+ 5 => [:uint8, {:value => 50}],
128
+ 7 => [:uint8, {:value => 70}]}
129
129
  create_choice(choices)
130
130
  }
131
131
  end
132
132
 
133
133
  describe BinData::Choice, "with single values" do
134
134
  let(:obj) {
135
- choices = {3 => :example_single,
136
- 5 => :example_single,
137
- 7 => :example_single}
138
- create_choice(choices)
135
+ create_choice({3 => :uint8, 5 => :uint8, 7 => :uint8})
139
136
  }
140
137
 
141
138
  it "assigns raw values" do
@@ -144,8 +141,8 @@ describe BinData::Choice, "with single values" do
144
141
  obj.must_equal 254
145
142
  end
146
143
 
147
- it "assigns Single values" do
148
- data = ExampleSingle.new(11)
144
+ it "assigns BinData values" do
145
+ data = BinData::Uint8.new(11)
149
146
 
150
147
  obj.choice = 3
151
148
  obj.assign(data)
@@ -204,9 +201,7 @@ end
204
201
 
205
202
  describe BinData::Choice, "with copy_on_change => true" do
206
203
  let(:obj) {
207
- choices = {3 => :example_single,
208
- 5 => :example_single,
209
- 7 => :example_single}
204
+ choices = {3 => :uint8, 5 => :uint8, 7 => :uint8}
210
205
  create_choice(choices, :copy_on_change => true)
211
206
  }
212
207
 
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::CountBytesRemaining do
6
6
  let(:obj) { BinData::CountBytesRemaining.new }
@@ -23,13 +23,13 @@ describe BinData::CountBytesRemaining do
23
23
  end
24
24
 
25
25
  it "does not write any data" do
26
- obj.to_binary_s.must_equal ""
26
+ obj.to_binary_s.must_equal_binary ""
27
27
  end
28
28
 
29
29
  it "allows setting value for completeness" do
30
30
  obj.assign("123")
31
31
  obj.must_equal "123"
32
- obj.to_binary_s.must_equal ""
32
+ obj.to_binary_s.must_equal_binary ""
33
33
  end
34
34
 
35
35
  it "accepts BinData::BasePrimitive parameters" do
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  module FloatTest
6
6
  def test_num_bytes
@@ -30,7 +30,7 @@ describe "A FloatLe" do
30
30
  end
31
31
 
32
32
  it "#to_binary_s" do
33
- @obj.to_binary_s.must_equal [Math::PI].pack('e')
33
+ @obj.to_binary_s.must_equal_binary [Math::PI].pack('e')
34
34
  end
35
35
  end
36
36
 
@@ -42,7 +42,7 @@ describe "A FloatBe" do
42
42
  end
43
43
 
44
44
  it "#to_binary_s" do
45
- @obj.to_binary_s.must_equal [Math::PI].pack('g')
45
+ @obj.to_binary_s.must_equal_binary [Math::PI].pack('g')
46
46
  end
47
47
  end
48
48
 
@@ -54,7 +54,7 @@ describe "A DoubleLe" do
54
54
  end
55
55
 
56
56
  it "#to_binary_s" do
57
- @obj.to_binary_s.must_equal [Math::PI].pack('E')
57
+ @obj.to_binary_s.must_equal_binary [Math::PI].pack('E')
58
58
  end
59
59
  end
60
60
 
@@ -67,6 +67,6 @@ describe "A DoubleBe" do
67
67
  end
68
68
 
69
69
  it "#to_binary_s" do
70
- @obj.to_binary_s.must_equal [Math::PI].pack('G')
70
+ @obj.to_binary_s.must_equal_binary [Math::PI].pack('G')
71
71
  end
72
72
  end
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  module AllIntegers
6
6
 
@@ -82,7 +82,7 @@ module AllIntegers
82
82
  subject = int_class.new
83
83
  subject.assign(val)
84
84
 
85
- subject.to_binary_s.must_equal int_to_binary_str(val)
85
+ subject.to_binary_s.must_equal_binary int_to_binary_str(val)
86
86
  end
87
87
  end
88
88
 
@@ -94,7 +94,7 @@ module AllIntegers
94
94
  subject = int_class.new
95
95
  subject.assign(val)
96
96
 
97
- subject.to_binary_s.must_equal int_to_binary_str(val)
97
+ subject.to_binary_s.must_equal_binary int_to_binary_str(val)
98
98
  end
99
99
  end
100
100
  end
@@ -128,7 +128,7 @@ module AllIntegers
128
128
  end
129
129
 
130
130
  def int_to_binary_str(val)
131
- str = binary("")
131
+ str = "".force_encoding(Encoding::BINARY)
132
132
  v = val & ((1 << (@nbytes * 8)) - 1)
133
133
  @nbytes.times do
134
134
  str.concat(v & 0xff)
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::IO::Read, "reading from non seekable stream" do
6
6
  before do
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  # A mock data object with customizable fields.
6
6
  class MockBinDataObject
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::Base, "offsets" do
6
6
  class ThreeByteReader < BinData::Base
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
 
6
6
  describe BinData::Base, "parameters" do
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::Primitive do
6
6
  it "is not registered" do
@@ -76,7 +76,7 @@ describe BinData::Primitive do
76
76
 
77
77
  it "produces binary string" do
78
78
  obj.assign(5)
79
- obj.to_binary_s.must_equal "\x05\x00"
79
+ obj.to_binary_s.must_equal_binary "\x05\x00"
80
80
  end
81
81
 
82
82
  it "reads value" do
@@ -86,7 +86,7 @@ describe BinData::Primitive do
86
86
 
87
87
  it "accepts standard parameters" do
88
88
  obj = PrimitiveWithEndian.new(:initial_value => 2)
89
- obj.to_binary_s.must_equal "\x02\x00"
89
+ obj.to_binary_s.must_equal_binary "\x02\x00"
90
90
  end
91
91
 
92
92
  it "returns num_bytes" do
@@ -180,12 +180,12 @@ describe BinData::Primitive, "subclassed with default parameter" do
180
180
 
181
181
  it "overrides initial_value" do
182
182
  a = ChildDerivedPrimitive.new(:initial_value => 7)
183
- a.to_binary_s.must_equal "\000\007"
183
+ a.to_binary_s.must_equal_binary "\000\007"
184
184
  end
185
185
 
186
186
  it "uses default parameter" do
187
187
  a = ChildDerivedPrimitive.new
188
- a.to_binary_s.must_equal "\000\005"
188
+ a.to_binary_s.must_equal_binary "\000\005"
189
189
  end
190
190
  end
191
191
 
@@ -205,6 +205,6 @@ describe BinData::Primitive, "with mutating #get and #set" do
205
205
  it "#to_binary_s applies mutator" do
206
206
  obj = MutatingPrimitive.new
207
207
  obj.assign(-50)
208
- obj.to_binary_s.must_equal "\062\000"
208
+ obj.to_binary_s.must_equal_binary "\062\000"
209
209
  end
210
210
  end