bindata 0.9.3 → 0.10.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.
Potentially problematic release.
This version of bindata might be problematic. Click here for more details.
- data/ChangeLog +18 -0
- data/NEWS +59 -0
- data/README +22 -23
- data/TODO +18 -12
- data/examples/gzip.rb +4 -4
- data/lib/bindata.rb +4 -3
- data/lib/bindata/array.rb +202 -132
- data/lib/bindata/base.rb +147 -166
- data/lib/bindata/{single.rb → base_primitive.rb} +82 -56
- data/lib/bindata/bits.rb +31 -770
- data/lib/bindata/choice.rb +157 -82
- data/lib/bindata/float.rb +25 -27
- data/lib/bindata/int.rb +144 -177
- data/lib/bindata/io.rb +59 -49
- data/lib/bindata/lazy.rb +80 -50
- data/lib/bindata/params.rb +134 -26
- data/lib/bindata/{single_value.rb → primitive.rb} +71 -64
- data/lib/bindata/{multi_value.rb → record.rb} +52 -70
- data/lib/bindata/registry.rb +49 -17
- data/lib/bindata/rest.rb +6 -10
- data/lib/bindata/sanitize.rb +55 -70
- data/lib/bindata/string.rb +60 -42
- data/lib/bindata/stringz.rb +34 -35
- data/lib/bindata/struct.rb +197 -152
- data/lib/bindata/trace.rb +35 -0
- data/spec/array_spec.rb +128 -112
- data/spec/{single_spec.rb → base_primitive_spec.rb} +102 -61
- data/spec/base_spec.rb +190 -185
- data/spec/bits_spec.rb +126 -98
- data/spec/choice_spec.rb +89 -98
- data/spec/example.rb +19 -0
- data/spec/float_spec.rb +28 -44
- data/spec/int_spec.rb +217 -127
- data/spec/io_spec.rb +41 -24
- data/spec/lazy_spec.rb +95 -49
- data/spec/primitive_spec.rb +191 -0
- data/spec/{multi_value_spec.rb → record_spec.rb} +124 -89
- data/spec/registry_spec.rb +53 -12
- data/spec/rest_spec.rb +2 -3
- data/spec/sanitize_spec.rb +47 -73
- data/spec/spec_common.rb +13 -1
- data/spec/string_spec.rb +34 -23
- data/spec/stringz_spec.rb +10 -18
- data/spec/struct_spec.rb +91 -63
- data/spec/system_spec.rb +291 -0
- metadata +12 -8
- data/spec/single_value_spec.rb +0 -131
data/spec/rest_spec.rb
CHANGED
@@ -17,14 +17,13 @@ describe BinData::Rest do
|
|
17
17
|
rest = BinData::Rest.new
|
18
18
|
rest.value = "123"
|
19
19
|
rest.value.should == "123"
|
20
|
-
rest.
|
20
|
+
rest.to_binary_s.should == "123"
|
21
21
|
end
|
22
22
|
|
23
|
-
it "should accept BinData::
|
23
|
+
it "should accept BinData::BasePrimitive parameters" do
|
24
24
|
rest = BinData::Rest.new(:check_value => "abc")
|
25
25
|
lambda {
|
26
26
|
rest.read("abc")
|
27
27
|
}.should_not raise_error(BinData::ValidityError)
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
data/spec/sanitize_spec.rb
CHANGED
@@ -4,76 +4,17 @@ require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
|
|
4
4
|
require 'bindata/sanitize'
|
5
5
|
require 'bindata/int'
|
6
6
|
|
7
|
-
describe BinData::Sanitizer, "class methods" do
|
8
|
-
it "should resolve type with endian" do
|
9
|
-
BinData::Sanitizer.type_exists?(:int16, :little).should be_true
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should return if type exists" do
|
13
|
-
BinData::Sanitizer.type_exists?(:int8).should be_true
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should raise if type doesn't exist" do
|
17
|
-
BinData::Sanitizer.type_exists?(:does_not_exist).should be_false
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should lookup types" do
|
21
|
-
BinData::Sanitizer.lookup(:int16, :little).should == BinData::Int16le
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
describe BinData::Sanitizer do
|
27
|
-
before(:each) do
|
28
|
-
@sanitizer = BinData::Sanitizer.new
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should raise error on unknown types" do
|
32
|
-
lambda {
|
33
|
-
@sanitizer.lookup_klass(:does_not_exist)
|
34
|
-
}.should raise_error(TypeError)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should lookup when endian is set" do
|
38
|
-
@sanitizer.with_endian(:little) do
|
39
|
-
klass = @sanitizer.lookup_klass(:int16)
|
40
|
-
klass.should == BinData::Int16le
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should nest with_endian calls" do
|
45
|
-
@sanitizer.with_endian(:little) do
|
46
|
-
klass = @sanitizer.lookup_klass(:int16)
|
47
|
-
klass.should == BinData::Int16le
|
48
|
-
|
49
|
-
@sanitizer.with_endian(:big) do
|
50
|
-
klass = @sanitizer.lookup_klass(:int16)
|
51
|
-
klass.should == BinData::Int16be
|
52
|
-
end
|
53
|
-
|
54
|
-
klass = @sanitizer.lookup_klass(:int16)
|
55
|
-
klass.should == BinData::Int16le
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should sanitize parameters" do
|
60
|
-
params = @sanitizer.sanitize_params(BinData::Int8, {:value => 3})
|
61
|
-
params.should have_key(:value)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
7
|
describe BinData::SanitizedParameters, "with bad input" do
|
66
8
|
before(:each) do
|
67
9
|
@mock = mock("dummy class")
|
68
|
-
@mock.stub!(:
|
10
|
+
@mock.stub!(:accepted_internal_parameters).and_return([:a, :b, :c])
|
69
11
|
@params = {:a => 1, :b => 2, :c => 3, :d => 4, :e => 5}
|
70
12
|
end
|
71
13
|
|
72
14
|
it "should convert keys to symbols" do
|
73
15
|
the_params = {'a' => 1, 'b' => 2, 'e' => 5}
|
74
16
|
sanitized = BinData::SanitizedParameters.new(@mock, the_params)
|
75
|
-
sanitized.
|
76
|
-
sanitized.extra_parameters.should == ({:e => 5})
|
17
|
+
sanitized.parameters.should == {:a => 1, :b => 2, :e => 5}
|
77
18
|
end
|
78
19
|
|
79
20
|
it "should raise error if parameter has nil value" do
|
@@ -87,29 +28,23 @@ end
|
|
87
28
|
describe BinData::SanitizedParameters do
|
88
29
|
before(:each) do
|
89
30
|
@mock = mock("dummy class")
|
90
|
-
@mock.stub!(:
|
31
|
+
@mock.stub!(:accepted_internal_parameters).and_return([:a, :b, :c])
|
91
32
|
@params = {:a => 1, :b => 2, :c => 3, :d => 4, :e => 5}
|
92
33
|
end
|
93
34
|
|
94
|
-
it "should partition" do
|
95
|
-
sanitized = BinData::SanitizedParameters.new(@mock, @params)
|
96
|
-
sanitized.internal_parameters.should == ({:a => 1, :b => 2, :c => 3})
|
97
|
-
sanitized.extra_parameters.should == ({:d => 4, :e => 5})
|
98
|
-
end
|
99
|
-
|
100
35
|
it "should respond_to keys" do
|
101
36
|
sanitized = BinData::SanitizedParameters.new(@mock, @params)
|
102
37
|
sanitized.should respond_to(:keys)
|
103
|
-
keys = sanitized.keys.collect { |
|
104
|
-
keys.sort.should ==
|
38
|
+
keys = sanitized.keys.collect { |k| k.to_s }
|
39
|
+
keys.sort.should == ['a', 'b', 'c', 'd', 'e']
|
105
40
|
end
|
106
41
|
|
107
42
|
it "should respond_to has_key?" do
|
108
43
|
sanitized = BinData::SanitizedParameters.new(@mock, @params)
|
109
44
|
sanitized.should respond_to(:has_key?)
|
110
|
-
sanitized.
|
111
|
-
sanitized.
|
112
|
-
sanitized.
|
45
|
+
sanitized.should have_key(:a)
|
46
|
+
sanitized.should have_key(:e)
|
47
|
+
sanitized.should_not have_key(:z)
|
113
48
|
end
|
114
49
|
|
115
50
|
it "should respond_to []" do
|
@@ -119,3 +54,42 @@ describe BinData::SanitizedParameters do
|
|
119
54
|
sanitized[:d].should == 4
|
120
55
|
end
|
121
56
|
end
|
57
|
+
|
58
|
+
describe BinData::Sanitizer do
|
59
|
+
before(:each) do
|
60
|
+
@sanitizer = BinData::Sanitizer.new
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should raise error on unknown types" do
|
64
|
+
lambda {
|
65
|
+
@sanitizer.lookup_class(:does_not_exist)
|
66
|
+
}.should raise_error(TypeError)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should lookup when endian is set" do
|
70
|
+
@sanitizer.with_endian(:little) do
|
71
|
+
the_class = @sanitizer.lookup_class(:int16)
|
72
|
+
the_class.should == BinData::Int16le
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should nest with_endian calls" do
|
77
|
+
@sanitizer.with_endian(:little) do
|
78
|
+
the_class = @sanitizer.lookup_class(:int16)
|
79
|
+
the_class.should == BinData::Int16le
|
80
|
+
|
81
|
+
@sanitizer.with_endian(:big) do
|
82
|
+
the_class = @sanitizer.lookup_class(:int16)
|
83
|
+
the_class.should == BinData::Int16be
|
84
|
+
end
|
85
|
+
|
86
|
+
the_class = @sanitizer.lookup_class(:int16)
|
87
|
+
the_class.should == BinData::Int16le
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should sanitize parameters" do
|
92
|
+
params = @sanitizer.sanitized_params(BinData::Int8, {:value => 3})
|
93
|
+
params.should have_key(:value)
|
94
|
+
end
|
95
|
+
end
|
data/spec/spec_common.rb
CHANGED
@@ -2,9 +2,21 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
|
|
2
2
|
|
3
3
|
begin
|
4
4
|
require 'rubygems'
|
5
|
-
gem 'rspec', '>
|
5
|
+
gem 'rspec', '> 1.2.2'
|
6
6
|
rescue LoadError
|
7
7
|
end
|
8
8
|
|
9
9
|
require 'spec'
|
10
|
+
require 'spec/autorun'
|
10
11
|
require 'stringio'
|
12
|
+
|
13
|
+
class Object
|
14
|
+
def expose_methods_for_testing
|
15
|
+
cls = (Class === self) ? self : (class << self ; self; end)
|
16
|
+
private_method_names = cls.private_instance_methods - Object.private_instance_methods
|
17
|
+
cls.send(:public, *private_method_names)
|
18
|
+
|
19
|
+
protected_method_names = cls.protected_instance_methods - Object.protected_instance_methods
|
20
|
+
cls.send(:public, *protected_method_names)
|
21
|
+
end
|
22
|
+
end
|
data/spec/string_spec.rb
CHANGED
@@ -22,14 +22,30 @@ describe BinData::String, "with mutually exclusive parameters" do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
describe BinData::String, "with deprecated parameters" do
|
25
|
-
it "should substitude :
|
26
|
-
obj = BinData::String.new(:
|
27
|
-
|
28
|
-
obj.read(io)
|
25
|
+
it "should substitude :trim_padding for :trim_value" do
|
26
|
+
obj = BinData::String.new(:trim_value => true)
|
27
|
+
obj.value = "abc\0"
|
29
28
|
obj.value.should == "abc"
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
32
|
+
describe BinData::String, "when assigning" do
|
33
|
+
before(:each) do
|
34
|
+
@small = BinData::String.new(:length => 3, :pad_char => "A")
|
35
|
+
@large = BinData::String.new(:length => 5, :pad_char => "B")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should copy data from small to large" do
|
39
|
+
@large.value = @small
|
40
|
+
@large.value.should == "AAABB"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should copy data from large to small" do
|
44
|
+
@small.value = @large
|
45
|
+
@small.value.should == "BBB"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
33
49
|
describe BinData::String, "with :read_length" do
|
34
50
|
before(:each) do
|
35
51
|
@str = BinData::String.new(:read_length => 5)
|
@@ -41,8 +57,7 @@ describe BinData::String, "with :read_length" do
|
|
41
57
|
end
|
42
58
|
|
43
59
|
it "should read :read_length bytes" do
|
44
|
-
|
45
|
-
@str.read(io)
|
60
|
+
@str.read("abcdefghij")
|
46
61
|
@str.value.should == "abcde"
|
47
62
|
end
|
48
63
|
|
@@ -50,8 +65,8 @@ describe BinData::String, "with :read_length" do
|
|
50
65
|
@str.value = "abc"
|
51
66
|
@str.num_bytes.should == 3
|
52
67
|
@str.clear
|
53
|
-
|
54
|
-
@str.read(
|
68
|
+
|
69
|
+
@str.read("abcdefghij")
|
55
70
|
@str.value.should == "abcde"
|
56
71
|
end
|
57
72
|
end
|
@@ -75,8 +90,7 @@ describe BinData::String, "with :length" do
|
|
75
90
|
end
|
76
91
|
|
77
92
|
it "should read :length bytes" do
|
78
|
-
|
79
|
-
@str.read(io)
|
93
|
+
@str.read("abcdefghij")
|
80
94
|
@str.value.should == "abcde"
|
81
95
|
end
|
82
96
|
|
@@ -113,8 +127,7 @@ describe BinData::String, "with :read_length and :initial_value" do
|
|
113
127
|
end
|
114
128
|
|
115
129
|
it "should forget :initial_value after reading" do
|
116
|
-
|
117
|
-
@str.read(io)
|
130
|
+
@str.read("ABCDEFGHIJKLMNOPQRST")
|
118
131
|
@str.num_bytes.should == 5
|
119
132
|
@str.value.should == "ABCDE"
|
120
133
|
end
|
@@ -124,6 +137,7 @@ end
|
|
124
137
|
describe BinData::String, "with :read_length and :value" do
|
125
138
|
before(:each) do
|
126
139
|
@str = BinData::String.new(:read_length => 5, :value => "abcdefghij")
|
140
|
+
@str.expose_methods_for_testing
|
127
141
|
end
|
128
142
|
|
129
143
|
it "should not be affected by :read_length before value is read" do
|
@@ -138,16 +152,13 @@ describe BinData::String, "with :read_length and :value" do
|
|
138
152
|
end
|
139
153
|
|
140
154
|
it "should not be affected by :read_length after reading" do
|
141
|
-
|
142
|
-
@str.read(io)
|
155
|
+
@str.read("ABCDEFGHIJKLMNOPQRST")
|
143
156
|
@str.num_bytes.should == 10
|
144
157
|
@str.value.should == "abcdefghij"
|
145
158
|
end
|
146
159
|
|
147
160
|
it "should return read value before calling done_read" do
|
148
|
-
|
149
|
-
|
150
|
-
@str.do_read(BinData::IO.new(io))
|
161
|
+
@str.do_read(BinData::IO.new("ABCDEFGHIJKLMNOPQRST"))
|
151
162
|
@str.value.should == "ABCDE"
|
152
163
|
|
153
164
|
@str.done_read
|
@@ -193,10 +204,10 @@ describe BinData::String, "with :pad_char" do
|
|
193
204
|
end
|
194
205
|
end
|
195
206
|
|
196
|
-
describe BinData::String, "with :
|
207
|
+
describe BinData::String, "with :trim_padding" do
|
197
208
|
it "set false is the default" do
|
198
209
|
str1 = BinData::String.new(:length => 5)
|
199
|
-
str2 = BinData::String.new(:length => 5, :
|
210
|
+
str2 = BinData::String.new(:length => 5, :trim_padding => false)
|
200
211
|
str1.value = "abc"
|
201
212
|
str2.value = "abc"
|
202
213
|
str1.value.should == "abc\0\0"
|
@@ -204,25 +215,25 @@ describe BinData::String, "with :trim_value" do
|
|
204
215
|
end
|
205
216
|
|
206
217
|
it "should trim the value" do
|
207
|
-
str = BinData::String.new(:pad_char => 'R', :
|
218
|
+
str = BinData::String.new(:pad_char => 'R', :trim_padding => true)
|
208
219
|
str.value = "abcRR"
|
209
220
|
str.value.should == "abc"
|
210
221
|
end
|
211
222
|
|
212
223
|
it "should not affect num_bytes" do
|
213
|
-
str = BinData::String.new(:pad_char => 'R', :
|
224
|
+
str = BinData::String.new(:pad_char => 'R', :trim_padding => true)
|
214
225
|
str.value = "abcRR"
|
215
226
|
str.num_bytes.should == 5
|
216
227
|
end
|
217
228
|
|
218
229
|
it "should trim if last char is :pad_char" do
|
219
|
-
str = BinData::String.new(:pad_char => 'R', :
|
230
|
+
str = BinData::String.new(:pad_char => 'R', :trim_padding => true)
|
220
231
|
str.value = "abcRR"
|
221
232
|
str.value.should == "abc"
|
222
233
|
end
|
223
234
|
|
224
235
|
it "should not trim if value contains :pad_char not at the end" do
|
225
|
-
str = BinData::String.new(:pad_char => 'R', :
|
236
|
+
str = BinData::String.new(:pad_char => 'R', :trim_padding => true)
|
226
237
|
str.value = "abcRRde"
|
227
238
|
str.value.should == "abcRRde"
|
228
239
|
end
|
data/spec/stringz_spec.rb
CHANGED
@@ -17,10 +17,7 @@ describe BinData::Stringz, "when empty" do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should write the zero byte terminator" do
|
20
|
-
|
21
|
-
@str.write(io)
|
22
|
-
io.rewind
|
23
|
-
io.read.should == "\0"
|
20
|
+
@str.to_binary_s.should == "\0"
|
24
21
|
end
|
25
22
|
end
|
26
23
|
|
@@ -39,10 +36,7 @@ describe BinData::Stringz, "with value set" do
|
|
39
36
|
end
|
40
37
|
|
41
38
|
it "should write the zero byte terminator" do
|
42
|
-
|
43
|
-
@str.write(io)
|
44
|
-
io.rewind
|
45
|
-
io.read.should == "abcd\0"
|
39
|
+
@str.to_binary_s.should == "abcd\0"
|
46
40
|
end
|
47
41
|
end
|
48
42
|
|
@@ -66,8 +60,7 @@ describe BinData::Stringz, "when reading" do
|
|
66
60
|
end
|
67
61
|
|
68
62
|
it "should fail if no zero byte is found" do
|
69
|
-
|
70
|
-
lambda {@str.read(io) }.should raise_error(EOFError)
|
63
|
+
lambda {@str.read("abcd") }.should raise_error(EOFError)
|
71
64
|
end
|
72
65
|
end
|
73
66
|
|
@@ -141,19 +134,18 @@ describe BinData::Stringz, "with max_length" do
|
|
141
134
|
@str.value.should == "abcd"
|
142
135
|
end
|
143
136
|
|
137
|
+
it "should write values greater than max_length" do
|
138
|
+
@str.value = "abcde"
|
139
|
+
@str.to_binary_s.should == "abcd\0"
|
140
|
+
end
|
141
|
+
|
144
142
|
it "should write values less than max_length" do
|
145
|
-
io = StringIO.new
|
146
143
|
@str.value = "abc"
|
147
|
-
@str.
|
148
|
-
io.rewind
|
149
|
-
io.read.should == "abc\0"
|
144
|
+
@str.to_binary_s.should == "abc\0"
|
150
145
|
end
|
151
146
|
|
152
147
|
it "should write values exactly max_length" do
|
153
|
-
io = StringIO.new
|
154
148
|
@str.value = "abcd"
|
155
|
-
@str.
|
156
|
-
io.rewind
|
157
|
-
io.read.should == "abcd\0"
|
149
|
+
@str.to_binary_s.should == "abcd\0"
|
158
150
|
end
|
159
151
|
end
|
data/spec/struct_spec.rb
CHANGED
@@ -3,36 +3,7 @@
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
|
4
4
|
require 'bindata'
|
5
5
|
|
6
|
-
describe BinData::Struct, "
|
7
|
-
before(:each) do
|
8
|
-
@params = { :hide => [:b, 'c'],
|
9
|
-
:fields => [
|
10
|
-
[:int8, :a],
|
11
|
-
[:int8, 'b', {:initial_value => 10}],
|
12
|
-
[:int8, :c],
|
13
|
-
[:int8, :d, {:value => :b}]] }
|
14
|
-
@obj = BinData::Struct.new(@params)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should only show fields that aren't hidden" do
|
18
|
-
@obj.field_names.should == ["a", "d"]
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should be able to access hidden fields directly" do
|
22
|
-
@obj.b.should == 10
|
23
|
-
@obj.c = 15
|
24
|
-
@obj.c.should == 15
|
25
|
-
|
26
|
-
@obj.should respond_to(:b=)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should not include hidden fields in snapshot" do
|
30
|
-
@obj.b = 5
|
31
|
-
@obj.snapshot.should == {"a" => 0, "d" => 5}
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe BinData::Struct do
|
6
|
+
describe BinData::Struct, "when initializing" do
|
36
7
|
it "should fail on non registered types" do
|
37
8
|
params = {:fields => [[:non_registered_type, :a]]}
|
38
9
|
lambda {
|
@@ -56,7 +27,6 @@ describe BinData::Struct do
|
|
56
27
|
end
|
57
28
|
|
58
29
|
it "should fail when field name shadows an existing method" do
|
59
|
-
# note that #invert is from Hash.instance_methods
|
60
30
|
params = {:fields => [[:int8, :object_id]]}
|
61
31
|
lambda {
|
62
32
|
BinData::Struct.new(params)
|
@@ -71,6 +41,35 @@ describe BinData::Struct do
|
|
71
41
|
end
|
72
42
|
end
|
73
43
|
|
44
|
+
describe BinData::Struct, "with hidden fields" do
|
45
|
+
before(:each) do
|
46
|
+
@params = { :hide => [:b, 'c'],
|
47
|
+
:fields => [
|
48
|
+
[:int8, :a],
|
49
|
+
[:int8, 'b', {:initial_value => 10}],
|
50
|
+
[:int8, :c],
|
51
|
+
[:int8, :d, {:value => :b}]] }
|
52
|
+
@obj = BinData::Struct.new(@params)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should only show fields that aren't hidden" do
|
56
|
+
@obj.field_names.should == ["a", "d"]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should be able to access hidden fields directly" do
|
60
|
+
@obj.b.should == 10
|
61
|
+
@obj.c = 15
|
62
|
+
@obj.c.should == 15
|
63
|
+
|
64
|
+
@obj.should respond_to(:b=)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should not include hidden fields in snapshot" do
|
68
|
+
@obj.b = 5
|
69
|
+
@obj.snapshot.should == {"a" => 0, "d" => 5}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
74
73
|
describe BinData::Struct, "with multiple fields" do
|
75
74
|
before(:each) do
|
76
75
|
@params = { :fields => [ [:int8, :a], [:int8, :b] ] }
|
@@ -86,9 +85,9 @@ describe BinData::Struct, "with multiple fields" do
|
|
86
85
|
end
|
87
86
|
|
88
87
|
it "should identify accepted parameters" do
|
89
|
-
BinData::Struct.
|
90
|
-
BinData::Struct.
|
91
|
-
BinData::Struct.
|
88
|
+
BinData::Struct.accepted_internal_parameters.should include(:fields)
|
89
|
+
BinData::Struct.accepted_internal_parameters.should include(:hide)
|
90
|
+
BinData::Struct.accepted_internal_parameters.should include(:endian)
|
92
91
|
end
|
93
92
|
|
94
93
|
it "should return field names" do
|
@@ -98,28 +97,23 @@ describe BinData::Struct, "with multiple fields" do
|
|
98
97
|
it "should clear" do
|
99
98
|
@obj.a = 6
|
100
99
|
@obj.clear
|
101
|
-
@obj.
|
100
|
+
@obj.should be_clear
|
102
101
|
end
|
103
102
|
|
104
103
|
it "should clear individual elements" do
|
105
104
|
@obj.a = 6
|
106
105
|
@obj.b = 7
|
107
106
|
@obj.clear(:a)
|
108
|
-
@obj.
|
109
|
-
@obj.
|
107
|
+
@obj.should be_clear(:a)
|
108
|
+
@obj.should_not be_clear(:b)
|
110
109
|
end
|
111
110
|
|
112
111
|
it "should write ordered" do
|
113
|
-
|
114
|
-
@obj.write(io)
|
115
|
-
|
116
|
-
io.rewind
|
117
|
-
io.read.should == "\x01\x02"
|
112
|
+
@obj.to_binary_s.should == "\x01\x02"
|
118
113
|
end
|
119
114
|
|
120
115
|
it "should read ordered" do
|
121
|
-
|
122
|
-
@obj.read(io)
|
116
|
+
@obj.read("\x03\x04")
|
123
117
|
|
124
118
|
@obj.a.should == 3
|
125
119
|
@obj.b.should == 4
|
@@ -132,10 +126,38 @@ describe BinData::Struct, "with multiple fields" do
|
|
132
126
|
snap.should == { "a" => 1, "b" => 2 }
|
133
127
|
end
|
134
128
|
|
135
|
-
it "should
|
136
|
-
@obj.
|
129
|
+
it "should assign from partial hash" do
|
130
|
+
@obj.assign("a" => 3)
|
131
|
+
@obj.a.should == 3
|
132
|
+
@obj.b.should == 0
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should assign from hash" do
|
136
|
+
@obj.assign("a" => 3, "b" => 4)
|
137
|
+
@obj.a.should == 3
|
138
|
+
@obj.b.should == 4
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should assign from Struct" do
|
142
|
+
src = BinData::Struct.new(@params)
|
143
|
+
src.a = 3
|
144
|
+
src.b = 4
|
145
|
+
|
146
|
+
@obj.assign(src)
|
147
|
+
@obj.a.should == 3
|
148
|
+
@obj.b.should == 4
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should assign from snapshot" do
|
152
|
+
src = BinData::Struct.new(@params)
|
153
|
+
src.a = 3
|
154
|
+
src.b = 4
|
155
|
+
|
156
|
+
@obj.assign(src.snapshot)
|
157
|
+
@obj.a.should == 3
|
158
|
+
@obj.b.should == 4
|
137
159
|
end
|
138
|
-
|
160
|
+
|
139
161
|
it "should fail on unknown method call" do
|
140
162
|
lambda { @obj.does_not_exist }.should raise_error(NoMethodError)
|
141
163
|
end
|
@@ -174,11 +196,11 @@ describe BinData::Struct, "with nested structs" do
|
|
174
196
|
@obj.c.z.should == 0
|
175
197
|
end
|
176
198
|
|
177
|
-
it "should return correct offset
|
178
|
-
@obj.
|
179
|
-
@obj.
|
180
|
-
@obj.
|
181
|
-
@obj.
|
199
|
+
it "should return correct offset" do
|
200
|
+
@obj.b.offset.should == 1
|
201
|
+
@obj.b.w.offset.should == 1
|
202
|
+
@obj.c.offset.should == 3
|
203
|
+
@obj.c.z.offset.should == 4
|
182
204
|
end
|
183
205
|
end
|
184
206
|
|
@@ -215,35 +237,42 @@ describe BinData::Struct, "with an endian defined" do
|
|
215
237
|
|
216
238
|
expected = [1, 2.0, 3, 4, 5, 6, 7, 8].pack('veCCVvNv')
|
217
239
|
|
218
|
-
|
219
|
-
@obj.write(io)
|
220
|
-
|
221
|
-
io.rewind
|
222
|
-
io.read.should == expected
|
240
|
+
@obj.to_binary_s.should == expected
|
223
241
|
end
|
224
242
|
end
|
225
243
|
|
226
244
|
describe BinData::Struct, "with bit fields" do
|
227
245
|
before(:each) do
|
228
|
-
@params = { :fields => [ [:bit1le, :a], [:bit2le, :b] ] }
|
246
|
+
@params = { :fields => [ [:bit1le, :a], [:bit2le, :b], [:uint8, :c], [:bit1le, :d] ] }
|
229
247
|
@obj = BinData::Struct.new(@params)
|
230
248
|
@obj.a = 1
|
231
249
|
@obj.b = 2
|
250
|
+
@obj.c = 3
|
251
|
+
@obj.d = 1
|
232
252
|
end
|
233
253
|
|
234
254
|
it "should return num_bytes" do
|
235
|
-
@obj.num_bytes.should ==
|
255
|
+
@obj.num_bytes.should == 3
|
236
256
|
end
|
237
257
|
|
238
258
|
it "should write" do
|
239
|
-
@obj.
|
259
|
+
@obj.to_binary_s.should == [0b0000_0101, 3, 1].pack("C*")
|
240
260
|
end
|
241
261
|
|
242
262
|
it "should read" do
|
243
|
-
str = [0b0000_0110].pack("C")
|
263
|
+
str = [0b0000_0110, 5, 0].pack("C*")
|
244
264
|
@obj.read(str)
|
245
265
|
@obj.a.should == 0
|
246
266
|
@obj.b.should == 3
|
267
|
+
@obj.c.should == 5
|
268
|
+
@obj.d.should == 0
|
269
|
+
end
|
270
|
+
|
271
|
+
it "should have correct offsets" do
|
272
|
+
@obj.a.offset.should == 0
|
273
|
+
@obj.b.offset.should == 0
|
274
|
+
@obj.c.offset.should == 1
|
275
|
+
@obj.d.offset.should == 2
|
247
276
|
end
|
248
277
|
end
|
249
278
|
|
@@ -256,8 +285,7 @@ describe BinData::Struct, "with nested endian" do
|
|
256
285
|
[:struct, :s, nested_params],
|
257
286
|
[:int16, :d]] }
|
258
287
|
obj = BinData::Struct.new(params)
|
259
|
-
|
260
|
-
obj.read(str)
|
288
|
+
obj.read("\x00\x01\x02\x00\x03\x00\x00\x04")
|
261
289
|
|
262
290
|
obj.a.should == 1
|
263
291
|
obj.s.b.should == 2
|