bindata 2.5.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/record_test.rb CHANGED
@@ -5,7 +5,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
5
5
  describe BinData::Record do
6
6
  it "is not registered" do
7
7
  _ {
8
- BinData::RegisteredClasses.lookup("Record")
8
+ BinData::RegisteredClasses.lookup("BinData", "Record")
9
9
  }.must_raise BinData::UnRegisteredTypeError
10
10
  end
11
11
  end
@@ -101,6 +101,15 @@ describe BinData::Record, "when defining with errors" do
101
101
  }.must_raise_on_line SyntaxError, 3, "endian must be called before defining fields in BadEndianPosRecord"
102
102
  end
103
103
 
104
+ it "fails when search_namespace is after a field" do
105
+ _ {
106
+ class BadSearchNamespacePosRecord < BinData::Record
107
+ string :a
108
+ search_namespace :ns
109
+ end
110
+ }.must_raise_on_line SyntaxError, 3, "search_namespace must be called before defining fields in BadSearchNamespacePosRecord"
111
+ end
112
+
104
113
  it "fails when search_prefix is after a field" do
105
114
  _ {
106
115
  class BadSearchPrefixPosRecord < BinData::Record
@@ -396,6 +405,88 @@ describe BinData::Record, "with an endian defined" do
396
405
  end
397
406
  end
398
407
 
408
+ describe BinData::Record, "with namespaces" do
409
+ module ModA
410
+ class MyInt < BinData::Int8; end
411
+ class ARecord < BinData::Record
412
+ my_int :a
413
+ end
414
+ end
415
+
416
+ module ModB
417
+ class BRecord < BinData::Record
418
+ mod_a_my_int :a
419
+ end
420
+ end
421
+
422
+ it "looks up with relative name" do
423
+ obj = ModA::ARecord.new
424
+ _(obj.a.class.name).must_equal "ModA::MyInt"
425
+ end
426
+
427
+ it "looks up with absolute name" do
428
+ obj = ModB::BRecord.new
429
+ _(obj.a.class.name).must_equal "ModA::MyInt"
430
+ end
431
+ end
432
+
433
+ describe BinData::Record, "with search_namespace" do
434
+ module ModA
435
+ class Snamespace < BinData::Int8; end
436
+ end
437
+ module ModB
438
+ class Snamespace < BinData::Int8; end
439
+ end
440
+
441
+ class RecordWithSearchNamespace < BinData::Record
442
+ search_namespace :ModA
443
+ snamespace :f
444
+ end
445
+
446
+ class RecordWithParentSearchNamespace < BinData::Record
447
+ search_namespace :ModA
448
+ struct :s do
449
+ snamespace :f
450
+ end
451
+ end
452
+
453
+ class RecordWithNestedSearchNamespace < BinData::Record
454
+ search_namespace :ModA
455
+ struct :s do
456
+ search_namespace :x
457
+ snamespace :f
458
+ end
459
+ end
460
+
461
+ class RecordWithPrioritisedNestedSearchNamespace < BinData::Record
462
+ search_namespace :ModB
463
+ struct :s do
464
+ search_namespace :ModA
465
+ snamespace :f
466
+ end
467
+ end
468
+
469
+ it "uses search_prefix" do
470
+ obj = RecordWithSearchNamespace.new
471
+ _(obj.f.class.name).must_equal "ModA::Snamespace"
472
+ end
473
+
474
+ it "uses parent search_prefix" do
475
+ obj = RecordWithParentSearchNamespace.new
476
+ _(obj.s.f.class.name).must_equal "ModA::Snamespace"
477
+ end
478
+
479
+ it "uses nested search_prefix" do
480
+ obj = RecordWithNestedSearchNamespace.new
481
+ _(obj.s.f.class.name).must_equal "ModA::Snamespace"
482
+ end
483
+
484
+ it "uses prioritised nested search_prefix" do
485
+ obj = RecordWithPrioritisedNestedSearchNamespace.new
486
+ _(obj.s.f.class.name).must_equal "ModA::Snamespace"
487
+ end
488
+ end
489
+
399
490
  describe BinData::Record, "with search_prefix" do
400
491
  class ASprefix < BinData::Int8; end
401
492
  class BSprefix < BinData::Int8; end
@@ -449,6 +540,29 @@ describe BinData::Record, "with search_prefix" do
449
540
  end
450
541
  end
451
542
 
543
+ describe BinData::Record, "with endian :big_and_little and search_namespace" do
544
+ module Ns1
545
+ class BNLIntBe < BinData::Int16be; end
546
+ class BNLIntLe < BinData::Int16le; end
547
+ end
548
+
549
+ class RecordWithBnLEndianAndSearchNamespace < BinData::Record
550
+ endian :big_and_little
551
+ search_namespace :ns1
552
+ bnl_int :a, value: 1
553
+ end
554
+
555
+ it "creates big endian version" do
556
+ obj = RecordWithBnLEndianAndSearchNamespaceBe.new
557
+ _(obj.to_binary_s).must_equal_binary "\x00\x01"
558
+ end
559
+
560
+ it "creates little endian version" do
561
+ obj = RecordWithBnLEndianAndSearchNamespaceLe.new
562
+ _(obj.to_binary_s).must_equal_binary "\x01\x00"
563
+ end
564
+ end
565
+
452
566
  describe BinData::Record, "with endian :big_and_little" do
453
567
  class RecordWithBnLEndian < BinData::Record
454
568
  endian :big_and_little
@@ -457,7 +571,7 @@ describe BinData::Record, "with endian :big_and_little" do
457
571
 
458
572
  it "is not registered" do
459
573
  _ {
460
- BinData::RegisteredClasses.lookup("RecordWithBnLEndian")
574
+ BinData::RegisteredClasses.lookup("", "RecordWithBnLEndian")
461
575
  }.must_raise BinData::UnRegisteredTypeError
462
576
  end
463
577
 
@@ -515,7 +629,7 @@ describe BinData::Record, "with endian :big_and_little when subclassed" do
515
629
 
516
630
  it "is not registered" do
517
631
  _ {
518
- BinData::RegisteredClasses.lookup("BRecordWithBnLEndian")
632
+ BinData::RegisteredClasses.lookup("", "BRecordWithBnLEndian")
519
633
  }.must_raise BinData::UnRegisteredTypeError
520
634
  end
521
635
 
@@ -7,31 +7,32 @@ describe BinData::Registry do
7
7
  B = Class.new
8
8
  C = Class.new
9
9
  D = Class.new
10
+ E = Class.new
10
11
 
11
12
  let(:r) { BinData::Registry.new }
12
13
 
13
14
  it "lookups registered names" do
14
- r.register('ASubClass', A)
15
- r.register('AnotherSubClass', B)
15
+ r.register("", 'ASubClass', A)
16
+ r.register("", 'AnotherSubClass', B)
16
17
 
17
- _(r.lookup('ASubClass')).must_equal A
18
- _(r.lookup('a_sub_class')).must_equal A
19
- _(r.lookup('AnotherSubClass')).must_equal B
20
- _(r.lookup('another_sub_class')).must_equal B
18
+ _(r.lookup("", 'ASubClass')).must_equal A
19
+ _(r.lookup("", 'a_sub_class')).must_equal A
20
+ _(r.lookup("", 'AnotherSubClass')).must_equal B
21
+ _(r.lookup("", 'another_sub_class')).must_equal B
21
22
  end
22
23
 
23
24
  it "does not lookup unregistered names" do
24
25
  _ {
25
- r.lookup('a_non_existent_sub_class')
26
+ r.lookup("", 'a_non_existent_sub_class')
26
27
  }.must_raise BinData::UnRegisteredTypeError
27
28
  end
28
29
 
29
30
  it "unregisters names" do
30
- r.register('ASubClass', A)
31
- r.unregister('ASubClass')
31
+ r.register("", 'ASubClass', A)
32
+ r.unregister("", 'ASubClass')
32
33
 
33
34
  _ {
34
- r.lookup('ASubClass')
35
+ r.lookup("", 'ASubClass')
35
36
  }.must_raise BinData::UnRegisteredTypeError
36
37
  end
37
38
 
@@ -39,10 +40,10 @@ describe BinData::Registry do
39
40
  w, $-w = $-w, nil # disable warning
40
41
 
41
42
  begin
42
- r.register('A', A)
43
- r.register('A', B)
43
+ r.register("", 'A', A)
44
+ r.register("", 'A', B)
44
45
 
45
- _(r.lookup('a')).must_equal B
46
+ _(r.lookup("", 'a')).must_equal B
46
47
  ensure
47
48
  $-w = w
48
49
  end
@@ -55,31 +56,27 @@ describe BinData::Registry do
55
56
  it "converts adjacent caps camelCase to underscores" do
56
57
  _(r.underscore_name('XYZCamelCase')).must_equal 'xyz_camel_case'
57
58
  end
58
-
59
- it "ignores the outer nestings of classes" do
60
- _(r.underscore_name('A::B::C')).must_equal 'c'
61
- end
62
59
  end
63
60
 
64
61
  describe BinData::Registry, "with numerics" do
65
62
  let(:r) { BinData::RegisteredClasses }
66
63
 
67
64
  it "lookup integers with endian" do
68
- _(r.lookup("int24", {endian: :big}).to_s).must_equal "BinData::Int24be"
69
- _(r.lookup("int24", {endian: :little}).to_s).must_equal "BinData::Int24le"
70
- _(r.lookup("uint24", {endian: :big}).to_s).must_equal "BinData::Uint24be"
71
- _(r.lookup("uint24", {endian: :little}).to_s).must_equal "BinData::Uint24le"
65
+ _(r.lookup("", 'int24', {endian: :big}).to_s).must_equal "BinData::Int24be"
66
+ _(r.lookup("", 'int24', {endian: :little}).to_s).must_equal "BinData::Int24le"
67
+ _(r.lookup("xx", 'uint24', {endian: :big}).to_s).must_equal "BinData::Uint24be"
68
+ _(r.lookup("yy", 'uint24', {endian: :little}).to_s).must_equal "BinData::Uint24le"
72
69
  end
73
70
 
74
71
  it "does not lookup integers without endian" do
75
72
  _ {
76
- r.lookup("int24")
73
+ r.lookup("", 'int24')
77
74
  }.must_raise BinData::UnRegisteredTypeError
78
75
  end
79
76
 
80
77
  it "provides a nice error message when endian is omitted" do
81
78
  begin
82
- r.lookup("int24")
79
+ r.lookup("", 'int24')
83
80
  rescue BinData::UnRegisteredTypeError => e
84
81
  _(e.message).must_equal "int24, do you need to specify endian?"
85
82
  end
@@ -87,41 +84,41 @@ describe BinData::Registry, "with numerics" do
87
84
 
88
85
  it "does not lookup non byte based integers" do
89
86
  _ {
90
- r.lookup("int3")
87
+ r.lookup("", 'int3')
91
88
  }.must_raise BinData::UnRegisteredTypeError
92
89
  _ {
93
- r.lookup("int3", {endian: :big})
90
+ r.lookup("", 'int3', {endian: :big})
94
91
  }.must_raise BinData::UnRegisteredTypeError
95
92
  _ {
96
- r.lookup("int3", {endian: :little})
93
+ r.lookup("", 'int3', {endian: :little})
97
94
  }.must_raise BinData::UnRegisteredTypeError
98
95
  end
99
96
 
100
97
  it "lookup floats with endian" do
101
- _(r.lookup("float", {endian: :big}).to_s).must_equal "BinData::FloatBe"
102
- _(r.lookup("float", {endian: :little}).to_s).must_equal "BinData::FloatLe"
103
- _(r.lookup("double", {endian: :big}).to_s).must_equal "BinData::DoubleBe"
104
- _(r.lookup("double", {endian: :little}).to_s).must_equal "BinData::DoubleLe"
98
+ _(r.lookup("", 'float', {endian: :big}).to_s).must_equal "BinData::FloatBe"
99
+ _(r.lookup("", 'float', {endian: :little}).to_s).must_equal "BinData::FloatLe"
100
+ _(r.lookup("xx", 'double', {endian: :big}).to_s).must_equal "BinData::DoubleBe"
101
+ _(r.lookup("yy", 'double', {endian: :little}).to_s).must_equal "BinData::DoubleLe"
105
102
  end
106
103
 
107
104
  it "lookup bits" do
108
- _(r.lookup("bit5").to_s).must_equal "BinData::Bit5"
109
- _(r.lookup("sbit5").to_s).must_equal "BinData::Sbit5"
110
- _(r.lookup("bit6le").to_s).must_equal "BinData::Bit6le"
105
+ _(r.lookup("", 'bit5').to_s).must_equal "BinData::Bit5"
106
+ _(r.lookup("", 'sbit5').to_s).must_equal "BinData::Sbit5"
107
+ _(r.lookup("", 'bit6le').to_s).must_equal "BinData::Bit6le"
111
108
  end
112
109
 
113
110
  it "lookup bits by ignoring endian" do
114
- _(r.lookup("bit2", {endian: :big}).to_s).must_equal "BinData::Bit2"
115
- _(r.lookup("bit3le", {endian: :big}).to_s).must_equal "BinData::Bit3le"
116
- _(r.lookup("bit2", {endian: :little}).to_s).must_equal "BinData::Bit2"
117
- _(r.lookup("bit3le", {endian: :little}).to_s).must_equal "BinData::Bit3le"
111
+ _(r.lookup("", 'bit2', {endian: :big}).to_s).must_equal "BinData::Bit2"
112
+ _(r.lookup("", 'bit3le', {endian: :big}).to_s).must_equal "BinData::Bit3le"
113
+ _(r.lookup("", 'bit2', {endian: :little}).to_s).must_equal "BinData::Bit2"
114
+ _(r.lookup("", 'bit3le', {endian: :little}).to_s).must_equal "BinData::Bit3le"
118
115
  end
119
116
 
120
117
  it "lookup signed bits by ignoring endian" do
121
- _(r.lookup("sbit2", {endian: :big}).to_s).must_equal "BinData::Sbit2"
122
- _(r.lookup("sbit3le", {endian: :big}).to_s).must_equal "BinData::Sbit3le"
123
- _(r.lookup("sbit2", {endian: :little}).to_s).must_equal "BinData::Sbit2"
124
- _(r.lookup("sbit3le", {endian: :little}).to_s).must_equal "BinData::Sbit3le"
118
+ _(r.lookup("", 'sbit2', {endian: :big}).to_s).must_equal "BinData::Sbit2"
119
+ _(r.lookup("", 'sbit3le', {endian: :big}).to_s).must_equal "BinData::Sbit3le"
120
+ _(r.lookup("", 'sbit2', {endian: :little}).to_s).must_equal "BinData::Sbit2"
121
+ _(r.lookup("", 'sbit3le', {endian: :little}).to_s).must_equal "BinData::Sbit3le"
125
122
  end
126
123
  end
127
124
 
@@ -129,53 +126,109 @@ describe BinData::Registry, "with endian specific types" do
129
126
  let(:r) { BinData::Registry.new }
130
127
 
131
128
  before do
132
- r.register('a_le', A)
133
- r.register('b_be', B)
129
+ r.register("", 'a_le', A)
130
+ r.register("", 'b_be', B)
134
131
  end
135
132
 
136
133
  it "lookup little endian types" do
137
- _(r.lookup('a', {endian: :little})).must_equal A
134
+ _(r.lookup("", 'a', {endian: :little})).must_equal A
138
135
  end
139
136
 
140
137
  it "lookup big endian types" do
141
- _(r.lookup('b', {endian: :big})).must_equal B
138
+ _(r.lookup("", 'b', {endian: :big})).must_equal B
142
139
  end
143
140
 
144
141
  it "does not lookup types with non existent endian" do
145
142
  _ {
146
- r.lookup('a', {endian: :big})
143
+ r.lookup("", 'a', {endian: :big})
147
144
  }.must_raise BinData::UnRegisteredTypeError
148
145
  end
149
146
 
150
147
  it "lookup prefers exact type" do
151
- r.register('c', C)
152
- r.register('c_le', D)
148
+ r.register("", 'c', C)
149
+ r.register("", 'c_le', D)
150
+
151
+ _(r.lookup("", 'c', {endian: :little})).must_equal C
152
+ end
153
+ end
154
+
155
+ describe BinData::Registry, "with search_namespace" do
156
+ let(:r) { BinData::Registry.new }
157
+
158
+ before do
159
+ r.register("", 'a_f', A)
160
+ r.register("", 'b_f', B)
161
+ end
162
+
163
+ it "lookup single search_namespace" do
164
+ _(r.lookup("", 'f', {search_namespace: :a})).must_equal A
165
+ end
166
+
167
+ it "lookup single search_namespace with endian" do
168
+ _(r.lookup("", 'f', {search_namespace: :a, endian: :little})).must_equal A
169
+ end
153
170
 
154
- _(r.lookup('c', {endian: :little})).must_equal C
171
+ it "lookup multiple search_namespace" do
172
+ _(r.lookup("", 'f', {search_namespace: [:x, :a]})).must_equal A
173
+ end
174
+
175
+ it "lookup first match in search_namespace" do
176
+ _(r.lookup("", 'f', {search_namespace: [:a, :b]})).must_equal A
155
177
  end
156
178
  end
157
179
 
158
- describe BinData::Registry, "with search_prefix" do
180
+ describe BinData::Registry, "with namespaces" do
159
181
  let(:r) { BinData::Registry.new }
160
182
 
161
183
  before do
162
- r.register('a_f', A)
163
- r.register('b_f', B)
184
+ r.register('ModA::ModB', 'obj1', A) # ModA::ModB::Obj1
185
+ r.register('ModA::ModB', 'obj2', B) # ModA::ModB::Obj2
186
+ r.register('mod_a_mod_b', 'ns1_obj3', C) # ModA::ModB::Ns1Obj3
187
+ r.register('mod_a_mod_c', 'Obj1', D) # ModA::ModC::Obj1
188
+ r.register('mod_a_mod_c', 'Obj4', E) # ModA::ModC::Obj4
189
+ end
190
+
191
+ it "lookups inside the namespace" do
192
+ _(r.lookup("mod_a_mod_c", 'obj1')).must_equal D
193
+ _(r.lookup("ModA::ModC", 'obj4')).must_equal E
194
+ end
195
+
196
+ it "doesn't lookup inside a different namespace" do
197
+ _ {
198
+ r.lookup("mod_a_mod_c", 'obj2')
199
+ }.must_raise BinData::UnRegisteredTypeError
200
+
201
+ _ {
202
+ r.lookup("mod_a", 'obj2')
203
+ }.must_raise BinData::UnRegisteredTypeError
204
+
205
+ _ {
206
+ r.lookup("", 'obj2')
207
+ }.must_raise BinData::UnRegisteredTypeError
164
208
  end
165
209
 
166
- it "lookup single search_prefix" do
167
- _(r.lookup('f', {search_prefix: :a})).must_equal A
210
+ it "lookups with relative namespace" do
211
+ _(r.lookup("mod_a_mod_c", 'mod_b_obj1')).must_equal A
212
+ _(r.lookup("mod_a", 'mod_b_obj1')).must_equal A
168
213
  end
169
214
 
170
- it "lookup single search_prefix with endian" do
171
- _(r.lookup('f', {search_prefix: :a, endian: :little})).must_equal A
215
+ it "lookups with absolute namespace" do
216
+ _(r.lookup("mod_a_mod_c", 'mod_a_mod_b_obj1')).must_equal A
217
+ _(r.lookup("", 'mod_a_mod_b_obj1')).must_equal A
172
218
  end
173
219
 
174
- it "lookup multiple search_prefix" do
175
- _(r.lookup('f', {search_prefix: [:x, :a]})).must_equal A
220
+ it "lookups with search_namespace" do
221
+ _(r.lookup("mod_a_mod_c", 'obj1', {search_namespace: :mod_b})).must_equal D
222
+ _(r.lookup("mod_a", 'obj1', {search_namespace: :mod_b})).must_equal A
223
+ _(r.lookup("mod_a_mod_c", 'obj2', {search_namespace: :mod_b})).must_equal B
224
+ _(r.lookup("mod_a_mod_c", 'obj2', {search_namespace: :mod_a_mod_b})).must_equal B
225
+ _(r.lookup("", 'obj1', {search_namespace: [:mod_a_mod_b, :mod_a_mod_c]})).must_equal A
226
+ _(r.lookup("", 'obj1', {search_namespace: [:mod_a_mod_c, :mod_a_mod_b]})).must_equal D
176
227
  end
177
228
 
178
- it "lookup first match in search_prefix" do
179
- _(r.lookup('f', {search_prefix: [:a, :b]})).must_equal A
229
+ it "lookups with old style search_prefix" do
230
+ _(r.lookup("mod_a_mod_b", 'obj3', {search_prefix: :ns1})).must_equal C
231
+ _(r.lookup("mod_a_mod_c", 'obj3', {search_prefix: :ns1})).must_equal C
232
+ _(r.lookup("", 'obj3', {search_prefix: :ns1})).must_equal C
180
233
  end
181
234
  end
data/test/string_test.rb CHANGED
@@ -295,6 +295,14 @@ describe BinData::String, "with Ruby 1.9 encodings" do
295
295
  end
296
296
  end
297
297
 
298
+ describe BinData::String, "with Ruby 3.0 keyword arguments" do
299
+ let(:obj) { BinData::String.new("testing\x92") }
300
+
301
+ it "successfully delegates calls with keyword args" do
302
+ _(obj.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')).must_equal "testing?"
303
+ end
304
+ end
305
+
298
306
  describe BinData::String, "warnings" do
299
307
  it "warns if has :asserted_value but no :length" do
300
308
  obj = BinData::String.new(asserted_value: "ABC")
data/test/struct_test.rb CHANGED
@@ -409,6 +409,66 @@ describe BinData::Struct, "with nested endian" do
409
409
  end
410
410
  end
411
411
 
412
+ describe BinData::Struct, "with a search_namespace" do
413
+ module ModA
414
+ class Short < BinData::Uint8; end
415
+ end
416
+ module ModB
417
+ class Short < BinData::Uint8; end
418
+ end
419
+
420
+ it "searches underscore symbol namespaces" do
421
+ obj = BinData::Struct.new(search_namespace: :mod_a,
422
+ fields: [ [:short, :f] ])
423
+ _(obj.f.class.name).must_equal "ModA::Short"
424
+ end
425
+
426
+ it "searches camelCase symbol namespaces" do
427
+ obj = BinData::Struct.new(search_namespace: :ModA,
428
+ fields: [ [:short, :f] ])
429
+ _(obj.f.class.name).must_equal "ModA::Short"
430
+ end
431
+
432
+ it "searches string namespaces" do
433
+ obj = BinData::Struct.new(search_namespace: "ModA",
434
+ fields: [ [:short, :f] ])
435
+ _(obj.f.class.name).must_equal "ModA::Short"
436
+ end
437
+
438
+ it "searches module namespaces" do
439
+ obj = BinData::Struct.new(search_namespace: ModA,
440
+ fields: [ [:short, :f] ])
441
+ _(obj.f.class.name).must_equal "ModA::Short"
442
+ end
443
+
444
+ it "searches multiple namespaces" do
445
+ obj = BinData::Struct.new(search_namespace: [:x, :ModA],
446
+ fields: [ [:short, :f] ])
447
+ _(obj.f.class.name).must_equal "ModA::Short"
448
+ end
449
+
450
+ it "uses parent search_namespace" do
451
+ nested_params = { fields: [[:short, :f]] }
452
+ obj = BinData::Struct.new(search_namespace: :ModA,
453
+ fields: [[:struct, :s, nested_params]])
454
+ _(obj.s.f.class.name).must_equal "ModA::Short"
455
+ end
456
+
457
+ it "searches parent search_namespace" do
458
+ nested_params = { search_namespace: :x, fields: [[:short, :f]] }
459
+ obj = BinData::Struct.new(search_namespace: :ModA,
460
+ fields: [[:struct, :s, nested_params]])
461
+ _(obj.s.f.class.name).must_equal "ModA::Short"
462
+ end
463
+
464
+ it "prioritises nested search_namespace" do
465
+ nested_params = { search_namespace: :ModA, fields: [[:short, :f]] }
466
+ obj = BinData::Struct.new(search_namespace: :b,
467
+ fields: [[:struct, :s, nested_params]])
468
+ _(obj.s.f.class.name).must_equal "ModA::Short"
469
+ end
470
+ end
471
+
412
472
  describe BinData::Struct, "with a search_prefix" do
413
473
  class AShort < BinData::Uint8; end
414
474
  class BShort < BinData::Uint8; end
@@ -506,10 +566,12 @@ end
506
566
 
507
567
  describe BinData::Struct, "with dynamically named types" do
508
568
  it "instantiates" do
509
- _ = BinData::Struct.new(name: :my_struct, fields: [[:int8, :a, {initial_value: 3}]])
569
+ _ = BinData::Struct.new(name: :my_struct, namespace: :ns, fields: [[:int8, :a, {initial_value: 3}]])
510
570
 
511
- obj = BinData::Struct.new(fields: [[:my_struct, :v]])
571
+ obj1 = BinData::Struct.new(fields: [[:ns_my_struct, :v]])
572
+ obj2 = BinData::Struct.new(namespace: :ns, fields: [[:my_struct, :v]])
512
573
 
513
- _(obj.v.a).must_equal 3
574
+ _(obj1.v.a).must_equal 3
575
+ _(obj2.v.a).must_equal 3
514
576
  end
515
577
  end
data/test/system_test.rb CHANGED
@@ -490,7 +490,6 @@ describe BinData::Buffer, "with seek_abs" do
490
490
  end
491
491
  end
492
492
 
493
-
494
493
  describe BinData::Record, "buffered readahead" do
495
494
  class BufferedReadaheadRecord < BinData::Record
496
495
  buffer :a, length: 5 do
@@ -512,3 +511,28 @@ describe BinData::Record, "buffered readahead" do
512
511
  _ { BufferedReadaheadRecord.read "123456X890" }.must_raise IOError
513
512
  end
514
513
  end
514
+
515
+ describe BinData::Record, "inside modules" do
516
+ module M
517
+ class Type1 < BinData::Record
518
+ int8 :v
519
+ end
520
+ class MyRec < BinData::Record
521
+ type1 :f1
522
+
523
+ struct :s1, fields: [ [:type1, :f2] ]
524
+ struct :s2 do
525
+ type1 :f3
526
+ end
527
+
528
+ array :arr1, type: :type1, initial_length: 3
529
+ array :arr2, initial_length: 3 do
530
+ type1
531
+ end
532
+ end
533
+ end
534
+
535
+ it "parses" do
536
+ M::MyRec.new
537
+ end
538
+ end
data/test/test_helper.rb CHANGED
@@ -43,11 +43,15 @@ module Minitest::Assertions
43
43
  ex = assert_raises(exp, &block)
44
44
  assert_equal(msg, ex.message) if msg
45
45
 
46
- idx = ex.backtrace.find_index { |bt| /:in `assert_raises_on_line'$/ =~ bt }
46
+ line_num_regex = /(.*):(\d+)(:in.*)$/
47
47
 
48
- line_num_regex = /.*:(\d+)(:.*|$)/
49
- err_line = line_num_regex.match(ex.backtrace[0])[1].to_i
50
- ref_line = line_num_regex.match(ex.backtrace[idx + 2])[1].to_i
48
+ filename = line_num_regex.match(ex.backtrace[0])[1]
49
+
50
+ filtered = ex.backtrace.grep(/^#{Regexp.escape(filename)}/)
51
+ top = filtered.grep(Regexp.new(Regexp.escape("in <top (required)>")))
52
+
53
+ err_line = line_num_regex.match(filtered[0])[2].to_i
54
+ ref_line = line_num_regex.match(top[0])[2].to_i - 1
51
55
 
52
56
  assert_equal((err_line - ref_line), line)
53
57
  end
metadata CHANGED
@@ -1,11 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bindata
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dion Mendel
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
10
  date: 1980-01-01 00:00:00.000000000 Z
@@ -28,16 +27,16 @@ dependencies:
28
27
  name: minitest
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
- - - ">"
30
+ - - "~>"
32
31
  - !ruby/object:Gem::Version
33
- version: 5.0.0
32
+ version: '5.0'
34
33
  type: :development
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
- - - ">"
37
+ - - "~>"
39
38
  - !ruby/object:Gem::Version
40
- version: 5.0.0
39
+ version: '5.0'
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: simplecov
43
42
  requirement: !ruby/object:Gem::Requirement
@@ -59,7 +58,7 @@ description: |
59
58
  data is, and BinData works out *how* to read and write data in this
60
59
  format. It is an easier ( and more readable ) alternative to
61
60
  ruby's #pack and #unpack methods.
62
- email: bindata@dm9.info
61
+ email: bindata@dmau.org
63
62
  executables: []
64
63
  extensions: []
65
64
  extra_rdoc_files:
@@ -149,8 +148,8 @@ files:
149
148
  homepage: https://github.com/dmendel/bindata
150
149
  licenses:
151
150
  - BSD-2-Clause
152
- metadata: {}
153
- post_install_message:
151
+ metadata:
152
+ changelog_uri: https://github.com/dmendel/bindata/blob/master/ChangeLog.rdoc
154
153
  rdoc_options:
155
154
  - "--main"
156
155
  - NEWS.rdoc
@@ -167,8 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
166
  - !ruby/object:Gem::Version
168
167
  version: '0'
169
168
  requirements: []
170
- rubygems_version: 3.4.22
171
- signing_key:
169
+ rubygems_version: 3.7.2
172
170
  specification_version: 4
173
171
  summary: A declarative way to read and write binary file formats
174
172
  test_files: []