bindata 1.5.1 → 1.6.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.

Files changed (61) hide show
  1. data/ChangeLog.rdoc +7 -0
  2. data/NEWS.rdoc +11 -0
  3. data/Rakefile +6 -1
  4. data/bindata.gemspec +2 -1
  5. data/doc/manual.md +17 -9
  6. data/examples/gzip.rb +2 -2
  7. data/examples/list.rb +2 -2
  8. data/lib/bindata/alignment.rb +4 -9
  9. data/lib/bindata/array.rb +57 -51
  10. data/lib/bindata/base.rb +13 -110
  11. data/lib/bindata/base_primitive.rb +130 -75
  12. data/lib/bindata/bits.rb +5 -7
  13. data/lib/bindata/choice.rb +24 -32
  14. data/lib/bindata/dsl.rb +1 -6
  15. data/lib/bindata/framework.rb +81 -0
  16. data/lib/bindata/int.rb +5 -7
  17. data/lib/bindata/name.rb +28 -0
  18. data/lib/bindata/offset.rb +42 -53
  19. data/lib/bindata/params.rb +33 -38
  20. data/lib/bindata/struct.rb +2 -6
  21. data/lib/bindata/trace.rb +16 -16
  22. data/lib/bindata/version.rb +1 -1
  23. data/lib/bindata/virtual.rb +3 -3
  24. data/{spec/alignment_spec.rb → test/alignment_test.rb} +17 -16
  25. data/test/array_test.rb +371 -0
  26. data/test/base_primitive_test.rb +312 -0
  27. data/test/base_test.rb +183 -0
  28. data/{spec/bits_spec.rb → test/bits_test.rb} +59 -59
  29. data/test/choice_test.rb +260 -0
  30. data/{spec/spec_common.rb → test/common.rb} +33 -18
  31. data/test/count_bytes_remaining_test.rb +41 -0
  32. data/{spec/deprecated_spec.rb → test/deprecated_test.rb} +5 -7
  33. data/test/float_test.rb +72 -0
  34. data/{spec/int_spec.rb → test/int_test.rb} +34 -43
  35. data/{spec/io_spec.rb → test/io_test.rb} +70 -71
  36. data/{spec/lazy_spec.rb → test/lazy_test.rb} +38 -39
  37. data/test/offset_test.rb +93 -0
  38. data/test/params_test.rb +144 -0
  39. data/{spec/primitive_spec.rb → test/primitive_test.rb} +42 -54
  40. data/{spec/record_spec.rb → test/record_test.rb} +133 -154
  41. data/test/registry_test.rb +104 -0
  42. data/test/rest_test.rb +29 -0
  43. data/test/skip_test.rb +28 -0
  44. data/{spec/string_spec.rb → test/string_test.rb} +96 -97
  45. data/test/stringz_test.rb +127 -0
  46. data/{spec/struct_spec.rb → test/struct_test.rb} +119 -120
  47. data/{spec/system_spec.rb → test/system_test.rb} +66 -106
  48. metadata +39 -38
  49. data/lib/a.rb +0 -24
  50. data/spec/array_spec.rb +0 -331
  51. data/spec/base_primitive_spec.rb +0 -238
  52. data/spec/base_spec.rb +0 -376
  53. data/spec/choice_spec.rb +0 -263
  54. data/spec/count_bytes_remaining_spec.rb +0 -38
  55. data/spec/example.rb +0 -21
  56. data/spec/float_spec.rb +0 -37
  57. data/spec/registry_spec.rb +0 -108
  58. data/spec/rest_spec.rb +0 -26
  59. data/spec/skip_spec.rb +0 -27
  60. data/spec/stringz_spec.rb +0 -118
  61. data/tasks/rspec.rake +0 -17
data/lib/a.rb DELETED
@@ -1,24 +0,0 @@
1
- require 'bindata'
2
-
3
- class A < BinData::Choice
4
- int8 1
5
- uint8 :default
6
- end
7
-
8
- a = A.new(:selection => 1)
9
-
10
- p a.class
11
-
12
- class BinData::Choice
13
- def selected_object
14
- current_choice
15
- end
16
- end
17
-
18
- case a.selected_object
19
- when BinData::Int8
20
- p "int8"
21
- else
22
- p "else"
23
- end
24
-
data/spec/array_spec.rb DELETED
@@ -1,331 +0,0 @@
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
-
@@ -1,238 +0,0 @@
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