1 require File.dirname(__FILE__) + '/../spec_helper'
4 def self.yielding_method(expected)
5 yield.should == expected
10 describe "The next statement" do
11 it "raises a LocalJumpError if used not within block or while/for loop" do
12 def bad_meth; next; end
13 lambda { bad_meth }.should raise_error(LocalJumpError)
17 describe "The next statement from within the block" do
18 it "ends block execution" do
28 it "causes block to return nil if invoked without arguments" do
29 lambda { 123; next; 456 }.call.should == nil
32 it "returns the argument passed" do
33 lambda { 123; next 234; 345 }.call.should == 234
36 it "returns to the invoking method" do
37 NextSpecs.yielding_method(nil) { next }.should == :method_return_value
40 it "returns to the invoking method, with the specified value" do
41 NextSpecs.yielding_method(nil) {
43 fail("next didn't end the block execution")
44 }.should == :method_return_value
46 NextSpecs.yielding_method(1) {
48 fail("next didn't end the block execution")
49 }.should == :method_return_value
51 NextSpecs.yielding_method([1, 2, 3]) {
53 fail("next didn't end the block execution")
54 }.should == :method_return_value
57 it "returns to the currently yielding method in case of chained calls" do
59 def self.meth_with_yield(&b)
60 yield.should == :next_return_value
63 def self.invoking_method(&b)
66 def self.enclosing_method
68 next :next_return_value
74 ChainedNextTest.enclosing_method.should == :method_return_value
79 describe "Assignment via next" do
80 it "assigns objects" do
81 def r(val); a = yield(); val.should == a; end
91 r([1,2]){next [*[1,2]]}
94 it "assigns splatted objects" do
95 def r(val); a = yield(); val.should == a; end
104 r([1,2]){next *[*[1,2]]}
107 it "assigns objects to a splatted reference" do
108 def r(val); *a = yield(); val.should == a; end
114 r([[nil]]){next [nil]}
116 r([[1,2]]){next [1,2]}
118 r([[1]]){next [*[1]]}
119 r([[1,2]]){next [*[1,2]]}
122 it "assigns splatted objects to a splatted reference via a splatted yield" do
123 def r(val); *a = *yield(); val.should == a; end
128 r([nil]){next *[nil]}
130 r([1,2]){next *[1,2]}
131 r([nil]){next *[*[]]}
133 r([1,2]){next *[*[1,2]]}
136 it "assigns objects to multiple variables" do
137 def r(val); a,b,*c = yield(); val.should == [a,b,c]; end
138 r([nil,nil,[]]){next}
139 r([nil,nil,[]]){next nil}
140 r([1,nil,[]]){next 1}
141 r([nil,nil,[]]){next []}
142 r([1,nil,[]]){next [1]}
143 r([nil,nil,[]]){next [nil]}
144 r([[],nil,[]]){next [[]]}
145 r([1,2,[]]){next [1,2]}
146 r([nil,nil,[]]){next [*[]]}
147 r([1,nil,[]]){next [*[1]]}
148 r([1,2,[]]){next [*[1,2]]}
151 it "assigns splatted objects to multiple variables" do
152 def r(val); a,b,*c = *yield(); val.should == [a,b,c]; end
153 r([nil,nil,[]]){next *nil}
154 r([1,nil,[]]){next *1}
155 r([nil,nil,[]]){next *[]}
156 r([1,nil,[]]){next *[1]}
157 r([nil,nil,[]]){next *[nil]}
158 r([nil,nil,[]]){next *[[]]}
159 r([1,2,[]]){next *[1,2]}
160 r([nil,nil,[]]){next *[*[]]}
161 r([1,nil,[]]){next *[*[1]]}
162 r([1,2,[]]){next *[*[1,2]]}