jbangert-bindata 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/BSDL +22 -0
- data/COPYING +52 -0
- data/ChangeLog.rdoc +204 -0
- data/Gemfile +2 -0
- data/INSTALL +11 -0
- data/NEWS.rdoc +164 -0
- data/README.md +54 -0
- data/Rakefile +13 -0
- data/bindata.gemspec +31 -0
- data/doc/manual.haml +407 -0
- data/doc/manual.md +1649 -0
- data/examples/NBT.txt +149 -0
- data/examples/gzip.rb +161 -0
- data/examples/ip_address.rb +22 -0
- data/examples/list.rb +124 -0
- data/examples/nbt.rb +178 -0
- data/lib/bindata.rb +33 -0
- data/lib/bindata/alignment.rb +83 -0
- data/lib/bindata/array.rb +335 -0
- data/lib/bindata/base.rb +388 -0
- data/lib/bindata/base_primitive.rb +214 -0
- data/lib/bindata/bits.rb +87 -0
- data/lib/bindata/choice.rb +216 -0
- data/lib/bindata/count_bytes_remaining.rb +35 -0
- data/lib/bindata/deprecated.rb +50 -0
- data/lib/bindata/dsl.rb +312 -0
- data/lib/bindata/float.rb +80 -0
- data/lib/bindata/int.rb +184 -0
- data/lib/bindata/io.rb +274 -0
- data/lib/bindata/lazy.rb +105 -0
- data/lib/bindata/offset.rb +91 -0
- data/lib/bindata/params.rb +135 -0
- data/lib/bindata/primitive.rb +135 -0
- data/lib/bindata/record.rb +110 -0
- data/lib/bindata/registry.rb +92 -0
- data/lib/bindata/rest.rb +35 -0
- data/lib/bindata/sanitize.rb +290 -0
- data/lib/bindata/skip.rb +48 -0
- data/lib/bindata/string.rb +145 -0
- data/lib/bindata/stringz.rb +96 -0
- data/lib/bindata/struct.rb +388 -0
- data/lib/bindata/trace.rb +94 -0
- data/lib/bindata/version.rb +3 -0
- data/setup.rb +1585 -0
- data/spec/alignment_spec.rb +61 -0
- data/spec/array_spec.rb +331 -0
- data/spec/base_primitive_spec.rb +238 -0
- data/spec/base_spec.rb +376 -0
- data/spec/bits_spec.rb +163 -0
- data/spec/choice_spec.rb +263 -0
- data/spec/count_bytes_remaining_spec.rb +38 -0
- data/spec/deprecated_spec.rb +31 -0
- data/spec/example.rb +21 -0
- data/spec/float_spec.rb +37 -0
- data/spec/int_spec.rb +216 -0
- data/spec/io_spec.rb +352 -0
- data/spec/lazy_spec.rb +217 -0
- data/spec/primitive_spec.rb +202 -0
- data/spec/record_spec.rb +530 -0
- data/spec/registry_spec.rb +108 -0
- data/spec/rest_spec.rb +26 -0
- data/spec/skip_spec.rb +27 -0
- data/spec/spec_common.rb +58 -0
- data/spec/string_spec.rb +300 -0
- data/spec/stringz_spec.rb +118 -0
- data/spec/struct_spec.rb +350 -0
- data/spec/system_spec.rb +380 -0
- data/tasks/manual.rake +36 -0
- data/tasks/rspec.rake +17 -0
- metadata +208 -0
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
+
require 'bindata'
|
5
|
+
|
6
|
+
describe BinData::ResumeByteAlignment do
|
7
|
+
class ResumeAlignmentRecord < BinData::Record
|
8
|
+
bit4 :a
|
9
|
+
resume_byte_alignment
|
10
|
+
bit4 :b
|
11
|
+
end
|
12
|
+
|
13
|
+
subject { ResumeAlignmentRecord.new }
|
14
|
+
|
15
|
+
it "resets read alignment" do
|
16
|
+
subject.read "\x12\x34"
|
17
|
+
|
18
|
+
subject.a.should == 1
|
19
|
+
subject.b.should == 3
|
20
|
+
end
|
21
|
+
|
22
|
+
it "resets write alignment" do
|
23
|
+
subject.assign(:a => 2, :b => 7)
|
24
|
+
|
25
|
+
subject.to_binary_s.should == "\x20\x70"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe BinData::BitAligned do
|
30
|
+
it "does not apply to BinData::Primitives" do
|
31
|
+
expect {
|
32
|
+
class BitAlignedPrimitive < BinData::Primitive
|
33
|
+
bit_aligned
|
34
|
+
end
|
35
|
+
}.to raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
class BitString < BinData::String
|
39
|
+
bit_aligned
|
40
|
+
end
|
41
|
+
|
42
|
+
class BitAlignedRecord < BinData::Record
|
43
|
+
bit4 :preamble
|
44
|
+
bit_string :str, :length => 2
|
45
|
+
bit4 :afterward
|
46
|
+
end
|
47
|
+
|
48
|
+
subject { BitAlignedRecord.new }
|
49
|
+
|
50
|
+
its(:num_bytes) { should == 3 }
|
51
|
+
|
52
|
+
it "reads as expected" do
|
53
|
+
subject.read("\x56\x36\x42")
|
54
|
+
subject.should == {"preamble" => 5, "str" => "cd", "afterward" => 2}
|
55
|
+
end
|
56
|
+
|
57
|
+
it "writes as expected" do
|
58
|
+
subject.assign(:preamble => 5, :str => "ab", :afterward => 1)
|
59
|
+
subject.to_binary_s.should == "\x56\x16\x21"
|
60
|
+
end
|
61
|
+
end
|
data/spec/array_spec.rb
ADDED
@@ -0,0 +1,331 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "example"))
|
5
|
+
require 'bindata/array'
|
6
|
+
require 'bindata/int'
|
7
|
+
require 'bindata/string'
|
8
|
+
|
9
|
+
describe BinData::Array, "when instantiating" do
|
10
|
+
context "with no mandatory parameters supplied" do
|
11
|
+
it "raises an error" do
|
12
|
+
args = {}
|
13
|
+
expect { BinData::Array.new(args) }.to raise_error(ArgumentError)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "with some but not all mandatory parameters supplied" do
|
18
|
+
it "raises an error" do
|
19
|
+
args = {:initial_length => 3}
|
20
|
+
expect { BinData::Array.new(args) }.to raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "fails if a given type is unknown" do
|
25
|
+
args = {:type => :does_not_exist, :initial_length => 3}
|
26
|
+
expect { BinData::Array.new(args) }.to raise_error(BinData::UnRegisteredTypeError)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "does not allow both :initial_length and :read_until" do
|
30
|
+
args = {:initial_length => 3, :read_until => lambda { false } }
|
31
|
+
expect { BinData::Array.new(args) }.to raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "accepts BinData::Base as :type" do
|
35
|
+
obj = BinData::Int8.new(:initial_value => 5)
|
36
|
+
array = BinData::Array.new(:type => obj, :initial_length => 1)
|
37
|
+
array.should == [5]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe BinData::Array, "with no elements" do
|
42
|
+
subject { BinData::Array.new(:type => :example_single) }
|
43
|
+
|
44
|
+
it { should be_clear }
|
45
|
+
it { should be_empty }
|
46
|
+
its(:length) { should be_zero }
|
47
|
+
its(:first) { should be_nil }
|
48
|
+
its(:last) { should be_nil }
|
49
|
+
|
50
|
+
it "returns [] for the first n elements" do
|
51
|
+
subject.first(3).should == []
|
52
|
+
end
|
53
|
+
|
54
|
+
it "returns [] for the last n elements" do
|
55
|
+
subject.last(3).should == []
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe BinData::Array, "with several elements" do
|
60
|
+
subject {
|
61
|
+
type = [:example_single, {:initial_value => lambda { index + 1 }}]
|
62
|
+
BinData::Array.new(:type => type, :initial_length => 5)
|
63
|
+
}
|
64
|
+
|
65
|
+
it { should be_clear }
|
66
|
+
it { should_not be_empty }
|
67
|
+
its(:size) { should == 5 }
|
68
|
+
its(:length) { should == 5 }
|
69
|
+
its(:snapshot) { should == [1, 2, 3, 4, 5] }
|
70
|
+
its(:inspect) { should == "[1, 2, 3, 4, 5]" }
|
71
|
+
|
72
|
+
it "coerces to ::Array if required" do
|
73
|
+
[0].concat(subject).should == [0, 1, 2, 3, 4, 5]
|
74
|
+
end
|
75
|
+
|
76
|
+
it "uses methods from Enumerable" do
|
77
|
+
subject.select { |x| (x % 2) == 0 }.should == [2, 4]
|
78
|
+
end
|
79
|
+
|
80
|
+
it "assigns primitive values" do
|
81
|
+
subject.assign([4, 5, 6])
|
82
|
+
subject.should == [4, 5, 6]
|
83
|
+
end
|
84
|
+
|
85
|
+
it "assigns bindata objects" do
|
86
|
+
subject.assign([ExampleSingle.new(4), ExampleSingle.new(5), ExampleSingle.new(6)])
|
87
|
+
subject.should == [4, 5, 6]
|
88
|
+
end
|
89
|
+
|
90
|
+
it "assigns a bindata array" do
|
91
|
+
array = BinData::Array.new([4, 5, 6], :type => :example_single)
|
92
|
+
subject.assign(array)
|
93
|
+
subject.should == [4, 5, 6]
|
94
|
+
end
|
95
|
+
|
96
|
+
it "returns the first element" do
|
97
|
+
subject.first.should == 1
|
98
|
+
end
|
99
|
+
|
100
|
+
it "returns the first n elements" do
|
101
|
+
subject[0...3].should == [1, 2, 3]
|
102
|
+
subject.first(3).should == [1, 2, 3]
|
103
|
+
subject.first(99).should == [1, 2, 3, 4, 5]
|
104
|
+
end
|
105
|
+
|
106
|
+
it "returns the last element" do
|
107
|
+
subject.last.should == 5
|
108
|
+
subject[-1].should == 5
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns the last n elements" do
|
112
|
+
subject.last(3).should == [3, 4, 5]
|
113
|
+
subject.last(99).should == [1, 2, 3, 4, 5]
|
114
|
+
|
115
|
+
subject[-3, 100].should == [3, 4, 5]
|
116
|
+
end
|
117
|
+
|
118
|
+
it "clears all" do
|
119
|
+
subject[1] = 8
|
120
|
+
subject.clear
|
121
|
+
subject.should == [1, 2, 3, 4, 5]
|
122
|
+
end
|
123
|
+
|
124
|
+
it "clears a single element" do
|
125
|
+
subject[1] = 8
|
126
|
+
subject[1].clear
|
127
|
+
subject[1].should == 2
|
128
|
+
end
|
129
|
+
|
130
|
+
it "is clear if all elements are clear" do
|
131
|
+
subject[1] = 8
|
132
|
+
subject[1].clear
|
133
|
+
subject.should be_clear
|
134
|
+
end
|
135
|
+
|
136
|
+
it "tests clear status of individual elements" do
|
137
|
+
subject[1] = 8
|
138
|
+
subject[0].should be_clear
|
139
|
+
subject[1].should_not be_clear
|
140
|
+
end
|
141
|
+
|
142
|
+
it "directly accesses elements" do
|
143
|
+
subject[1] = 8
|
144
|
+
subject[1].should == 8
|
145
|
+
end
|
146
|
+
|
147
|
+
it "symmetrically reads and writes" do
|
148
|
+
subject[1] = 8
|
149
|
+
str = subject.to_binary_s
|
150
|
+
|
151
|
+
subject.clear
|
152
|
+
subject[1].should == 2
|
153
|
+
|
154
|
+
subject.read(str)
|
155
|
+
subject[1].should == 8
|
156
|
+
end
|
157
|
+
|
158
|
+
it "identifies index of elements" do
|
159
|
+
subject.index(3).should == 2
|
160
|
+
end
|
161
|
+
|
162
|
+
it "returns nil for index of non existent element" do
|
163
|
+
subject.index(42).should be_nil
|
164
|
+
end
|
165
|
+
|
166
|
+
it "has correct debug name" do
|
167
|
+
subject[2].debug_name.should == "obj[2]"
|
168
|
+
end
|
169
|
+
|
170
|
+
it "has correct offset" do
|
171
|
+
subject[2].offset.should == ExampleSingle.new.num_bytes * 2
|
172
|
+
end
|
173
|
+
|
174
|
+
it "has correct num_bytes" do
|
175
|
+
subject.num_bytes.should == 5 * ExampleSingle.new.num_bytes
|
176
|
+
end
|
177
|
+
|
178
|
+
it "has correct num_bytes for individual elements" do
|
179
|
+
subject[0].num_bytes.should == ExampleSingle.new.num_bytes
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe BinData::Array, "when accessing elements" do
|
184
|
+
subject {
|
185
|
+
type = [:example_single, {:initial_value => lambda { index + 1 }}]
|
186
|
+
data = BinData::Array.new(:type => type, :initial_length => 5)
|
187
|
+
data.assign([1, 2, 3, 4, 5])
|
188
|
+
data
|
189
|
+
}
|
190
|
+
|
191
|
+
it "inserts with positive indexes" do
|
192
|
+
subject.insert(2, 30, 40)
|
193
|
+
subject.snapshot.should == [1, 2, 30, 40, 3, 4, 5]
|
194
|
+
end
|
195
|
+
|
196
|
+
it "inserts with negative indexes" do
|
197
|
+
subject.insert(-2, 30, 40)
|
198
|
+
subject.snapshot.should == [1, 2, 3, 4, 30, 40, 5]
|
199
|
+
end
|
200
|
+
|
201
|
+
it "pushes" do
|
202
|
+
subject.push(30, 40)
|
203
|
+
subject.snapshot.should == [1, 2, 3, 4, 5, 30, 40]
|
204
|
+
end
|
205
|
+
|
206
|
+
it "concats" do
|
207
|
+
subject.concat([30, 40])
|
208
|
+
subject.snapshot.should == [1, 2, 3, 4, 5, 30, 40]
|
209
|
+
end
|
210
|
+
|
211
|
+
it "unshifts" do
|
212
|
+
subject.unshift(30, 40)
|
213
|
+
subject.snapshot.should == [30, 40, 1, 2, 3, 4, 5]
|
214
|
+
end
|
215
|
+
|
216
|
+
it "automatically extends on [index]" do
|
217
|
+
subject[9].should == 10
|
218
|
+
subject.snapshot.should == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
219
|
+
end
|
220
|
+
|
221
|
+
it "automatically extends on []=" do
|
222
|
+
subject[9] = 30
|
223
|
+
subject.snapshot.should == [1, 2, 3, 4, 5, 6, 7, 8, 9, 30]
|
224
|
+
end
|
225
|
+
|
226
|
+
it "automatically extends on insert" do
|
227
|
+
subject.insert(7, 30, 40)
|
228
|
+
subject.snapshot.should == [1, 2, 3, 4, 5, 6, 7, 30, 40]
|
229
|
+
end
|
230
|
+
|
231
|
+
it "does not extend on at" do
|
232
|
+
subject.at(9).should be_nil
|
233
|
+
subject.length.should == 5
|
234
|
+
end
|
235
|
+
|
236
|
+
it "does not extend on [start, length]" do
|
237
|
+
subject[9, 2].should be_nil
|
238
|
+
subject.length.should == 5
|
239
|
+
end
|
240
|
+
|
241
|
+
it "does not extend on [range]" do
|
242
|
+
subject[9 .. 10].should be_nil
|
243
|
+
subject.length.should == 5
|
244
|
+
end
|
245
|
+
|
246
|
+
it "raises error on bad input to []" do
|
247
|
+
expect { subject["a"] }.to raise_error(TypeError)
|
248
|
+
expect { subject[1, "a"] }.to raise_error(TypeError)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
describe BinData::Array, "with :read_until" do
|
253
|
+
|
254
|
+
context "containing +element+" do
|
255
|
+
it "reads until the sentinel is reached" do
|
256
|
+
read_until = lambda { element == 5 }
|
257
|
+
subject = BinData::Array.new(:type => :int8, :read_until => read_until)
|
258
|
+
|
259
|
+
subject.read "\x01\x02\x03\x04\x05\x06\x07\x08"
|
260
|
+
subject.should == [1, 2, 3, 4, 5]
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
context "containing +array+ and +index+" do
|
265
|
+
it "reads until the sentinel is reached" do
|
266
|
+
read_until = lambda { index >= 2 and array[index - 2] == 5 }
|
267
|
+
subject = BinData::Array.new(:type => :int8, :read_until => read_until)
|
268
|
+
|
269
|
+
subject.read "\x01\x02\x03\x04\x05\x06\x07\x08"
|
270
|
+
subject.should == [1, 2, 3, 4, 5, 6, 7]
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
context ":eof" do
|
275
|
+
it "reads records until eof" do
|
276
|
+
subject = BinData::Array.new(:type => :int8, :read_until => :eof)
|
277
|
+
|
278
|
+
subject.read "\x01\x02\x03"
|
279
|
+
subject.should == [1, 2, 3]
|
280
|
+
end
|
281
|
+
|
282
|
+
it "reads records until eof, ignoring partial records" do
|
283
|
+
subject = BinData::Array.new(:type => :int16be, :read_until => :eof)
|
284
|
+
|
285
|
+
subject.read "\x00\x01\x00\x02\x03"
|
286
|
+
subject.should == [1, 2]
|
287
|
+
end
|
288
|
+
|
289
|
+
it "reports exceptions" do
|
290
|
+
array_type = [:string, {:read_length => lambda { unknown_variable }}]
|
291
|
+
subject = BinData::Array.new(:type => array_type, :read_until => :eof)
|
292
|
+
expect { subject.read "\x00\x01\x00\x02\x03" }.to raise_error
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
describe BinData::Array, "nested within an Array" do
|
298
|
+
subject {
|
299
|
+
nested_array_params = { :type => [:int8, { :initial_value => :index }],
|
300
|
+
:initial_length => lambda { index + 1 } }
|
301
|
+
BinData::Array.new(:type => [:array, nested_array_params],
|
302
|
+
:initial_length => 3)
|
303
|
+
}
|
304
|
+
|
305
|
+
its(:snapshot) { should == [ [0], [0, 1], [0, 1, 2] ] }
|
306
|
+
|
307
|
+
it "maintains structure when reading" do
|
308
|
+
subject.read "\x04\x05\x06\x07\x08\x09"
|
309
|
+
subject.should == [ [4], [5, 6], [7, 8, 9] ]
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
describe BinData::Array, "subclassed" do
|
314
|
+
class IntArray < BinData::Array
|
315
|
+
endian :big
|
316
|
+
default_parameter :initial_element_value => 0
|
317
|
+
|
318
|
+
uint16 :initial_value => :initial_element_value
|
319
|
+
end
|
320
|
+
|
321
|
+
it "forwards parameters" do
|
322
|
+
subject = IntArray.new(:initial_length => 7)
|
323
|
+
subject.length.should == 7
|
324
|
+
end
|
325
|
+
|
326
|
+
it "overrides default parameters" do
|
327
|
+
subject = IntArray.new(:initial_length => 3, :initial_element_value => 5)
|
328
|
+
subject.to_binary_s.should == "\x00\x05\x00\x05\x00\x05"
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
@@ -0,0 +1,238 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "example"))
|
5
|
+
require 'bindata/base_primitive'
|
6
|
+
require 'bindata/io'
|
7
|
+
|
8
|
+
describe BinData::BasePrimitive do
|
9
|
+
it "is not registered" do
|
10
|
+
expect {
|
11
|
+
BinData::RegisteredClasses.lookup("BasePrimitive")
|
12
|
+
}.to raise_error(BinData::UnRegisteredTypeError)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe BinData::BasePrimitive, "all subclasses" do
|
17
|
+
class SubClassOfBasePrimitive < BinData::BasePrimitive
|
18
|
+
expose_methods_for_testing
|
19
|
+
end
|
20
|
+
|
21
|
+
subject { SubClassOfBasePrimitive.new }
|
22
|
+
|
23
|
+
it "raise errors on unimplemented methods" do
|
24
|
+
expect { subject.value_to_binary_string(nil) }.to raise_error(NotImplementedError)
|
25
|
+
expect { subject.read_and_return_value(nil) }.to raise_error(NotImplementedError)
|
26
|
+
expect { subject.sensible_default }.to raise_error(NotImplementedError)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe BinData::BasePrimitive do
|
31
|
+
it "conforms to rule 1 for returning a value" do
|
32
|
+
data = ExampleSingle.new(:value => 5)
|
33
|
+
data.should == 5
|
34
|
+
end
|
35
|
+
|
36
|
+
it "conforms to rule 2 for returning a value" do
|
37
|
+
io = ExampleSingle.io_with_value(42)
|
38
|
+
data = ExampleSingle.new(:value => 5)
|
39
|
+
data.read(io)
|
40
|
+
|
41
|
+
data.stub(:reading?).and_return(true)
|
42
|
+
data.should == 42
|
43
|
+
end
|
44
|
+
|
45
|
+
it "conforms to rule 3 for returning a value" do
|
46
|
+
data = ExampleSingle.new(:initial_value => 5)
|
47
|
+
data.should be_clear
|
48
|
+
data.should == 5
|
49
|
+
end
|
50
|
+
|
51
|
+
it "conforms to rule 4 for returning a value" do
|
52
|
+
data = ExampleSingle.new(:initial_value => 5)
|
53
|
+
data.assign(17)
|
54
|
+
data.should_not be_clear
|
55
|
+
data.should == 17
|
56
|
+
end
|
57
|
+
|
58
|
+
it "conforms to rule 5 for returning a value" do
|
59
|
+
data = ExampleSingle.new
|
60
|
+
data.should be_clear
|
61
|
+
data.should == 0
|
62
|
+
end
|
63
|
+
|
64
|
+
it "conforms to rule 6 for returning a value" do
|
65
|
+
data = ExampleSingle.new
|
66
|
+
data.assign(8)
|
67
|
+
data.should_not be_clear
|
68
|
+
data.should == 8
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe ExampleSingle do
|
73
|
+
subject { ExampleSingle.new(5) }
|
74
|
+
|
75
|
+
it "fails when assigning nil values" do
|
76
|
+
expect { subject.assign(nil) }.to raise_error(ArgumentError)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "sets and retrieves values" do
|
80
|
+
subject.assign(7)
|
81
|
+
subject.should == 7
|
82
|
+
end
|
83
|
+
|
84
|
+
it "sets and retrieves BinData::BasePrimitives" do
|
85
|
+
subject.assign(ExampleSingle.new(7))
|
86
|
+
subject.should == 7
|
87
|
+
end
|
88
|
+
|
89
|
+
it "responds to known methods" do
|
90
|
+
subject.should respond_to(:num_bytes)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "responds to known methods in #snapshot" do
|
94
|
+
subject.should respond_to(:div)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "does not respond to unknown methods in self or #snapshot" do
|
98
|
+
subject.should_not respond_to(:does_not_exist)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "behaves as #snapshot" do
|
102
|
+
(subject + 1).should == 6
|
103
|
+
(1 + subject).should == 6
|
104
|
+
end
|
105
|
+
|
106
|
+
it "is equal to other ExampleSingle" do
|
107
|
+
subject.should == ExampleSingle.new(5)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "is equal to raw values" do
|
111
|
+
subject.should == 5
|
112
|
+
5.should == subject
|
113
|
+
end
|
114
|
+
|
115
|
+
it "can be used as a hash key" do
|
116
|
+
hash = {5 => 17}
|
117
|
+
|
118
|
+
hash[subject].should == 17
|
119
|
+
end
|
120
|
+
|
121
|
+
it "is sortable" do
|
122
|
+
[ExampleSingle.new(5), ExampleSingle.new(3)].sort.should == [3, 5]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe BinData::BasePrimitive, "after initialisation" do
|
127
|
+
subject { ExampleSingle.new }
|
128
|
+
|
129
|
+
it "does not allow both :initial_value and :value" do
|
130
|
+
params = {:initial_value => 1, :value => 2}
|
131
|
+
expect { ExampleSingle.new(params) }.to raise_error(ArgumentError)
|
132
|
+
end
|
133
|
+
|
134
|
+
it { should be_clear }
|
135
|
+
its(:value) { should == 0 }
|
136
|
+
its(:num_bytes) { should == 4 }
|
137
|
+
|
138
|
+
it "has symmetric IO" do
|
139
|
+
subject.assign(42)
|
140
|
+
written = subject.to_binary_s
|
141
|
+
|
142
|
+
ExampleSingle.read(written).should == 42
|
143
|
+
end
|
144
|
+
|
145
|
+
it "sets and retrieves values" do
|
146
|
+
subject.value = 5
|
147
|
+
subject.value.should == 5
|
148
|
+
end
|
149
|
+
|
150
|
+
it "is not clear after setting value" do
|
151
|
+
subject.assign(5)
|
152
|
+
subject.should_not be_clear
|
153
|
+
end
|
154
|
+
|
155
|
+
it "is not clear after reading" do
|
156
|
+
subject.read("\x11\x22\x33\x44")
|
157
|
+
subject.should_not be_clear
|
158
|
+
end
|
159
|
+
|
160
|
+
it "returns a snapshot" do
|
161
|
+
subject.assign(5)
|
162
|
+
subject.snapshot.should == 5
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe BinData::BasePrimitive, "with :initial_value" do
|
167
|
+
subject { ExampleSingle.new(:initial_value => 5) }
|
168
|
+
|
169
|
+
its(:value) { should == 5 }
|
170
|
+
|
171
|
+
it "forgets :initial_value after being set" do
|
172
|
+
subject.assign(17)
|
173
|
+
subject.should_not == 5
|
174
|
+
end
|
175
|
+
|
176
|
+
it "forgets :initial_value after reading" do
|
177
|
+
subject.read("\x11\x22\x33\x44")
|
178
|
+
subject.should_not == 5
|
179
|
+
end
|
180
|
+
|
181
|
+
it "remembers :initial_value after being cleared" do
|
182
|
+
subject.assign(17)
|
183
|
+
subject.clear
|
184
|
+
subject.should == 5
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
describe BinData::BasePrimitive, "with :value" do
|
189
|
+
subject { ExampleSingle.new(:value => 5) }
|
190
|
+
|
191
|
+
its(:value) { should == 5 }
|
192
|
+
|
193
|
+
let(:io) { ExampleSingle.io_with_value(56) }
|
194
|
+
|
195
|
+
it "changes during reading" do
|
196
|
+
subject.read(io)
|
197
|
+
subject.stub(:reading?).and_return(true)
|
198
|
+
subject.should == 56
|
199
|
+
end
|
200
|
+
|
201
|
+
it "does not change after reading" do
|
202
|
+
subject.read(io)
|
203
|
+
subject.should == 5
|
204
|
+
end
|
205
|
+
|
206
|
+
it "is unaffected by assigning" do
|
207
|
+
subject.assign(17)
|
208
|
+
subject.should == 5
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
describe BinData::BasePrimitive, "checking read value" do
|
213
|
+
let(:io) { ExampleSingle.io_with_value(12) }
|
214
|
+
|
215
|
+
context ":check_value is non boolean" do
|
216
|
+
it "succeeds when check_value is correct" do
|
217
|
+
data = ExampleSingle.new(:check_value => 12)
|
218
|
+
expect { data.read(io) }.not_to raise_error
|
219
|
+
end
|
220
|
+
|
221
|
+
it "fails when check_value is incorrect" do
|
222
|
+
data = ExampleSingle.new(:check_value => lambda { 99 })
|
223
|
+
expect { data.read(io) }.to raise_error(BinData::ValidityError)
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
context ":check_value is boolean" do
|
228
|
+
it "succeeds when check_value is true" do
|
229
|
+
data = ExampleSingle.new(:check_value => lambda { value < 20 })
|
230
|
+
expect { data.read(io) }.not_to raise_error
|
231
|
+
end
|
232
|
+
|
233
|
+
it "fails when check_value is false" do
|
234
|
+
data = ExampleSingle.new(:check_value => lambda { value > 20 })
|
235
|
+
expect { data.read(io) }.to raise_error(BinData::ValidityError)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|