bindata 0.6.0 → 0.7.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.

@@ -3,25 +3,25 @@
3
3
  require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
4
  require 'bindata/base'
5
5
 
6
- context "A data object with mandatory option" do
7
- context_setup do
6
+ describe "A data object with mandatory option" do
7
+ before(:all) do
8
8
  eval <<-END
9
9
  class Mandatory < BinData::Base
10
10
  mandatory_parameter :p1
11
11
  end
12
12
  END
13
13
  end
14
- specify "should ensure that those options are present" do
14
+ it "should ensure that those options are present" do
15
15
  lambda { Mandatory.new(:p1 => "a") }.should_not raise_error
16
16
  end
17
17
 
18
- specify "should fail when those options are not present" do
18
+ it "should fail when those options are not present" do
19
19
  lambda { Mandatory.new(:p2 => "a") }.should raise_error(ArgumentError)
20
20
  end
21
21
  end
22
22
 
23
- context "A data object with mutually exclusive options" do
24
- context_setup do
23
+ describe "A data object with mutually exclusive options" do
24
+ before(:all) do
25
25
  eval <<-END
26
26
  class MutexParam < BinData::Base
27
27
  optional_parameters :p1, :p2
@@ -33,22 +33,22 @@ context "A data object with mutually exclusive options" do
33
33
  END
34
34
  end
35
35
 
36
- specify "should not fail when neither of those options is present" do
36
+ it "should not fail when neither of those options is present" do
37
37
  lambda { MutexParam.new }.should_not raise_error
38
38
  end
39
39
 
40
- specify "should not fail when only one of those options is present" do
40
+ it "should not fail when only one of those options is present" do
41
41
  lambda { MutexParam.new(:p1 => "a") }.should_not raise_error
42
42
  lambda { MutexParam.new(:p2 => "a") }.should_not raise_error
43
43
  end
44
44
 
45
- specify "should fail when both those options are present" do
45
+ it "should fail when both those options are present" do
46
46
  lambda { MutexParam.new(:p1 => "a", :p2 => "b") }.should raise_error(ArgumentError)
47
47
  end
48
48
  end
49
49
 
50
- context "A data object with parameters" do
51
- context_setup do
50
+ describe "A data object with parameters" do
51
+ before(:all) do
52
52
  eval <<-END
53
53
  class WithParam < BinData::Base
54
54
  mandatory_parameter :p1
@@ -58,18 +58,18 @@ context "A data object with parameters" do
58
58
  END
59
59
  end
60
60
 
61
- specify "should not allow nil parameters" do
61
+ it "should not allow nil parameters" do
62
62
  lambda { WithParam.new(:p1 => 1, :p2 => nil) }.should raise_error(ArgumentError)
63
63
  end
64
64
 
65
- specify "should identify extra parameters" do
65
+ it "should identify extra parameters" do
66
66
  env = mock("env")
67
67
  env.should_receive(:params=).with(:p4 => 4, :p5 => 5)
68
68
  env.should_receive(:data_object=)
69
69
  obj = WithParam.new({:p1 => 1, :p3 => 3, :p4 => 4, :p5 => 5}, env)
70
70
  end
71
71
 
72
- specify "should only recall mandatory and optional parameters" do
72
+ it "should only recall mandatory and optional parameters" do
73
73
  obj = WithParam.new(:p1 => 1, :p3 => 3, :p4 => 4, :p5 => 5)
74
74
  obj.should have_param(:p1)
75
75
  obj.should_not have_param(:p2)
@@ -78,25 +78,25 @@ context "A data object with parameters" do
78
78
  obj.should_not have_param(:p5)
79
79
  end
80
80
 
81
- specify "should evaluate mandatory and optional parameters" do
81
+ it "should evaluate mandatory and optional parameters" do
82
82
  obj = WithParam.new(:p1 => 1, :p3 => lambda {1 + 2}, :p4 => 4, :p5 => 5)
83
83
  obj.eval_param(:p1).should eql(1)
84
- obj.eval_param(:p2).should_be_nil
84
+ obj.eval_param(:p2).should be_nil
85
85
  obj.eval_param(:p3).should eql(3)
86
- obj.eval_param(:p4).should_be_nil
87
- obj.eval_param(:p5).should_be_nil
86
+ obj.eval_param(:p4).should be_nil
87
+ obj.eval_param(:p5).should be_nil
88
88
  end
89
89
 
90
- specify "should be able to access without evaluating" do
90
+ it "should be able to access without evaluating" do
91
91
  obj = WithParam.new(:p1 => :asym, :p3 => lambda {1 + 2})
92
92
  obj.param(:p1).should eql(:asym)
93
- obj.param(:p2).should_be_nil
93
+ obj.param(:p2).should be_nil
94
94
  obj.param(:p3).should respond_to(:arity)
95
95
  end
96
96
  end
97
97
 
98
- context "A data object with :check_offset" do
99
- context_setup do
98
+ describe "A data object with :check_offset" do
99
+ before(:all) do
100
100
  eval <<-END
101
101
  class TenByteOffset < BinData::Base
102
102
  def do_read(io)
@@ -111,28 +111,28 @@ context "A data object with :check_offset" do
111
111
  END
112
112
  end
113
113
 
114
- specify "should fail if offset is incorrect" do
114
+ it "should fail if offset is incorrect" do
115
115
  io = StringIO.new("12345678901234567890")
116
116
  io.seek(2)
117
117
  obj = TenByteOffset.new(:check_offset => 8)
118
118
  lambda { obj.read(io) }.should raise_error(BinData::ValidityError)
119
119
  end
120
120
 
121
- specify "should succeed if offset is correct" do
121
+ it "should succeed if offset is correct" do
122
122
  io = StringIO.new("12345678901234567890")
123
123
  io.seek(3)
124
124
  obj = TenByteOffset.new(:check_offset => 10)
125
125
  lambda { obj.read(io) }.should_not raise_error
126
126
  end
127
127
 
128
- specify "should fail if :check_offset fails" do
128
+ it "should fail if :check_offset fails" do
129
129
  io = StringIO.new("12345678901234567890")
130
130
  io.seek(4)
131
131
  obj = TenByteOffset.new(:check_offset => lambda { offset == 11 } )
132
132
  lambda { obj.read(io) }.should raise_error(BinData::ValidityError)
133
133
  end
134
134
 
135
- specify "should succeed if :check_offset succeeds" do
135
+ it "should succeed if :check_offset succeeds" do
136
136
  io = StringIO.new("12345678901234567890")
137
137
  io.seek(5)
138
138
  obj = TenByteOffset.new(:check_offset => lambda { offset == 10 } )
@@ -140,8 +140,8 @@ context "A data object with :check_offset" do
140
140
  end
141
141
  end
142
142
 
143
- context "A data object with :readwrite => false" do
144
- context_setup do
143
+ describe "A data object with :readwrite => false" do
144
+ before(:all) do
145
145
  eval <<-END
146
146
  class NoIO < BinData::Base
147
147
  def _do_read(io)
@@ -161,25 +161,25 @@ context "A data object with :readwrite => false" do
161
161
  @obj = NoIO.new :readwrite => false
162
162
  end
163
163
 
164
- specify "should not read" do
164
+ it "should not read" do
165
165
  io = StringIO.new("12345678901234567890")
166
166
  @obj.read(io)
167
167
  @obj._do_read.should_not eql(true)
168
168
  end
169
169
 
170
- specify "should not write" do
170
+ it "should not write" do
171
171
  io = StringIO.new
172
172
  @obj.write(io)
173
173
  @obj._do_write.should_not eql(true)
174
174
  end
175
175
 
176
- specify "should have zero num_bytes" do
176
+ it "should have zero num_bytes" do
177
177
  @obj.num_bytes.should eql(0)
178
178
  end
179
179
  end
180
180
 
181
- context "A data object defining a value method" do
182
- context_setup do
181
+ describe "A data object defining a value method" do
182
+ before(:all) do
183
183
  eval <<-END
184
184
  class SingleValueObject < BinData::Base
185
185
  def value; end
@@ -187,7 +187,7 @@ context "A data object defining a value method" do
187
187
  END
188
188
  end
189
189
 
190
- specify "should be a single value object" do
190
+ it "should be a single value object" do
191
191
  obj = SingleValueObject.new
192
192
  obj.should be_a_single_value
193
193
  end
@@ -6,8 +6,8 @@ require 'bindata/int'
6
6
  require 'bindata/lazy'
7
7
  require 'bindata/struct'
8
8
 
9
- context "Instantiating a Choice" do
10
- specify "should ensure mandatory parameters are supplied" do
9
+ describe "Instantiating a Choice" do
10
+ it "should ensure mandatory parameters are supplied" do
11
11
  args = {}
12
12
  lambda { BinData::Choice.new(args) }.should raise_error(ArgumentError)
13
13
  args = {:selection => 1}
@@ -16,14 +16,14 @@ context "Instantiating a Choice" do
16
16
  lambda { BinData::Choice.new(args) }.should raise_error(ArgumentError)
17
17
  end
18
18
 
19
- specify "should fail if a given type is unknown" do
19
+ it "should fail if a given type is unknown" do
20
20
  args = {:choices => [:does_not_exist], :selection => 0}
21
21
  lambda { BinData::Choice.new(args) }.should raise_error(TypeError)
22
22
  end
23
23
  end
24
24
 
25
- context "A Choice with several choices" do
26
- setup do
25
+ describe "A Choice with several choices" do
26
+ before(:each) do
27
27
  # allow specifications to select the choice
28
28
  @env = BinData::LazyEvalEnv.new
29
29
  @env.class.class_eval { attr_accessor :choose }
@@ -38,7 +38,7 @@ context "A Choice with several choices" do
38
38
  @env)
39
39
  end
40
40
 
41
- specify "should be able to select the choice" do
41
+ it "should be able to select the choice" do
42
42
  @env.choose = 0
43
43
  @data.value.should eql(3)
44
44
  @env.choose = 1
@@ -51,20 +51,20 @@ context "A Choice with several choices" do
51
51
  @data.value.should eql(7)
52
52
  end
53
53
 
54
- specify "should not be able to select an invalid choice" do
54
+ it "should not be able to select an invalid choice" do
55
55
  @env.choose = -1
56
56
  lambda { @data.value }.should raise_error(IndexError)
57
57
  @env.choose = 5
58
58
  lambda { @data.value }.should raise_error(IndexError)
59
59
  end
60
60
 
61
- specify "should be able to interact directly with the choice" do
61
+ it "should be able to interact directly with the choice" do
62
62
  @env.choose = 0
63
63
  @data.value = 17
64
64
  @data.value.should eql(17)
65
65
  end
66
66
 
67
- specify "should handle missing methods correctly" do
67
+ it "should handle missing methods correctly" do
68
68
  @env.choose = 0
69
69
 
70
70
  @data.should respond_to(:value)
@@ -72,7 +72,7 @@ context "A Choice with several choices" do
72
72
  lambda { @data.does_not_exist }.should raise_error(NoMethodError)
73
73
  end
74
74
 
75
- specify "should delegate methods to the selected single choice" do
75
+ it "should delegate methods to the selected single choice" do
76
76
  @env.choose = 1
77
77
  @data.value = 17
78
78
 
@@ -95,7 +95,7 @@ context "A Choice with several choices" do
95
95
  @data.snapshot.should eql(17)
96
96
  end
97
97
 
98
- specify "should delegate methods to the selected complex choice" do
98
+ it "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
101
  @data.field_names.should eql(["a"])
@@ -3,22 +3,22 @@
3
3
  require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
4
  require 'bindata/float'
5
5
 
6
- context "A FloatLe" do
7
- setup do
6
+ describe "A FloatLe" do
7
+ before(:each) do
8
8
  @obj = BinData::FloatLe.new
9
9
  @obj.value = Math::PI
10
10
 
11
11
  @io = StringIO.new
12
12
  end
13
13
 
14
- specify "should write the expected value" do
14
+ it "should write the expected value" do
15
15
  @obj.write(@io)
16
16
  @io.rewind
17
17
 
18
18
  @io.read.should == [Math::PI].pack('e')
19
19
  end
20
20
 
21
- specify "should read the same value as written" do
21
+ it "should read the same value as written" do
22
22
  @obj.write(@io)
23
23
  @io.rewind
24
24
 
@@ -28,22 +28,22 @@ context "A FloatLe" do
28
28
  end
29
29
  end
30
30
 
31
- context "A FloatBe" do
32
- setup do
31
+ describe "A FloatBe" do
32
+ before(:each) do
33
33
  @obj = BinData::FloatBe.new
34
34
  @obj.value = Math::PI
35
35
 
36
36
  @io = StringIO.new
37
37
  end
38
38
 
39
- specify "should write the expected value" do
39
+ it "should write the expected value" do
40
40
  @obj.write(@io)
41
41
  @io.rewind
42
42
 
43
43
  @io.read.should == [Math::PI].pack('g')
44
44
  end
45
45
 
46
- specify "should read the same value as written" do
46
+ it "should read the same value as written" do
47
47
  @obj.write(@io)
48
48
  @io.rewind
49
49
 
@@ -53,22 +53,22 @@ context "A FloatBe" do
53
53
  end
54
54
  end
55
55
 
56
- context "A DoubleLe" do
57
- setup do
56
+ describe "A DoubleLe" do
57
+ before(:each) do
58
58
  @obj = BinData::DoubleLe.new
59
59
  @obj.value = Math::PI
60
60
 
61
61
  @io = StringIO.new
62
62
  end
63
63
 
64
- specify "should write the expected value" do
64
+ it "should write the expected value" do
65
65
  @obj.write(@io)
66
66
  @io.rewind
67
67
 
68
68
  @io.read.should == [Math::PI].pack('E')
69
69
  end
70
70
 
71
- specify "should read the same value as written" do
71
+ it "should read the same value as written" do
72
72
  @obj.write(@io)
73
73
  @io.rewind
74
74
 
@@ -79,22 +79,22 @@ context "A DoubleLe" do
79
79
  end
80
80
 
81
81
 
82
- context "A DoubleBe" do
83
- setup do
82
+ describe "A DoubleBe" do
83
+ before(:each) do
84
84
  @obj = BinData::DoubleBe.new
85
85
  @obj.value = Math::PI
86
86
 
87
87
  @io = StringIO.new
88
88
  end
89
89
 
90
- specify "should write the expected value" do
90
+ it "should write the expected value" do
91
91
  @obj.write(@io)
92
92
  @io.rewind
93
93
 
94
94
  @io.read.should == [Math::PI].pack('G')
95
95
  end
96
96
 
97
- specify "should read the same value as written" do
97
+ it "should read the same value as written" do
98
98
  @obj.write(@io)
99
99
  @io.rewind
100
100
 
@@ -3,8 +3,8 @@
3
3
  require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
4
  require 'bindata/int'
5
5
 
6
- context "All signed integers" do
7
- specify "should have a sensible value of zero" do
6
+ describe "All signed integers" do
7
+ it "should have a sensible value of zero" do
8
8
  [BinData::Int8,
9
9
  BinData::Int16le,
10
10
  BinData::Int16be,
@@ -16,7 +16,7 @@ context "All signed integers" do
16
16
  end
17
17
  end
18
18
 
19
- specify "should pass these tests" do
19
+ it "should pass these tests" do
20
20
  [
21
21
  [1, true, BinData::Int8],
22
22
  [2, false, BinData::Int16le],
@@ -33,8 +33,8 @@ context "All signed integers" do
33
33
  end
34
34
  end
35
35
 
36
- context "All unsigned integers" do
37
- specify "should have a sensible value of zero" do
36
+ describe "All unsigned integers" do
37
+ it "should have a sensible value of zero" do
38
38
  [BinData::Uint8,
39
39
  BinData::Uint16le,
40
40
  BinData::Uint16be,
@@ -46,7 +46,7 @@ context "All unsigned integers" do
46
46
  end
47
47
  end
48
48
 
49
- specify "should pass these tests" do
49
+ it "should pass these tests" do
50
50
  [
51
51
  [1, true, BinData::Uint8],
52
52
  [2, false, BinData::Uint16le],
@@ -3,65 +3,52 @@
3
3
  require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
4
  require 'bindata/lazy'
5
5
 
6
- # A mock data object that can substitute for BinData::Simple or BinData::Struct
6
+ # A mock data object with customizable fields.
7
7
  class MockDataObject
8
- def initialize(value = nil, fields = {})
9
- @value = value
10
- @fields = fields
11
- end
12
- attr_accessor :value
13
-
14
- def field_names
15
- @fields.keys.collect { |k| k.to_s }
16
- end
17
- def respond_to?(symbol, include_private = false)
18
- field_names.include?(symbol.id2name) ? true : super
19
- end
20
- def method_missing(symbol, *args)
21
- @fields[symbol] || super
8
+ def initialize(fields = {})
9
+ fields.each do |k,v|
10
+ self.class.send(:define_method, k.to_sym) { v }
11
+ end
22
12
  end
23
13
  end
24
14
 
25
- context "A single environment" do
26
- setup do
27
- @do1 = MockDataObject.new('v1', :f1 => 'f1')
15
+ describe "A single environment" do
16
+ before(:each) do
17
+ @do1 = MockDataObject.new(:f1 => 'f1')
28
18
  @e1 = BinData::LazyEvalEnv.new
29
19
  @e1.data_object = @do1
30
- @e1.params = {:p1 => 'p1'}
20
+ @e1.add_variable(:v1, 'v1')
31
21
  end
32
22
 
33
- specify "should evaluate value" do
34
- @e1.lazy_eval(lambda { value }).should eql('v1')
23
+ it "should accept symbols as a shortcut to lambda" do
24
+ @e1.lazy_eval(lambda { o1 }, :o1 => 'o1').should eql('o1')
25
+ @e1.lazy_eval(lambda { v1 }).should eql('v1')
26
+ @e1.lazy_eval(:o1, :o1 => 'o1').should eql('o1')
27
+ @e1.lazy_eval(:v1).should eql('v1')
35
28
  end
36
29
 
37
- specify "should evaluate index" do
38
- @e1.index = 7
39
- @e1.lazy_eval(lambda { index }).should eql(7)
30
+ it "should evaluate overrides" do
31
+ @e1.lazy_eval(:o1, :o1 => 'o1').should eql('o1')
40
32
  end
41
33
 
42
- specify "should evaluate offset" do
43
- @e1.offset = 9
44
- @e1.lazy_eval(lambda { offset }).should eql(9)
34
+ it "should evaluate variables" do
35
+ @e1.lazy_eval(:v1).should eql('v1')
45
36
  end
46
37
 
47
- specify "should not resolve any unknown fields" do
48
- lambda { @e1.lazy_eval(lambda { unknown }) }.should raise_error(NameError)
49
- lambda { @e1.lazy_eval(lambda { p1 }) }.should raise_error(NameError)
50
- lambda { @e1.lazy_eval(lambda { f1 }) }.should raise_error(NameError)
38
+ it "should prioritise overrides over variables" do
39
+ @e1.lazy_eval(:v1, :v1 => 'o1').should eql('o1')
51
40
  end
52
41
 
53
- specify "should accept symbols as a shortcut to lambda" do
54
- @e1.index = 7
55
- @e1.offset = 9
56
- @e1.lazy_eval(:value).should eql('v1')
57
- @e1.lazy_eval(:index).should eql(7)
58
- @e1.lazy_eval(:offset).should eql(9)
42
+ it "should not resolve any unknown fields" do
43
+ lambda { @e1.lazy_eval(:unknown) }.should raise_error(NameError)
44
+ lambda { @e1.lazy_eval(:p1) }.should raise_error(NameError)
45
+ lambda { @e1.lazy_eval(:f1) }.should raise_error(NameError)
59
46
  end
60
47
  end
61
48
 
62
- context "An environment with one parent" do
63
- setup do
64
- @do2 = MockDataObject.new(nil, :f2 => 'f2', :common => 'field2')
49
+ describe "An environment with one parent" do
50
+ before(:each) do
51
+ @do2 = MockDataObject.new(:f2 => 'f2', :common1 => 'f2', :common2 => 'f2')
65
52
  @do1 = MockDataObject.new
66
53
 
67
54
  @e2 = BinData::LazyEvalEnv.new
@@ -70,28 +57,35 @@ context "An environment with one parent" do
70
57
  @e2.data_object = @do2
71
58
  @e1.data_object = @do1
72
59
 
73
- @e2.params = {:p2 => 'p2', :l2 => lambda { 'l2' }, :common => 'param2'}
60
+ @e1.add_variable(:common2, 'v1')
61
+
62
+ @e2.params = {:p2 => 'p2', :common1 => 'p2', :common2 => 'p2'}
63
+ @e2.add_variable(:common2, 'v2')
74
64
  end
75
65
 
76
- specify "should evaluate parent parameter" do
66
+ it "should evaluate parent parameter" do
77
67
  @e1.lazy_eval(:p2).should eql('p2')
78
68
  end
79
69
 
80
- specify "should evaluate parent field" do
70
+ it "should evaluate parent field" do
81
71
  @e1.lazy_eval(:f2).should eql('f2')
82
72
  end
83
73
 
84
- specify "should prefer parent param over parent field" do
85
- @e1.lazy_eval(:common).should eql('param2')
74
+ it "should prefer parent param over parent field" do
75
+ @e1.lazy_eval(:common1).should eql('p2')
76
+ end
77
+
78
+ it "should prefer variable over parent param" do
79
+ @e1.lazy_eval(:common2).should eql('v1')
86
80
  end
87
81
  end
88
82
 
89
- context "A nested environment" do
90
- setup do
91
- @do4 = MockDataObject.new(nil, :f4 => 'f4')
92
- @do3 = MockDataObject.new(nil, :f3 => 'f3')
93
- @do2 = MockDataObject.new(nil, :f2 => 'f2')
94
- @do1 = MockDataObject.new(nil, :f1 => 'f1')
83
+ describe "A nested environment" do
84
+ before(:each) do
85
+ @do4 = MockDataObject.new(:f4 => 'f4')
86
+ @do3 = MockDataObject.new(:f3 => 'f3')
87
+ @do2 = MockDataObject.new(:f2 => 'f2', :fs2 => :s3)
88
+ @do1 = MockDataObject.new(:f1 => 'f1')
95
89
 
96
90
  @e4 = BinData::LazyEvalEnv.new
97
91
  @e3 = BinData::LazyEvalEnv.new(@e4)
@@ -104,17 +98,27 @@ context "A nested environment" do
104
98
  @e1.data_object = @do1
105
99
 
106
100
  @e4.params = {:p4 => 'p4', :s4 => 's4', :l4 => 'l4'}
107
- @e3.params = {:p3 => 'p3', :s3 => :s4, :l3 => lambda { l4 }}
101
+ @e3.params = {:p3 => 'p3', :s3 => :s4, :s4 => 'xx', :l3 => lambda { l4 }}
108
102
  @e2.params = {:p2 => 'p2', :s2 => :s3, :l2 => lambda { l3 }}
103
+
104
+ @e2.add_variable(:v2, 'v2')
109
105
  end
110
106
 
111
- specify "should access parent environments" do
107
+ it "should access parent environments" do
112
108
  @e1.lazy_eval(lambda { parent.p3 }).should eql('p3')
113
109
  @e1.lazy_eval(lambda { parent.parent.p4 }).should eql('p4')
114
110
  end
115
111
 
116
- specify "should cascade lambdas" do
112
+ it "should cascade lambdas" do
117
113
  @e1.lazy_eval(lambda { l2 }).should eql('l4')
118
114
  @e1.lazy_eval(lambda { s2 }).should eql('s4')
119
115
  end
116
+
117
+ it "should access parent environments by cascading" do
118
+ @e1.lazy_eval(lambda { p3 }).should eql('p3')
119
+ @e1.lazy_eval(lambda { p4 }).should eql('p4')
120
+ @e1.lazy_eval(lambda { v2 }).should eql('v2')
121
+ @e1.lazy_eval(lambda { s3 }).should eql('s4')
122
+ @e1.lazy_eval(lambda { fs2 }).should eql('s4')
123
+ end
120
124
  end