bindata 1.8.3 → 2.0.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.

@@ -49,11 +49,7 @@ class ExampleSingle < BinData::BasePrimitive
49
49
  end
50
50
 
51
51
  def binary(str)
52
- if str.respond_to?(:force_encoding)
53
- str.dup.force_encoding(Encoding::BINARY)
54
- else
55
- str
56
- end
52
+ str.dup.force_encoding(Encoding::BINARY)
57
53
  end
58
54
 
59
55
  module Kernel
@@ -152,21 +152,6 @@ describe BinData::IO::Write, "when writing" do
152
152
  stream.value.must_equal "abcd"
153
153
  end
154
154
 
155
- it "has #offset" do
156
- io.offset.must_equal 0
157
-
158
- io.writebytes("abcd")
159
- io.offset.must_equal 4
160
-
161
- io.writebytes("ABCD")
162
- io.offset.must_equal 8
163
- end
164
-
165
- it "rounds up #offset when writing bits" do
166
- io.writebits(123, 9, :little)
167
- io.offset.must_equal 2
168
- end
169
-
170
155
  it "flushes" do
171
156
  io.writebytes("abcd")
172
157
  io.flush
@@ -73,16 +73,12 @@ end
73
73
 
74
74
  describe BinData::LazyEvaluator, "with one parent" do
75
75
  subject {
76
- parent_methods = {:m1 => 'Pm1', :m2 => 'Pm2', :com1 => 'PmC', :mm => 3}
76
+ parent_methods = {:m1 => 'Pm1', :com1 => 'PmC', :mm => 3}
77
77
  parent_params = {:p1 => 'Pp1', :com1 => 'PpC'}
78
78
  parent_obj = MockBinDataObject.new(parent_methods, parent_params)
79
79
 
80
- class << parent_obj
81
- def echo(a1, a2)
82
- [a1, a2]
83
- end
84
-
85
- private :m2
80
+ def parent_obj.echo(a1, a2)
81
+ [a1, a2]
86
82
  end
87
83
 
88
84
  methods = {:m1 => 'm1', :com1 => 'mC'}
@@ -122,10 +118,6 @@ describe BinData::LazyEvaluator, "with one parent" do
122
118
  lazy_eval(lambda { echo(p1, m1) }).must_equal ['Pp1', 'Pm1']
123
119
  end
124
120
 
125
- it "invokes private methods in the parent" do
126
- lazy_eval(lambda { m2 }).must_equal 'Pm2'
127
- end
128
-
129
121
  it "resolves parameters in preference to methods in the parent" do
130
122
  lazy_eval(lambda { com1 }).must_equal 'PpC'
131
123
  end
@@ -105,12 +105,12 @@ describe BinData::Record, "with anonymous fields" do
105
105
  let(:obj) { AnonymousRecord.new }
106
106
 
107
107
  it "only shows non anonymous fields" do
108
- obj.field_names.must_equal ["a"]
108
+ obj.field_names.must_equal [:a]
109
109
  end
110
110
 
111
111
  it "does not include anonymous fields in snapshot" do
112
112
  obj.a = 5
113
- obj.snapshot.must_equal({"a" => 5})
113
+ obj.snapshot.must_equal({:a => 5})
114
114
  end
115
115
 
116
116
  it "writes anonymous fields" do
@@ -133,7 +133,7 @@ describe BinData::Record, "with hidden fields" do
133
133
  let(:obj) { HiddenRecord.new }
134
134
 
135
135
  it "only shows fields that aren't hidden" do
136
- obj.field_names.must_equal ["a", "d"]
136
+ obj.field_names.must_equal [:a, :d]
137
137
  end
138
138
 
139
139
  it "accesses hidden fields directly" do
@@ -146,7 +146,7 @@ describe BinData::Record, "with hidden fields" do
146
146
 
147
147
  it "does not include hidden fields in snapshot" do
148
148
  obj.b = 5
149
- obj.snapshot.must_equal({"a" => 0, "d" => 5})
149
+ obj.snapshot.must_equal({:a => 0, :d => 5})
150
150
  end
151
151
  end
152
152
 
@@ -198,11 +198,11 @@ describe BinData::Record, "with multiple fields" do
198
198
  snap = obj.snapshot
199
199
  snap.a.must_equal 1
200
200
  snap.b.must_equal 2
201
- snap.must_equal({ "a" => 1, "b" => 2 })
201
+ snap.must_equal({ :a => 1, :b => 2 })
202
202
  end
203
203
 
204
204
  it "returns field_names" do
205
- obj.field_names.must_equal ["a", "b"]
205
+ obj.field_names.must_equal [:a, :b]
206
206
  end
207
207
 
208
208
  it "fails on unknown method call" do
@@ -227,11 +227,11 @@ describe BinData::Record, "with nested structs" do
227
227
  let(:obj) { NestedStructRecord.new }
228
228
 
229
229
  it "includes nested field names" do
230
- obj.field_names.must_equal ["a", "b", "c"]
230
+ obj.field_names.must_equal [:a, :b, :c]
231
231
  end
232
232
 
233
233
  it "hides nested field names" do
234
- obj.b.field_names.must_equal ["x"]
234
+ obj.b.field_names.must_equal [:x]
235
235
  end
236
236
 
237
237
  it "accesses nested fields" do
@@ -241,12 +241,12 @@ describe BinData::Record, "with nested structs" do
241
241
  obj.c.y.must_equal 3
242
242
  end
243
243
 
244
- it "returns correct abs_offset" do
245
- obj.abs_offset.must_equal 0
246
- obj.b.abs_offset.must_equal 1
247
- obj.b.w.abs_offset.must_equal 1
248
- obj.c.abs_offset.must_equal 3
249
- obj.c.z.abs_offset.must_equal 4
244
+ it "returns correct offset" do
245
+ obj.offset.must_equal 0
246
+ obj.b.offset.must_equal 1
247
+ obj.b.w.offset.must_equal 1
248
+ obj.c.offset.must_equal 3
249
+ obj.c.z.offset.must_equal 4
250
250
  end
251
251
 
252
252
  it "returns correct rel_offset" do
@@ -276,7 +276,7 @@ describe BinData::Record, "with nested array of primitives" do
276
276
  let(:obj) { NestedPrimitiveArrayRecord.new }
277
277
 
278
278
  it "uses block as :type" do
279
- obj.snapshot.must_equal({"a" => [0, 1, 2]})
279
+ obj.snapshot.must_equal({:a => [0, 1, 2]})
280
280
  end
281
281
  end
282
282
 
@@ -292,7 +292,7 @@ describe BinData::Record, "with nested array of structs" do
292
292
 
293
293
  it "uses block as struct for :type" do
294
294
  obj.a[0].b = 2
295
- obj.snapshot.must_equal({"a" => [{"b" => 2, "c" => 0}]})
295
+ obj.snapshot.must_equal({:a => [{:b => 2, :c => 0}]})
296
296
  end
297
297
  end
298
298
 
@@ -472,19 +472,13 @@ describe BinData::Record, "with :onlyif" do
472
472
 
473
473
  it "initial state" do
474
474
  obj.num_bytes.must_equal 2
475
- obj.snapshot.must_equal({"a" => 3, "b" => 5})
475
+ obj.snapshot.must_equal({:a => 3, :b => 5})
476
476
  obj.to_binary_s.must_equal "\x03\x05"
477
477
  end
478
478
 
479
- it "identifies if fields are included" do
480
- obj.a?.must_equal true
481
- obj.b?.must_equal true
482
- obj.c?.must_equal false
483
- end
484
-
485
479
  it "reads as lambdaed" do
486
480
  obj.read("\x01\x02")
487
- obj.snapshot.must_equal({"a" => 1, "c" => 2})
481
+ obj.snapshot.must_equal({:a => 1, :c => 2})
488
482
  end
489
483
  end
490
484
 
@@ -502,14 +496,14 @@ describe BinData::Record, "derived classes" do
502
496
  end
503
497
 
504
498
  it "does not affect parent" do
505
- ParentRecord.new.field_names.must_equal ["a"]
499
+ ParentRecord.new.field_names.must_equal [:a]
506
500
  end
507
501
 
508
502
  it "inherits fields for first child" do
509
- Child1Record.new.field_names.must_equal ["a", "b"]
503
+ Child1Record.new.field_names.must_equal [:a, :b]
510
504
  end
511
505
 
512
506
  it "inherits fields for second child" do
513
- Child2Record.new.field_names.must_equal ["a", "b", "c"]
507
+ Child2Record.new.field_names.must_equal [:a, :b, :c]
514
508
  end
515
509
  end
@@ -260,40 +260,38 @@ describe BinData::String, "with :pad_front" do
260
260
  end
261
261
 
262
262
  describe BinData::String, "with Ruby 1.9 encodings" do
263
- if RUBY_VERSION >= "1.9"
264
- class UTF8String < BinData::String
265
- def snapshot
266
- super.force_encoding('UTF-8')
267
- end
263
+ class UTF8String < BinData::String
264
+ def snapshot
265
+ super.force_encoding('UTF-8')
268
266
  end
267
+ end
269
268
 
270
- let(:obj) { UTF8String.new }
271
- let(:binary_str) { binary("\xC3\x85\xC3\x84\xC3\x96") }
272
- let(:utf8_str) { binary_str.dup.force_encoding('UTF-8') }
269
+ let(:obj) { UTF8String.new }
270
+ let(:binary_str) { binary("\xC3\x85\xC3\x84\xC3\x96") }
271
+ let(:utf8_str) { binary_str.dup.force_encoding('UTF-8') }
273
272
 
274
- it "stores assigned values as binary" do
275
- obj.assign(utf8_str)
276
- obj.to_binary_s.must_equal binary_str
277
- end
273
+ it "stores assigned values as binary" do
274
+ obj.assign(utf8_str)
275
+ obj.to_binary_s.must_equal binary_str
276
+ end
278
277
 
279
- it "stores read values as binary" do
280
- obj = UTF8String.new(:read_length => binary_str.length)
281
- obj.read(binary_str)
278
+ it "stores read values as binary" do
279
+ obj = UTF8String.new(:read_length => binary_str.length)
280
+ obj.read(binary_str)
282
281
 
283
- obj.to_binary_s.must_equal binary_str
284
- end
282
+ obj.to_binary_s.must_equal binary_str
283
+ end
285
284
 
286
- it "returns values in correct encoding" do
287
- obj.assign(utf8_str)
285
+ it "returns values in correct encoding" do
286
+ obj.assign(utf8_str)
288
287
 
289
- obj.snapshot.must_equal utf8_str
290
- end
288
+ obj.snapshot.must_equal utf8_str
289
+ end
291
290
 
292
- it "has correct num_bytes" do
293
- obj.assign(utf8_str)
291
+ it "has correct num_bytes" do
292
+ obj.assign(utf8_str)
294
293
 
295
- obj.num_bytes.must_equal binary_str.length
296
- end
294
+ obj.num_bytes.must_equal binary_str.length
297
295
  end
298
296
  end
299
297
 
@@ -51,12 +51,12 @@ describe BinData::Struct, "with anonymous fields" do
51
51
  }
52
52
 
53
53
  it "only shows non anonymous fields" do
54
- obj.field_names.must_equal ["a"]
54
+ obj.field_names.must_equal [:a]
55
55
  end
56
56
 
57
57
  it "does not include anonymous fields in snapshot" do
58
58
  obj.a = 5
59
- obj.snapshot.must_equal({"a" => 5})
59
+ obj.snapshot.must_equal({:a => 5})
60
60
  end
61
61
 
62
62
  it "writes anonymous fields" do
@@ -78,7 +78,7 @@ describe BinData::Struct, "with hidden fields" do
78
78
  }
79
79
 
80
80
  it "only shows fields that aren't hidden" do
81
- obj.field_names.must_equal ["a", "d"]
81
+ obj.field_names.must_equal [:a, :d]
82
82
  end
83
83
 
84
84
  it "accesses hidden fields directly" do
@@ -91,7 +91,7 @@ describe BinData::Struct, "with hidden fields" do
91
91
 
92
92
  it "does not include hidden fields in snapshot" do
93
93
  obj.b = 7
94
- obj.snapshot.must_equal({"a" => 0, "d" => 7})
94
+ obj.snapshot.must_equal({:a => 0, :d => 7})
95
95
  end
96
96
 
97
97
  it "detects hidden fields with has_key?" do
@@ -103,7 +103,7 @@ describe BinData::Struct, "with multiple fields" do
103
103
  let(:params) { { :fields => [ [:int8, :a], [:int8, :b] ] } }
104
104
  let(:obj) { BinData::Struct.new({:a => 1, :b => 2}, params) }
105
105
 
106
- specify { obj.field_names.must_equal ["a", "b"] }
106
+ specify { obj.field_names.must_equal [:a, :b] }
107
107
  specify { obj.to_binary_s.must_equal "\x01\x02" }
108
108
 
109
109
  it "returns num_bytes" do
@@ -156,17 +156,17 @@ describe BinData::Struct, "with multiple fields" do
156
156
  snap = obj.snapshot
157
157
  snap.a.must_equal 1
158
158
  snap.b.must_equal 2
159
- snap.must_equal({ "a" => 1, "b" => 2 })
159
+ snap.must_equal({ :a => 1, :b => 2 })
160
160
  end
161
161
 
162
162
  it "assigns from partial hash" do
163
- obj.assign("a" => 3)
163
+ obj.assign(:a => 3)
164
164
  obj.a.must_equal 3
165
165
  obj.b.must_equal 0
166
166
  end
167
167
 
168
168
  it "assigns from hash" do
169
- obj.assign("a" => 3, "b" => 4)
169
+ obj.assign(:a => 3, :b => 4)
170
170
  obj.a.must_equal 3
171
171
  obj.b.must_equal 4
172
172
  end
@@ -202,19 +202,19 @@ describe BinData::Struct, "with multiple fields" do
202
202
 
203
203
  describe "#snapshot" do
204
204
  it "has ordered #keys" do
205
- obj.snapshot.keys.must_equal ["a", "b"]
205
+ obj.snapshot.keys.must_equal [:a, :b]
206
206
  end
207
207
 
208
208
  it "has ordered #each" do
209
209
  keys = []
210
210
  obj.snapshot.each { |el| keys << el[0] }
211
- keys.must_equal ["a", "b"]
211
+ keys.must_equal [:a, :b]
212
212
  end
213
213
 
214
214
  it "has ordered #each_pair" do
215
215
  keys = []
216
216
  obj.snapshot.each_pair { |k, v| keys << k }
217
- keys.must_equal ["a", "b"]
217
+ keys.must_equal [:a, :b]
218
218
  end
219
219
  end
220
220
  end
@@ -234,7 +234,7 @@ describe BinData::Struct, "with nested structs" do
234
234
  BinData::Struct.new(params)
235
235
  }
236
236
 
237
- specify { obj.field_names.must_equal ["a", "b", "c"] }
237
+ specify { obj.field_names.must_equal [:a, :b, :c] }
238
238
 
239
239
  it "returns num_bytes" do
240
240
  obj.b.num_bytes.must_equal 2
@@ -250,11 +250,11 @@ describe BinData::Struct, "with nested structs" do
250
250
  obj.c.z.must_equal 0
251
251
  end
252
252
 
253
- it "returns correct abs_offset" do
254
- obj.b.abs_offset.must_equal 1
255
- obj.b.w.abs_offset.must_equal 1
256
- obj.c.abs_offset.must_equal 3
257
- obj.c.z.abs_offset.must_equal 4
253
+ it "returns correct offset" do
254
+ obj.b.offset.must_equal 1
255
+ obj.b.w.offset.must_equal 1
256
+ obj.c.offset.must_equal 3
257
+ obj.c.z.offset.must_equal 4
258
258
  end
259
259
  end
260
260
 
@@ -313,10 +313,10 @@ describe BinData::Struct, "with bit fields" do
313
313
  end
314
314
 
315
315
  it "has correct offsets" do
316
- obj.a.rel_offset.must_equal 0
317
- obj.b.rel_offset.must_equal 0
318
- obj.c.rel_offset.must_equal 1
319
- obj.d.rel_offset.must_equal 2
316
+ obj.a.offset.must_equal 0
317
+ obj.b.offset.must_equal 0
318
+ obj.c.offset.must_equal 1
319
+ obj.d.offset.must_equal 2
320
320
  end
321
321
  end
322
322
 
@@ -338,37 +338,6 @@ describe BinData::Struct, "with nested endian" do
338
338
  end
339
339
  end
340
340
 
341
- describe BinData::Struct, "with byte_align" do
342
- let(:obj) {
343
- params = { :fields => [[:int8, :a],
344
- [:int8, :b, {:byte_align => 5}],
345
- [:bit2, :c],
346
- [:int8, :d, {:byte_align => 3}]] }
347
- BinData::Struct.new(params)
348
- }
349
-
350
- it "has #num_bytes" do
351
- obj.num_bytes.must_equal 10
352
- end
353
-
354
- it "reads" do
355
- obj.read("\x01\x00\x00\x00\x00\x02\xc0\x00\x00\x04")
356
- obj.snapshot.must_equal({ "a" => 1, "b" => 2, "c" => 3, "d" => 4 })
357
- end
358
-
359
- it "writes" do
360
- obj.assign(:a => 1, :b => 2, :c => 3, :d => 4)
361
- obj.to_binary_s.must_equal binary("\x01\x00\x00\x00\x00\x02\xc0\x00\x00\x04")
362
- end
363
-
364
- it "has correct offsets" do
365
- obj.a.rel_offset.must_equal 0
366
- obj.b.rel_offset.must_equal 5
367
- obj.c.rel_offset.must_equal 6
368
- obj.d.rel_offset.must_equal 9
369
- end
370
- end
371
-
372
341
  describe BinData::Struct, "with dynamically named types" do
373
342
  it "instantiates" do
374
343
  dyn = BinData::Struct.new(:name => :my_struct, :fields => [[:int8, :a, {:initial_value => 3}]])
@@ -17,7 +17,7 @@ describe "lambdas with index" do
17
17
  it "uses index of nearest containing array" do
18
18
  arr = BinData::Array.new(:type => :nested_lambda_with_index,
19
19
  :initial_length => 3)
20
- arr.snapshot.must_equal [{"a" => 0}, {"a" => 10}, {"a" => 20}]
20
+ arr.snapshot.must_equal [{:a => 0}, {:a => 10}, {:a => 20}]
21
21
  end
22
22
 
23
23
  it "fails if there is no containing array" do
@@ -93,7 +93,7 @@ describe BinData::Record, "with choice field" do
93
93
 
94
94
  it "has correct offset" do
95
95
  obj = RecordWithNestedChoiceField.new
96
- obj.x.b.abs_offset.must_equal 2
96
+ obj.x.b.offset.must_equal 2
97
97
  end
98
98
  end
99
99
 
@@ -240,17 +240,17 @@ describe "Forward referencing with Primitive" do
240
240
  let(:obj) { FRPrimitive.new }
241
241
 
242
242
  it "initialises" do
243
- obj.snapshot.must_equal({"len" => 0, "data" => ""})
243
+ obj.snapshot.must_equal({:len => 0, :data => ""})
244
244
  end
245
245
 
246
246
  it "reads" do
247
247
  obj.read("\x04test")
248
- obj.snapshot.must_equal({"len" => 4, "data" => "test"})
248
+ obj.snapshot.must_equal({:len => 4, :data => "test"})
249
249
  end
250
250
 
251
251
  it "sets value" do
252
252
  obj.data = "hello"
253
- obj.snapshot.must_equal({"len" => 5, "data" => "hello"})
253
+ obj.snapshot.must_equal({:len => 5, :data => "hello"})
254
254
  end
255
255
  end
256
256
 
@@ -263,17 +263,17 @@ describe "Forward referencing with Array" do
263
263
  let(:obj) { FRArray.new }
264
264
 
265
265
  it "initialises" do
266
- obj.snapshot.must_equal({"len" => 0, "data" => []})
266
+ obj.snapshot.must_equal({:len => 0, :data => []})
267
267
  end
268
268
 
269
269
  it "reads" do
270
270
  obj.read("\x04\x01\x02\x03\x04")
271
- obj.snapshot.must_equal({"len" => 4, "data" => [1, 2, 3, 4]})
271
+ obj.snapshot.must_equal({:len => 4, :data => [1, 2, 3, 4]})
272
272
  end
273
273
 
274
274
  it "sets value" do
275
275
  obj.data = [1, 2, 3]
276
- obj.snapshot.must_equal({"len" => 3, "data" => [1, 2, 3]})
276
+ obj.snapshot.must_equal({:len => 3, :data => [1, 2, 3]})
277
277
  end
278
278
  end
279
279
 
@@ -299,7 +299,7 @@ describe BinData::Record, "with custom sized integers" do
299
299
 
300
300
  it "reads as expected" do
301
301
  str = "\x00\x00\x00\x00\x05"
302
- CustomIntRecord.read(str).snapshot.must_equal({"a" => 5})
302
+ CustomIntRecord.read(str).snapshot.must_equal({:a => 5})
303
303
  end
304
304
  end
305
305