1 require File.dirname(__FILE__) + '/../spec_helper'
3 describe "The next statement" do
4 it "raises a LocalJumpError if used not within block or while/for loop" do
5 def bad_meth; next; end
6 lambda { bad_meth }.should raise_error(LocalJumpError)
9 it "ends block execution if used within block" do
19 it "causes block to return nil" do
20 lambda { 123; next; 456 }.call.should == nil
23 it "returns the argument passed" do
24 lambda { 123; next 234; 345 }.call.should == 234
28 describe "Assignment via next" do
29 it "assigns objects" do
30 def r(val); a = yield(); val.should == a; end
40 r([1,2]){next [*[1,2]]}
43 it "assigns splatted objects" do
44 def r(val); a = yield(); val.should == a; end
53 r([1,2]){next *[*[1,2]]}
56 it "assigns objects to a splatted reference" do
57 def r(val); *a = yield(); val.should == a; end
63 r([[nil]]){next [nil]}
65 r([[1,2]]){next [1,2]}
68 r([[1,2]]){next [*[1,2]]}
71 it "assigns splatted objects to a splatted reference via a splatted yield" do
72 def r(val); *a = *yield(); val.should == a; end
82 r([1,2]){next *[*[1,2]]}
85 it "assigns objects to multiple variables" do
86 def r(val); a,b,*c = yield(); val.should == [a,b,c]; end
88 r([nil,nil,[]]){next nil}
90 r([nil,nil,[]]){next []}
91 r([1,nil,[]]){next [1]}
92 r([nil,nil,[]]){next [nil]}
93 r([[],nil,[]]){next [[]]}
94 r([1,2,[]]){next [1,2]}
95 r([nil,nil,[]]){next [*[]]}
96 r([1,nil,[]]){next [*[1]]}
97 r([1,2,[]]){next [*[1,2]]}
100 it "assigns splatted objects to multiple variables" do
101 def r(val); a,b,*c = *yield(); val.should == [a,b,c]; end
102 r([nil,nil,[]]){next *nil}
103 r([1,nil,[]]){next *1}
104 r([nil,nil,[]]){next *[]}
105 r([1,nil,[]]){next *[1]}
106 r([nil,nil,[]]){next *[nil]}
107 r([nil,nil,[]]){next *[[]]}
108 r([1,2,[]]){next *[1,2]}
109 r([nil,nil,[]]){next *[*[]]}
110 r([1,nil,[]]){next *[*[1]]}
111 r([1,2,[]]){next *[*[1,2]]}