bindata 0.5.0 → 0.5.1
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 +4 -3
- data/README +7 -7
- data/TODO +0 -1
- data/lib/bindata.rb +1 -1
- data/lib/bindata/struct.rb +9 -0
- data/spec/array_spec.rb +13 -13
- data/spec/base_spec.rb +6 -6
- data/spec/choice_spec.rb +13 -13
- data/spec/int_spec.rb +5 -5
- data/spec/lazy_spec.rb +13 -13
- data/spec/registry_spec.rb +6 -6
- data/spec/single_spec.rb +21 -21
- data/spec/string_spec.rb +33 -33
- data/spec/stringz_spec.rb +24 -24
- data/spec/struct_spec.rb +31 -17
- metadata +2 -2
data/ChangeLog
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
= BinData Changelog
|
2
2
|
|
3
|
-
== Version
|
3
|
+
== Version 0.5.1 (2007-03-21)
|
4
4
|
|
5
|
-
Updated documentation.
|
5
|
+
* Updated documentation.
|
6
|
+
* Struct now raises an error if a field name shadows an existing method.
|
6
7
|
|
7
8
|
== Version 0.5.0 (2007-03-14)
|
8
9
|
|
9
|
-
Initial public release.
|
10
|
+
* Initial public release.
|
data/README
CHANGED
@@ -50,11 +50,11 @@ The structure of the data in this example is
|
|
50
50
|
|
51
51
|
The BinData declaration matches the english description closely. Just for
|
52
52
|
fun, lets look at how we'd implement this using #pack and #unpack. Here's
|
53
|
-
the writing
|
53
|
+
the writing code, have a go at the reading code.
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
comment = "this is a comment"
|
56
|
+
some_ints = [2, 3, 8, 9, 1, 8]
|
57
|
+
File.open(...) do |io|
|
58
58
|
io.write([comment, some_ints.size, *some_ints].pack("Z*CN*"))
|
59
59
|
end
|
60
60
|
|
@@ -67,9 +67,9 @@ fields.
|
|
67
67
|
...
|
68
68
|
end
|
69
69
|
|
70
|
-
*type* is the name of a supplied type (e.g.
|
71
|
-
user defined type. For user defined types, convert the class name
|
72
|
-
CamelCase to lowercase underscore_style.
|
70
|
+
*type* is the name of a supplied type (e.g. <tt>uint32be</tt>, +string+)
|
71
|
+
or a user defined type. For user defined types, convert the class name
|
72
|
+
from CamelCase to lowercase underscore_style.
|
73
73
|
|
74
74
|
*field_name* is the name by which you can access the data. Use either a
|
75
75
|
String or a Symbol. You may specify a name as nil, but this is described
|
data/TODO
CHANGED
data/lib/bindata.rb
CHANGED
data/lib/bindata/struct.rb
CHANGED
@@ -87,6 +87,12 @@ module BinData
|
|
87
87
|
raise SyntaxError, "duplicate field '#{name}' in #{self}", caller
|
88
88
|
end
|
89
89
|
|
90
|
+
# check that name doesn't shadow an existing method
|
91
|
+
if self.instance_methods.include?(name)
|
92
|
+
raise NameError.new("", name),
|
93
|
+
"field '#{name}' shadows an existing method", caller
|
94
|
+
end
|
95
|
+
|
90
96
|
# remember this field. These fields will be recalled upon creating
|
91
97
|
# an instance of this class
|
92
98
|
@fields.push([type, name, params])
|
@@ -110,6 +116,9 @@ module BinData
|
|
110
116
|
@fields = param(:fields).collect do |type, name, params|
|
111
117
|
klass = self.class.lookup(type)
|
112
118
|
raise TypeError, "unknown type '#{type}' for #{self}" if klass.nil?
|
119
|
+
if methods.include?(name)
|
120
|
+
raise NameError.new("field '#{name}' shadows an existing method",name)
|
121
|
+
end
|
113
122
|
[name, klass.new(params, create_env)]
|
114
123
|
end
|
115
124
|
end
|
data/spec/array_spec.rb
CHANGED
@@ -28,20 +28,20 @@ context "An Array with several elements" do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
specify "should return a correct snapshot" do
|
31
|
-
@data.snapshot.should
|
31
|
+
@data.snapshot.should eql([1, 2, 3, 4, 5])
|
32
32
|
end
|
33
33
|
|
34
34
|
specify "should have correct num elements" do
|
35
|
-
@data.length.should
|
36
|
-
@data.size.should
|
35
|
+
@data.length.should eql(5)
|
36
|
+
@data.size.should eql(5)
|
37
37
|
end
|
38
38
|
|
39
39
|
specify "should have correct num_bytes" do
|
40
|
-
@data.num_bytes.should
|
40
|
+
@data.num_bytes.should eql(10)
|
41
41
|
end
|
42
42
|
|
43
43
|
specify "should have correct num_bytes for individual elements" do
|
44
|
-
@data.num_bytes(0).should
|
44
|
+
@data.num_bytes(0).should eql(2)
|
45
45
|
end
|
46
46
|
|
47
47
|
specify "should have no field_names" do
|
@@ -50,23 +50,23 @@ context "An Array with several elements" do
|
|
50
50
|
|
51
51
|
specify "should be able to directly access elements" do
|
52
52
|
@data[1] = 8
|
53
|
-
@data[1].should
|
53
|
+
@data[1].should eql(8)
|
54
54
|
end
|
55
55
|
|
56
56
|
specify "should be able to use methods from Enumerable" do
|
57
|
-
@data.select { |x| (x % 2) == 0 }.should
|
57
|
+
@data.select { |x| (x % 2) == 0 }.should eql([2, 4])
|
58
58
|
end
|
59
59
|
|
60
60
|
specify "should clear" do
|
61
61
|
@data[1] = 8
|
62
62
|
@data.clear
|
63
|
-
@data.collect.should
|
63
|
+
@data.collect.should eql([1, 2, 3, 4, 5])
|
64
64
|
end
|
65
65
|
|
66
66
|
specify "should clear a single element" do
|
67
67
|
@data[1] = 8
|
68
68
|
@data.clear(1)
|
69
|
-
@data[1].should
|
69
|
+
@data[1].should eql(2)
|
70
70
|
end
|
71
71
|
|
72
72
|
specify "should be clear upon creation" do
|
@@ -92,10 +92,10 @@ context "An Array with several elements" do
|
|
92
92
|
|
93
93
|
@data.clear
|
94
94
|
io.rewind
|
95
|
-
@data[1].should
|
95
|
+
@data[1].should eql(2)
|
96
96
|
|
97
97
|
@data.read(io)
|
98
|
-
@data[1].should
|
98
|
+
@data[1].should eql(8)
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
@@ -108,7 +108,7 @@ context "An Array containing structs" do
|
|
108
108
|
end
|
109
109
|
|
110
110
|
specify "should access elements, not values" do
|
111
|
-
@data[3].a.should
|
111
|
+
@data[3].a.should eql(3)
|
112
112
|
end
|
113
113
|
|
114
114
|
specify "should not be able to modify elements" do
|
@@ -116,6 +116,6 @@ context "An Array containing structs" do
|
|
116
116
|
end
|
117
117
|
|
118
118
|
specify "should interate over each element" do
|
119
|
-
@data.collect { |s| s.a }.should
|
119
|
+
@data.collect { |s| s.a }.should eql([0, 1, 2, 3, 4])
|
120
120
|
end
|
121
121
|
end
|
data/spec/base_spec.rb
CHANGED
@@ -80,16 +80,16 @@ context "A data object with parameters" do
|
|
80
80
|
|
81
81
|
specify "should evaluate mandatory and optional parameters" do
|
82
82
|
obj = WithParam.new(:p1 => 1, :p3 => lambda {1 + 2}, :p4 => 4, :p5 => 5)
|
83
|
-
obj.eval_param(:p1).should
|
83
|
+
obj.eval_param(:p1).should eql(1)
|
84
84
|
obj.eval_param(:p2).should_be_nil
|
85
|
-
obj.eval_param(:p3).should
|
85
|
+
obj.eval_param(:p3).should eql(3)
|
86
86
|
obj.eval_param(:p4).should_be_nil
|
87
87
|
obj.eval_param(:p5).should_be_nil
|
88
88
|
end
|
89
89
|
|
90
90
|
specify "should be able to access without evaluating" do
|
91
91
|
obj = WithParam.new(:p1 => :asym, :p3 => lambda {1 + 2})
|
92
|
-
obj.param(:p1).should
|
92
|
+
obj.param(:p1).should eql(:asym)
|
93
93
|
obj.param(:p2).should_be_nil
|
94
94
|
obj.param(:p3).should respond_to(:arity)
|
95
95
|
end
|
@@ -164,17 +164,17 @@ context "A data object with :readwrite => false" do
|
|
164
164
|
specify "should not read" do
|
165
165
|
io = StringIO.new("12345678901234567890")
|
166
166
|
@obj.read(io)
|
167
|
-
@obj._do_read.should_not
|
167
|
+
@obj._do_read.should_not eql(true)
|
168
168
|
end
|
169
169
|
|
170
170
|
specify "should not write" do
|
171
171
|
io = StringIO.new
|
172
172
|
@obj.write(io)
|
173
|
-
@obj._do_write.should_not
|
173
|
+
@obj._do_write.should_not eql(true)
|
174
174
|
end
|
175
175
|
|
176
176
|
specify "should have zero num_bytes" do
|
177
|
-
@obj.num_bytes.should
|
177
|
+
@obj.num_bytes.should eql(0)
|
178
178
|
end
|
179
179
|
end
|
180
180
|
|
data/spec/choice_spec.rb
CHANGED
@@ -40,15 +40,15 @@ context "A Choice with several choices" do
|
|
40
40
|
|
41
41
|
specify "should be able to select the choice" do
|
42
42
|
@env.choose = 0
|
43
|
-
@data.value.should
|
43
|
+
@data.value.should eql(3)
|
44
44
|
@env.choose = 1
|
45
|
-
@data.value.should
|
45
|
+
@data.value.should eql(5)
|
46
46
|
@env.choose = 2
|
47
|
-
@data.value.should
|
47
|
+
@data.value.should eql(0)
|
48
48
|
@env.choose = 3
|
49
|
-
@data.a.should
|
49
|
+
@data.a.should eql(0)
|
50
50
|
@env.choose = 4
|
51
|
-
@data.value.should
|
51
|
+
@data.value.should eql(7)
|
52
52
|
end
|
53
53
|
|
54
54
|
specify "should not be able to select an invalid choice" do
|
@@ -61,7 +61,7 @@ context "A Choice with several choices" do
|
|
61
61
|
specify "should be able to interact directly with the choice" do
|
62
62
|
@env.choose = 0
|
63
63
|
@data.value = 17
|
64
|
-
@data.value.should
|
64
|
+
@data.value.should eql(17)
|
65
65
|
end
|
66
66
|
|
67
67
|
specify "should handle missing methods correctly" do
|
@@ -77,29 +77,29 @@ context "A Choice with several choices" do
|
|
77
77
|
@data.value = 17
|
78
78
|
|
79
79
|
@data.find_obj_for_name("does_not_exist").should be_nil
|
80
|
-
@data.num_bytes.should
|
80
|
+
@data.num_bytes.should eql(2)
|
81
81
|
@data.field_names.should be_empty
|
82
|
-
@data.value.should
|
82
|
+
@data.value.should eql(17)
|
83
83
|
|
84
84
|
io = StringIO.new
|
85
85
|
@data.write(io)
|
86
86
|
|
87
87
|
@data.clear
|
88
88
|
@data.clear?.should be_true
|
89
|
-
@data.value.should
|
89
|
+
@data.value.should eql(5)
|
90
90
|
|
91
91
|
io.rewind
|
92
92
|
@data.read(io)
|
93
|
-
@data.value.should
|
93
|
+
@data.value.should eql(17)
|
94
94
|
|
95
|
-
@data.snapshot.should
|
95
|
+
@data.snapshot.should eql(17)
|
96
96
|
end
|
97
97
|
|
98
98
|
specify "should delegate methods to the selected complex choice" do
|
99
99
|
@env.choose = 3
|
100
100
|
@data.find_obj_for_name("a").should_not be_nil
|
101
|
-
@data.field_names.should
|
102
|
-
@data.num_bytes.should
|
101
|
+
@data.field_names.should eql(["a"])
|
102
|
+
@data.num_bytes.should eql(1)
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
data/spec/int_spec.rb
CHANGED
@@ -10,7 +10,7 @@ context "All signed integers" do
|
|
10
10
|
BinData::Int16be,
|
11
11
|
BinData::Int32le,
|
12
12
|
BinData::Int32be].each do |klass|
|
13
|
-
klass.new.value.should
|
13
|
+
klass.new.value.should eql(0)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -36,7 +36,7 @@ context "All unsigned integers" do
|
|
36
36
|
BinData::Uint16be,
|
37
37
|
BinData::Uint32le,
|
38
38
|
BinData::Uint32be].each do |klass|
|
39
|
-
klass.new.value.should
|
39
|
+
klass.new.value.should eql(0)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -60,7 +60,7 @@ def test_read_write(klass, val, clamped_val, str)
|
|
60
60
|
# set the data and ensure clamping occurs
|
61
61
|
data = klass.new
|
62
62
|
data.value = val
|
63
|
-
data.value.should
|
63
|
+
data.value.should eql(clamped_val)
|
64
64
|
|
65
65
|
# write the data
|
66
66
|
io = StringIO.new
|
@@ -68,13 +68,13 @@ def test_read_write(klass, val, clamped_val, str)
|
|
68
68
|
|
69
69
|
# check that we write the expected byte pattern
|
70
70
|
io.rewind
|
71
|
-
io.read.should
|
71
|
+
io.read.should eql(str)
|
72
72
|
|
73
73
|
# check that we read in the same data that was written
|
74
74
|
io.rewind
|
75
75
|
data = klass.new
|
76
76
|
data.read(io)
|
77
|
-
data.value.should
|
77
|
+
data.value.should eql(clamped_val)
|
78
78
|
end
|
79
79
|
|
80
80
|
# return test data for testing unsigned ints
|
data/spec/lazy_spec.rb
CHANGED
@@ -31,17 +31,17 @@ context "A single environment" do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
specify "should evaluate value" do
|
34
|
-
@e1.lazy_eval(lambda { value }).should
|
34
|
+
@e1.lazy_eval(lambda { value }).should eql('v1')
|
35
35
|
end
|
36
36
|
|
37
37
|
specify "should evaluate index" do
|
38
38
|
@e1.index = 7
|
39
|
-
@e1.lazy_eval(lambda { index }).should
|
39
|
+
@e1.lazy_eval(lambda { index }).should eql(7)
|
40
40
|
end
|
41
41
|
|
42
42
|
specify "should evaluate offset" do
|
43
43
|
@e1.offset = 9
|
44
|
-
@e1.lazy_eval(lambda { offset }).should
|
44
|
+
@e1.lazy_eval(lambda { offset }).should eql(9)
|
45
45
|
end
|
46
46
|
|
47
47
|
specify "should not resolve any unknown fields" do
|
@@ -53,9 +53,9 @@ context "A single environment" do
|
|
53
53
|
specify "should accept symbols as a shortcut to lambda" do
|
54
54
|
@e1.index = 7
|
55
55
|
@e1.offset = 9
|
56
|
-
@e1.lazy_eval(:value).should
|
57
|
-
@e1.lazy_eval(:index).should
|
58
|
-
@e1.lazy_eval(:offset).should
|
56
|
+
@e1.lazy_eval(:value).should eql('v1')
|
57
|
+
@e1.lazy_eval(:index).should eql(7)
|
58
|
+
@e1.lazy_eval(:offset).should eql(9)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
@@ -74,15 +74,15 @@ context "An environment with one parent" do
|
|
74
74
|
end
|
75
75
|
|
76
76
|
specify "should evaluate parent parameter" do
|
77
|
-
@e1.lazy_eval(:p2).should
|
77
|
+
@e1.lazy_eval(:p2).should eql('p2')
|
78
78
|
end
|
79
79
|
|
80
80
|
specify "should evaluate parent field" do
|
81
|
-
@e1.lazy_eval(:f2).should
|
81
|
+
@e1.lazy_eval(:f2).should eql('f2')
|
82
82
|
end
|
83
83
|
|
84
84
|
specify "should prefer parent param over parent field" do
|
85
|
-
@e1.lazy_eval(:common).should
|
85
|
+
@e1.lazy_eval(:common).should eql('param2')
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
@@ -109,12 +109,12 @@ context "A nested environment" do
|
|
109
109
|
end
|
110
110
|
|
111
111
|
specify "should access parent environments" do
|
112
|
-
@e1.lazy_eval(lambda { parent.p3 }).should
|
113
|
-
@e1.lazy_eval(lambda { parent.parent.p4 }).should
|
112
|
+
@e1.lazy_eval(lambda { parent.p3 }).should eql('p3')
|
113
|
+
@e1.lazy_eval(lambda { parent.parent.p4 }).should eql('p4')
|
114
114
|
end
|
115
115
|
|
116
116
|
specify "should cascade lambdas" do
|
117
|
-
@e1.lazy_eval(lambda { l2 }).should
|
118
|
-
@e1.lazy_eval(lambda { s2 }).should
|
117
|
+
@e1.lazy_eval(lambda { l2 }).should eql('l4')
|
118
|
+
@e1.lazy_eval(lambda { s2 }).should eql('s4')
|
119
119
|
end
|
120
120
|
end
|
data/spec/registry_spec.rb
CHANGED
@@ -18,8 +18,8 @@ context "The Registry" do
|
|
18
18
|
@r.register('ASubClass', A)
|
19
19
|
@r.register('AnotherSubClass', B)
|
20
20
|
|
21
|
-
@r.lookup('a_sub_class').should
|
22
|
-
@r.lookup('another_sub_class').should
|
21
|
+
@r.lookup('a_sub_class').should eql(A)
|
22
|
+
@r.lookup('another_sub_class').should eql(B)
|
23
23
|
end
|
24
24
|
|
25
25
|
specify "should not lookup unregistered names" do
|
@@ -27,21 +27,21 @@ context "The Registry" do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
specify "should convert CamelCase to underscores" do
|
30
|
-
@r.register('CamelCase', A).should
|
30
|
+
@r.register('CamelCase', A).should eql('camel_case')
|
31
31
|
end
|
32
32
|
|
33
33
|
specify "should convert adjacent caps camelCase to underscores" do
|
34
|
-
@r.register('XYZCamelCase', A).should
|
34
|
+
@r.register('XYZCamelCase', A).should eql('xyz_camel_case')
|
35
35
|
end
|
36
36
|
|
37
37
|
specify "should ignore the outer nestings of classes" do
|
38
|
-
@r.register('A::B::C', A).should
|
38
|
+
@r.register('A::B::C', A).should eql('c')
|
39
39
|
end
|
40
40
|
|
41
41
|
specify "should allow overriding of registered classes" do
|
42
42
|
@r.register('A', A)
|
43
43
|
@r.register('A', B)
|
44
44
|
|
45
|
-
@r.lookup('a').should
|
45
|
+
@r.lookup('a').should eql(B)
|
46
46
|
end
|
47
47
|
end
|
data/spec/single_spec.rb
CHANGED
@@ -22,18 +22,18 @@ context "The sample implementation of Single" do
|
|
22
22
|
io.rewind
|
23
23
|
data = ConcreteSingle.new
|
24
24
|
data.read(io)
|
25
|
-
data.value.should
|
25
|
+
data.value.should eql(42)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
context "The class Single" do
|
30
30
|
specify "should register subclasses" do
|
31
|
-
BinData::Single.lookup(:concrete_single).should
|
31
|
+
BinData::Single.lookup(:concrete_single).should eql(ConcreteSingle)
|
32
32
|
end
|
33
33
|
|
34
34
|
specify "should read and return a value" do
|
35
35
|
io = StringIO.new([123456].pack("V"))
|
36
|
-
ConcreteSingle.read(io).should
|
36
|
+
ConcreteSingle.read(io).should eql(123456)
|
37
37
|
data = ConcreteSingle.new
|
38
38
|
end
|
39
39
|
end
|
@@ -42,7 +42,7 @@ context "A Single object" do
|
|
42
42
|
specify "should conform to rule 1 for returning a value" do
|
43
43
|
data = ConcreteSingle.new(:value => 5)
|
44
44
|
data.should_not be_in_read
|
45
|
-
data.value.should
|
45
|
+
data.value.should eql(5)
|
46
46
|
end
|
47
47
|
|
48
48
|
specify "should conform to rule 2 for returning a value" do
|
@@ -50,33 +50,33 @@ context "A Single object" do
|
|
50
50
|
data = ConcreteSingle.new(:value => 5)
|
51
51
|
data.do_read(io)
|
52
52
|
data.should be_in_read
|
53
|
-
data.value.should
|
53
|
+
data.value.should eql(42)
|
54
54
|
end
|
55
55
|
|
56
56
|
specify "should conform to rule 3 for returning a value" do
|
57
57
|
data = ConcreteSingle.new(:initial_value => 5)
|
58
58
|
data.should be_clear
|
59
|
-
data.value.should
|
59
|
+
data.value.should eql(5)
|
60
60
|
end
|
61
61
|
|
62
62
|
specify "should conform to rule 4 for returning a value" do
|
63
63
|
data = ConcreteSingle.new(:initial_value => 5)
|
64
64
|
data.value = 17
|
65
65
|
data.should_not be_clear
|
66
|
-
data.value.should
|
66
|
+
data.value.should eql(17)
|
67
67
|
end
|
68
68
|
|
69
69
|
specify "should conform to rule 5 for returning a value" do
|
70
70
|
data = ConcreteSingle.new
|
71
71
|
data.should be_clear
|
72
|
-
data.value.should
|
72
|
+
data.value.should eql(0)
|
73
73
|
end
|
74
74
|
|
75
75
|
specify "should conform to rule 6 for returning a value" do
|
76
76
|
data = ConcreteSingle.new
|
77
77
|
data.value = 8
|
78
78
|
data.should_not be_clear
|
79
|
-
data.value.should
|
79
|
+
data.value.should eql(8)
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
@@ -91,12 +91,12 @@ context "A new Single object" do
|
|
91
91
|
end
|
92
92
|
|
93
93
|
specify "should have a sensible value" do
|
94
|
-
@data.value.should
|
94
|
+
@data.value.should eql(0)
|
95
95
|
end
|
96
96
|
|
97
97
|
specify "should allowing setting and retrieving value" do
|
98
98
|
@data.value = 5
|
99
|
-
@data.value.should
|
99
|
+
@data.value.should eql(5)
|
100
100
|
end
|
101
101
|
|
102
102
|
specify "should be clear" do
|
@@ -115,7 +115,7 @@ context "A new Single object" do
|
|
115
115
|
end
|
116
116
|
|
117
117
|
specify "should return num_bytes" do
|
118
|
-
@data.num_bytes.should
|
118
|
+
@data.num_bytes.should eql(4)
|
119
119
|
end
|
120
120
|
|
121
121
|
specify "should not contain any field names" do
|
@@ -124,7 +124,7 @@ context "A new Single object" do
|
|
124
124
|
|
125
125
|
specify "should return a snapshot" do
|
126
126
|
@data.value = 5
|
127
|
-
@data.snapshot.should
|
127
|
+
@data.snapshot.should eql(5)
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
@@ -134,24 +134,24 @@ context "A Single with :initial_value" do
|
|
134
134
|
end
|
135
135
|
|
136
136
|
specify "should return that initial value before reading or being set" do
|
137
|
-
@data.value.should
|
137
|
+
@data.value.should eql(5)
|
138
138
|
end
|
139
139
|
|
140
140
|
specify "should forget :initial_value after being set" do
|
141
141
|
@data.value = 17
|
142
|
-
@data.value.should_not
|
142
|
+
@data.value.should_not eql(5)
|
143
143
|
end
|
144
144
|
|
145
145
|
specify "should forget :initial_value after reading" do
|
146
146
|
io = StringIO.new([56].pack("V"))
|
147
147
|
@data.read(io)
|
148
|
-
@data.value.should_not
|
148
|
+
@data.value.should_not eql(5)
|
149
149
|
end
|
150
150
|
|
151
151
|
specify "should remember :initial_value after being cleared" do
|
152
152
|
@data.value = 17
|
153
153
|
@data.clear
|
154
|
-
@data.value.should
|
154
|
+
@data.value.should eql(5)
|
155
155
|
end
|
156
156
|
end
|
157
157
|
|
@@ -161,25 +161,25 @@ context "A Single with :value" do
|
|
161
161
|
end
|
162
162
|
|
163
163
|
specify "should return that :value" do
|
164
|
-
@data.value.should
|
164
|
+
@data.value.should eql(5)
|
165
165
|
end
|
166
166
|
|
167
167
|
specify "should change during reading" do
|
168
168
|
io = StringIO.new([56].pack("V"))
|
169
169
|
@data.do_read(io)
|
170
|
-
@data.value.should
|
170
|
+
@data.value.should eql(56)
|
171
171
|
@data.done_read
|
172
172
|
end
|
173
173
|
|
174
174
|
specify "should not change after reading" do
|
175
175
|
io = StringIO.new([56].pack("V"))
|
176
176
|
@data.read(io)
|
177
|
-
@data.value.should
|
177
|
+
@data.value.should eql(5)
|
178
178
|
end
|
179
179
|
|
180
180
|
specify "should not be able to change the value" do
|
181
181
|
@data.value = 17
|
182
|
-
@data.value.should
|
182
|
+
@data.value.should eql(5)
|
183
183
|
end
|
184
184
|
end
|
185
185
|
|
data/spec/string_spec.rb
CHANGED
@@ -31,29 +31,29 @@ context "A String with :initial_length" do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
specify "should set num_bytes" do
|
34
|
-
@str.num_bytes.should
|
34
|
+
@str.num_bytes.should eql(5)
|
35
35
|
end
|
36
36
|
|
37
37
|
specify "should fill value with pad_char" do
|
38
|
-
@str.value.should
|
38
|
+
@str.value.should eql("\0\0\0\0\0")
|
39
39
|
end
|
40
40
|
|
41
41
|
specify "should read :initial_length bytes" do
|
42
42
|
io = StringIO.new("abcdefghij")
|
43
43
|
@str.read(io)
|
44
|
-
@str.value.should
|
44
|
+
@str.value.should eql("abcde")
|
45
45
|
end
|
46
46
|
|
47
47
|
specify "should forget :initial_length after value is set" do
|
48
48
|
@str.value = "abc"
|
49
|
-
@str.num_bytes.should
|
49
|
+
@str.num_bytes.should eql(3)
|
50
50
|
end
|
51
51
|
|
52
52
|
specify "should remember :initial_length after value is cleared" do
|
53
53
|
@str.value = "abc"
|
54
|
-
@str.num_bytes.should
|
54
|
+
@str.num_bytes.should eql(3)
|
55
55
|
@str.clear
|
56
|
-
@str.num_bytes.should
|
56
|
+
@str.num_bytes.should eql(5)
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
@@ -63,37 +63,37 @@ context "A String with :length" do
|
|
63
63
|
end
|
64
64
|
|
65
65
|
specify "should set num_bytes" do
|
66
|
-
@str.num_bytes.should
|
66
|
+
@str.num_bytes.should eql(5)
|
67
67
|
end
|
68
68
|
|
69
69
|
specify "should fill value with pad_char" do
|
70
|
-
@str.value.should
|
70
|
+
@str.value.should eql("\0\0\0\0\0")
|
71
71
|
end
|
72
72
|
|
73
73
|
specify "should retain :length after value is set" do
|
74
74
|
@str.value = "abcdefghij"
|
75
|
-
@str.num_bytes.should
|
75
|
+
@str.num_bytes.should eql(5)
|
76
76
|
end
|
77
77
|
|
78
78
|
specify "should read :length bytes" do
|
79
79
|
io = StringIO.new("abcdefghij")
|
80
80
|
@str.read(io)
|
81
|
-
@str.value.should
|
81
|
+
@str.value.should eql("abcde")
|
82
82
|
end
|
83
83
|
|
84
84
|
specify "should pad values less than :length" do
|
85
85
|
@str.value = "abc"
|
86
|
-
@str.value.should
|
86
|
+
@str.value.should eql("abc\0\0")
|
87
87
|
end
|
88
88
|
|
89
89
|
specify "should accept values exactly :length" do
|
90
90
|
@str.value = "abcde"
|
91
|
-
@str.value.should
|
91
|
+
@str.value.should eql("abcde")
|
92
92
|
end
|
93
93
|
|
94
94
|
specify "should truncate values greater than :length" do
|
95
95
|
@str.value = "abcdefg"
|
96
|
-
@str.value.should
|
96
|
+
@str.value.should eql("abcde")
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -103,31 +103,31 @@ context "A String with :initial_length and :value" do
|
|
103
103
|
end
|
104
104
|
|
105
105
|
specify "should use :initial_length before value is read" do
|
106
|
-
@str.num_bytes.should
|
107
|
-
@str.value.should
|
106
|
+
@str.num_bytes.should eql(5)
|
107
|
+
@str.value.should eql("abcde")
|
108
108
|
end
|
109
109
|
|
110
110
|
specify "should use :initial_length for reading" do
|
111
111
|
io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
|
112
112
|
@str.read(io)
|
113
|
-
io.pos.should
|
113
|
+
io.pos.should eql(5)
|
114
114
|
end
|
115
115
|
|
116
116
|
specify "should forget :initial_length after reading" do
|
117
117
|
io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
|
118
118
|
@str.read(io)
|
119
|
-
@str.num_bytes.should
|
120
|
-
@str.value.should
|
119
|
+
@str.num_bytes.should eql(10)
|
120
|
+
@str.value.should eql("abcdefghij")
|
121
121
|
end
|
122
122
|
|
123
123
|
specify "should return read value before calling done_read" do
|
124
124
|
io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
|
125
125
|
|
126
126
|
@str.do_read(io)
|
127
|
-
@str.value.should
|
127
|
+
@str.value.should eql("ABCDE")
|
128
128
|
|
129
129
|
@str.done_read
|
130
|
-
@str.value.should
|
130
|
+
@str.value.should eql("abcdefghij")
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
@@ -137,16 +137,16 @@ context "A String with :length and :initial_value" do
|
|
137
137
|
end
|
138
138
|
|
139
139
|
specify "should apply :length to :initial_value" do
|
140
|
-
@str.num_bytes.should
|
141
|
-
@str.value.should
|
140
|
+
@str.num_bytes.should eql(5)
|
141
|
+
@str.value.should eql("abcde")
|
142
142
|
end
|
143
143
|
|
144
144
|
specify "should forget :initial_value after reading" do
|
145
145
|
io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
|
146
146
|
@str.read(io)
|
147
|
-
io.pos.should
|
148
|
-
@str.num_bytes.should
|
149
|
-
@str.value.should
|
147
|
+
io.pos.should eql(5)
|
148
|
+
@str.num_bytes.should eql(5)
|
149
|
+
@str.value.should eql("ABCDE")
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
@@ -154,13 +154,13 @@ context "A String with :pad_char" do
|
|
154
154
|
specify "should accept a numeric value for :pad_char" do
|
155
155
|
@str = BinData::String.new(:length => 5, :pad_char => 6)
|
156
156
|
@str.value = "abc"
|
157
|
-
@str.value.should
|
157
|
+
@str.value.should eql("abc\x06\x06")
|
158
158
|
end
|
159
159
|
|
160
160
|
specify "should accept a character for :pad_char" do
|
161
161
|
@str = BinData::String.new(:length => 5, :pad_char => "R")
|
162
162
|
@str.value = "abc"
|
163
|
-
@str.value.should
|
163
|
+
@str.value.should eql("abcRR")
|
164
164
|
end
|
165
165
|
|
166
166
|
specify "should not accept a string for :pad_char" do
|
@@ -175,31 +175,31 @@ context "A String with :trim_value" do
|
|
175
175
|
str2 = BinData::String.new(:length => 5, :trim_value => false)
|
176
176
|
str1.value = "abc"
|
177
177
|
str2.value = "abc"
|
178
|
-
str1.value.should
|
179
|
-
str2.value.should
|
178
|
+
str1.value.should eql("abc\0\0")
|
179
|
+
str2.value.should eql("abc\0\0")
|
180
180
|
end
|
181
181
|
|
182
182
|
specify "should trim the value" do
|
183
183
|
str = BinData::String.new(:pad_char => 'R', :trim_value => true)
|
184
184
|
str.value = "abcRR"
|
185
|
-
str.value.should
|
185
|
+
str.value.should eql("abc")
|
186
186
|
end
|
187
187
|
|
188
188
|
specify "should not affect num_bytes" do
|
189
189
|
str = BinData::String.new(:pad_char => 'R', :trim_value => true)
|
190
190
|
str.value = "abcRR"
|
191
|
-
str.num_bytes.should
|
191
|
+
str.num_bytes.should eql(5)
|
192
192
|
end
|
193
193
|
|
194
194
|
specify "should trim if last char is :pad_char" do
|
195
195
|
str = BinData::String.new(:pad_char => 'R', :trim_value => true)
|
196
196
|
str.value = "abcRR"
|
197
|
-
str.value.should
|
197
|
+
str.value.should eql("abc")
|
198
198
|
end
|
199
199
|
|
200
200
|
specify "should not trim if value contains :pad_char not at the end" do
|
201
201
|
str = BinData::String.new(:pad_char => 'R', :trim_value => true)
|
202
202
|
str.value = "abcRRde"
|
203
|
-
str.value.should
|
203
|
+
str.value.should eql("abcRRde")
|
204
204
|
end
|
205
205
|
end
|
data/spec/stringz_spec.rb
CHANGED
@@ -9,18 +9,18 @@ context "An empty Stringz data object" do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
specify "should include the zero byte in num_bytes total" do
|
12
|
-
@str.num_bytes.should
|
12
|
+
@str.num_bytes.should eql(1)
|
13
13
|
end
|
14
14
|
|
15
15
|
specify "should not append the zero byte terminator to the value" do
|
16
|
-
@str.value.should
|
16
|
+
@str.value.should eql("")
|
17
17
|
end
|
18
18
|
|
19
19
|
specify "should write the zero byte terminator" do
|
20
20
|
io = StringIO.new
|
21
21
|
@str.write(io)
|
22
22
|
io.rewind
|
23
|
-
io.read.should
|
23
|
+
io.read.should eql("\0")
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -31,18 +31,18 @@ context "A Stringz data object with value set" do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
specify "should include the zero byte in num_bytes total" do
|
34
|
-
@str.num_bytes.should
|
34
|
+
@str.num_bytes.should eql(5)
|
35
35
|
end
|
36
36
|
|
37
37
|
specify "should not append the zero byte terminator to the value" do
|
38
|
-
@str.value.should
|
38
|
+
@str.value.should eql("abcd")
|
39
39
|
end
|
40
40
|
|
41
41
|
specify "should write the zero byte terminator" do
|
42
42
|
io = StringIO.new
|
43
43
|
@str.write(io)
|
44
44
|
io.rewind
|
45
|
-
io.read.should
|
45
|
+
io.read.should eql("abcd\0")
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -54,15 +54,15 @@ context "Reading with a Stringz data object" do
|
|
54
54
|
specify "should stop at the first zero byte" do
|
55
55
|
io = StringIO.new("abcd\0xyz\0")
|
56
56
|
@str.read(io)
|
57
|
-
@str.value.should
|
58
|
-
io.read(1).should
|
57
|
+
@str.value.should eql("abcd")
|
58
|
+
io.read(1).should eql("x")
|
59
59
|
end
|
60
60
|
|
61
61
|
specify "should handle a zero length string" do
|
62
62
|
io = StringIO.new("\0abcd")
|
63
63
|
@str.read(io)
|
64
|
-
@str.value.should
|
65
|
-
io.read(1).should
|
64
|
+
@str.value.should eql("")
|
65
|
+
io.read(1).should eql("a")
|
66
66
|
end
|
67
67
|
|
68
68
|
specify "should fail if no zero byte is found" do
|
@@ -78,27 +78,27 @@ context "Setting the value of a Stringz data object" do
|
|
78
78
|
|
79
79
|
specify "should include the zero byte in num_bytes total" do
|
80
80
|
@str.value = "abcd"
|
81
|
-
@str.num_bytes.should
|
81
|
+
@str.num_bytes.should eql(5)
|
82
82
|
end
|
83
83
|
|
84
84
|
specify "should accept empty strings" do
|
85
85
|
@str.value = ""
|
86
|
-
@str.value.should
|
86
|
+
@str.value.should eql("")
|
87
87
|
end
|
88
88
|
|
89
89
|
specify "should accept strings that aren't zero terminated" do
|
90
90
|
@str.value = "abcd"
|
91
|
-
@str.value.should
|
91
|
+
@str.value.should eql("abcd")
|
92
92
|
end
|
93
93
|
|
94
94
|
specify "should accept strings that are zero terminated" do
|
95
95
|
@str.value = "abcd\0"
|
96
|
-
@str.value.should
|
96
|
+
@str.value.should eql("abcd")
|
97
97
|
end
|
98
98
|
|
99
99
|
specify "should accept up to the first zero byte" do
|
100
100
|
@str.value = "abcd\0xyz\0"
|
101
|
-
@str.value.should
|
101
|
+
@str.value.should eql("abcd")
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
@@ -110,35 +110,35 @@ context "A Stringz data object with max_length" do
|
|
110
110
|
specify "should read less than max_length" do
|
111
111
|
io = StringIO.new("abc\0xyz")
|
112
112
|
@str.read(io)
|
113
|
-
@str.value.should
|
113
|
+
@str.value.should eql("abc")
|
114
114
|
end
|
115
115
|
|
116
116
|
specify "should read exactly max_length" do
|
117
117
|
io = StringIO.new("abcd\0xyz")
|
118
118
|
@str.read(io)
|
119
|
-
@str.value.should
|
119
|
+
@str.value.should eql("abcd")
|
120
120
|
end
|
121
121
|
|
122
122
|
specify "should read no more than max_length" do
|
123
123
|
io = StringIO.new("abcdefg\0xyz")
|
124
124
|
@str.read(io)
|
125
|
-
@str.value.should
|
126
|
-
io.read(1).should
|
125
|
+
@str.value.should eql("abcd")
|
126
|
+
io.read(1).should eql("f")
|
127
127
|
end
|
128
128
|
|
129
129
|
specify "should accept values less than max_length" do
|
130
130
|
@str.value = "abc"
|
131
|
-
@str.value.should
|
131
|
+
@str.value.should eql("abc")
|
132
132
|
end
|
133
133
|
|
134
134
|
specify "should accept values exactly max_length" do
|
135
135
|
@str.value = "abcd"
|
136
|
-
@str.value.should
|
136
|
+
@str.value.should eql("abcd")
|
137
137
|
end
|
138
138
|
|
139
139
|
specify "should trim values greater than max_length" do
|
140
140
|
@str.value = "abcde"
|
141
|
-
@str.value.should
|
141
|
+
@str.value.should eql("abcd")
|
142
142
|
end
|
143
143
|
|
144
144
|
specify "should write values less than max_length" do
|
@@ -146,7 +146,7 @@ context "A Stringz data object with max_length" do
|
|
146
146
|
@str.value = "abc"
|
147
147
|
@str.write(io)
|
148
148
|
io.rewind
|
149
|
-
io.read.should
|
149
|
+
io.read.should eql("abc\0")
|
150
150
|
end
|
151
151
|
|
152
152
|
specify "should write values exactly max_length" do
|
@@ -154,6 +154,6 @@ context "A Stringz data object with max_length" do
|
|
154
154
|
@str.value = "abcd"
|
155
155
|
@str.write(io)
|
156
156
|
io.rewind
|
157
|
-
io.read.should
|
157
|
+
io.read.should eql("abcd\0")
|
158
158
|
end
|
159
159
|
end
|
data/spec/struct_spec.rb
CHANGED
@@ -23,9 +23,9 @@ context "A Struct with hidden fields" do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
specify "should be able to access hidden fields directly" do
|
26
|
-
@obj.b.should
|
26
|
+
@obj.b.should eql(10)
|
27
27
|
@obj.c = 15
|
28
|
-
@obj.c.should
|
28
|
+
@obj.c.should eql(15)
|
29
29
|
|
30
30
|
@obj.should respond_to?(:b=)
|
31
31
|
end
|
@@ -58,6 +58,20 @@ context "Defining a Struct" do
|
|
58
58
|
END
|
59
59
|
}.should raise_error(SyntaxError)
|
60
60
|
end
|
61
|
+
|
62
|
+
specify "should fail when field name shadows an existing method" do
|
63
|
+
lambda {
|
64
|
+
eval <<-END
|
65
|
+
class ExistingName < BinData::Struct
|
66
|
+
int8 :object_id
|
67
|
+
end
|
68
|
+
END
|
69
|
+
}.should raise_error(NameError)
|
70
|
+
|
71
|
+
lambda {
|
72
|
+
BinData::Struct.new(:fields => [[:int8, :object_id]])
|
73
|
+
}.should raise_error(NameError)
|
74
|
+
end
|
61
75
|
end
|
62
76
|
|
63
77
|
context "A Struct with multiple fields" do
|
@@ -69,9 +83,9 @@ context "A Struct with multiple fields" do
|
|
69
83
|
end
|
70
84
|
|
71
85
|
specify "should return num_bytes" do
|
72
|
-
@obj.num_bytes(:a).should
|
73
|
-
@obj.num_bytes(:b).should
|
74
|
-
@obj.num_bytes.should
|
86
|
+
@obj.num_bytes(:a).should eql(1)
|
87
|
+
@obj.num_bytes(:b).should eql(1)
|
88
|
+
@obj.num_bytes.should eql(2)
|
75
89
|
end
|
76
90
|
|
77
91
|
specify "should clear" do
|
@@ -93,21 +107,21 @@ context "A Struct with multiple fields" do
|
|
93
107
|
@obj.write(io)
|
94
108
|
|
95
109
|
io.rewind
|
96
|
-
io.read.should
|
110
|
+
io.read.should eql("\x01\x02")
|
97
111
|
end
|
98
112
|
|
99
113
|
specify "should read ordered" do
|
100
114
|
io = StringIO.new "\x03\x04"
|
101
115
|
@obj.read(io)
|
102
116
|
|
103
|
-
@obj.a.should
|
104
|
-
@obj.b.should
|
117
|
+
@obj.a.should eql(3)
|
118
|
+
@obj.b.should eql(4)
|
105
119
|
end
|
106
120
|
|
107
121
|
specify "should return a snapshot" do
|
108
122
|
snap = @obj.snapshot
|
109
|
-
snap.a.should
|
110
|
-
snap.b.should
|
123
|
+
snap.a.should eql(1)
|
124
|
+
snap.b.should eql(2)
|
111
125
|
snap.should == { "a" => 1, "b" => 2 }
|
112
126
|
end
|
113
127
|
|
@@ -175,16 +189,16 @@ context "A Struct with nested structs" do
|
|
175
189
|
end
|
176
190
|
|
177
191
|
specify "should access nested fields" do
|
178
|
-
@obj.a.should
|
179
|
-
@obj.b.w.should
|
180
|
-
@obj.b.x.should
|
181
|
-
@obj.y.should
|
192
|
+
@obj.a.should eql(6)
|
193
|
+
@obj.b.w.should eql(3)
|
194
|
+
@obj.b.x.should eql(6)
|
195
|
+
@obj.y.should eql(3)
|
182
196
|
end
|
183
197
|
|
184
198
|
specify "should return correct offset of" do
|
185
|
-
@obj.offset_of("b").should
|
186
|
-
@obj.offset_of("y").should
|
187
|
-
@obj.offset_of("z").should
|
199
|
+
@obj.offset_of("b").should eql(1)
|
200
|
+
@obj.offset_of("y").should eql(3)
|
201
|
+
@obj.offset_of("z").should eql(4)
|
188
202
|
end
|
189
203
|
end
|
190
204
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: bindata
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.5.
|
7
|
-
date: 2007-03-
|
6
|
+
version: 0.5.1
|
7
|
+
date: 2007-03-21 00:00:00 +09:00
|
8
8
|
summary: A declarative way to read and write binary file formats
|
9
9
|
require_paths:
|
10
10
|
- lib
|