bindata 1.4.3 → 1.4.4
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/BSDL +1 -1
- data/ChangeLog +8 -0
- data/NEWS +7 -0
- data/lib/bindata.rb +3 -3
- data/lib/bindata/base.rb +7 -0
- data/lib/bindata/base_primitive.rb +2 -2
- data/lib/bindata/choice.rb +4 -0
- data/lib/bindata/io.rb +1 -1
- data/lib/bindata/lazy.rb +2 -2
- data/lib/bindata/sanitize.rb +9 -0
- data/lib/bindata/string.rb +33 -16
- data/manual.haml +55 -53
- data/manual.md +346 -253
- data/spec/alignment_spec.rb +7 -7
- data/spec/array_spec.rb +67 -66
- data/spec/base_primitive_spec.rb +47 -49
- data/spec/base_spec.rb +70 -73
- data/spec/bits_spec.rb +14 -14
- data/spec/choice_spec.rb +44 -41
- data/spec/count_bytes_remaining_spec.rb +8 -11
- data/spec/deprecated_spec.rb +29 -29
- data/spec/float_spec.rb +12 -12
- data/spec/int_spec.rb +24 -24
- data/spec/io_spec.rb +56 -56
- data/spec/lazy_spec.rb +41 -39
- data/spec/primitive_spec.rb +31 -33
- data/spec/record_spec.rb +55 -57
- data/spec/registry_spec.rb +25 -25
- data/spec/rest_spec.rb +5 -5
- data/spec/skip_spec.rb +3 -3
- data/spec/spec_common.rb +2 -2
- data/spec/string_spec.rb +38 -38
- data/spec/stringz_spec.rb +18 -18
- data/spec/struct_spec.rb +48 -48
- data/spec/system_spec.rb +38 -37
- data/spec/wrapper_spec.rb +21 -23
- data/tasks/pkg.rake +3 -3
- data/tasks/rdoc.rake +1 -1
- data/tasks/rspec.rake +7 -7
- metadata +51 -53
data/spec/lazy_spec.rb
CHANGED
@@ -26,6 +26,8 @@ class MockBinDataObject
|
|
26
26
|
def lazy_evaluator
|
27
27
|
BinData::LazyEvaluator.new(self)
|
28
28
|
end
|
29
|
+
|
30
|
+
alias_method :safe_respond_to?, :respond_to?
|
29
31
|
end
|
30
32
|
|
31
33
|
def lazy_eval(*rest)
|
@@ -39,34 +41,34 @@ describe BinData::LazyEvaluator, "with no parents" do
|
|
39
41
|
MockBinDataObject.new(methods, params)
|
40
42
|
}
|
41
43
|
|
42
|
-
it "
|
44
|
+
it "evaluates raw value when instantiated" do
|
43
45
|
lazy_eval(5).should == 5
|
44
46
|
end
|
45
47
|
|
46
|
-
it "
|
48
|
+
it "evaluates raw value" do
|
47
49
|
lazy_eval(5).should == 5
|
48
50
|
end
|
49
51
|
|
50
|
-
it "
|
52
|
+
it "evaluates value" do
|
51
53
|
lazy_eval(lambda { 5 }).should == 5
|
52
54
|
end
|
53
55
|
|
54
|
-
it "
|
56
|
+
it "evaluates overrides" do
|
55
57
|
lazy_eval(lambda { o1 }, :o1 => 'o1').should == 'o1'
|
56
58
|
end
|
57
59
|
|
58
|
-
it "
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
it "does not resolve any unknown methods" do
|
61
|
+
expect { lazy_eval(lambda { unknown }) }.to raise_error(NameError)
|
62
|
+
expect { lazy_eval(lambda { m1 }) }.to raise_error(NameError)
|
63
|
+
expect { lazy_eval(lambda { p1 }) }.to raise_error(NameError)
|
62
64
|
end
|
63
65
|
|
64
|
-
it "
|
66
|
+
it "does not have a parent" do
|
65
67
|
lazy_eval(lambda { parent }).should be_nil
|
66
68
|
end
|
67
69
|
|
68
|
-
it "
|
69
|
-
|
70
|
+
it "does not resolve #index" do
|
71
|
+
expect { lazy_eval(lambda { index }) }.to raise_error(NoMethodError)
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
@@ -85,48 +87,48 @@ describe BinData::LazyEvaluator, "with one parent" do
|
|
85
87
|
MockBinDataObject.new(methods, params, parent_obj)
|
86
88
|
}
|
87
89
|
|
88
|
-
it "
|
90
|
+
it "evaluates raw value" do
|
89
91
|
lazy_eval(5).should == 5
|
90
92
|
end
|
91
93
|
|
92
|
-
it "
|
94
|
+
it "evaluates value" do
|
93
95
|
lazy_eval(lambda { 5 }).should == 5
|
94
96
|
end
|
95
97
|
|
96
|
-
it "
|
98
|
+
it "evaluates overrides before params" do
|
97
99
|
lazy_eval(lambda { p1 }, :p1 => 'o1').should == 'o1'
|
98
100
|
end
|
99
101
|
|
100
|
-
it "
|
102
|
+
it "evaluates overrides before methods" do
|
101
103
|
lazy_eval(lambda { m1 }, :m1 => 'o1').should == 'o1'
|
102
104
|
end
|
103
105
|
|
104
|
-
it "
|
105
|
-
|
106
|
+
it "does not resolve any unknown methods" do
|
107
|
+
expect { lazy_eval(lambda { unknown }) }.to raise_error(NameError)
|
106
108
|
end
|
107
109
|
|
108
|
-
it "
|
110
|
+
it "resolves parameters in the parent" do
|
109
111
|
lazy_eval(lambda { p1 }).should == 'Pp1'
|
110
112
|
end
|
111
113
|
|
112
|
-
it "
|
114
|
+
it "resolves methods in the parent" do
|
113
115
|
lazy_eval(lambda { m1 }).should == 'Pm1'
|
114
116
|
end
|
115
117
|
|
116
|
-
it "
|
118
|
+
it "invokes methods in the parent" do
|
117
119
|
lazy_eval(lambda { echo(p1, m1) }).should == ['Pp1', 'Pm1']
|
118
120
|
end
|
119
121
|
|
120
|
-
it "
|
122
|
+
it "resolves parameters in preference to methods in the parent" do
|
121
123
|
lazy_eval(lambda { com }).should == 'PpC'
|
122
124
|
end
|
123
125
|
|
124
|
-
it "
|
126
|
+
it "has a parent" do
|
125
127
|
lazy_eval(lambda { parent }).should_not be_nil
|
126
128
|
end
|
127
129
|
|
128
|
-
it "
|
129
|
-
|
130
|
+
it "does not resolve #index" do
|
131
|
+
expect { lazy_eval(lambda { index }) }.to raise_error(NoMethodError)
|
130
132
|
end
|
131
133
|
end
|
132
134
|
|
@@ -157,59 +159,59 @@ describe BinData::LazyEvaluator, "with nested parents" do
|
|
157
159
|
MockBinDataObject.new(methods, params, parent_obj)
|
158
160
|
}
|
159
161
|
|
160
|
-
it "
|
162
|
+
it "accepts symbols as a shortcut to lambdas" do
|
161
163
|
lazy_eval(:p1).should == 'Pp1'
|
162
164
|
lazy_eval(:p2).should == 'PPp2'
|
163
165
|
lazy_eval(:m1).should == 'Pm1'
|
164
166
|
lazy_eval(:m2).should == 'PPm2'
|
165
167
|
end
|
166
168
|
|
167
|
-
it "
|
168
|
-
|
169
|
+
it "does not resolve any unknown methods" do
|
170
|
+
expect { lazy_eval(lambda { unknown }) }.to raise_error(NameError)
|
169
171
|
end
|
170
172
|
|
171
|
-
it "
|
173
|
+
it "resolves parameters in the parent" do
|
172
174
|
lazy_eval(lambda { p1 }).should == 'Pp1'
|
173
175
|
end
|
174
176
|
|
175
|
-
it "
|
177
|
+
it "resolves methods in the parent" do
|
176
178
|
lazy_eval(lambda { m1 }).should == 'Pm1'
|
177
179
|
end
|
178
180
|
|
179
|
-
it "
|
181
|
+
it "resolves parameters in the parent's parent" do
|
180
182
|
lazy_eval(lambda { p2 }).should == 'PPp2'
|
181
183
|
end
|
182
184
|
|
183
|
-
it "
|
185
|
+
it "resolves methods in the parent's parent" do
|
184
186
|
lazy_eval(lambda { m2 }).should == 'PPm2'
|
185
187
|
end
|
186
188
|
|
187
|
-
it "
|
189
|
+
it "invokes methods in the parent" do
|
188
190
|
lazy_eval(lambda { echo(m1) }).should == ['P', 'Pm1']
|
189
191
|
end
|
190
192
|
|
191
|
-
it "
|
193
|
+
it "invokes methods in the parent's parent" do
|
192
194
|
lazy_eval(lambda { parent.echo(m1) }, { :m1 => 'o1'}).should == ['PP', 'o1']
|
193
195
|
end
|
194
196
|
|
195
|
-
it "
|
197
|
+
it "invokes methods in the parent's parent" do
|
196
198
|
lazy_eval(lambda { echo2(m1) }).should == ['PP2', 'Pm1']
|
197
199
|
end
|
198
200
|
|
199
|
-
it "
|
201
|
+
it "resolves parameters in preference to methods in the parent" do
|
200
202
|
lazy_eval(lambda { com }).should == 'PpC'
|
201
203
|
end
|
202
204
|
|
203
|
-
it "
|
205
|
+
it "resolves methods in the parent explicitly" do
|
204
206
|
lazy_eval(lambda { parent.m1 }).should == 'PPm1'
|
205
207
|
end
|
206
208
|
|
207
|
-
it "
|
209
|
+
it "cascades lambdas " do
|
208
210
|
lazy_eval(lambda { sym1 }).should == 'PPm2'
|
209
211
|
lazy_eval(lambda { sym2 }).should == 'PPm2'
|
210
212
|
end
|
211
213
|
|
212
|
-
it "
|
213
|
-
|
214
|
+
it "does not resolve #index" do
|
215
|
+
expect { lazy_eval(lambda { index }) }.to raise_error(NoMethodError)
|
214
216
|
end
|
215
217
|
end
|
data/spec/primitive_spec.rb
CHANGED
@@ -4,12 +4,10 @@ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
|
4
4
|
require 'bindata'
|
5
5
|
|
6
6
|
describe BinData::Primitive do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
r.lookup("Primitive")
|
12
|
-
}.should raise_error(BinData::UnRegisteredTypeError)
|
7
|
+
it "is not registered" do
|
8
|
+
expect {
|
9
|
+
BinData::RegisteredClasses.lookup("Primitive")
|
10
|
+
}.to raise_error(BinData::UnRegisteredTypeError)
|
13
11
|
end
|
14
12
|
end
|
15
13
|
|
@@ -20,14 +18,14 @@ describe BinData::Primitive, "all subclasses" do
|
|
20
18
|
|
21
19
|
subject { SubClassOfPrimitive.new }
|
22
20
|
|
23
|
-
it "
|
24
|
-
|
25
|
-
|
21
|
+
it "raise errors on unimplemented methods" do
|
22
|
+
expect { subject.set(nil) }.to raise_error(NotImplementedError)
|
23
|
+
expect { subject.get }.to raise_error(NotImplementedError)
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
27
|
describe BinData::Primitive, "when defining with errors" do
|
30
|
-
it "
|
28
|
+
it "fails on non registered types" do
|
31
29
|
lambda {
|
32
30
|
class BadTypePrimitive < BinData::Primitive
|
33
31
|
non_registered_type :a
|
@@ -37,7 +35,7 @@ describe BinData::Primitive, "when defining with errors" do
|
|
37
35
|
}
|
38
36
|
end
|
39
37
|
|
40
|
-
it "
|
38
|
+
it "fails on duplicate names" do
|
41
39
|
lambda {
|
42
40
|
class DuplicateNamePrimitive < BinData::Primitive
|
43
41
|
int8 :a
|
@@ -49,7 +47,7 @@ describe BinData::Primitive, "when defining with errors" do
|
|
49
47
|
}
|
50
48
|
end
|
51
49
|
|
52
|
-
it "
|
50
|
+
it "fails when field name shadows an existing method" do
|
53
51
|
lambda {
|
54
52
|
class ExistingNamePrimitive < BinData::Primitive
|
55
53
|
int8 :object_id
|
@@ -59,7 +57,7 @@ describe BinData::Primitive, "when defining with errors" do
|
|
59
57
|
}
|
60
58
|
end
|
61
59
|
|
62
|
-
it "
|
60
|
+
it "fails on unknown endian" do
|
63
61
|
lambda {
|
64
62
|
class BadEndianPrimitive < BinData::Primitive
|
65
63
|
endian 'a bad value'
|
@@ -80,37 +78,37 @@ describe BinData::Primitive do
|
|
80
78
|
|
81
79
|
subject { PrimitiveWithEndian.new }
|
82
80
|
|
83
|
-
it "
|
81
|
+
it "assigns value" do
|
84
82
|
subject.value = 5
|
85
83
|
subject.value.should == 5
|
86
84
|
end
|
87
85
|
|
88
|
-
it "
|
86
|
+
it "produces binary string" do
|
89
87
|
subject.assign(5)
|
90
88
|
subject.to_binary_s.should == "\x05\x00"
|
91
89
|
end
|
92
90
|
|
93
|
-
it "
|
91
|
+
it "reads value" do
|
94
92
|
subject.read("\x00\x01")
|
95
93
|
subject.should == 0x100
|
96
94
|
end
|
97
95
|
|
98
|
-
it "
|
96
|
+
it "accepts standard parameters" do
|
99
97
|
subject = PrimitiveWithEndian.new(:initial_value => 2)
|
100
98
|
subject.to_binary_s.should == "\x02\x00"
|
101
99
|
end
|
102
100
|
|
103
|
-
it "
|
101
|
+
it "returns num_bytes" do
|
104
102
|
subject.num_bytes.should == 2
|
105
103
|
end
|
106
104
|
|
107
|
-
it "
|
108
|
-
|
105
|
+
it "raises error on missing methods" do
|
106
|
+
expect {
|
109
107
|
subject.does_not_exist
|
110
|
-
}.
|
108
|
+
}.to raise_error(NoMethodError)
|
111
109
|
end
|
112
110
|
|
113
|
-
it "
|
111
|
+
it "uses read value whilst reading" do
|
114
112
|
subject = PrimitiveWithEndian.new(:value => 2)
|
115
113
|
subject.read "\x05\x00"
|
116
114
|
subject.should == 2
|
@@ -119,7 +117,7 @@ describe BinData::Primitive do
|
|
119
117
|
subject.should == 5
|
120
118
|
end
|
121
119
|
|
122
|
-
it "
|
120
|
+
it "behaves as primitive" do
|
123
121
|
subject.assign(5)
|
124
122
|
(2 + subject).should == 7
|
125
123
|
end
|
@@ -132,7 +130,7 @@ describe BinData::Primitive, "requiring custom parameters" do
|
|
132
130
|
def set(v); self.a = v; end
|
133
131
|
end
|
134
132
|
|
135
|
-
it "
|
133
|
+
it "passes parameters correctly" do
|
136
134
|
subject = PrimitiveWithCustom.new(:iv => 5)
|
137
135
|
subject.should == 5
|
138
136
|
end
|
@@ -147,11 +145,11 @@ describe BinData::Primitive, "with custom mandatory parameters" do
|
|
147
145
|
def set(v); self.a = v; end
|
148
146
|
end
|
149
147
|
|
150
|
-
it "
|
151
|
-
|
148
|
+
it "raises error if mandatory parameter is not supplied" do
|
149
|
+
expect { MandatoryPrimitive.new }.to raise_error(ArgumentError)
|
152
150
|
end
|
153
151
|
|
154
|
-
it "
|
152
|
+
it "uses mandatory parameter" do
|
155
153
|
subject = MandatoryPrimitive.new(:arg1 => 5)
|
156
154
|
subject.should == 5
|
157
155
|
end
|
@@ -166,16 +164,16 @@ describe BinData::Primitive, "with custom default parameters" do
|
|
166
164
|
def set(v); self.a = v; end
|
167
165
|
end
|
168
166
|
|
169
|
-
it "
|
170
|
-
|
167
|
+
it "does not raise error if default parameter is not supplied" do
|
168
|
+
expect { DefaultPrimitive.new }.not_to raise_error(ArgumentError)
|
171
169
|
end
|
172
170
|
|
173
|
-
it "
|
171
|
+
it "uses default parameter" do
|
174
172
|
subject = DefaultPrimitive.new
|
175
173
|
subject.should == 5
|
176
174
|
end
|
177
175
|
|
178
|
-
it "
|
176
|
+
it "overrides default parameter" do
|
179
177
|
subject = DefaultPrimitive.new(:arg1 => 7)
|
180
178
|
subject.should == 7
|
181
179
|
end
|
@@ -192,12 +190,12 @@ describe BinData::Primitive, "subclassed with default parameter" do
|
|
192
190
|
default_parameter :initial_value => 5
|
193
191
|
end
|
194
192
|
|
195
|
-
it "
|
193
|
+
it "overrides initial_value" do
|
196
194
|
a = ChildDerivedPrimitive.new(:initial_value => 7)
|
197
195
|
a.to_binary_s.should == "\000\007"
|
198
196
|
end
|
199
197
|
|
200
|
-
it "
|
198
|
+
it "uses default parameter" do
|
201
199
|
a = ChildDerivedPrimitive.new
|
202
200
|
a.to_binary_s.should == "\000\005"
|
203
201
|
end
|
data/spec/record_spec.rb
CHANGED
@@ -4,17 +4,15 @@ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
|
4
4
|
require 'bindata'
|
5
5
|
|
6
6
|
describe BinData::Record do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
r.lookup("Record")
|
12
|
-
}.should raise_error(BinData::UnRegisteredTypeError)
|
7
|
+
it "is not registered" do
|
8
|
+
expect {
|
9
|
+
BinData::RegisteredClasses.lookup("Record")
|
10
|
+
}.to raise_error(BinData::UnRegisteredTypeError)
|
13
11
|
end
|
14
12
|
end
|
15
13
|
|
16
14
|
describe BinData::Record, "when defining with errors" do
|
17
|
-
it "
|
15
|
+
it "fails on non registered types" do
|
18
16
|
lambda {
|
19
17
|
class BadTypeRecord < BinData::Record
|
20
18
|
non_registered_type :a
|
@@ -24,7 +22,7 @@ describe BinData::Record, "when defining with errors" do
|
|
24
22
|
}
|
25
23
|
end
|
26
24
|
|
27
|
-
it "
|
25
|
+
it "gives correct error message for non registered nested types" do
|
28
26
|
lambda {
|
29
27
|
class BadNestedTypeRecord < BinData::Record
|
30
28
|
array :a, :type => :non_registered_type
|
@@ -34,7 +32,7 @@ describe BinData::Record, "when defining with errors" do
|
|
34
32
|
}
|
35
33
|
end
|
36
34
|
|
37
|
-
it "
|
35
|
+
it "gives correct error message for non registered nested types in blocks" do
|
38
36
|
lambda {
|
39
37
|
class BadNestedTypeInBlockRecord < BinData::Record
|
40
38
|
array :a do
|
@@ -46,7 +44,7 @@ describe BinData::Record, "when defining with errors" do
|
|
46
44
|
}
|
47
45
|
end
|
48
46
|
|
49
|
-
it "
|
47
|
+
it "fails on nested choice when missing names" do
|
50
48
|
lambda {
|
51
49
|
class MissingChoiceNamesRecord < BinData::Record
|
52
50
|
choice do
|
@@ -59,7 +57,7 @@ describe BinData::Record, "when defining with errors" do
|
|
59
57
|
}
|
60
58
|
end
|
61
59
|
|
62
|
-
it "
|
60
|
+
it "fails on malformed names" do
|
63
61
|
lambda {
|
64
62
|
class MalformedNameRecord < BinData::Record
|
65
63
|
int8 :a
|
@@ -70,7 +68,7 @@ describe BinData::Record, "when defining with errors" do
|
|
70
68
|
}
|
71
69
|
end
|
72
70
|
|
73
|
-
it "
|
71
|
+
it "fails on duplicate names" do
|
74
72
|
lambda {
|
75
73
|
class DuplicateNameRecord < BinData::Record
|
76
74
|
int8 :a
|
@@ -82,7 +80,7 @@ describe BinData::Record, "when defining with errors" do
|
|
82
80
|
}
|
83
81
|
end
|
84
82
|
|
85
|
-
it "
|
83
|
+
it "fails on reserved names" do
|
86
84
|
lambda {
|
87
85
|
class ReservedNameRecord < BinData::Record
|
88
86
|
int8 :a
|
@@ -93,7 +91,7 @@ describe BinData::Record, "when defining with errors" do
|
|
93
91
|
}
|
94
92
|
end
|
95
93
|
|
96
|
-
it "
|
94
|
+
it "fails when field name shadows an existing method" do
|
97
95
|
lambda {
|
98
96
|
class ExistingNameRecord < BinData::Record
|
99
97
|
int8 :object_id
|
@@ -103,7 +101,7 @@ describe BinData::Record, "when defining with errors" do
|
|
103
101
|
}
|
104
102
|
end
|
105
103
|
|
106
|
-
it "
|
104
|
+
it "fails on unknown endian" do
|
107
105
|
lambda {
|
108
106
|
class BadEndianRecord < BinData::Record
|
109
107
|
endian 'a bad value'
|
@@ -125,16 +123,16 @@ describe BinData::Record, "with anonymous fields" do
|
|
125
123
|
|
126
124
|
subject { AnonymousRecord.new }
|
127
125
|
|
128
|
-
it "
|
126
|
+
it "only shows non anonymous fields" do
|
129
127
|
subject.field_names.should == ["a"]
|
130
128
|
end
|
131
129
|
|
132
|
-
it "
|
130
|
+
it "does not include anonymous fields in snapshot" do
|
133
131
|
subject.a = 5
|
134
132
|
subject.snapshot.should == {"a" => 5}
|
135
133
|
end
|
136
134
|
|
137
|
-
it "
|
135
|
+
it "writes anonymous fields" do
|
138
136
|
str = "\001\002\003\004\005"
|
139
137
|
subject.read(str)
|
140
138
|
subject.a.clear
|
@@ -153,11 +151,11 @@ describe BinData::Record, "with hidden fields" do
|
|
153
151
|
|
154
152
|
subject { HiddenRecord.new }
|
155
153
|
|
156
|
-
it "
|
154
|
+
it "only shows fields that aren't hidden" do
|
157
155
|
subject.field_names.should == ["a", "d"]
|
158
156
|
end
|
159
157
|
|
160
|
-
it "
|
158
|
+
it "accesses hidden fields directly" do
|
161
159
|
subject.b.should == 10
|
162
160
|
subject.c = 15
|
163
161
|
subject.c.should == 15
|
@@ -165,7 +163,7 @@ describe BinData::Record, "with hidden fields" do
|
|
165
163
|
subject.should respond_to(:b=)
|
166
164
|
end
|
167
165
|
|
168
|
-
it "
|
166
|
+
it "does not include hidden fields in snapshot" do
|
169
167
|
subject.b = 5
|
170
168
|
subject.snapshot.should == {"a" => 0, "d" => 5}
|
171
169
|
end
|
@@ -179,24 +177,24 @@ describe BinData::Record, "with multiple fields" do
|
|
179
177
|
|
180
178
|
subject { MultiFieldRecord.new(:a => 1, :b => 2) }
|
181
179
|
|
182
|
-
it "
|
180
|
+
it "returns num_bytes" do
|
183
181
|
subject.a.num_bytes.should == 1
|
184
182
|
subject.b.num_bytes.should == 1
|
185
183
|
subject.num_bytes.should == 2
|
186
184
|
end
|
187
185
|
|
188
|
-
it "
|
186
|
+
it "identifies accepted parameters" do
|
189
187
|
BinData::Record.accepted_parameters.all.should include(:hide)
|
190
188
|
BinData::Record.accepted_parameters.all.should include(:endian)
|
191
189
|
end
|
192
190
|
|
193
|
-
it "
|
191
|
+
it "clears" do
|
194
192
|
subject.a = 6
|
195
193
|
subject.clear
|
196
194
|
subject.should be_clear
|
197
195
|
end
|
198
196
|
|
199
|
-
it "
|
197
|
+
it "clears individual elements" do
|
200
198
|
subject.a = 6
|
201
199
|
subject.b = 7
|
202
200
|
subject.a.clear
|
@@ -204,30 +202,30 @@ describe BinData::Record, "with multiple fields" do
|
|
204
202
|
subject.b.should_not be_clear
|
205
203
|
end
|
206
204
|
|
207
|
-
it "
|
205
|
+
it "writes ordered" do
|
208
206
|
subject.to_binary_s.should == "\x01\x02"
|
209
207
|
end
|
210
208
|
|
211
|
-
it "
|
209
|
+
it "reads ordered" do
|
212
210
|
subject.read("\x03\x04")
|
213
211
|
|
214
212
|
subject.a.should == 3
|
215
213
|
subject.b.should == 4
|
216
214
|
end
|
217
215
|
|
218
|
-
it "
|
216
|
+
it "returns a snapshot" do
|
219
217
|
snap = subject.snapshot
|
220
218
|
snap.a.should == 1
|
221
219
|
snap.b.should == 2
|
222
220
|
snap.should == { "a" => 1, "b" => 2 }
|
223
221
|
end
|
224
222
|
|
225
|
-
it "
|
223
|
+
it "returns field_names" do
|
226
224
|
subject.field_names.should == ["a", "b"]
|
227
225
|
end
|
228
226
|
|
229
|
-
it "
|
230
|
-
|
227
|
+
it "fails on unknown method call" do
|
228
|
+
expect { subject.does_not_exist }.to raise_error(NoMethodError)
|
231
229
|
end
|
232
230
|
end
|
233
231
|
|
@@ -247,22 +245,22 @@ describe BinData::Record, "with nested structs" do
|
|
247
245
|
|
248
246
|
subject { NestedStructRecord.new }
|
249
247
|
|
250
|
-
it "
|
248
|
+
it "includes nested field names" do
|
251
249
|
subject.field_names.should == ["a", "b", "c"]
|
252
250
|
end
|
253
251
|
|
254
|
-
it "
|
252
|
+
it "hides nested field names" do
|
255
253
|
subject.b.field_names.should == ["x"]
|
256
254
|
end
|
257
255
|
|
258
|
-
it "
|
256
|
+
it "accesses nested fields" do
|
259
257
|
subject.a.should == 6
|
260
258
|
subject.b.w.should == 3
|
261
259
|
subject.b.x.should == 6
|
262
260
|
subject.c.y.should == 3
|
263
261
|
end
|
264
262
|
|
265
|
-
it "
|
263
|
+
it "returns correct offset" do
|
266
264
|
subject.offset.should == 0
|
267
265
|
subject.b.offset.should == 1
|
268
266
|
subject.b.w.offset.should == 1
|
@@ -270,7 +268,7 @@ describe BinData::Record, "with nested structs" do
|
|
270
268
|
subject.c.z.offset.should == 4
|
271
269
|
end
|
272
270
|
|
273
|
-
it "
|
271
|
+
it "returns correct rel_offset" do
|
274
272
|
subject.rel_offset.should == 0
|
275
273
|
subject.b.rel_offset.should == 1
|
276
274
|
subject.b.w.rel_offset.should == 0
|
@@ -278,7 +276,7 @@ describe BinData::Record, "with nested structs" do
|
|
278
276
|
subject.c.z.rel_offset.should == 1
|
279
277
|
end
|
280
278
|
|
281
|
-
it "
|
279
|
+
it "assigns nested fields" do
|
282
280
|
subject.assign(:a => 2, :b => {:w => 4})
|
283
281
|
subject.a.should == 2
|
284
282
|
subject.b.w.should == 4
|
@@ -296,7 +294,7 @@ describe BinData::Record, "with nested array of primitives" do
|
|
296
294
|
|
297
295
|
subject { NestedPrimitiveArrayRecord.new }
|
298
296
|
|
299
|
-
it "
|
297
|
+
it "uses block as :type" do
|
300
298
|
subject.snapshot.should == {"a" => [0, 1, 2]}
|
301
299
|
end
|
302
300
|
end
|
@@ -311,7 +309,7 @@ describe BinData::Record, "with nested array of structs" do
|
|
311
309
|
|
312
310
|
subject { NestedStructArrayRecord.new }
|
313
311
|
|
314
|
-
it "
|
312
|
+
it "uses block as struct for :type" do
|
315
313
|
subject.a[0].b = 2
|
316
314
|
subject.snapshot.should == {"a" => [{"b" => 2, "c" => 0}]}
|
317
315
|
end
|
@@ -383,7 +381,7 @@ describe BinData::Record, "with an endian defined" do
|
|
383
381
|
|
384
382
|
subject { RecordWithEndian.new }
|
385
383
|
|
386
|
-
it "
|
384
|
+
it "uses correct endian" do
|
387
385
|
subject.a = 1
|
388
386
|
subject.b = 2.0
|
389
387
|
subject.c[0] = 3
|
@@ -407,11 +405,11 @@ describe BinData::Record, "defined recursively" do
|
|
407
405
|
recursive_record :nxt, :onlyif => lambda { has_nxt > 0 }
|
408
406
|
end
|
409
407
|
|
410
|
-
it "
|
408
|
+
it "can be created" do
|
411
409
|
subject = RecursiveRecord.new
|
412
410
|
end
|
413
411
|
|
414
|
-
it "
|
412
|
+
it "reads" do
|
415
413
|
str = "\x00\x01\x01\x00\x02\x01\x00\x03\x00"
|
416
414
|
subject = RecursiveRecord.read(str)
|
417
415
|
subject.val.should == 1
|
@@ -419,14 +417,14 @@ describe BinData::Record, "defined recursively" do
|
|
419
417
|
subject.nxt.nxt.val.should == 3
|
420
418
|
end
|
421
419
|
|
422
|
-
it "
|
420
|
+
it "is assignable on demand" do
|
423
421
|
subject = RecursiveRecord.new
|
424
422
|
subject.val = 13
|
425
423
|
subject.nxt.val = 14
|
426
424
|
subject.nxt.nxt.val = 15
|
427
425
|
end
|
428
426
|
|
429
|
-
it "
|
427
|
+
it "writes" do
|
430
428
|
subject = RecursiveRecord.new
|
431
429
|
subject.val = 5
|
432
430
|
subject.nxt.val = 6
|
@@ -442,11 +440,11 @@ describe BinData::Record, "with custom mandatory parameters" do
|
|
442
440
|
uint8 :a, :value => :arg1
|
443
441
|
end
|
444
442
|
|
445
|
-
it "
|
446
|
-
|
443
|
+
it "raises error if mandatory parameter is not supplied" do
|
444
|
+
expect { MandatoryRecord.new }.to raise_error(ArgumentError)
|
447
445
|
end
|
448
446
|
|
449
|
-
it "
|
447
|
+
it "uses mandatory parameter" do
|
450
448
|
subject = MandatoryRecord.new(:arg1 => 5)
|
451
449
|
subject.a.should == 5
|
452
450
|
end
|
@@ -460,26 +458,26 @@ describe BinData::Record, "with custom default parameters" do
|
|
460
458
|
uint8 :b
|
461
459
|
end
|
462
460
|
|
463
|
-
it "
|
464
|
-
|
461
|
+
it "does not raise error if default parameter is not supplied" do
|
462
|
+
expect { DefaultRecord.new }.not_to raise_error(ArgumentError)
|
465
463
|
end
|
466
464
|
|
467
|
-
it "
|
465
|
+
it "uses default parameter" do
|
468
466
|
subject = DefaultRecord.new
|
469
467
|
subject.a.should == 5
|
470
468
|
end
|
471
469
|
|
472
|
-
it "
|
470
|
+
it "overrides default parameter" do
|
473
471
|
subject = DefaultRecord.new(:arg1 => 7)
|
474
472
|
subject.a.should == 7
|
475
473
|
end
|
476
474
|
|
477
|
-
it "
|
475
|
+
it "accepts values" do
|
478
476
|
subject = DefaultRecord.new(:b => 2)
|
479
477
|
subject.b.should == 2
|
480
478
|
end
|
481
479
|
|
482
|
-
it "
|
480
|
+
it "accepts values and parameters" do
|
483
481
|
subject = DefaultRecord.new({:b => 2}, :arg1 => 3)
|
484
482
|
subject.a.should == 3
|
485
483
|
subject.b.should == 2
|
@@ -499,7 +497,7 @@ describe BinData::Record, "with :onlyif" do
|
|
499
497
|
its(:snapshot) { should == {"a" => 3, "b" => 5} }
|
500
498
|
its(:to_binary_s) { should == "\x03\x05" }
|
501
499
|
|
502
|
-
it "
|
500
|
+
it "reads as expected" do
|
503
501
|
subject.read("\x01\x02")
|
504
502
|
subject.snapshot.should == {"a" => 1, "c" => 2}
|
505
503
|
end
|
@@ -518,15 +516,15 @@ describe BinData::Record, "derived classes" do
|
|
518
516
|
uint8 :c
|
519
517
|
end
|
520
518
|
|
521
|
-
it "
|
519
|
+
it "does not affect parent" do
|
522
520
|
ParentRecord.new.field_names.should == ["a"]
|
523
521
|
end
|
524
522
|
|
525
|
-
it "
|
523
|
+
it "inherits fields for first child" do
|
526
524
|
Child1Record.new.field_names.should == ["a", "b"]
|
527
525
|
end
|
528
526
|
|
529
|
-
it "
|
527
|
+
it "inherits fields for second child" do
|
530
528
|
Child2Record.new.field_names.should == ["a", "b", "c"]
|
531
529
|
end
|
532
530
|
end
|