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.
- data/ChangeLog +7 -0
- data/README +4 -0
- data/TODO +0 -5
- data/lib/bindata.rb +1 -1
- data/lib/bindata/array.rb +96 -11
- data/lib/bindata/base.rb +16 -13
- data/lib/bindata/lazy.rb +32 -28
- data/lib/bindata/single.rb +9 -6
- data/lib/bindata/struct.rb +1 -2
- data/spec/array_spec.rb +124 -25
- data/spec/base_spec.rb +34 -34
- data/spec/choice_spec.rb +11 -11
- data/spec/float_spec.rb +16 -16
- data/spec/int_spec.rb +6 -6
- data/spec/lazy_spec.rb +58 -54
- data/spec/registry_spec.rb +9 -9
- data/spec/single_spec.rb +41 -41
- data/spec/spec_common.rb +1 -1
- data/spec/string_spec.rb +41 -41
- data/spec/stringz_spec.rb +32 -32
- data/spec/struct_spec.rb +33 -33
- metadata +2 -2
data/spec/base_spec.rb
CHANGED
@@ -3,25 +3,25 @@
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
|
4
4
|
require 'bindata/base'
|
5
5
|
|
6
|
-
|
7
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
24
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
51
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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).
|
84
|
+
obj.eval_param(:p2).should be_nil
|
85
85
|
obj.eval_param(:p3).should eql(3)
|
86
|
-
obj.eval_param(:p4).
|
87
|
-
obj.eval_param(:p5).
|
86
|
+
obj.eval_param(:p4).should be_nil
|
87
|
+
obj.eval_param(:p5).should be_nil
|
88
88
|
end
|
89
89
|
|
90
|
-
|
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).
|
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
|
-
|
99
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
144
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
182
|
-
|
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
|
-
|
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
|
data/spec/choice_spec.rb
CHANGED
@@ -6,8 +6,8 @@ require 'bindata/int'
|
|
6
6
|
require 'bindata/lazy'
|
7
7
|
require 'bindata/struct'
|
8
8
|
|
9
|
-
|
10
|
-
|
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
|
-
|
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
|
-
|
26
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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"])
|
data/spec/float_spec.rb
CHANGED
@@ -3,22 +3,22 @@
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
|
4
4
|
require 'bindata/float'
|
5
5
|
|
6
|
-
|
7
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
32
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
57
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
83
|
-
|
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
|
-
|
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
|
-
|
97
|
+
it "should read the same value as written" do
|
98
98
|
@obj.write(@io)
|
99
99
|
@io.rewind
|
100
100
|
|
data/spec/int_spec.rb
CHANGED
@@ -3,8 +3,8 @@
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
|
4
4
|
require 'bindata/int'
|
5
5
|
|
6
|
-
|
7
|
-
|
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
|
-
|
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
|
-
|
37
|
-
|
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
|
-
|
49
|
+
it "should pass these tests" do
|
50
50
|
[
|
51
51
|
[1, true, BinData::Uint8],
|
52
52
|
[2, false, BinData::Uint16le],
|
data/spec/lazy_spec.rb
CHANGED
@@ -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
|
6
|
+
# A mock data object with customizable fields.
|
7
7
|
class MockDataObject
|
8
|
-
def initialize(
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
26
|
-
|
27
|
-
@do1 = MockDataObject.new(
|
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.
|
20
|
+
@e1.add_variable(:v1, 'v1')
|
31
21
|
end
|
32
22
|
|
33
|
-
|
34
|
-
@e1.lazy_eval(lambda {
|
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
|
-
|
38
|
-
@e1.
|
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
|
-
|
43
|
-
@e1.
|
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
|
-
|
48
|
-
|
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
|
-
|
54
|
-
@e1.
|
55
|
-
@e1.
|
56
|
-
@e1.lazy_eval(:
|
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
|
-
|
63
|
-
|
64
|
-
@do2 = MockDataObject.new(
|
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
|
-
@
|
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
|
-
|
66
|
+
it "should evaluate parent parameter" do
|
77
67
|
@e1.lazy_eval(:p2).should eql('p2')
|
78
68
|
end
|
79
69
|
|
80
|
-
|
70
|
+
it "should evaluate parent field" do
|
81
71
|
@e1.lazy_eval(:f2).should eql('f2')
|
82
72
|
end
|
83
73
|
|
84
|
-
|
85
|
-
@e1.lazy_eval(:
|
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
|
-
|
90
|
-
|
91
|
-
@do4 = MockDataObject.new(
|
92
|
-
@do3 = MockDataObject.new(
|
93
|
-
@do2 = MockDataObject.new(
|
94
|
-
@do1 = MockDataObject.new(
|
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
|
-
|
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
|
-
|
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
|