bindata 0.9.3 → 0.10.0
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/ChangeLog +18 -0
- data/NEWS +59 -0
- data/README +22 -23
- data/TODO +18 -12
- data/examples/gzip.rb +4 -4
- data/lib/bindata.rb +4 -3
- data/lib/bindata/array.rb +202 -132
- data/lib/bindata/base.rb +147 -166
- data/lib/bindata/{single.rb → base_primitive.rb} +82 -56
- data/lib/bindata/bits.rb +31 -770
- data/lib/bindata/choice.rb +157 -82
- data/lib/bindata/float.rb +25 -27
- data/lib/bindata/int.rb +144 -177
- data/lib/bindata/io.rb +59 -49
- data/lib/bindata/lazy.rb +80 -50
- data/lib/bindata/params.rb +134 -26
- data/lib/bindata/{single_value.rb → primitive.rb} +71 -64
- data/lib/bindata/{multi_value.rb → record.rb} +52 -70
- data/lib/bindata/registry.rb +49 -17
- data/lib/bindata/rest.rb +6 -10
- data/lib/bindata/sanitize.rb +55 -70
- data/lib/bindata/string.rb +60 -42
- data/lib/bindata/stringz.rb +34 -35
- data/lib/bindata/struct.rb +197 -152
- data/lib/bindata/trace.rb +35 -0
- data/spec/array_spec.rb +128 -112
- data/spec/{single_spec.rb → base_primitive_spec.rb} +102 -61
- data/spec/base_spec.rb +190 -185
- data/spec/bits_spec.rb +126 -98
- data/spec/choice_spec.rb +89 -98
- data/spec/example.rb +19 -0
- data/spec/float_spec.rb +28 -44
- data/spec/int_spec.rb +217 -127
- data/spec/io_spec.rb +41 -24
- data/spec/lazy_spec.rb +95 -49
- data/spec/primitive_spec.rb +191 -0
- data/spec/{multi_value_spec.rb → record_spec.rb} +124 -89
- data/spec/registry_spec.rb +53 -12
- data/spec/rest_spec.rb +2 -3
- data/spec/sanitize_spec.rb +47 -73
- data/spec/spec_common.rb +13 -1
- data/spec/string_spec.rb +34 -23
- data/spec/stringz_spec.rb +10 -18
- data/spec/struct_spec.rb +91 -63
- data/spec/system_spec.rb +291 -0
- metadata +12 -8
- data/spec/single_value_spec.rb +0 -131
@@ -0,0 +1,35 @@
|
|
1
|
+
module BinData
|
2
|
+
# reference to the current tracer
|
3
|
+
@tracer ||= nil
|
4
|
+
|
5
|
+
class Tracer #:nodoc:
|
6
|
+
def initialize(io)
|
7
|
+
@trace_io = io
|
8
|
+
end
|
9
|
+
|
10
|
+
def trace(msg)
|
11
|
+
@trace_io.puts(msg)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Turn on trace information when reading a BinData object.
|
16
|
+
# If +block+ is given then the tracing only occurs for that block.
|
17
|
+
# This is useful for debugging a BinData declaration.
|
18
|
+
def trace_reading(io = STDERR, &block)
|
19
|
+
@tracer = Tracer.new(io)
|
20
|
+
if block_given?
|
21
|
+
begin
|
22
|
+
block.call
|
23
|
+
ensure
|
24
|
+
@tracer = nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def trace_message(&block) #:nodoc:
|
30
|
+
return if @tracer.nil?
|
31
|
+
block.call(@tracer)
|
32
|
+
end
|
33
|
+
|
34
|
+
module_function :trace_reading, :trace_message
|
35
|
+
end
|
data/spec/array_spec.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
|
4
|
+
require File.expand_path(File.dirname(__FILE__)) + '/example'
|
4
5
|
require 'bindata/array'
|
5
|
-
require 'bindata/bits'
|
6
6
|
require 'bindata/int'
|
7
|
-
require 'bindata/struct'
|
8
7
|
|
9
8
|
describe BinData::Array, "when instantiating" do
|
10
9
|
it "should ensure mandatory parameters are supplied" do
|
@@ -27,11 +26,11 @@ end
|
|
27
26
|
|
28
27
|
describe BinData::Array, "with no elements" do
|
29
28
|
before(:each) do
|
30
|
-
@data = BinData::Array.new(:type => :
|
29
|
+
@data = BinData::Array.new(:type => :example_single)
|
31
30
|
end
|
32
31
|
|
33
|
-
it "should
|
34
|
-
@data.
|
32
|
+
it "should be clear" do
|
33
|
+
@data.should be_clear
|
35
34
|
end
|
36
35
|
|
37
36
|
it "should return zero length" do
|
@@ -57,30 +56,51 @@ describe BinData::Array, "with no elements" do
|
|
57
56
|
it "should return [] for the last n elements" do
|
58
57
|
@data.last(3).should == []
|
59
58
|
end
|
60
|
-
|
61
|
-
it "should append an element" do
|
62
|
-
@data.append(99)
|
63
|
-
@data.length.should == 1
|
64
|
-
@data.last.should == 99
|
65
|
-
end
|
66
59
|
end
|
67
60
|
|
68
61
|
describe BinData::Array, "with several elements" do
|
69
62
|
before(:each) do
|
70
|
-
type = [:
|
63
|
+
type = [:example_single, {:initial_value => lambda { index + 1 }}]
|
71
64
|
@data = BinData::Array.new(:type => type, :initial_length => 5)
|
72
65
|
end
|
73
66
|
|
74
|
-
it "should
|
75
|
-
@data.
|
67
|
+
it "should have correct debug name" do
|
68
|
+
@data[2].debug_name.should == "obj[2]"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should have correct offset" do
|
72
|
+
@data[2].offset.should == ExampleSingle.new.num_bytes * 2
|
76
73
|
end
|
77
74
|
|
78
75
|
it "should return a correct snapshot" do
|
79
76
|
@data.snapshot.should == [1, 2, 3, 4, 5]
|
80
77
|
end
|
81
78
|
|
79
|
+
it "should assign primitive values" do
|
80
|
+
@data.assign([4, 5, 6])
|
81
|
+
@data.snapshot.should == [4, 5, 6]
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should assign bindata objects" do
|
85
|
+
o1 = ExampleSingle.new
|
86
|
+
o1.value = 4
|
87
|
+
o2 = ExampleSingle.new
|
88
|
+
o2.value = 5
|
89
|
+
o3 = ExampleSingle.new
|
90
|
+
o3.value = 6
|
91
|
+
@data.assign([o1, o2, o3])
|
92
|
+
@data.snapshot.should == [4, 5, 6]
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should assign bindata array" do
|
96
|
+
array = BinData::Array.new(:type => :example_single)
|
97
|
+
array.push(4, 5, 6)
|
98
|
+
@data.assign(array)
|
99
|
+
@data.snapshot.should == [4, 5, 6]
|
100
|
+
end
|
101
|
+
|
82
102
|
it "should coerce to ::Array if required" do
|
83
|
-
(
|
103
|
+
[0].concat(@data).should == [0, 1, 2, 3, 4, 5]
|
84
104
|
end
|
85
105
|
|
86
106
|
it "should return the first element" do
|
@@ -111,11 +131,11 @@ describe BinData::Array, "with several elements" do
|
|
111
131
|
end
|
112
132
|
|
113
133
|
it "should have correct num_bytes" do
|
114
|
-
@data.num_bytes.should ==
|
134
|
+
@data.num_bytes.should == 5 * ExampleSingle.new.num_bytes
|
115
135
|
end
|
116
136
|
|
117
137
|
it "should have correct num_bytes for individual elements" do
|
118
|
-
@data.num_bytes(0).should ==
|
138
|
+
@data.num_bytes(0).should == ExampleSingle.new.num_bytes
|
119
139
|
end
|
120
140
|
|
121
141
|
it "should be able to directly access elements" do
|
@@ -135,12 +155,6 @@ describe BinData::Array, "with several elements" do
|
|
135
155
|
@data.select { |x| (x % 2) == 0 }.should == [2, 4]
|
136
156
|
end
|
137
157
|
|
138
|
-
it "should automatically extend" do
|
139
|
-
@data[9] = 3
|
140
|
-
@data[8].should == 9
|
141
|
-
@data.length.should == 10
|
142
|
-
end
|
143
|
-
|
144
158
|
it "should clear" do
|
145
159
|
@data[1] = 8
|
146
160
|
@data.clear
|
@@ -153,19 +167,14 @@ describe BinData::Array, "with several elements" do
|
|
153
167
|
@data[1].should == 2
|
154
168
|
end
|
155
169
|
|
156
|
-
it "should clear a single element out of range without extending" do
|
157
|
-
@data.clear(9)
|
158
|
-
@data.length.should == 5
|
159
|
-
end
|
160
|
-
|
161
170
|
it "should be clear upon creation" do
|
162
|
-
@data.
|
171
|
+
@data.should be_clear
|
163
172
|
end
|
164
173
|
|
165
174
|
it "should be clear if all elements are clear" do
|
166
175
|
@data[1] = 8
|
167
176
|
@data.clear(1)
|
168
|
-
@data.
|
177
|
+
@data.should be_clear
|
169
178
|
end
|
170
179
|
|
171
180
|
it "should test clear status of individual elements" do
|
@@ -174,62 +183,110 @@ describe BinData::Array, "with several elements" do
|
|
174
183
|
@data.clear?(1).should be_false
|
175
184
|
end
|
176
185
|
|
177
|
-
it "should
|
178
|
-
@data.clear?(9).should be_true
|
179
|
-
@data.length.should == 5
|
180
|
-
end
|
181
|
-
|
182
|
-
it "should read and write correctly" do
|
183
|
-
io = StringIO.new
|
186
|
+
it "should symmetrically read and write" do
|
184
187
|
@data[1] = 8
|
185
|
-
@data.
|
188
|
+
str = @data.to_binary_s
|
186
189
|
|
187
190
|
@data.clear
|
188
|
-
io.rewind
|
189
191
|
@data[1].should == 2
|
190
192
|
|
191
|
-
@data.read(
|
193
|
+
@data.read(str)
|
192
194
|
@data[1].should == 8
|
193
195
|
end
|
194
196
|
|
195
|
-
it "should
|
196
|
-
@data.
|
197
|
-
|
198
|
-
|
197
|
+
it "should identify index of elements" do
|
198
|
+
@data.index(3).should == 2
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should return nil for index of non existent element" do
|
202
|
+
@data.index(42).should be_nil
|
199
203
|
end
|
200
204
|
end
|
201
205
|
|
202
|
-
describe BinData::Array, "
|
206
|
+
describe BinData::Array, "when accessing elements" do
|
203
207
|
before(:each) do
|
204
|
-
type = [:
|
205
|
-
{:initial_value => lambda { parent.index }}],
|
206
|
-
[:int8, :b]]}]
|
208
|
+
type = [:example_single, {:initial_value => lambda { index + 1 }}]
|
207
209
|
@data = BinData::Array.new(:type => type, :initial_length => 5)
|
210
|
+
@data[0] = 1
|
211
|
+
@data[1] = 2
|
212
|
+
@data[2] = 3
|
213
|
+
@data[3] = 4
|
214
|
+
@data[4] = 5
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should insert with positive indexes" do
|
218
|
+
@data.insert(2, 30, 40)
|
219
|
+
@data.snapshot.should == [1, 2, 30, 40, 3, 4, 5]
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should insert with negative indexes" do
|
223
|
+
@data.insert(-2, 30, 40)
|
224
|
+
@data.snapshot.should == [1, 2, 3, 4, 30, 40, 5]
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should push" do
|
228
|
+
@data.push(30, 40)
|
229
|
+
@data.snapshot.should == [1, 2, 3, 4, 5, 30, 40]
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should concat" do
|
233
|
+
@data.concat([30, 40])
|
234
|
+
@data.snapshot.should == [1, 2, 3, 4, 5, 30, 40]
|
208
235
|
end
|
209
236
|
|
210
|
-
it "should
|
211
|
-
@data
|
237
|
+
it "should unshift" do
|
238
|
+
@data.unshift(30, 40)
|
239
|
+
@data.snapshot.should == [30, 40, 1, 2, 3, 4, 5]
|
212
240
|
end
|
213
241
|
|
214
|
-
it "should
|
215
|
-
@data.
|
242
|
+
it "should automatically extend on [index]" do
|
243
|
+
@data[9].should == 10
|
244
|
+
@data.snapshot.should == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
216
245
|
end
|
217
246
|
|
218
|
-
it "should
|
219
|
-
|
247
|
+
it "should automatically extend on []=" do
|
248
|
+
@data[9] = 30
|
249
|
+
@data.snapshot.should == [1, 2, 3, 4, 5, 6, 7, 8, 9, 30]
|
220
250
|
end
|
221
251
|
|
222
|
-
it "should
|
223
|
-
@data.
|
252
|
+
it "should automatically extend on insert" do
|
253
|
+
@data.insert(7, 30, 40)
|
254
|
+
@data.snapshot.should == [1, 2, 3, 4, 5, 6, 7, 30, 40]
|
224
255
|
end
|
225
256
|
|
226
|
-
it "should
|
227
|
-
|
228
|
-
|
229
|
-
|
257
|
+
it "should not extend on at" do
|
258
|
+
@data.at(9).should be_nil
|
259
|
+
@data.length.should == 5
|
260
|
+
end
|
261
|
+
|
262
|
+
it "should not extend on [start, length]" do
|
263
|
+
@data[9, 2].should be_nil
|
264
|
+
@data.length.should == 5
|
265
|
+
end
|
266
|
+
|
267
|
+
it "should not extend on [range]" do
|
268
|
+
@data[9 .. 10].should be_nil
|
269
|
+
@data.length.should == 5
|
270
|
+
end
|
230
271
|
|
231
|
-
|
232
|
-
@data.
|
272
|
+
it "should not extend on clear" do
|
273
|
+
@data.clear(9)
|
274
|
+
@data.length.should == 5
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should not extend on clear?" do
|
278
|
+
@data.clear?(9).should be_true
|
279
|
+
@data.length.should == 5
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should not extend on num_bytes" do
|
283
|
+
@data.num_bytes(9).should == 0
|
284
|
+
@data.length.should == 5
|
285
|
+
end
|
286
|
+
|
287
|
+
it "should raise error on bad input to []" do
|
288
|
+
lambda { @data["a"] }.should raise_error(TypeError)
|
289
|
+
lambda { @data[1, "a"] }.should raise_error(TypeError)
|
233
290
|
end
|
234
291
|
end
|
235
292
|
|
@@ -239,27 +296,22 @@ describe BinData::Array, "with :read_until containing +element+" do
|
|
239
296
|
@data = BinData::Array.new(:type => :int8, :read_until => read_until)
|
240
297
|
end
|
241
298
|
|
242
|
-
it "should append to an empty array" do
|
243
|
-
@data.append(3)
|
244
|
-
@data.first.should == 3
|
245
|
-
end
|
246
|
-
|
247
299
|
it "should read until the sentinel is reached" do
|
248
|
-
|
249
|
-
@data.read(
|
300
|
+
str = "\x01\x02\x03\x04\x05\x06\x07"
|
301
|
+
@data.read(str)
|
250
302
|
@data.length.should == 5
|
251
303
|
end
|
252
304
|
end
|
253
305
|
|
254
306
|
describe BinData::Array, "with :read_until containing +array+ and +index+" do
|
255
307
|
before(:each) do
|
256
|
-
read_until = lambda { index >=2 and array[index - 2] == 5 }
|
308
|
+
read_until = lambda { index >= 2 and array[index - 2] == 5 }
|
257
309
|
@data = BinData::Array.new(:type => :int8, :read_until => read_until)
|
258
310
|
end
|
259
311
|
|
260
312
|
it "should read until the sentinel is reached" do
|
261
|
-
|
262
|
-
@data.read(
|
313
|
+
str = "\x01\x02\x03\x04\x05\x06\x07\x08"
|
314
|
+
@data.read(str)
|
263
315
|
@data.length.should == 7
|
264
316
|
end
|
265
317
|
end
|
@@ -267,54 +319,19 @@ end
|
|
267
319
|
describe BinData::Array, "with :read_until => :eof" do
|
268
320
|
it "should read records until eof" do
|
269
321
|
obj = BinData::Array.new(:type => :int8, :read_until => :eof)
|
270
|
-
|
271
|
-
obj.read(
|
322
|
+
str = "\x01\x02\x03"
|
323
|
+
obj.read(str)
|
272
324
|
obj.snapshot.should == [1, 2, 3]
|
273
325
|
end
|
274
326
|
|
275
327
|
it "should read records until eof, ignoring partial records" do
|
276
328
|
obj = BinData::Array.new(:type => :int16be, :read_until => :eof)
|
277
|
-
|
278
|
-
obj.read(
|
329
|
+
str = "\x00\x01\x00\x02\x03"
|
330
|
+
obj.read(str)
|
279
331
|
obj.snapshot.should == [1, 2]
|
280
332
|
end
|
281
333
|
end
|
282
334
|
|
283
|
-
describe BinData::Array, "of bits" do
|
284
|
-
before(:each) do
|
285
|
-
@data = BinData::Array.new(:type => :bit1, :initial_length => 15)
|
286
|
-
end
|
287
|
-
|
288
|
-
it "should read" do
|
289
|
-
str = [0b0001_0100, 0b1000_1000].pack("CC")
|
290
|
-
@data.read(str)
|
291
|
-
@data[0].should == 0
|
292
|
-
@data[1].should == 0
|
293
|
-
@data[2].should == 0
|
294
|
-
@data[3].should == 1
|
295
|
-
@data[4].should == 0
|
296
|
-
@data[5].should == 1
|
297
|
-
@data[6].should == 0
|
298
|
-
@data[7].should == 0
|
299
|
-
@data[8].should == 1
|
300
|
-
@data[9].should == 0
|
301
|
-
@data[10].should == 0
|
302
|
-
@data[11].should == 0
|
303
|
-
@data[12].should == 1
|
304
|
-
@data[13].should == 0
|
305
|
-
@data[14].should == 0
|
306
|
-
end
|
307
|
-
|
308
|
-
it "should write" do
|
309
|
-
@data[3] = 1
|
310
|
-
@data.to_s.should == [0b0001_0000, 0b0000_0000].pack("CC")
|
311
|
-
end
|
312
|
-
|
313
|
-
it "should return num_bytes" do
|
314
|
-
@data.num_bytes.should == 2
|
315
|
-
end
|
316
|
-
end
|
317
|
-
|
318
335
|
describe BinData::Array, "nested within an Array" do
|
319
336
|
before(:each) do
|
320
337
|
nested_array_params = { :type => [:int8, { :initial_value => :index }],
|
@@ -333,4 +350,3 @@ describe BinData::Array, "nested within an Array" do
|
|
333
350
|
@data.snapshot.should == [ [4], [5, 6], [7, 8, 9] ]
|
334
351
|
end
|
335
352
|
end
|
336
|
-
|
@@ -1,68 +1,139 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
|
4
|
+
require File.expand_path(File.dirname(__FILE__)) + '/example'
|
5
|
+
require 'bindata/base_primitive'
|
4
6
|
require 'bindata/io'
|
5
|
-
require 'bindata/single'
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
def
|
10
|
-
|
11
|
-
|
8
|
+
class ExampleSingle
|
9
|
+
# reopen example to make @in_read public for testing
|
10
|
+
def in_read?
|
11
|
+
@in_read
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe BinData::BasePrimitive, "when subclassing" do
|
16
|
+
before(:all) do
|
17
|
+
eval <<-END
|
18
|
+
class SubClassOfSingle < BinData::BasePrimitive
|
19
|
+
expose_methods_for_testing
|
20
|
+
end
|
21
|
+
END
|
22
|
+
end
|
12
23
|
|
13
|
-
|
24
|
+
before(:each) do
|
25
|
+
@obj = SubClassOfSingle.new
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should raise errors on unimplemented methods" do
|
29
|
+
lambda { @obj.value_to_binary_string(nil) }.should raise_error(NotImplementedError)
|
30
|
+
lambda { @obj.read_and_return_value(nil) }.should raise_error(NotImplementedError)
|
31
|
+
lambda { @obj.sensible_default }.should raise_error(NotImplementedError)
|
32
|
+
end
|
14
33
|
end
|
15
34
|
|
16
|
-
describe BinData::
|
35
|
+
describe BinData::BasePrimitive do
|
17
36
|
it "should conform to rule 1 for returning a value" do
|
18
|
-
data =
|
37
|
+
data = ExampleSingle.new(:value => 5)
|
19
38
|
data.should_not be_in_read
|
20
39
|
data.value.should == 5
|
21
40
|
end
|
22
41
|
|
23
42
|
it "should conform to rule 2 for returning a value" do
|
24
43
|
io = StringIO.new([42].pack("V"))
|
25
|
-
data =
|
44
|
+
data = ExampleSingle.new(:value => 5)
|
45
|
+
data.expose_methods_for_testing
|
26
46
|
data.do_read(BinData::IO.new(io))
|
27
47
|
data.should be_in_read
|
28
48
|
data.value.should == 42
|
29
49
|
end
|
30
50
|
|
31
51
|
it "should conform to rule 3 for returning a value" do
|
32
|
-
data =
|
52
|
+
data = ExampleSingle.new(:initial_value => 5)
|
33
53
|
data.should be_clear
|
34
54
|
data.value.should == 5
|
35
55
|
end
|
36
56
|
|
37
57
|
it "should conform to rule 4 for returning a value" do
|
38
|
-
data =
|
58
|
+
data = ExampleSingle.new(:initial_value => 5)
|
39
59
|
data.value = 17
|
40
60
|
data.should_not be_clear
|
41
61
|
data.value.should == 17
|
42
62
|
end
|
43
63
|
|
44
64
|
it "should conform to rule 5 for returning a value" do
|
45
|
-
data =
|
65
|
+
data = ExampleSingle.new
|
46
66
|
data.should be_clear
|
47
67
|
data.value.should == 0
|
48
68
|
end
|
49
69
|
|
50
70
|
it "should conform to rule 6 for returning a value" do
|
51
|
-
data =
|
71
|
+
data = ExampleSingle.new
|
52
72
|
data.value = 8
|
53
73
|
data.should_not be_clear
|
54
74
|
data.value.should == 8
|
55
75
|
end
|
56
76
|
end
|
57
77
|
|
58
|
-
describe
|
78
|
+
describe ExampleSingle do
|
59
79
|
before(:each) do
|
60
|
-
@data =
|
80
|
+
@data = ExampleSingle.new
|
81
|
+
@data.value = 5
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should fail when assigning nil values" do
|
85
|
+
lambda { @data.assign(nil) }.should raise_error(ArgumentError)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should allowing setting and retrieving value" do
|
89
|
+
@data.value = 7
|
90
|
+
@data.value.should == 7
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should allowing setting and retrieving BinData::BasePrimitives" do
|
94
|
+
obj = ExampleSingle.new
|
95
|
+
obj.value = 7
|
96
|
+
@data.value = obj
|
97
|
+
@data.value.should == 7
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should respond to known methods" do
|
101
|
+
@data.should respond_to(:num_bytes)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should respond to known methods in #snapshot" do
|
105
|
+
@data.should respond_to(:div)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should not respond to unknown methods in self or #snapshot" do
|
109
|
+
@data.should_not respond_to(:does_not_exist)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should behave as #snapshot" do
|
113
|
+
(@data + 1).should == 6
|
114
|
+
(1 + @data).should == 6
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should be equal to other ExampleSingle" do
|
118
|
+
other = ExampleSingle.new
|
119
|
+
other.value = 5
|
120
|
+
@data.should == other
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should be equal to raw values" do
|
124
|
+
@data.should == 5
|
125
|
+
5.should == @data
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe BinData::BasePrimitive, "after initialisation" do
|
130
|
+
before(:each) do
|
131
|
+
@data = ExampleSingle.new
|
61
132
|
end
|
62
133
|
|
63
134
|
it "should not allow both :initial_value and :value" do
|
64
135
|
params = {:initial_value => 1, :value => 2}
|
65
|
-
lambda {
|
136
|
+
lambda { ExampleSingle.new(params) }.should raise_error(ArgumentError)
|
66
137
|
end
|
67
138
|
|
68
139
|
it "should have a sensible value" do
|
@@ -70,18 +141,10 @@ describe BinData::Single, "after initialisation" do
|
|
70
141
|
end
|
71
142
|
|
72
143
|
it "should have symmetric IO" do
|
73
|
-
io = StringIO.new
|
74
144
|
@data.value = 42
|
75
|
-
@data.
|
145
|
+
written = @data.to_binary_s
|
76
146
|
|
77
|
-
|
78
|
-
@data = ConcreteSingle.new
|
79
|
-
@data.read(io)
|
80
|
-
@data.value.should == 42
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should be a single_value" do
|
84
|
-
@data.should be_single_value
|
147
|
+
ExampleSingle.read(written).should == 42
|
85
148
|
end
|
86
149
|
|
87
150
|
it "should allowing setting and retrieving value" do
|
@@ -99,8 +162,7 @@ describe BinData::Single, "after initialisation" do
|
|
99
162
|
end
|
100
163
|
|
101
164
|
it "should not be clear after reading" do
|
102
|
-
|
103
|
-
@data.read(io)
|
165
|
+
@data.read("\x11\x22\x33\x44")
|
104
166
|
@data.should_not be_clear
|
105
167
|
end
|
106
168
|
|
@@ -114,9 +176,9 @@ describe BinData::Single, "after initialisation" do
|
|
114
176
|
end
|
115
177
|
end
|
116
178
|
|
117
|
-
describe BinData::
|
179
|
+
describe BinData::BasePrimitive, "with :initial_value" do
|
118
180
|
before(:each) do
|
119
|
-
@data =
|
181
|
+
@data = ExampleSingle.new(:initial_value => 5)
|
120
182
|
end
|
121
183
|
|
122
184
|
it "should return that initial value before reading or being set" do
|
@@ -129,8 +191,7 @@ describe BinData::Single, "with :initial_value" do
|
|
129
191
|
end
|
130
192
|
|
131
193
|
it "should forget :initial_value after reading" do
|
132
|
-
|
133
|
-
@data.read(io)
|
194
|
+
@data.read("\x11\x22\x33\x44")
|
134
195
|
@data.value.should_not == 5
|
135
196
|
end
|
136
197
|
|
@@ -141,9 +202,10 @@ describe BinData::Single, "with :initial_value" do
|
|
141
202
|
end
|
142
203
|
end
|
143
204
|
|
144
|
-
describe BinData::
|
205
|
+
describe BinData::BasePrimitive, "with :value" do
|
145
206
|
before(:each) do
|
146
|
-
@data =
|
207
|
+
@data = ExampleSingle.new(:value => 5)
|
208
|
+
@data.expose_methods_for_testing
|
147
209
|
end
|
148
210
|
|
149
211
|
it "should return that :value" do
|
@@ -169,49 +231,28 @@ describe BinData::Single, "with :value" do
|
|
169
231
|
end
|
170
232
|
end
|
171
233
|
|
172
|
-
describe BinData::
|
234
|
+
describe BinData::BasePrimitive, "with :check_value" do
|
173
235
|
before(:each) do
|
174
236
|
@io = StringIO.new([34].pack("V"))
|
175
237
|
end
|
176
238
|
|
177
239
|
it "should succeed when check_value is non boolean and correct" do
|
178
|
-
data =
|
240
|
+
data = ExampleSingle.new(:check_value => 34)
|
179
241
|
lambda { data.read(@io) }.should_not raise_error
|
180
242
|
end
|
181
243
|
|
182
244
|
it "should fail when check_value is non boolean and incorrect" do
|
183
|
-
data =
|
245
|
+
data = ExampleSingle.new(:check_value => lambda { 123 * 5 })
|
184
246
|
lambda { data.read(@io) }.should raise_error(BinData::ValidityError)
|
185
247
|
end
|
186
248
|
|
187
249
|
it "should succeed when check_value is boolean and true" do
|
188
|
-
data =
|
250
|
+
data = ExampleSingle.new(:check_value => lambda { (value % 2) == 0})
|
189
251
|
lambda { data.read(@io) }.should_not raise_error
|
190
252
|
end
|
191
253
|
|
192
254
|
it "should fail when check_value is boolean and false" do
|
193
|
-
data =
|
255
|
+
data = ExampleSingle.new(:check_value => lambda { value > 100 })
|
194
256
|
lambda { data.read(@io) }.should raise_error(BinData::ValidityError)
|
195
257
|
end
|
196
258
|
end
|
197
|
-
|
198
|
-
describe BinData::Single, "when subclassing" do
|
199
|
-
before(:all) do
|
200
|
-
eval <<-END
|
201
|
-
class SubClassOfSingle < BinData::Single
|
202
|
-
public :val_to_str, :read_val, :sensible_default
|
203
|
-
end
|
204
|
-
END
|
205
|
-
end
|
206
|
-
|
207
|
-
before(:each) do
|
208
|
-
@obj = SubClassOfSingle.new
|
209
|
-
end
|
210
|
-
|
211
|
-
it "should raise errors on unimplemented methods" do
|
212
|
-
lambda { @obj.val_to_str(nil) }.should raise_error(NotImplementedError)
|
213
|
-
lambda { @obj.read_val(nil) }.should raise_error(NotImplementedError)
|
214
|
-
lambda { @obj.sensible_default }.should raise_error(NotImplementedError)
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|