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.
- data/ChangeLog.rdoc +7 -0
- data/NEWS.rdoc +11 -0
- data/Rakefile +6 -1
- data/bindata.gemspec +2 -1
- data/doc/manual.md +17 -9
- data/examples/gzip.rb +2 -2
- data/examples/list.rb +2 -2
- data/lib/bindata/alignment.rb +4 -9
- data/lib/bindata/array.rb +57 -51
- data/lib/bindata/base.rb +13 -110
- data/lib/bindata/base_primitive.rb +130 -75
- data/lib/bindata/bits.rb +5 -7
- data/lib/bindata/choice.rb +24 -32
- data/lib/bindata/dsl.rb +1 -6
- data/lib/bindata/framework.rb +81 -0
- data/lib/bindata/int.rb +5 -7
- data/lib/bindata/name.rb +28 -0
- data/lib/bindata/offset.rb +42 -53
- data/lib/bindata/params.rb +33 -38
- data/lib/bindata/struct.rb +2 -6
- data/lib/bindata/trace.rb +16 -16
- data/lib/bindata/version.rb +1 -1
- data/lib/bindata/virtual.rb +3 -3
- data/{spec/alignment_spec.rb → test/alignment_test.rb} +17 -16
- data/test/array_test.rb +371 -0
- data/test/base_primitive_test.rb +312 -0
- data/test/base_test.rb +183 -0
- data/{spec/bits_spec.rb → test/bits_test.rb} +59 -59
- data/test/choice_test.rb +260 -0
- data/{spec/spec_common.rb → test/common.rb} +33 -18
- data/test/count_bytes_remaining_test.rb +41 -0
- data/{spec/deprecated_spec.rb → test/deprecated_test.rb} +5 -7
- data/test/float_test.rb +72 -0
- data/{spec/int_spec.rb → test/int_test.rb} +34 -43
- data/{spec/io_spec.rb → test/io_test.rb} +70 -71
- data/{spec/lazy_spec.rb → test/lazy_test.rb} +38 -39
- data/test/offset_test.rb +93 -0
- data/test/params_test.rb +144 -0
- data/{spec/primitive_spec.rb → test/primitive_test.rb} +42 -54
- data/{spec/record_spec.rb → test/record_test.rb} +133 -154
- data/test/registry_test.rb +104 -0
- data/test/rest_test.rb +29 -0
- data/test/skip_test.rb +28 -0
- data/{spec/string_spec.rb → test/string_test.rb} +96 -97
- data/test/stringz_test.rb +127 -0
- data/{spec/struct_spec.rb → test/struct_test.rb} +119 -120
- data/{spec/system_spec.rb → test/system_test.rb} +66 -106
- metadata +39 -38
- data/lib/a.rb +0 -24
- data/spec/array_spec.rb +0 -331
- data/spec/base_primitive_spec.rb +0 -238
- data/spec/base_spec.rb +0 -376
- data/spec/choice_spec.rb +0 -263
- data/spec/count_bytes_remaining_spec.rb +0 -38
- data/spec/example.rb +0 -21
- data/spec/float_spec.rb +0 -37
- data/spec/registry_spec.rb +0 -108
- data/spec/rest_spec.rb +0 -26
- data/spec/skip_spec.rb +0 -27
- data/spec/stringz_spec.rb +0 -118
- data/tasks/rspec.rake +0 -17
@@ -1,38 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
-
require 'bindata/count_bytes_remaining'
|
5
|
-
|
6
|
-
describe BinData::CountBytesRemaining do
|
7
|
-
it { should == 0 }
|
8
|
-
its(:num_bytes) { should be_zero }
|
9
|
-
|
10
|
-
it "counts till end of stream" do
|
11
|
-
data = "abcdefghij"
|
12
|
-
subject.read(data).should == 10
|
13
|
-
end
|
14
|
-
|
15
|
-
it "does not read any data" do
|
16
|
-
io = StringIO.new "abcdefghij"
|
17
|
-
subject.read(io)
|
18
|
-
|
19
|
-
io.pos.should == 0
|
20
|
-
end
|
21
|
-
|
22
|
-
it "does not write any data" do
|
23
|
-
subject.to_binary_s.should == ""
|
24
|
-
end
|
25
|
-
|
26
|
-
it "allows setting value for completeness" do
|
27
|
-
subject.assign("123")
|
28
|
-
subject.should == "123"
|
29
|
-
subject.to_binary_s.should == ""
|
30
|
-
end
|
31
|
-
|
32
|
-
it "accepts BinData::BasePrimitive parameters" do
|
33
|
-
count = BinData::CountBytesRemaining.new(:check_value => 2)
|
34
|
-
expect {
|
35
|
-
count.read("xyz")
|
36
|
-
}.to raise_error(BinData::ValidityError)
|
37
|
-
end
|
38
|
-
end
|
data/spec/example.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'bindata/base_primitive'
|
2
|
-
|
3
|
-
class ExampleSingle < BinData::BasePrimitive
|
4
|
-
def self.io_with_value(val)
|
5
|
-
BinData::IO.new([val].pack("V"))
|
6
|
-
end
|
7
|
-
|
8
|
-
private
|
9
|
-
|
10
|
-
def value_to_binary_string(val)
|
11
|
-
[val].pack("V")
|
12
|
-
end
|
13
|
-
|
14
|
-
def read_and_return_value(io)
|
15
|
-
io.readbytes(4).unpack("V").at(0)
|
16
|
-
end
|
17
|
-
|
18
|
-
def sensible_default
|
19
|
-
0
|
20
|
-
end
|
21
|
-
end
|
data/spec/float_spec.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
-
require 'bindata/float'
|
5
|
-
|
6
|
-
describe "A FloatLe" do
|
7
|
-
subject { BinData::FloatLe.new(Math::PI) }
|
8
|
-
|
9
|
-
its(:num_bytes) { should == 4 }
|
10
|
-
its(:to_binary_s) { should == [Math::PI].pack('e') }
|
11
|
-
its(:value_read_from_written) { should be_within(0.000001).of(Math::PI) }
|
12
|
-
end
|
13
|
-
|
14
|
-
describe "A FloatBe" do
|
15
|
-
subject { BinData::FloatBe.new(Math::PI) }
|
16
|
-
|
17
|
-
its(:num_bytes) { should == 4 }
|
18
|
-
its(:to_binary_s) { should == [Math::PI].pack('g') }
|
19
|
-
its(:value_read_from_written) { should be_within(0.000001).of(Math::PI) }
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "A DoubleLe" do
|
23
|
-
subject { BinData::DoubleLe.new(Math::PI) }
|
24
|
-
|
25
|
-
its(:num_bytes) { should == 8 }
|
26
|
-
its(:to_binary_s) { should == [Math::PI].pack('E') }
|
27
|
-
its(:value_read_from_written) { should be_within(0.0000000000000001).of(Math::PI) }
|
28
|
-
end
|
29
|
-
|
30
|
-
|
31
|
-
describe "A DoubleBe" do
|
32
|
-
subject { BinData::DoubleBe.new(Math::PI) }
|
33
|
-
|
34
|
-
its(:num_bytes) { should == 8 }
|
35
|
-
its(:to_binary_s) { should == [Math::PI].pack('G') }
|
36
|
-
its(:value_read_from_written) { should be_within(0.0000000000000001).of(Math::PI) }
|
37
|
-
end
|
data/spec/registry_spec.rb
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
-
require 'bindata/bits'
|
5
|
-
require 'bindata/int'
|
6
|
-
require 'bindata/float'
|
7
|
-
require 'bindata/registry'
|
8
|
-
|
9
|
-
describe BinData::Registry do
|
10
|
-
A = Class.new
|
11
|
-
B = Class.new
|
12
|
-
C = Class.new
|
13
|
-
D = Class.new
|
14
|
-
|
15
|
-
let(:r) { BinData::Registry.new }
|
16
|
-
|
17
|
-
it "lookups registered names" do
|
18
|
-
r.register('ASubClass', A)
|
19
|
-
r.register('AnotherSubClass', B)
|
20
|
-
|
21
|
-
r.lookup('ASubClass').should == A
|
22
|
-
r.lookup('a_sub_class').should == A
|
23
|
-
r.lookup('AnotherSubClass').should == B
|
24
|
-
r.lookup('another_sub_class').should == B
|
25
|
-
end
|
26
|
-
|
27
|
-
it "does not lookup unregistered names" do
|
28
|
-
expect {
|
29
|
-
r.lookup('a_non_existent_sub_class')
|
30
|
-
}.to raise_error(BinData::UnRegisteredTypeError)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "unregisters names" do
|
34
|
-
r.register('ASubClass', A)
|
35
|
-
r.unregister('ASubClass')
|
36
|
-
|
37
|
-
expect {
|
38
|
-
r.lookup('ASubClass')
|
39
|
-
}.to raise_error(BinData::UnRegisteredTypeError)
|
40
|
-
end
|
41
|
-
|
42
|
-
it "allows overriding of registered classes" do
|
43
|
-
r.register('A', A)
|
44
|
-
r.register('A', B)
|
45
|
-
|
46
|
-
r.lookup('a').should == B
|
47
|
-
end
|
48
|
-
|
49
|
-
it "converts CamelCase to underscores" do
|
50
|
-
r.underscore_name('CamelCase').should == 'camel_case'
|
51
|
-
end
|
52
|
-
|
53
|
-
it "converts adjacent caps camelCase to underscores" do
|
54
|
-
r.underscore_name('XYZCamelCase').should == 'xyz_camel_case'
|
55
|
-
end
|
56
|
-
|
57
|
-
it "ignores the outer nestings of classes" do
|
58
|
-
r.underscore_name('A::B::C').should == 'c'
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
describe BinData::Registry, "with numerics" do
|
63
|
-
let(:r) { BinData::RegisteredClasses }
|
64
|
-
|
65
|
-
it "lookup integers with endian" do
|
66
|
-
r.lookup("int24", :big).to_s.should == "BinData::Int24be"
|
67
|
-
r.lookup("int24", :little).to_s.should == "BinData::Int24le"
|
68
|
-
r.lookup("uint24", :big).to_s.should == "BinData::Uint24be"
|
69
|
-
r.lookup("uint24", :little).to_s.should == "BinData::Uint24le"
|
70
|
-
end
|
71
|
-
|
72
|
-
it "does not lookup integers without endian" do
|
73
|
-
expect {
|
74
|
-
r.lookup("int24")
|
75
|
-
}.to raise_error(BinData::UnRegisteredTypeError)
|
76
|
-
end
|
77
|
-
|
78
|
-
it "does not lookup non byte based integers" do
|
79
|
-
expect {
|
80
|
-
r.lookup("int3")
|
81
|
-
}.to raise_error(BinData::UnRegisteredTypeError)
|
82
|
-
expect {
|
83
|
-
r.lookup("int3", :big)
|
84
|
-
}.to raise_error(BinData::UnRegisteredTypeError)
|
85
|
-
expect {
|
86
|
-
r.lookup("int3", :little)
|
87
|
-
}.to raise_error(BinData::UnRegisteredTypeError)
|
88
|
-
end
|
89
|
-
|
90
|
-
it "lookup floats with endian" do
|
91
|
-
r.lookup("float", :big).to_s.should == "BinData::FloatBe"
|
92
|
-
r.lookup("float", :little).to_s.should == "BinData::FloatLe"
|
93
|
-
r.lookup("double", :big).to_s.should == "BinData::DoubleBe"
|
94
|
-
r.lookup("double", :little).to_s.should == "BinData::DoubleLe"
|
95
|
-
end
|
96
|
-
|
97
|
-
it "lookup bits" do
|
98
|
-
r.lookup("bit5").to_s.should == "BinData::Bit5"
|
99
|
-
r.lookup("bit6le").to_s.should == "BinData::Bit6le"
|
100
|
-
end
|
101
|
-
|
102
|
-
it "lookup bits by ignoring endian" do
|
103
|
-
r.lookup("bit2", :big).to_s.should == "BinData::Bit2"
|
104
|
-
r.lookup("bit3le", :big).to_s.should == "BinData::Bit3le"
|
105
|
-
r.lookup("bit2", :little).to_s.should == "BinData::Bit2"
|
106
|
-
r.lookup("bit3le", :little).to_s.should == "BinData::Bit3le"
|
107
|
-
end
|
108
|
-
end
|
data/spec/rest_spec.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
-
require 'bindata/rest'
|
5
|
-
|
6
|
-
describe BinData::Rest do
|
7
|
-
it { should == "" }
|
8
|
-
|
9
|
-
it "reads till end of stream" do
|
10
|
-
data = "abcdefghij"
|
11
|
-
subject.read(data).should == data
|
12
|
-
end
|
13
|
-
|
14
|
-
it "allows setting value for completeness" do
|
15
|
-
subject.assign("123")
|
16
|
-
subject.should == "123"
|
17
|
-
subject.to_binary_s.should == "123"
|
18
|
-
end
|
19
|
-
|
20
|
-
it "accepts BinData::BasePrimitive parameters" do
|
21
|
-
rest = BinData::Rest.new(:check_value => "abc")
|
22
|
-
expect {
|
23
|
-
rest.read("xyz")
|
24
|
-
}.to raise_error(BinData::ValidityError)
|
25
|
-
end
|
26
|
-
end
|
data/spec/skip_spec.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
-
require 'bindata/skip'
|
5
|
-
|
6
|
-
describe BinData::Skip do
|
7
|
-
subject { BinData::Skip.new(:length => 5) }
|
8
|
-
let(:io) { StringIO.new("abcdefghij") }
|
9
|
-
|
10
|
-
it { should == "" }
|
11
|
-
its(:to_binary_s) { should == "\000" * 5 }
|
12
|
-
|
13
|
-
it "skips bytes" do
|
14
|
-
subject.read(io)
|
15
|
-
io.pos.should == 5
|
16
|
-
end
|
17
|
-
|
18
|
-
it "has expected binary representation after setting value" do
|
19
|
-
subject.assign("123")
|
20
|
-
subject.to_binary_s.should == "\000" * 5
|
21
|
-
end
|
22
|
-
|
23
|
-
it "has expected binary representation after reading" do
|
24
|
-
subject.read(io)
|
25
|
-
subject.to_binary_s.should == "\000" * 5
|
26
|
-
end
|
27
|
-
end
|
data/spec/stringz_spec.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
|
-
require 'bindata/stringz'
|
5
|
-
|
6
|
-
describe BinData::Stringz, "when empty" do
|
7
|
-
its(:value) { should == "" }
|
8
|
-
its(:num_bytes) { should == 1 }
|
9
|
-
its(:to_binary_s) { should == "\0" }
|
10
|
-
end
|
11
|
-
|
12
|
-
describe BinData::Stringz, "with value set" do
|
13
|
-
subject { BinData::Stringz.new("abcd") }
|
14
|
-
|
15
|
-
its(:value) { should == "abcd" }
|
16
|
-
its(:num_bytes) { should == 5 }
|
17
|
-
its(:to_binary_s) { should == "abcd\0" }
|
18
|
-
end
|
19
|
-
|
20
|
-
describe BinData::Stringz, "when reading" do
|
21
|
-
it "stops at the first zero byte" do
|
22
|
-
io = StringIO.new("abcd\0xyz\0")
|
23
|
-
subject.read(io)
|
24
|
-
io.pos.should == 5
|
25
|
-
subject.should == "abcd"
|
26
|
-
end
|
27
|
-
|
28
|
-
it "handles a zero length string" do
|
29
|
-
io = StringIO.new("\0abcd")
|
30
|
-
subject.read(io)
|
31
|
-
io.pos.should == 1
|
32
|
-
subject.should == ""
|
33
|
-
end
|
34
|
-
|
35
|
-
it "fails if no zero byte is found" do
|
36
|
-
expect {subject.read("abcd") }.to raise_error(EOFError)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe BinData::Stringz, "when setting the value" do
|
41
|
-
it "includes the zero byte in num_bytes total" do
|
42
|
-
subject.assign("abcd")
|
43
|
-
subject.num_bytes.should == 5
|
44
|
-
end
|
45
|
-
|
46
|
-
it "accepts empty strings" do
|
47
|
-
subject.assign("")
|
48
|
-
subject.should == ""
|
49
|
-
end
|
50
|
-
|
51
|
-
it "accepts strings that aren't zero terminated" do
|
52
|
-
subject.assign("abcd")
|
53
|
-
subject.should == "abcd"
|
54
|
-
end
|
55
|
-
|
56
|
-
it "accepts strings that are zero terminated" do
|
57
|
-
subject.assign("abcd\0")
|
58
|
-
subject.should == "abcd"
|
59
|
-
end
|
60
|
-
|
61
|
-
it "accepts up to the first zero byte" do
|
62
|
-
subject.assign("abcd\0xyz\0")
|
63
|
-
subject.should == "abcd"
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
describe BinData::Stringz, "with max_length" do
|
68
|
-
subject { BinData::Stringz.new(:max_length => 5) }
|
69
|
-
|
70
|
-
it "reads less than max_length" do
|
71
|
-
io = StringIO.new("abc\0xyz")
|
72
|
-
subject.read(io)
|
73
|
-
subject.should == "abc"
|
74
|
-
end
|
75
|
-
|
76
|
-
it "reads exactly max_length" do
|
77
|
-
io = StringIO.new("abcd\0xyz")
|
78
|
-
subject.read(io)
|
79
|
-
subject.should == "abcd"
|
80
|
-
end
|
81
|
-
|
82
|
-
it "reads no more than max_length" do
|
83
|
-
io = StringIO.new("abcdefg\0xyz")
|
84
|
-
subject.read(io)
|
85
|
-
io.pos.should == 5
|
86
|
-
subject.should == "abcd"
|
87
|
-
end
|
88
|
-
|
89
|
-
it "accepts values less than max_length" do
|
90
|
-
subject.assign("abc")
|
91
|
-
subject.should == "abc"
|
92
|
-
end
|
93
|
-
|
94
|
-
it "accepts values exactly max_length" do
|
95
|
-
subject.assign("abcd")
|
96
|
-
subject.should == "abcd"
|
97
|
-
end
|
98
|
-
|
99
|
-
it "trims values greater than max_length" do
|
100
|
-
subject.assign("abcde")
|
101
|
-
subject.should == "abcd"
|
102
|
-
end
|
103
|
-
|
104
|
-
it "writes values greater than max_length" do
|
105
|
-
subject.assign("abcde")
|
106
|
-
subject.to_binary_s.should == "abcd\0"
|
107
|
-
end
|
108
|
-
|
109
|
-
it "writes values less than max_length" do
|
110
|
-
subject.assign("abc")
|
111
|
-
subject.to_binary_s.should == "abc\0"
|
112
|
-
end
|
113
|
-
|
114
|
-
it "writes values exactly max_length" do
|
115
|
-
subject.assign("abcd")
|
116
|
-
subject.to_binary_s.should == "abcd\0"
|
117
|
-
end
|
118
|
-
end
|
data/tasks/rspec.rake
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'rspec'
|
3
|
-
require 'rspec/core/rake_task'
|
4
|
-
|
5
|
-
RSpec::Core::RakeTask.new("spec") do |t|
|
6
|
-
# t.ruby_opts = "-w"
|
7
|
-
t.rcov = false
|
8
|
-
t.pattern = 'spec/**/*_spec.rb' #.exclude("spec/deprecated_spec.rb", "spec/wrapper_spec.rb")
|
9
|
-
end
|
10
|
-
|
11
|
-
RSpec::Core::RakeTask.new("rcov") do |t|
|
12
|
-
t.ruby_opts = "-w"
|
13
|
-
t.rcov = true
|
14
|
-
end
|
15
|
-
rescue LoadError
|
16
|
-
puts "Rspec must be installed to run tests"
|
17
|
-
end
|