bindata 0.11.0 → 0.11.1
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 +6 -0
- data/TODO +17 -0
- data/TUTORIAL +949 -0
- data/lib/bindata.rb +1 -1
- data/lib/bindata/array.rb +4 -4
- data/lib/bindata/choice.rb +1 -1
- data/lib/bindata/deprecated.rb +2 -2
- data/lib/bindata/float.rb +5 -5
- data/lib/bindata/int.rb +33 -28
- data/lib/bindata/registry.rb +2 -2
- data/lib/bindata/sanitize.rb +1 -0
- data/lib/bindata/struct.rb +4 -4
- data/spec/array_spec.rb +1 -1
- data/spec/base_primitive_spec.rb +3 -7
- data/spec/base_spec.rb +29 -59
- data/spec/deprecated_spec.rb +4 -8
- data/spec/int_spec.rb +25 -60
- data/spec/primitive_spec.rb +37 -65
- data/spec/record_spec.rb +63 -105
- data/spec/registry_spec.rb +6 -6
- data/spec/system_spec.rb +32 -56
- data/spec/wrapper_spec.rb +28 -29
- metadata +3 -2
data/spec/deprecated_spec.rb
CHANGED
@@ -7,10 +7,8 @@ require 'bindata'
|
|
7
7
|
describe BinData::SingleValue, "when defining" do
|
8
8
|
it "should allow inheriting from deprecated SingleValue" do
|
9
9
|
lambda {
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
END
|
10
|
+
class SubclassSingleValue < BinData::SingleValue
|
11
|
+
end
|
14
12
|
}.should_not raise_error
|
15
13
|
end
|
16
14
|
end
|
@@ -18,10 +16,8 @@ end
|
|
18
16
|
describe BinData::MultiValue, "when defining" do
|
19
17
|
it "should allow inheriting from deprecated MultiValue" do
|
20
18
|
lambda {
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
END
|
19
|
+
class SubclassMultiValue < BinData::MultiValue
|
20
|
+
end
|
25
21
|
}.should_not raise_error
|
26
22
|
end
|
27
23
|
end
|
data/spec/int_spec.rb
CHANGED
@@ -138,28 +138,32 @@ share_examples_for "All Integers" do
|
|
138
138
|
end
|
139
139
|
(@endian == :little) ? str : str.reverse
|
140
140
|
end
|
141
|
+
|
142
|
+
def create_mapping_of_class_to_nbits(endian, signed)
|
143
|
+
base = signed ? "Int" : "Uint"
|
144
|
+
signed_sym = signed ? :signed : :unsigned
|
145
|
+
endian_str = (endian == :little) ? "le" : "be"
|
146
|
+
|
147
|
+
result = {}
|
148
|
+
result[BinData.const_get("#{base}8")] = 1
|
149
|
+
(1 .. 20).each do |nbytes|
|
150
|
+
nbits = nbytes * 8
|
151
|
+
class_name = "#{base}#{nbits}#{endian_str}"
|
152
|
+
BinData::Int.define_class(nbits, endian, signed_sym)
|
153
|
+
result[BinData.const_get(class_name)] = nbytes
|
154
|
+
end
|
155
|
+
|
156
|
+
result
|
157
|
+
end
|
141
158
|
end
|
142
159
|
|
143
160
|
describe "All signed big endian integers" do
|
144
161
|
it_should_behave_like "All Integers"
|
145
162
|
|
146
163
|
before(:all) do
|
147
|
-
BinData::Integer.define_class(24, :big, :signed)
|
148
|
-
BinData::Integer.define_class(48, :big, :signed)
|
149
|
-
BinData::Integer.define_class(96, :big, :signed)
|
150
164
|
@endian = :big
|
151
165
|
@signed = true
|
152
|
-
@ints =
|
153
|
-
BinData::Int8 => 1,
|
154
|
-
BinData::Int8be => 1,
|
155
|
-
BinData::Int16be => 2,
|
156
|
-
BinData::Int24be => 3,
|
157
|
-
BinData::Int32be => 4,
|
158
|
-
BinData::Int48be => 6,
|
159
|
-
BinData::Int64be => 8,
|
160
|
-
BinData::Int96be => 12,
|
161
|
-
BinData::Int128be => 16,
|
162
|
-
}
|
166
|
+
@ints = create_mapping_of_class_to_nbits(@endian, @signed)
|
163
167
|
end
|
164
168
|
end
|
165
169
|
|
@@ -167,22 +171,9 @@ describe "All unsigned big endian integers" do
|
|
167
171
|
it_should_behave_like "All Integers"
|
168
172
|
|
169
173
|
before(:all) do
|
170
|
-
BinData::Integer.define_class(24, :big, :unsigned)
|
171
|
-
BinData::Integer.define_class(48, :big, :unsigned)
|
172
|
-
BinData::Integer.define_class(96, :big, :unsigned)
|
173
174
|
@endian = :big
|
174
175
|
@signed = false
|
175
|
-
@ints =
|
176
|
-
BinData::Uint8 => 1,
|
177
|
-
BinData::Uint8be => 1,
|
178
|
-
BinData::Uint16be => 2,
|
179
|
-
BinData::Uint24be => 3,
|
180
|
-
BinData::Uint32be => 4,
|
181
|
-
BinData::Uint48be => 6,
|
182
|
-
BinData::Uint64be => 8,
|
183
|
-
BinData::Uint96be => 12,
|
184
|
-
BinData::Uint128be => 16,
|
185
|
-
}
|
176
|
+
@ints = create_mapping_of_class_to_nbits(@endian, @signed)
|
186
177
|
end
|
187
178
|
end
|
188
179
|
|
@@ -190,22 +181,9 @@ describe "All signed little endian integers" do
|
|
190
181
|
it_should_behave_like "All Integers"
|
191
182
|
|
192
183
|
before(:all) do
|
193
|
-
BinData::Integer.define_class(24, :little, :signed)
|
194
|
-
BinData::Integer.define_class(48, :little, :signed)
|
195
|
-
BinData::Integer.define_class(96, :little, :signed)
|
196
184
|
@endian = :little
|
197
185
|
@signed = true
|
198
|
-
@ints =
|
199
|
-
BinData::Int8 => 1,
|
200
|
-
BinData::Int8le => 1,
|
201
|
-
BinData::Int16le => 2,
|
202
|
-
BinData::Int24le => 3,
|
203
|
-
BinData::Int32le => 4,
|
204
|
-
BinData::Int48le => 6,
|
205
|
-
BinData::Int64le => 8,
|
206
|
-
BinData::Int96le => 12,
|
207
|
-
BinData::Int128le => 16,
|
208
|
-
}
|
186
|
+
@ints = create_mapping_of_class_to_nbits(@endian, @signed)
|
209
187
|
end
|
210
188
|
end
|
211
189
|
|
@@ -213,41 +191,28 @@ describe "All unsigned little endian integers" do
|
|
213
191
|
it_should_behave_like "All Integers"
|
214
192
|
|
215
193
|
before(:all) do
|
216
|
-
BinData::Integer.define_class(24, :little, :unsigned)
|
217
|
-
BinData::Integer.define_class(48, :little, :unsigned)
|
218
|
-
BinData::Integer.define_class(96, :little, :unsigned)
|
219
194
|
@endian = :little
|
220
195
|
@signed = false
|
221
|
-
@ints =
|
222
|
-
BinData::Uint8 => 1,
|
223
|
-
BinData::Uint8le => 1,
|
224
|
-
BinData::Uint16le => 2,
|
225
|
-
BinData::Uint24le => 3,
|
226
|
-
BinData::Uint32le => 4,
|
227
|
-
BinData::Uint48le => 6,
|
228
|
-
BinData::Uint64le => 8,
|
229
|
-
BinData::Uint96le => 12,
|
230
|
-
BinData::Uint128le => 16,
|
231
|
-
}
|
196
|
+
@ints = create_mapping_of_class_to_nbits(@endian, @signed)
|
232
197
|
end
|
233
198
|
end
|
234
199
|
|
235
200
|
describe "Custom defined integers" do
|
236
201
|
it "should fail unless bits are a multiple of 8" do
|
237
202
|
lambda {
|
238
|
-
BinData::
|
203
|
+
BinData::Int.define_class(7, :little, :unsigned)
|
239
204
|
}.should raise_error
|
240
205
|
|
241
206
|
lambda {
|
242
|
-
BinData::
|
207
|
+
BinData::Int.define_class(7, :big, :unsigned)
|
243
208
|
}.should raise_error
|
244
209
|
|
245
210
|
lambda {
|
246
|
-
BinData::
|
211
|
+
BinData::Int.define_class(7, :little, :signed)
|
247
212
|
}.should raise_error
|
248
213
|
|
249
214
|
lambda {
|
250
|
-
BinData::
|
215
|
+
BinData::Int.define_class(7, :big, :signed)
|
251
216
|
}.should raise_error
|
252
217
|
end
|
253
218
|
end
|
data/spec/primitive_spec.rb
CHANGED
@@ -4,12 +4,8 @@ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
|
4
4
|
require 'bindata'
|
5
5
|
|
6
6
|
describe BinData::Primitive, "when subclassing" do
|
7
|
-
|
8
|
-
|
9
|
-
class SubClassOfPrimitive < BinData::Primitive
|
10
|
-
expose_methods_for_testing
|
11
|
-
end
|
12
|
-
END
|
7
|
+
class SubClassOfPrimitive < BinData::Primitive
|
8
|
+
expose_methods_for_testing
|
13
9
|
end
|
14
10
|
|
15
11
|
before(:each) do
|
@@ -25,57 +21,45 @@ end
|
|
25
21
|
describe BinData::Primitive, "when defining" do
|
26
22
|
it "should fail on non registered types" do
|
27
23
|
lambda {
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
END
|
24
|
+
class BadTypePrimitive < BinData::Primitive
|
25
|
+
non_registered_type :a
|
26
|
+
end
|
33
27
|
}.should raise_error(TypeError)
|
34
28
|
end
|
35
29
|
|
36
30
|
it "should fail on duplicate names" do
|
37
31
|
lambda {
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
END
|
32
|
+
class DuplicateNamePrimitive < BinData::Primitive
|
33
|
+
int8 :a
|
34
|
+
int8 :b
|
35
|
+
int8 :a
|
36
|
+
end
|
45
37
|
}.should raise_error(SyntaxError)
|
46
38
|
end
|
47
39
|
|
48
40
|
it "should fail when field name shadows an existing method" do
|
49
41
|
lambda {
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
54
|
-
END
|
42
|
+
class ExistingNamePrimitive < BinData::Primitive
|
43
|
+
int8 :object_id
|
44
|
+
end
|
55
45
|
}.should raise_error(NameError)
|
56
46
|
end
|
57
47
|
|
58
48
|
it "should fail on unknown endian" do
|
59
49
|
lambda {
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
64
|
-
END
|
50
|
+
class BadEndianPrimitive < BinData::Primitive
|
51
|
+
endian 'a bad value'
|
52
|
+
end
|
65
53
|
}.should raise_error(ArgumentError)
|
66
54
|
end
|
67
55
|
end
|
68
56
|
|
69
57
|
describe BinData::Primitive do
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
def get; self.a; end
|
76
|
-
def set(v); self.a = v; end
|
77
|
-
end
|
78
|
-
END
|
58
|
+
class PrimitiveWithEndian < BinData::Primitive
|
59
|
+
endian :little
|
60
|
+
int16 :a
|
61
|
+
def get; self.a; end
|
62
|
+
def set(v); self.a = v; end
|
79
63
|
end
|
80
64
|
|
81
65
|
before(:each) do
|
@@ -114,14 +98,10 @@ describe BinData::Primitive do
|
|
114
98
|
end
|
115
99
|
|
116
100
|
describe BinData::Primitive, "requiring custom parameters" do
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
def get; self.a; end
|
122
|
-
def set(v); self.a = v; end
|
123
|
-
end
|
124
|
-
END
|
101
|
+
class PrimitiveWithCustom < BinData::Primitive
|
102
|
+
int8 :a, :initial_value => :iv
|
103
|
+
def get; self.a; end
|
104
|
+
def set(v); self.a = v; end
|
125
105
|
end
|
126
106
|
|
127
107
|
it "should pass parameters correctly" do
|
@@ -131,16 +111,12 @@ describe BinData::Primitive, "requiring custom parameters" do
|
|
131
111
|
end
|
132
112
|
|
133
113
|
describe BinData::Primitive, "with custom mandatory parameters" do
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
def get; self.a; end
|
141
|
-
def set(v); self.a = v; end
|
142
|
-
end
|
143
|
-
END
|
114
|
+
class MandatoryPrimitive < BinData::Primitive
|
115
|
+
mandatory_parameter :arg1
|
116
|
+
|
117
|
+
uint8 :a, :value => :arg1
|
118
|
+
def get; self.a; end
|
119
|
+
def set(v); self.a = v; end
|
144
120
|
end
|
145
121
|
|
146
122
|
it "should raise error if mandatory parameter is not supplied" do
|
@@ -154,16 +130,12 @@ describe BinData::Primitive, "with custom mandatory parameters" do
|
|
154
130
|
end
|
155
131
|
|
156
132
|
describe BinData::Primitive, "with custom default parameters" do
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
def get; self.a; end
|
164
|
-
def set(v); self.a = v; end
|
165
|
-
end
|
166
|
-
END
|
133
|
+
class DefaultPrimitive < BinData::Primitive
|
134
|
+
default_parameter :arg1 => 5
|
135
|
+
|
136
|
+
uint8 :a, :value => :arg1
|
137
|
+
def get; self.a; end
|
138
|
+
def set(v); self.a = v; end
|
167
139
|
end
|
168
140
|
|
169
141
|
it "should not raise error if default parameter is not supplied" do
|
data/spec/record_spec.rb
CHANGED
@@ -6,69 +6,55 @@ require 'bindata'
|
|
6
6
|
describe BinData::Record, "when defining" do
|
7
7
|
it "should fail on non registered types" do
|
8
8
|
lambda {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
END
|
9
|
+
class BadTypeRecord < BinData::Record
|
10
|
+
non_registerd_type :a
|
11
|
+
end
|
14
12
|
}.should raise_error(TypeError)
|
15
13
|
end
|
16
14
|
|
17
15
|
it "should fail on duplicate names" do
|
18
16
|
lambda {
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
END
|
17
|
+
class DuplicateNameRecord < BinData::Record
|
18
|
+
int8 :a
|
19
|
+
int8 :b
|
20
|
+
int8 :a
|
21
|
+
end
|
26
22
|
}.should raise_error(SyntaxError)
|
27
23
|
end
|
28
24
|
|
29
25
|
it "should fail on reserved names" do
|
30
26
|
lambda {
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
END
|
27
|
+
class ReservedNameRecord < BinData::Record
|
28
|
+
int8 :a
|
29
|
+
int8 :invert # from Hash.instance_methods
|
30
|
+
end
|
37
31
|
}.should raise_error(NameError)
|
38
32
|
end
|
39
33
|
|
40
34
|
it "should fail when field name shadows an existing method" do
|
41
35
|
lambda {
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
46
|
-
END
|
36
|
+
class ExistingNameRecord < BinData::Record
|
37
|
+
int8 :object_id
|
38
|
+
end
|
47
39
|
}.should raise_error(NameError)
|
48
40
|
end
|
49
41
|
|
50
42
|
it "should fail on unknown endian" do
|
51
43
|
lambda {
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
END
|
44
|
+
class BadEndianRecord < BinData::Record
|
45
|
+
endian 'a bad value'
|
46
|
+
end
|
57
47
|
}.should raise_error(ArgumentError)
|
58
48
|
end
|
59
49
|
end
|
60
50
|
|
61
51
|
describe BinData::Record, "with hidden fields" do
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
int8 :c
|
69
|
-
int8 :d, :value => :b
|
70
|
-
end
|
71
|
-
END
|
52
|
+
class HiddenRecord < BinData::Record
|
53
|
+
hide :b, 'c'
|
54
|
+
int8 :a
|
55
|
+
int8 'b', :initial_value => 10
|
56
|
+
int8 :c
|
57
|
+
int8 :d, :value => :b
|
72
58
|
end
|
73
59
|
|
74
60
|
before(:each) do
|
@@ -94,13 +80,9 @@ describe BinData::Record, "with hidden fields" do
|
|
94
80
|
end
|
95
81
|
|
96
82
|
describe BinData::Record, "with multiple fields" do
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
int8 :a
|
101
|
-
int8 :b
|
102
|
-
end
|
103
|
-
END
|
83
|
+
class MultiFieldRecord < BinData::Record
|
84
|
+
int8 :a
|
85
|
+
int8 :b
|
104
86
|
end
|
105
87
|
|
106
88
|
before(:each) do
|
@@ -162,24 +144,20 @@ describe BinData::Record, "with multiple fields" do
|
|
162
144
|
end
|
163
145
|
|
164
146
|
describe BinData::Record, "with nested structs" do
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
int8 :x, :value => :the_val
|
170
|
-
end
|
147
|
+
class Inner1Record < BinData::Record
|
148
|
+
int8 :w, :initial_value => 3
|
149
|
+
int8 :x, :value => :the_val
|
150
|
+
end
|
171
151
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
152
|
+
class Inner2Record < BinData::Record
|
153
|
+
int8 :y, :value => lambda { parent.b.w }
|
154
|
+
int8 :z
|
155
|
+
end
|
176
156
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
end
|
182
|
-
END
|
157
|
+
class RecordOuter < BinData::Record
|
158
|
+
int8 :a, :initial_value => 6
|
159
|
+
inner1_record :b, :the_val => :a
|
160
|
+
inner2_record :c
|
183
161
|
end
|
184
162
|
|
185
163
|
before(:each) do
|
@@ -206,19 +184,15 @@ describe BinData::Record, "with nested structs" do
|
|
206
184
|
end
|
207
185
|
|
208
186
|
describe BinData::Record, "with an endian defined" do
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
struct :e, :fields => [ [:uint16, :f], [:uint32be, :g] ]
|
219
|
-
struct :h, :fields => [ [:struct, :i, {:fields => [[:uint16, :j]]}] ]
|
220
|
-
end
|
221
|
-
END
|
187
|
+
class RecordWithEndian < BinData::Record
|
188
|
+
endian :little
|
189
|
+
|
190
|
+
uint16 :a
|
191
|
+
float :b
|
192
|
+
array :c, :type => :int8, :initial_length => 2
|
193
|
+
choice :d, :choices => [ [:uint16], [:uint32] ], :selection => 1
|
194
|
+
struct :e, :fields => [ [:uint16, :f], [:uint32be, :g] ]
|
195
|
+
struct :h, :fields => [ [:struct, :i, {:fields => [[:uint16, :j]]}] ]
|
222
196
|
end
|
223
197
|
|
224
198
|
before(:each) do
|
@@ -242,15 +216,11 @@ describe BinData::Record, "with an endian defined" do
|
|
242
216
|
end
|
243
217
|
|
244
218
|
describe BinData::Record, "defined recursively" do
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
uint8 :has_nxt, :value => lambda { nxt.clear? ? 0 : 1 }
|
251
|
-
recursive_record :nxt, :onlyif => lambda { has_nxt > 0 }
|
252
|
-
end
|
253
|
-
END
|
219
|
+
class RecursiveRecord < BinData::Record
|
220
|
+
endian :big
|
221
|
+
uint16 :val
|
222
|
+
uint8 :has_nxt, :value => lambda { nxt.clear? ? 0 : 1 }
|
223
|
+
recursive_record :nxt, :onlyif => lambda { has_nxt > 0 }
|
254
224
|
end
|
255
225
|
|
256
226
|
it "should be able to be created" do
|
@@ -282,14 +252,10 @@ describe BinData::Record, "defined recursively" do
|
|
282
252
|
end
|
283
253
|
|
284
254
|
describe BinData::Record, "with custom mandatory parameters" do
|
285
|
-
|
286
|
-
|
287
|
-
class MandatoryRecord < BinData::Record
|
288
|
-
mandatory_parameter :arg1
|
255
|
+
class MandatoryRecord < BinData::Record
|
256
|
+
mandatory_parameter :arg1
|
289
257
|
|
290
|
-
|
291
|
-
end
|
292
|
-
END
|
258
|
+
uint8 :a, :value => :arg1
|
293
259
|
end
|
294
260
|
|
295
261
|
it "should raise error if mandatory parameter is not supplied" do
|
@@ -303,14 +269,10 @@ describe BinData::Record, "with custom mandatory parameters" do
|
|
303
269
|
end
|
304
270
|
|
305
271
|
describe BinData::Record, "with custom default parameters" do
|
306
|
-
|
307
|
-
|
308
|
-
class DefaultRecord < BinData::Record
|
309
|
-
default_parameter :arg1 => 5
|
272
|
+
class DefaultRecord < BinData::Record
|
273
|
+
default_parameter :arg1 => 5
|
310
274
|
|
311
|
-
|
312
|
-
end
|
313
|
-
END
|
275
|
+
uint8 :a, :value => :arg1
|
314
276
|
end
|
315
277
|
|
316
278
|
it "should not raise error if default parameter is not supplied" do
|
@@ -329,14 +291,10 @@ describe BinData::Record, "with custom default parameters" do
|
|
329
291
|
end
|
330
292
|
|
331
293
|
describe BinData::Record, "with :onlyif" do
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
uint8 :b, :initial_value => 5, :onlyif => lambda { a == 3 }
|
337
|
-
uint8 :c, :initial_value => 7, :onlyif => lambda { a != 3 }
|
338
|
-
end
|
339
|
-
END
|
294
|
+
class OnlyIfRecord < BinData::Record
|
295
|
+
uint8 :a, :initial_value => 3
|
296
|
+
uint8 :b, :initial_value => 5, :onlyif => lambda { a == 3 }
|
297
|
+
uint8 :c, :initial_value => 7, :onlyif => lambda { a != 3 }
|
340
298
|
end
|
341
299
|
|
342
300
|
before(:each) do
|