Updated MSpec source to a54f23d0.
[rbx.git] / spec / frozen / 1.8 / language / next_spec.rb
blob55898ae75e3fc961ee347e9c9d47ec8a47948f1b
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)
7   end
9   it "ends block execution if used within block" do
10     a = []
11     lambda {
12       a << 1
13       next
14       a << 2
15     }.call
16     a.should == [1]
17   end
19   it "causes block to return nil" do
20     lambda { 123; next; 456 }.call.should == nil
21   end
23   it "returns the argument passed" do
24     lambda { 123; next 234; 345 }.call.should == 234
25   end
26 end
28 describe "Assignment via next" do
29   it "assigns objects" do
30     def r(val); a = yield(); val.should == a; end
31     r(nil){next}
32     r(nil){next nil}
33     r(1){next 1}
34     r([]){next []}
35     r([1]){next [1]}
36     r([nil]){next [nil]}
37     r([[]]){next [[]]}
38     r([]){next [*[]]}
39     r([1]){next [*[1]]}
40     r([1,2]){next [*[1,2]]}
41   end
42   
43   it "assigns splatted objects" do
44     def r(val); a = yield(); val.should == a; end
45     r(nil){next *nil}
46     r(1){next *1}
47     r(nil){next *[]}
48     r(1){next *[1]}
49     r(nil){next *[nil]}
50     r([]){next *[[]]}
51     r(nil){next *[*[]]}
52     r(1){next *[*[1]]}
53     r([1,2]){next *[*[1,2]]}
54   end
55   
56   it "assigns objects to a splatted reference" do
57     def r(val); *a = yield(); val.should == a; end
58     r([nil]){next}
59     r([nil]){next nil}
60     r([1]){next 1}
61     r([[]]){next []}
62     r([[1]]){next [1]}
63     r([[nil]]){next [nil]}
64     r([[[]]]){next [[]]}
65     r([[1,2]]){next [1,2]}
66     r([[]]){next [*[]]}
67     r([[1]]){next [*[1]]}
68     r([[1,2]]){next [*[1,2]]}
69   end
70   
71   it "assigns splatted objects to a splatted reference via a splatted yield" do
72     def r(val); *a = *yield(); val.should == a; end
73     r([nil]){next *nil}
74     r([1]){next *1}
75     r([nil]){next *[]}
76     r([1]){next *[1]}
77     r([nil]){next *[nil]}
78     r([]){next *[[]]}
79     r([1,2]){next *[1,2]}
80     r([nil]){next *[*[]]}
81     r([1]){next *[*[1]]}
82     r([1,2]){next *[*[1,2]]}
83   end
84   
85   it "assigns objects to multiple variables" do
86     def r(val); a,b,*c = yield(); val.should == [a,b,c]; end
87     r([nil,nil,[]]){next}
88     r([nil,nil,[]]){next nil}
89     r([1,nil,[]]){next 1}
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]]}
98   end
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]]}
112  end