bindata 1.2.1 → 1.2.2
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 +8 -0
- data/Rakefile +2 -1
- data/lib/bindata.rb +1 -1
- data/lib/bindata/array.rb +26 -30
- data/lib/bindata/base.rb +60 -51
- data/lib/bindata/base_primitive.rb +35 -44
- data/lib/bindata/bits.rb +6 -7
- data/lib/bindata/choice.rb +18 -22
- data/lib/bindata/deprecated.rb +42 -0
- data/lib/bindata/dsl.rb +12 -0
- data/lib/bindata/float.rb +1 -1
- data/lib/bindata/int.rb +5 -5
- data/lib/bindata/lazy.rb +2 -2
- data/lib/bindata/primitive.rb +1 -1
- data/lib/bindata/record.rb +25 -0
- data/lib/bindata/string.rb +5 -5
- data/lib/bindata/stringz.rb +5 -5
- data/lib/bindata/struct.rb +29 -33
- data/lib/bindata/wrapper.rb +16 -27
- data/manual.md +1187 -0
- data/spec/base_primitive_spec.rb +6 -14
- data/spec/base_spec.rb +38 -37
- data/spec/deprecated_spec.rb +36 -0
- data/spec/primitive_spec.rb +9 -0
- data/spec/record_spec.rb +11 -0
- data/spec/string_spec.rb +5 -6
- metadata +5 -5
- data/lib/bench.rb +0 -102
data/spec/base_primitive_spec.rb
CHANGED
@@ -5,13 +5,6 @@ require File.expand_path(File.join(File.dirname(__FILE__), "example"))
|
|
5
5
|
require 'bindata/base_primitive'
|
6
6
|
require 'bindata/io'
|
7
7
|
|
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
8
|
describe BinData::BasePrimitive, "when subclassing" do
|
16
9
|
class SubClassOfBasePrimitive < BinData::BasePrimitive
|
17
10
|
expose_methods_for_testing
|
@@ -31,16 +24,15 @@ end
|
|
31
24
|
describe BinData::BasePrimitive do
|
32
25
|
it "should conform to rule 1 for returning a value" do
|
33
26
|
data = ExampleSingle.new(:value => 5)
|
34
|
-
data.should_not be_in_read
|
35
27
|
data.value.should == 5
|
36
28
|
end
|
37
29
|
|
38
30
|
it "should conform to rule 2 for returning a value" do
|
39
31
|
io = ExampleSingle.io_with_value(42)
|
40
32
|
data = ExampleSingle.new(:value => 5)
|
41
|
-
data.
|
42
|
-
|
43
|
-
data.
|
33
|
+
data.read(io)
|
34
|
+
|
35
|
+
def data.reading?; true; end
|
44
36
|
data.value.should == 42
|
45
37
|
end
|
46
38
|
|
@@ -210,7 +202,6 @@ end
|
|
210
202
|
describe BinData::BasePrimitive, "with :value" do
|
211
203
|
before(:each) do
|
212
204
|
@data = ExampleSingle.new(:value => 5)
|
213
|
-
@data.expose_methods_for_testing
|
214
205
|
end
|
215
206
|
|
216
207
|
it "should return that :value" do
|
@@ -219,9 +210,10 @@ describe BinData::BasePrimitive, "with :value" do
|
|
219
210
|
|
220
211
|
it "should change during reading" do
|
221
212
|
io = ExampleSingle.io_with_value(56)
|
222
|
-
@data.
|
213
|
+
@data.read(io)
|
214
|
+
|
215
|
+
def @data.reading?; true; end
|
223
216
|
@data.value.should == 56
|
224
|
-
@data.done_read
|
225
217
|
end
|
226
218
|
|
227
219
|
it "should not change after reading" do
|
data/spec/base_spec.rb
CHANGED
@@ -7,26 +7,22 @@ class BaseStub < BinData::Base
|
|
7
7
|
# Override to avoid NotImplemented errors
|
8
8
|
def clear; end
|
9
9
|
def clear?; end
|
10
|
-
def
|
11
|
-
def
|
12
|
-
def
|
13
|
-
def
|
14
|
-
def
|
15
|
-
def _snapshot; end
|
16
|
-
|
17
|
-
expose_methods_for_testing
|
10
|
+
def assign(x); end
|
11
|
+
def snapshot; end
|
12
|
+
def do_read(io) end
|
13
|
+
def do_write(io) end
|
14
|
+
def do_num_bytes; end
|
18
15
|
end
|
19
16
|
|
20
17
|
class MockBaseStub < BaseStub
|
21
18
|
attr_accessor :mock
|
22
19
|
def clear; mock.clear; end
|
23
20
|
def clear?; mock.clear?; end
|
24
|
-
def
|
25
|
-
def
|
26
|
-
def
|
27
|
-
def
|
28
|
-
def
|
29
|
-
def _snapshot; mock._snapshot; end
|
21
|
+
def assign(x); mock.assign(x); end
|
22
|
+
def snapshot; mock.snapshot; end
|
23
|
+
def do_read(io) mock.do_read(io); end
|
24
|
+
def do_write(io) mock.do_write(io); end
|
25
|
+
def do_num_bytes; mock.do_num_bytes; end
|
30
26
|
end
|
31
27
|
|
32
28
|
describe BinData::Base, "when subclassing" do
|
@@ -42,11 +38,10 @@ describe BinData::Base, "when subclassing" do
|
|
42
38
|
lambda { @obj.clear }.should raise_error(NotImplementedError)
|
43
39
|
lambda { @obj.clear? }.should raise_error(NotImplementedError)
|
44
40
|
lambda { @obj.assign(nil) }.should raise_error(NotImplementedError)
|
45
|
-
lambda { @obj.
|
46
|
-
lambda { @obj.
|
47
|
-
lambda { @obj.
|
48
|
-
lambda { @obj.
|
49
|
-
lambda { @obj._snapshot }.should raise_error(NotImplementedError)
|
41
|
+
lambda { @obj.snapshot }.should raise_error(NotImplementedError)
|
42
|
+
lambda { @obj.do_read(nil) }.should raise_error(NotImplementedError)
|
43
|
+
lambda { @obj.do_write(nil) }.should raise_error(NotImplementedError)
|
44
|
+
lambda { @obj.do_num_bytes }.should raise_error(NotImplementedError)
|
50
45
|
end
|
51
46
|
end
|
52
47
|
|
@@ -176,10 +171,14 @@ end
|
|
176
171
|
|
177
172
|
describe BinData::Base, "with :check_offset" do
|
178
173
|
class TenByteOffsetBase < BaseStub
|
174
|
+
def initialize(params = {})
|
175
|
+
super({})
|
176
|
+
@child = BaseStub.new(params, self)
|
177
|
+
end
|
178
|
+
|
179
179
|
def do_read(io)
|
180
|
-
# advance the io position before checking offset
|
181
180
|
io.seekbytes(10)
|
182
|
-
|
181
|
+
@child.do_read(io)
|
183
182
|
end
|
184
183
|
end
|
185
184
|
|
@@ -214,10 +213,14 @@ end
|
|
214
213
|
|
215
214
|
describe BinData::Base, "with :adjust_offset" do
|
216
215
|
class TenByteAdjustingOffsetBase < BaseStub
|
216
|
+
def initialize(params = {})
|
217
|
+
super({})
|
218
|
+
@child = BaseStub.new(params, self)
|
219
|
+
end
|
220
|
+
|
217
221
|
def do_read(io)
|
218
|
-
# advance the io position before checking offset
|
219
222
|
io.seekbytes(10)
|
220
|
-
|
223
|
+
@child.do_read(io)
|
221
224
|
end
|
222
225
|
end
|
223
226
|
|
@@ -252,6 +255,10 @@ describe BinData::Base, "with :adjust_offset" do
|
|
252
255
|
end
|
253
256
|
|
254
257
|
describe BinData::Base, "as black box" do
|
258
|
+
it "should return bindata_name" do
|
259
|
+
BaseStub.bindata_name.should == "base_stub"
|
260
|
+
end
|
261
|
+
|
255
262
|
it "should access parent" do
|
256
263
|
parent = BaseStub.new
|
257
264
|
child = BaseStub.new(nil, parent)
|
@@ -305,7 +312,7 @@ describe BinData::Base, "as black box" do
|
|
305
312
|
|
306
313
|
it "should write the same as to_binary_s" do
|
307
314
|
class WriteToSBase < BaseStub
|
308
|
-
def
|
315
|
+
def do_write(io) io.writebytes("abc"); end
|
309
316
|
end
|
310
317
|
|
311
318
|
obj = WriteToSBase.new
|
@@ -321,30 +328,24 @@ describe BinData::Base, "as white box" do
|
|
321
328
|
@obj.mock = mock('mock')
|
322
329
|
end
|
323
330
|
|
324
|
-
it "should forward read to
|
331
|
+
it "should forward read to do_read" do
|
325
332
|
@obj.mock.should_receive(:clear).ordered
|
326
|
-
@obj.mock.should_receive(:
|
327
|
-
@obj.mock.should_receive(:_done_read).ordered
|
333
|
+
@obj.mock.should_receive(:do_read).ordered
|
328
334
|
@obj.read(nil)
|
329
335
|
end
|
330
336
|
|
331
|
-
it "should forward write to
|
332
|
-
@obj.mock.should_receive(:
|
337
|
+
it "should forward write to do_write" do
|
338
|
+
@obj.mock.should_receive(:do_write)
|
333
339
|
@obj.write(nil)
|
334
340
|
end
|
335
341
|
|
336
|
-
it "should forward num_bytes to
|
337
|
-
@obj.mock.should_receive(:
|
342
|
+
it "should forward num_bytes to do_num_bytes" do
|
343
|
+
@obj.mock.should_receive(:do_num_bytes).and_return(42)
|
338
344
|
@obj.num_bytes.should == 42
|
339
345
|
end
|
340
346
|
|
341
347
|
it "should round up fractional num_bytes" do
|
342
|
-
@obj.mock.should_receive(:
|
348
|
+
@obj.mock.should_receive(:do_num_bytes).and_return(42.1)
|
343
349
|
@obj.num_bytes.should == 43
|
344
350
|
end
|
345
|
-
|
346
|
-
it "should forward snapshot to _snapshot" do
|
347
|
-
@obj.mock.should_receive(:_snapshot).and_return("abc")
|
348
|
-
@obj.snapshot.should == "abc"
|
349
|
-
end
|
350
351
|
end
|
data/spec/deprecated_spec.rb
CHANGED
@@ -52,3 +52,39 @@ describe BinData::Base, "when defining" do
|
|
52
52
|
}.should_not raise_error
|
53
53
|
end
|
54
54
|
end
|
55
|
+
|
56
|
+
describe BinData::Base do
|
57
|
+
class DeprecatedBase < BinData::Base
|
58
|
+
end
|
59
|
+
|
60
|
+
before(:each) do
|
61
|
+
@obj = DeprecatedBase.new
|
62
|
+
@io = "abcde"
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should forward _do_read to do_read" do
|
66
|
+
@obj.should_receive(:do_read).with(@io)
|
67
|
+
@obj._do_read(@io)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should forward _do_write to do_write" do
|
71
|
+
@obj.should_receive(:do_write).with(@io)
|
72
|
+
@obj._do_write(@io)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should forward _do_num_bytes to do_num_bytes" do
|
76
|
+
@obj.should_receive(:do_num_bytes)
|
77
|
+
@obj._do_num_bytes
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should forward _assign to assign" do
|
81
|
+
val = 3
|
82
|
+
@obj.should_receive(:assign).with(val)
|
83
|
+
@obj._assign(val)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should forward _snapshot to snapshot" do
|
87
|
+
@obj.should_receive(:snapshot)
|
88
|
+
@obj._snapshot
|
89
|
+
end
|
90
|
+
end
|
data/spec/primitive_spec.rb
CHANGED
@@ -103,6 +103,15 @@ describe BinData::Primitive do
|
|
103
103
|
@obj.does_not_exist
|
104
104
|
}.should raise_error(NoMethodError)
|
105
105
|
end
|
106
|
+
|
107
|
+
it "should use read value whilst reading" do
|
108
|
+
obj = PrimitiveWithEndian.new(:value => 2)
|
109
|
+
obj.read "\x05\x00"
|
110
|
+
obj.value.should == 2
|
111
|
+
|
112
|
+
def obj.reading?; true; end
|
113
|
+
obj.value.should == 5
|
114
|
+
end
|
106
115
|
end
|
107
116
|
|
108
117
|
describe BinData::Primitive, "requiring custom parameters" do
|
data/spec/record_spec.rb
CHANGED
@@ -49,6 +49,17 @@ describe BinData::Record, "when defining with errors" do
|
|
49
49
|
}
|
50
50
|
end
|
51
51
|
|
52
|
+
it "should fail on malformed names" do
|
53
|
+
lambda {
|
54
|
+
class MalformedNameRecord < BinData::Record
|
55
|
+
int8 :a
|
56
|
+
int8 45
|
57
|
+
end
|
58
|
+
}.should raise_error_on_line(NameError, 3) { |err|
|
59
|
+
err.message.should == "field '45' is an illegal fieldname in #{MalformedNameRecord}"
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
52
63
|
it "should fail on duplicate names" do
|
53
64
|
lambda {
|
54
65
|
class DuplicateNameRecord < BinData::Record
|
data/spec/string_spec.rb
CHANGED
@@ -128,7 +128,6 @@ end
|
|
128
128
|
describe BinData::String, "with :read_length and :value" do
|
129
129
|
before(:each) do
|
130
130
|
@str = BinData::String.new(:read_length => 5, :value => "abcdefghij")
|
131
|
-
@str.expose_methods_for_testing
|
132
131
|
end
|
133
132
|
|
134
133
|
it "should not be affected by :read_length before value is read" do
|
@@ -148,12 +147,12 @@ describe BinData::String, "with :read_length and :value" do
|
|
148
147
|
@str.value.should == "abcdefghij"
|
149
148
|
end
|
150
149
|
|
151
|
-
it "should return read value
|
152
|
-
@str.
|
153
|
-
@str.value.should == "ABCDE"
|
150
|
+
it "should return read value while reading" do
|
151
|
+
@str.read("ABCDEFGHIJKLMNOPQRST")
|
154
152
|
|
155
|
-
@str.
|
156
|
-
|
153
|
+
def @str.reading?; true; end
|
154
|
+
|
155
|
+
@str.value.should == "ABCDE"
|
157
156
|
end
|
158
157
|
end
|
159
158
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bindata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 2
|
10
|
+
version: 1.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dion Mendel
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-12-14 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -122,7 +122,6 @@ files:
|
|
122
122
|
- spec/spec_common.rb
|
123
123
|
- spec/deprecated_spec.rb
|
124
124
|
- lib/bindata.rb
|
125
|
-
- lib/bench.rb
|
126
125
|
- lib/bindata/skip.rb
|
127
126
|
- lib/bindata/choice.rb
|
128
127
|
- lib/bindata/dsl.rb
|
@@ -152,6 +151,7 @@ files:
|
|
152
151
|
- tasks/rdoc.rake
|
153
152
|
- setup.rb
|
154
153
|
- manual.haml
|
154
|
+
- manual.md
|
155
155
|
has_rdoc: true
|
156
156
|
homepage: http://bindata.rubyforge.org
|
157
157
|
licenses: []
|
data/lib/bench.rb
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
#require 'rubygems'; gem 'bindata'
|
2
|
-
|
3
|
-
require 'bindata'
|
4
|
-
|
5
|
-
COUNT = 50000
|
6
|
-
|
7
|
-
Test1 = Struct.new(:a, :b, :c)
|
8
|
-
|
9
|
-
class Test2
|
10
|
-
attr_accessor :a, :b, :c
|
11
|
-
end
|
12
|
-
|
13
|
-
class Test3
|
14
|
-
def initialize
|
15
|
-
@a = @b = @c = 1
|
16
|
-
end
|
17
|
-
|
18
|
-
def a=(v)
|
19
|
-
@a = v
|
20
|
-
end
|
21
|
-
def b=(v)
|
22
|
-
@b = v
|
23
|
-
end
|
24
|
-
def c=(v)
|
25
|
-
@c = v
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class Test4 < BinData::Record
|
30
|
-
endian :little
|
31
|
-
|
32
|
-
int8 :a
|
33
|
-
int8 :b
|
34
|
-
int8 :c
|
35
|
-
end
|
36
|
-
|
37
|
-
def test1(klass)
|
38
|
-
a = Time.now
|
39
|
-
z = []
|
40
|
-
COUNT.times do
|
41
|
-
obj = klass.new
|
42
|
-
# obj.a = 1
|
43
|
-
# obj.b = 1
|
44
|
-
# obj.c = 1
|
45
|
-
z << obj
|
46
|
-
end
|
47
|
-
b = Time.now
|
48
|
-
puts "#{klass}: #{b - a}"
|
49
|
-
end
|
50
|
-
|
51
|
-
def test2
|
52
|
-
a = Time.now
|
53
|
-
z = []
|
54
|
-
COUNT.times do |i|
|
55
|
-
eval <<-END
|
56
|
-
class A#{i} < BinData::Record
|
57
|
-
int8 :a
|
58
|
-
int8 :b
|
59
|
-
int8 :c
|
60
|
-
int8 :d
|
61
|
-
int8 :e
|
62
|
-
int8 :f
|
63
|
-
int8 :g
|
64
|
-
int8 :h
|
65
|
-
int8 :i
|
66
|
-
int8 :j
|
67
|
-
int8 :k
|
68
|
-
int8 :l
|
69
|
-
int8 :m
|
70
|
-
int8 :n
|
71
|
-
int8 :o
|
72
|
-
int8 :p
|
73
|
-
int8 :q
|
74
|
-
int8 :r
|
75
|
-
int8 :s
|
76
|
-
int8 :t
|
77
|
-
end
|
78
|
-
END
|
79
|
-
end
|
80
|
-
b = Time.now
|
81
|
-
puts "test2: #{b - a}"
|
82
|
-
end
|
83
|
-
|
84
|
-
|
85
|
-
def prof(&block)
|
86
|
-
require 'rubygems'
|
87
|
-
require 'ruby-prof'
|
88
|
-
result = RubyProf.profile do
|
89
|
-
block.call
|
90
|
-
end
|
91
|
-
|
92
|
-
printer = RubyProf::GraphPrinter.new(result)
|
93
|
-
printer.print(STDOUT, 0)
|
94
|
-
end
|
95
|
-
|
96
|
-
|
97
|
-
#test1(Test1)
|
98
|
-
#test1(Test2)
|
99
|
-
#test1(Test3)
|
100
|
-
test1(Test4)
|
101
|
-
|
102
|
-
#test2
|