bindata 1.4.3 → 1.4.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.
- data/BSDL +1 -1
- data/ChangeLog +8 -0
- data/NEWS +7 -0
- data/lib/bindata.rb +3 -3
- data/lib/bindata/base.rb +7 -0
- data/lib/bindata/base_primitive.rb +2 -2
- data/lib/bindata/choice.rb +4 -0
- data/lib/bindata/io.rb +1 -1
- data/lib/bindata/lazy.rb +2 -2
- data/lib/bindata/sanitize.rb +9 -0
- data/lib/bindata/string.rb +33 -16
- data/manual.haml +55 -53
- data/manual.md +346 -253
- data/spec/alignment_spec.rb +7 -7
- data/spec/array_spec.rb +67 -66
- data/spec/base_primitive_spec.rb +47 -49
- data/spec/base_spec.rb +70 -73
- data/spec/bits_spec.rb +14 -14
- data/spec/choice_spec.rb +44 -41
- data/spec/count_bytes_remaining_spec.rb +8 -11
- data/spec/deprecated_spec.rb +29 -29
- data/spec/float_spec.rb +12 -12
- data/spec/int_spec.rb +24 -24
- data/spec/io_spec.rb +56 -56
- data/spec/lazy_spec.rb +41 -39
- data/spec/primitive_spec.rb +31 -33
- data/spec/record_spec.rb +55 -57
- data/spec/registry_spec.rb +25 -25
- data/spec/rest_spec.rb +5 -5
- data/spec/skip_spec.rb +3 -3
- data/spec/spec_common.rb +2 -2
- data/spec/string_spec.rb +38 -38
- data/spec/stringz_spec.rb +18 -18
- data/spec/struct_spec.rb +48 -48
- data/spec/system_spec.rb +38 -37
- data/spec/wrapper_spec.rb +21 -23
- data/tasks/pkg.rake +3 -3
- data/tasks/rdoc.rake +1 -1
- data/tasks/rspec.rake +7 -7
- metadata +51 -53
data/spec/base_spec.rb
CHANGED
@@ -21,31 +21,31 @@ describe BinData::Base, "all subclasses" do
|
|
21
21
|
|
22
22
|
subject { SubClassOfBase.new }
|
23
23
|
|
24
|
-
it "
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
it "raises errors on unimplemented methods" do
|
25
|
+
expect { subject.clear }.to raise_error(NotImplementedError)
|
26
|
+
expect { subject.clear? }.to raise_error(NotImplementedError)
|
27
|
+
expect { subject.assign(nil) }.to raise_error(NotImplementedError)
|
28
|
+
expect { subject.snapshot }.to raise_error(NotImplementedError)
|
29
|
+
expect { subject.do_read(nil) }.to raise_error(NotImplementedError)
|
30
|
+
expect { subject.do_write(nil) }.to raise_error(NotImplementedError)
|
31
|
+
expect { subject.do_num_bytes }.to raise_error(NotImplementedError)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
35
|
describe BinData::Base, "with parameters" do
|
36
|
-
it "
|
37
|
-
|
36
|
+
it "raises error when parameter name is invalid" do
|
37
|
+
expect {
|
38
38
|
class InvalidParameterNameBase < BinData::Base
|
39
39
|
optional_parameter :eval # i.e. Kernel#eval
|
40
40
|
end
|
41
|
-
}.
|
41
|
+
}.to raise_error(NameError)
|
42
42
|
end
|
43
43
|
|
44
|
-
it "
|
45
|
-
|
44
|
+
it "raises an error when parameter has nil value" do
|
45
|
+
expect { BaseStub.new(:a => nil) }.to raise_error(ArgumentError)
|
46
46
|
end
|
47
47
|
|
48
|
-
it "
|
48
|
+
it "converts parameter keys to symbols" do
|
49
49
|
subject = BaseStub.new('a' => 3)
|
50
50
|
subject.should have_parameter(:a)
|
51
51
|
end
|
@@ -57,18 +57,18 @@ describe BinData::Base, "with mandatory parameters" do
|
|
57
57
|
mandatory_parameter :p2
|
58
58
|
end
|
59
59
|
|
60
|
-
it "
|
60
|
+
it "ensures that all mandatory parameters are present" do
|
61
61
|
params = {:p1 => "a", :p2 => "b" }
|
62
|
-
|
62
|
+
expect { MandatoryBase.new(params) }.not_to raise_error
|
63
63
|
end
|
64
64
|
|
65
|
-
it "
|
65
|
+
it "fails when only some mandatory parameters are present" do
|
66
66
|
params = {:p1 => "a", :xx => "b" }
|
67
|
-
|
67
|
+
expect { MandatoryBase.new(params) }.to raise_error(ArgumentError)
|
68
68
|
end
|
69
69
|
|
70
|
-
it "
|
71
|
-
|
70
|
+
it "fails when no mandatory parameters are present" do
|
71
|
+
expect { MandatoryBase.new() }.to raise_error(ArgumentError)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
@@ -77,12 +77,12 @@ describe BinData::Base, "with default parameters" do
|
|
77
77
|
default_parameter :p1 => "a"
|
78
78
|
end
|
79
79
|
|
80
|
-
it "
|
80
|
+
it "uses default parameters when not specified" do
|
81
81
|
subject = DefaultBase.new
|
82
82
|
subject.eval_parameter(:p1).should == "a"
|
83
83
|
end
|
84
84
|
|
85
|
-
it "
|
85
|
+
it "can override default parameters" do
|
86
86
|
subject = DefaultBase.new(:p1 => "b")
|
87
87
|
subject.eval_parameter(:p1).should == "b"
|
88
88
|
end
|
@@ -94,17 +94,17 @@ describe BinData::Base, "with mutually exclusive parameters" do
|
|
94
94
|
mutually_exclusive_parameters :p1, :p2
|
95
95
|
end
|
96
96
|
|
97
|
-
it "
|
98
|
-
|
97
|
+
it "does not fail when neither of those parameters are present" do
|
98
|
+
expect { MutexParamBase.new }.not_to raise_error
|
99
99
|
end
|
100
100
|
|
101
|
-
it "
|
102
|
-
|
103
|
-
|
101
|
+
it "does not fail when only one of those parameters is present" do
|
102
|
+
expect { MutexParamBase.new(:p1 => "a") }.not_to raise_error
|
103
|
+
expect { MutexParamBase.new(:p2 => "b") }.not_to raise_error
|
104
104
|
end
|
105
105
|
|
106
|
-
it "
|
107
|
-
|
106
|
+
it "fails when both those parameters are present" do
|
107
|
+
expect { MutexParamBase.new(:p1 => "a", :p2 => "b") }.to raise_error(ArgumentError)
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
@@ -115,7 +115,7 @@ describe BinData::Base, "with multiple parameters" do
|
|
115
115
|
optional_parameter :p3
|
116
116
|
end
|
117
117
|
|
118
|
-
it "
|
118
|
+
it "identifies internally accepted parameters" do
|
119
119
|
accepted = WithParamBase.accepted_parameters.all
|
120
120
|
accepted.should include(:p1)
|
121
121
|
accepted.should include(:p2)
|
@@ -129,21 +129,21 @@ describe BinData::Base, "with multiple parameters" do
|
|
129
129
|
WithParamBase.new(params)
|
130
130
|
}
|
131
131
|
|
132
|
-
it "
|
132
|
+
it "evaluates parameters" do
|
133
133
|
subject.eval_parameter(:p1).should == 1
|
134
134
|
subject.eval_parameter(:p2).should == 2
|
135
|
-
|
135
|
+
expect { subject.eval_parameter(:p3) }.to raise_error(NoMethodError)
|
136
136
|
subject.eval_parameter(:p4).should == 4
|
137
137
|
end
|
138
138
|
|
139
|
-
it "
|
139
|
+
it "gets parameters without evaluating" do
|
140
140
|
subject.get_parameter(:p1).should == 1
|
141
141
|
subject.get_parameter(:p2).should == 2
|
142
142
|
subject.get_parameter(:p3).should == :xx
|
143
143
|
subject.get_parameter(:p4).should respond_to(:arity)
|
144
144
|
end
|
145
145
|
|
146
|
-
it "
|
146
|
+
it "has parameters" do
|
147
147
|
subject.should have_parameter(:p1)
|
148
148
|
subject.should have_parameter(:p2)
|
149
149
|
subject.should have_parameter(:p3)
|
@@ -172,7 +172,7 @@ describe BinData::Base, "when initializing" do
|
|
172
172
|
end
|
173
173
|
end
|
174
174
|
|
175
|
-
it "
|
175
|
+
it "calls both #initialize_xxx methods" do
|
176
176
|
BaseInit.recorded_calls {
|
177
177
|
BaseInit.new
|
178
178
|
}.should == [:initialize_shared_instance, :initialize_instance]
|
@@ -182,7 +182,7 @@ describe BinData::Base, "when initializing" do
|
|
182
182
|
subject { BaseInit.new(:check_offset => 1) }
|
183
183
|
|
184
184
|
describe "#new" do
|
185
|
-
it "
|
185
|
+
it "calls #initialize_instance" do
|
186
186
|
obj = subject
|
187
187
|
|
188
188
|
BaseInit.recorded_calls {
|
@@ -190,24 +190,24 @@ describe BinData::Base, "when initializing" do
|
|
190
190
|
}.should == [:initialize_instance]
|
191
191
|
end
|
192
192
|
|
193
|
-
it "
|
193
|
+
it "copies parameters" do
|
194
194
|
obj = subject.new
|
195
195
|
obj.eval_parameter(:check_offset).should == 1
|
196
196
|
end
|
197
197
|
|
198
|
-
it "
|
198
|
+
it "performs action for :check_offset" do
|
199
199
|
obj = subject.new
|
200
|
-
|
200
|
+
expect {
|
201
201
|
obj.read("abc")
|
202
|
-
}.
|
202
|
+
}.to raise_error(BinData::ValidityError)
|
203
203
|
end
|
204
204
|
|
205
|
-
it "
|
205
|
+
it "assigns value" do
|
206
206
|
obj = subject.new(3)
|
207
207
|
obj.snapshot.should == 3
|
208
208
|
end
|
209
209
|
|
210
|
-
it "
|
210
|
+
it "sets parent" do
|
211
211
|
obj = subject.new(3, "p")
|
212
212
|
obj.parent.should == "p"
|
213
213
|
end
|
@@ -215,21 +215,18 @@ describe BinData::Base, "when initializing" do
|
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
218
|
-
describe BinData::Base, "as a factory" do
|
219
|
-
end
|
220
|
-
|
221
218
|
describe BinData::Base, "as black box" do
|
222
219
|
context "class methods" do
|
223
|
-
it "
|
220
|
+
it "returns bindata_name" do
|
224
221
|
BaseStub.bindata_name.should == "base_stub"
|
225
222
|
end
|
226
223
|
|
227
|
-
it "
|
224
|
+
it "instantiates self for ::read" do
|
228
225
|
BaseStub.read("").class.should == BaseStub
|
229
226
|
end
|
230
227
|
end
|
231
228
|
|
232
|
-
it "
|
229
|
+
it "accesses parent" do
|
233
230
|
parent = BaseStub.new
|
234
231
|
child = BaseStub.new(nil, parent)
|
235
232
|
child.parent.should == parent
|
@@ -237,25 +234,25 @@ describe BinData::Base, "as black box" do
|
|
237
234
|
|
238
235
|
subject { BaseStub.new }
|
239
236
|
|
240
|
-
it "
|
237
|
+
it "returns self for #read" do
|
241
238
|
subject.read("").should == subject
|
242
239
|
end
|
243
240
|
|
244
|
-
it "
|
241
|
+
it "returns self for #write" do
|
245
242
|
subject.write("").should == subject
|
246
243
|
end
|
247
244
|
|
248
|
-
it "
|
245
|
+
it "forwards #inspect to snapshot" do
|
249
246
|
subject.stub(:snapshot).and_return([1, 2, 3])
|
250
247
|
subject.inspect.should == subject.snapshot.inspect
|
251
248
|
end
|
252
249
|
|
253
|
-
it "
|
250
|
+
it "forwards #to_s to snapshot" do
|
254
251
|
subject.stub(:snapshot).and_return([1, 2, 3])
|
255
252
|
subject.to_s.should == subject.snapshot.to_s
|
256
253
|
end
|
257
254
|
|
258
|
-
it "
|
255
|
+
it "pretty prints object as snapshot" do
|
259
256
|
subject.stub(:snapshot).and_return([1, 2, 3])
|
260
257
|
actual_io = StringIO.new
|
261
258
|
expected_io = StringIO.new
|
@@ -267,7 +264,7 @@ describe BinData::Base, "as black box" do
|
|
267
264
|
actual_io.value.should == expected_io.value
|
268
265
|
end
|
269
266
|
|
270
|
-
it "
|
267
|
+
it "writes the same as to_binary_s" do
|
271
268
|
class WriteToSBase < BaseStub
|
272
269
|
def do_write(io) io.writebytes("abc"); end
|
273
270
|
end
|
@@ -282,23 +279,23 @@ end
|
|
282
279
|
describe BinData::Base, "as white box" do
|
283
280
|
subject { BaseStub.new }
|
284
281
|
|
285
|
-
it "
|
282
|
+
it "forwards read to do_read" do
|
286
283
|
subject.should_receive(:clear).ordered
|
287
284
|
subject.should_receive(:do_read).ordered
|
288
285
|
subject.read(nil)
|
289
286
|
end
|
290
287
|
|
291
|
-
it "
|
288
|
+
it "forwards write to do_write" do
|
292
289
|
subject.should_receive(:do_write)
|
293
290
|
subject.write(nil)
|
294
291
|
end
|
295
292
|
|
296
|
-
it "
|
293
|
+
it "forwards num_bytes to do_num_bytes" do
|
297
294
|
subject.should_receive(:do_num_bytes).and_return(42)
|
298
295
|
subject.num_bytes.should == 42
|
299
296
|
end
|
300
297
|
|
301
|
-
it "
|
298
|
+
it "rounds up fractional num_bytes" do
|
302
299
|
subject.should_receive(:do_num_bytes).and_return(42.1)
|
303
300
|
subject.num_bytes.should == 43
|
304
301
|
end
|
@@ -325,55 +322,55 @@ describe BinData::Base, "checking offsets" do
|
|
325
322
|
let(:io) { StringIO.new("12345678901234567890") }
|
326
323
|
|
327
324
|
context "with :check_offset" do
|
328
|
-
it "
|
325
|
+
it "fails when offset is incorrect" do
|
329
326
|
io.seek(2)
|
330
327
|
subject = TenByteOffsetBase.create(:check_offset => 8)
|
331
|
-
|
328
|
+
expect { subject.read(io) }.to raise_error(BinData::ValidityError)
|
332
329
|
end
|
333
330
|
|
334
|
-
it "
|
331
|
+
it "succeeds when offset is correct" do
|
335
332
|
io.seek(3)
|
336
333
|
subject = TenByteOffsetBase.create(:check_offset => 10)
|
337
|
-
|
334
|
+
expect { subject.read(io) }.not_to raise_error
|
338
335
|
end
|
339
336
|
|
340
|
-
it "
|
337
|
+
it "fails when :check_offset fails" do
|
341
338
|
io.seek(4)
|
342
339
|
subject = TenByteOffsetBase.create(:check_offset => lambda { offset == 11 } )
|
343
|
-
|
340
|
+
expect { subject.read(io) }.to raise_error(BinData::ValidityError)
|
344
341
|
end
|
345
342
|
|
346
|
-
it "
|
343
|
+
it "succeeds when :check_offset succeeds" do
|
347
344
|
io.seek(5)
|
348
345
|
subject = TenByteOffsetBase.create(:check_offset => lambda { offset == 10 } )
|
349
|
-
|
346
|
+
expect { subject.read(io) }.not_to raise_error
|
350
347
|
end
|
351
348
|
end
|
352
349
|
|
353
350
|
context "with :adjust_offset" do
|
354
|
-
it "
|
351
|
+
it "is mutually exclusive with :check_offset" do
|
355
352
|
params = { :check_offset => 8, :adjust_offset => 8 }
|
356
|
-
|
353
|
+
expect { TenByteOffsetBase.create(params) }.to raise_error(ArgumentError)
|
357
354
|
end
|
358
355
|
|
359
|
-
it "
|
356
|
+
it "adjust offset when incorrect" do
|
360
357
|
io.seek(2)
|
361
358
|
subject = TenByteOffsetBase.create(:adjust_offset => 13)
|
362
359
|
subject.read(io)
|
363
360
|
io.pos.should == (2 + 13)
|
364
361
|
end
|
365
362
|
|
366
|
-
it "
|
363
|
+
it "succeeds when offset is correct" do
|
367
364
|
io.seek(3)
|
368
365
|
subject = TenByteOffsetBase.create(:adjust_offset => 10)
|
369
|
-
|
366
|
+
expect { subject.read(io) }.not_to raise_error
|
370
367
|
io.pos.should == (3 + 10)
|
371
368
|
end
|
372
369
|
|
373
|
-
it "
|
370
|
+
it "fails if cannot adjust offset" do
|
374
371
|
io.seek(4)
|
375
372
|
subject = TenByteOffsetBase.create(:adjust_offset => -5)
|
376
|
-
|
373
|
+
expect { subject.read(io) }.to raise_error(BinData::ValidityError)
|
377
374
|
end
|
378
375
|
end
|
379
376
|
end
|
data/spec/bits_spec.rb
CHANGED
@@ -6,7 +6,7 @@ require 'bindata/bits'
|
|
6
6
|
describe "Bits of size 1" do
|
7
7
|
let(:bit_classes) { [BinData::Bit1, BinData::Bit1le] }
|
8
8
|
|
9
|
-
it "
|
9
|
+
it "accept true as value" do
|
10
10
|
bit_classes.each do |bit_class|
|
11
11
|
subject = bit_class.new
|
12
12
|
subject.assign(true)
|
@@ -14,7 +14,7 @@ describe "Bits of size 1" do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
it "
|
17
|
+
it "accept false as value" do
|
18
18
|
bit_classes.each do |bit_class|
|
19
19
|
subject = bit_class.new
|
20
20
|
subject.assign(false)
|
@@ -22,7 +22,7 @@ describe "Bits of size 1" do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
it "
|
25
|
+
it "accept nil as value" do
|
26
26
|
bit_classes.each do |bit_class|
|
27
27
|
subject = bit_class.new
|
28
28
|
subject.assign(nil)
|
@@ -31,15 +31,15 @@ describe "Bits of size 1" do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
shared_examples "All bitfields" do
|
35
35
|
|
36
|
-
it "
|
36
|
+
it "have a sensible value of zero" do
|
37
37
|
all_classes do |bit_class|
|
38
38
|
bit_class.new.should be_zero
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
it "
|
42
|
+
it "avoid underflow" do
|
43
43
|
all_classes do |bit_class|
|
44
44
|
subject = bit_class.new
|
45
45
|
|
@@ -48,7 +48,7 @@ share_examples_for "All bitfields" do
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
it "
|
51
|
+
it "avoid overflow" do
|
52
52
|
all_classes do |bit_class|
|
53
53
|
subject = bit_class.new
|
54
54
|
|
@@ -57,7 +57,7 @@ share_examples_for "All bitfields" do
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
it "
|
60
|
+
it "assign values" do
|
61
61
|
all_classes do |bit_class|
|
62
62
|
some_values_within_range.each do |val|
|
63
63
|
subject = bit_class.new
|
@@ -68,7 +68,7 @@ share_examples_for "All bitfields" do
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
it "
|
71
|
+
it "assign values from other bit objects" do
|
72
72
|
all_classes do |bit_class|
|
73
73
|
some_values_within_range.each do |val|
|
74
74
|
subject = bit_class.new
|
@@ -79,7 +79,7 @@ share_examples_for "All bitfields" do
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
it "
|
82
|
+
it "symmetrically #read and #write" do
|
83
83
|
all_classes do |bit_class|
|
84
84
|
some_values_within_range.each do |val|
|
85
85
|
subject = bit_class.new
|
@@ -129,13 +129,13 @@ def generate_bit_classes_to_test(endian)
|
|
129
129
|
end
|
130
130
|
|
131
131
|
describe "Big endian bitfields" do
|
132
|
-
|
132
|
+
include_examples "All bitfields"
|
133
133
|
|
134
134
|
before(:all) do
|
135
135
|
@bits = generate_bit_classes_to_test(:big)
|
136
136
|
end
|
137
137
|
|
138
|
-
it "
|
138
|
+
it "read big endian values" do
|
139
139
|
@bits.each_pair do |bit_class, nbits|
|
140
140
|
nbytes = (nbits + 7) / 8
|
141
141
|
str = [0b1000_0000].pack("C") + "\000" * (nbytes - 1)
|
@@ -146,13 +146,13 @@ describe "Big endian bitfields" do
|
|
146
146
|
end
|
147
147
|
|
148
148
|
describe "Little endian bitfields" do
|
149
|
-
|
149
|
+
include_examples "All bitfields"
|
150
150
|
|
151
151
|
before(:all) do
|
152
152
|
@bits = generate_bit_classes_to_test(:little)
|
153
153
|
end
|
154
154
|
|
155
|
-
it "
|
155
|
+
it "read little endian values" do
|
156
156
|
@bits.each_pair do |bit_class, nbits|
|
157
157
|
nbytes = (nbits + 7) / 8
|
158
158
|
str = [0b0000_0001].pack("C") + "\000" * (nbytes - 1)
|