bindata 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bindata might be problematic. Click here for more details.

data/spec/array_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
- require File.expand_path(File.dirname(__FILE__)) + '/example'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
+ require File.expand_path(File.join(File.dirname(__FILE__), "example"))
5
5
  require 'bindata/array'
6
6
  require 'bindata/int'
7
7
 
@@ -135,7 +135,7 @@ describe BinData::Array, "with several elements" do
135
135
  end
136
136
 
137
137
  it "should have correct num_bytes for individual elements" do
138
- @data.num_bytes(0).should == ExampleSingle.new.num_bytes
138
+ @data[0].num_bytes.should == ExampleSingle.new.num_bytes
139
139
  end
140
140
 
141
141
  it "should be able to directly access elements" do
@@ -163,7 +163,7 @@ describe BinData::Array, "with several elements" do
163
163
 
164
164
  it "should clear a single element" do
165
165
  @data[1] = 8
166
- @data.clear(1)
166
+ @data[1].clear
167
167
  @data[1].should == 2
168
168
  end
169
169
 
@@ -173,14 +173,14 @@ describe BinData::Array, "with several elements" do
173
173
 
174
174
  it "should be clear if all elements are clear" do
175
175
  @data[1] = 8
176
- @data.clear(1)
176
+ @data[1].clear
177
177
  @data.should be_clear
178
178
  end
179
179
 
180
180
  it "should test clear status of individual elements" do
181
181
  @data[1] = 8
182
- @data.clear?(0).should be_true
183
- @data.clear?(1).should be_false
182
+ @data[0].should be_clear
183
+ @data[1].should_not be_clear
184
184
  end
185
185
 
186
186
  it "should symmetrically read and write" do
@@ -269,21 +269,6 @@ describe BinData::Array, "when accessing elements" do
269
269
  @data.length.should == 5
270
270
  end
271
271
 
272
- it "should not extend on clear" do
273
- @data.clear(9)
274
- @data.length.should == 5
275
- end
276
-
277
- it "should not extend on clear?" do
278
- @data.clear?(9).should be_true
279
- @data.length.should == 5
280
- end
281
-
282
- it "should not extend on num_bytes" do
283
- @data.num_bytes(9).should == 0
284
- @data.length.should == 5
285
- end
286
-
287
272
  it "should raise error on bad input to []" do
288
273
  lambda { @data["a"] }.should raise_error(TypeError)
289
274
  lambda { @data[1, "a"] }.should raise_error(TypeError)
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
- require File.expand_path(File.dirname(__FILE__)) + '/example'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
+ require File.expand_path(File.join(File.dirname(__FILE__), "example"))
5
5
  require 'bindata/base_primitive'
6
6
  require 'bindata/io'
7
7
 
@@ -124,6 +124,15 @@ describe ExampleSingle do
124
124
  @data.should == 5
125
125
  5.should == @data
126
126
  end
127
+
128
+ it "should work as hash keys" do
129
+ hash = {5 => 17}
130
+
131
+ obj = ExampleSingle.new
132
+ obj.value = 5
133
+
134
+ hash[obj].should == 17
135
+ end
127
136
  end
128
137
 
129
138
  describe BinData::BasePrimitive, "after initialisation" do
data/spec/base_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
4
  require 'bindata/base'
5
5
 
6
6
  class BaseStub < BinData::Base
@@ -56,6 +56,28 @@ describe BinData::Base, "when subclassing" do
56
56
  end
57
57
  end
58
58
 
59
+ describe BinData::Base, "with parameters" do
60
+ it "should raise error if parameter has nil value" do
61
+ lambda { BaseStub.new(:a => nil) }.should raise_error(ArgumentError)
62
+ end
63
+
64
+ it "should raise error if parameter name is invalid" do
65
+ lambda {
66
+ eval <<-END
67
+ class InvalidParameterNameBase < BinData::Base
68
+ optional_parameter :eval # i.e. Kernel#eval
69
+ end
70
+ END
71
+ }.should raise_error(NameError)
72
+ end
73
+
74
+ it "should convert keys to symbols" do
75
+ obj = BaseStub.new('a' => 3)
76
+ obj.should have_parameter(:a)
77
+ obj.get_parameter(:a).should == 3
78
+ end
79
+ end
80
+
59
81
  describe BinData::Base, "with mandatory parameters" do
60
82
  before(:all) do
61
83
  eval <<-END
@@ -139,7 +161,7 @@ describe BinData::Base, "with multiple parameters" do
139
161
  end
140
162
 
141
163
  it "should identify internally accepted parameters" do
142
- accepted = WithParamBase.accepted_internal_parameters
164
+ accepted = WithParamBase.accepted_parameters.all
143
165
  accepted.should include(:p1)
144
166
  accepted.should include(:p2)
145
167
  accepted.should include(:p3)
data/spec/bits_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
4
  require 'bindata/bits'
5
5
 
6
6
  describe "Bits of size 1" do
data/spec/choice_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
- require File.expand_path(File.dirname(__FILE__)) + '/example'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
+ require File.expand_path(File.join(File.dirname(__FILE__), "example"))
5
5
  require 'bindata/choice'
6
6
 
7
7
  class Chooser
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
+ require File.expand_path(File.join(File.dirname(__FILE__), "example"))
5
+ require 'bindata'
6
+
7
+ describe BinData::SingleValue, "when defining" do
8
+ it "should allow inheriting from deprecated SingleValue" do
9
+ lambda {
10
+ eval <<-END
11
+ class SubclassSingleValue < BinData::SingleValue
12
+ end
13
+ END
14
+ }.should_not raise_error
15
+ end
16
+ end
17
+
18
+ describe BinData::MultiValue, "when defining" do
19
+ it "should allow inheriting from deprecated MultiValue" do
20
+ lambda {
21
+ eval <<-END
22
+ class SubclassMultiValue < BinData::MultiValue
23
+ end
24
+ END
25
+ }.should_not raise_error
26
+ end
27
+ end
28
+
29
+ describe BinData::Array, "with several elements" do
30
+ before(:each) do
31
+ type = [:example_single, {:initial_value => lambda { index + 1 }}]
32
+ @data = BinData::Array.new(:type => type, :initial_length => 5)
33
+ end
34
+
35
+ it "should clear a single element" do
36
+ @data[1] = 8
37
+ @data.clear(1)
38
+ @data[1].should == 2
39
+ end
40
+
41
+ it "should test clear status of individual elements" do
42
+ @data[1] = 8
43
+ @data.clear?(0).should be_true
44
+ @data.clear?(1).should be_false
45
+ end
46
+
47
+ it "should have correct num_bytes for individual elements" do
48
+ @data.num_bytes(0).should == ExampleSingle.new.num_bytes
49
+ end
50
+
51
+ it "should not extend on clear" do
52
+ @data.clear(9)
53
+ @data.length.should == 5
54
+ end
55
+
56
+ it "should not extend on clear?" do
57
+ @data.clear?(9).should be_true
58
+ @data.length.should == 5
59
+ end
60
+
61
+ it "should not extend on num_bytes" do
62
+ @data.num_bytes(9).should == 0
63
+ @data.length.should == 5
64
+ end
65
+ end
66
+
67
+ describe BinData::String, "with deprecated parameters" do
68
+ it "should substitude :trim_padding for :trim_value" do
69
+ obj = BinData::String.new(:trim_value => true)
70
+ obj.value = "abc\0"
71
+ obj.value.should == "abc"
72
+ end
73
+ end
74
+
75
+ describe BinData::Struct, "with multiple fields" do
76
+ before(:each) do
77
+ @params = { :fields => [ [:int8, :a], [:int8, :b] ] }
78
+ @obj = BinData::Struct.new(@params)
79
+ @obj.a = 1
80
+ @obj.b = 2
81
+ end
82
+
83
+ it "should return num_bytes" do
84
+ @obj.num_bytes(:a).should == 1
85
+ @obj.num_bytes(:b).should == 1
86
+ @obj.num_bytes.should == 2
87
+ end
88
+
89
+ it "should clear individual elements" do
90
+ @obj.a = 6
91
+ @obj.b = 7
92
+ @obj.clear(:a)
93
+ @obj.should be_clear(:a)
94
+ @obj.should_not be_clear(:b)
95
+ end
96
+ end
data/spec/float_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
4
  require 'bindata/float'
5
5
 
6
6
  describe "A FloatLe" do
data/spec/int_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
4
  require 'bindata'
5
5
 
6
6
  share_examples_for "All Integers" do
data/spec/io_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
4
  require 'bindata/io'
5
5
 
6
6
  describe BinData::IO do
data/spec/lazy_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
4
  require 'bindata/lazy'
5
5
 
6
6
  # A mock data object with customizable fields.
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
3
+ 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
@@ -23,15 +23,6 @@ describe BinData::Primitive, "when subclassing" do
23
23
  end
24
24
 
25
25
  describe BinData::Primitive, "when defining" do
26
- it "should allow inheriting from deprecated SingleValue" do
27
- lambda {
28
- eval <<-END
29
- class SubclassSingleValue < BinData::SingleValue
30
- end
31
- END
32
- }.should_not raise_error
33
- end
34
-
35
26
  it "should fail on non registered types" do
36
27
  lambda {
37
28
  eval <<-END
data/spec/record_spec.rb CHANGED
@@ -1,18 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
4
  require 'bindata'
5
5
 
6
6
  describe BinData::Record, "when defining" do
7
- it "should allow inheriting from deprecated MultiValue" do
8
- lambda {
9
- eval <<-END
10
- class SubclassMultiValue < BinData::MultiValue
11
- end
12
- END
13
- }.should_not raise_error
14
- end
15
-
16
7
  it "should fail on non registered types" do
17
8
  lambda {
18
9
  eval <<-END
@@ -119,14 +110,14 @@ describe BinData::Record, "with multiple fields" do
119
110
  end
120
111
 
121
112
  it "should return num_bytes" do
122
- @obj.num_bytes(:a).should == 1
123
- @obj.num_bytes(:b).should == 1
113
+ @obj.a.num_bytes.should == 1
114
+ @obj.b.num_bytes.should == 1
124
115
  @obj.num_bytes.should == 2
125
116
  end
126
117
 
127
118
  it "should identify accepted parameters" do
128
- BinData::Record.accepted_internal_parameters.should include(:hide)
129
- BinData::Record.accepted_internal_parameters.should include(:endian)
119
+ BinData::Record.accepted_parameters.all.should include(:hide)
120
+ BinData::Record.accepted_parameters.all.should include(:endian)
130
121
  end
131
122
 
132
123
  it "should clear" do
@@ -138,9 +129,9 @@ describe BinData::Record, "with multiple fields" do
138
129
  it "should clear individual elements" do
139
130
  @obj.a = 6
140
131
  @obj.b = 7
141
- @obj.clear(:a)
142
- @obj.should be_clear(:a)
143
- @obj.should_not be_clear(:b)
132
+ @obj.a.clear
133
+ @obj.a.should be_clear
134
+ @obj.b.should_not be_clear
144
135
  end
145
136
 
146
137
  it "should write ordered" do
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
4
+ require 'bindata/bits'
5
+ require 'bindata/int'
4
6
  require 'bindata/registry'
5
7
 
6
8
  describe BinData::Registry do
@@ -57,32 +59,50 @@ describe BinData::Registry do
57
59
  end
58
60
 
59
61
  it "should lookup integers with endian" do
60
- # @r.register("Int24be", A)
61
- # @r.register("Int24le", B)
62
- # @r.register("Uint24be", C)
63
- # @r.register("Uint24le", D)
64
- #
65
- # @r.lookup("int24", :big).should == A
66
- # @r.lookup("int24", :little).should == B
67
- # @r.lookup("uint24", :big).should == C
68
- # @r.lookup("uint24", :little).should == D
62
+ @r.register("Int24be", A)
63
+ @r.register("Int24le", B)
64
+ @r.register("Uint24be", C)
65
+ @r.register("Uint24le", D)
66
+
67
+ @r.lookup("int24", :big).should == A
68
+ @r.lookup("int24", :little).should == B
69
+ @r.lookup("uint24", :big).should == C
70
+ @r.lookup("uint24", :little).should == D
69
71
  end
70
72
 
71
73
  it "should not lookup integers without endian" do
72
- # @r.register("Int24be", A)
74
+ @r.register("Int24be", A)
73
75
 
74
- # @r.lookup("int24").should be_nil
76
+ @r.lookup("int24").should be_nil
75
77
  end
76
78
 
77
79
  it "should lookup floats with endian" do
78
- # @r.register("FloatBe", A)
79
- # @r.register("FloatLe", B)
80
- # @r.register("DoubleBe", C)
81
- # @r.register("DoubleLe", D)
82
- #
83
- # @r.lookup("float", :big).should == A
84
- # @r.lookup("float", :little).should == B
85
- # @r.lookup("double", :big).should == C
86
- # @r.lookup("double", :little).should == D
80
+ @r.register("FloatBe", A)
81
+ @r.register("FloatLe", B)
82
+ @r.register("DoubleBe", C)
83
+ @r.register("DoubleLe", D)
84
+
85
+ @r.lookup("float", :big).should == A
86
+ @r.lookup("float", :little).should == B
87
+ @r.lookup("double", :big).should == C
88
+ @r.lookup("double", :little).should == D
89
+ end
90
+
91
+ it "should automatically create classes for integers" do
92
+ BinData.const_defined?(:Uint40be).should be_false
93
+ @r.lookup("uint40be")
94
+ BinData.const_defined?(:Uint40be).should be_true
95
+ end
96
+
97
+ it "should automatically create classes for big endian bits" do
98
+ BinData.const_defined?(:Bit801).should be_false
99
+ @r.lookup("bit801")
100
+ BinData.const_defined?(:Bit801).should be_true
101
+ end
102
+
103
+ it "should automatically create classes for little endian bits" do
104
+ BinData.const_defined?(:Bit802le).should be_false
105
+ @r.lookup("bit802le")
106
+ BinData.const_defined?(:Bit802le).should be_true
87
107
  end
88
108
  end