Newer
Older
require "../../spec_helper"
describe "Code gen: next" do
it "codegens next" do
run("
def foo
yield
end
foo do
next 1
end
").to_i.should eq(1)
end
it "codegens next conditionally" do
run("
def foo
yield 1
yield 2
yield 3
yield 4
end
a = 0
foo do |i|
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
a += i
end
a
").to_i.should eq(4)
end
it "codegens next conditionally with int type (2)" do
run("
def foo
x = 0
x += yield 1
x += yield 2
x += yield 3
x += yield 4
x
end
foo do |i|
if i == 1
next 10
elsif i == 2
next 20
elsif i == 3
next 30
end
40
end
").to_i.should eq(100)
end
it "codegens next with break (1)" do
run("
def foo
yield 1
end
foo do |i|
if i == 1
break 20
else
next 10
end
end
").to_i.should eq(20)
end
it "codegens next with break (2)" do
run("
def foo
a = 0
a += yield 1
a += yield 2
a
end
foo do |i|
if i == 1
next 10
elsif i == 3
break 20
end
30
end
").to_i.should eq(40)
end
it "codegens next with break (3)" do
run("
def foo
a = 0
a += yield 1
a += yield 2
a
end
foo do |i|
if i == 1
next 10
elsif i == 2
break 20
end
30
end
").to_i.should eq(20)
end
it "codegens next with while inside block" do
run("
def foo
a = 0
a += yield 4
a += yield 5
a
end
foo do |i|
a = 0
b = 0
while a < 4
a += 1
b += a
end
if b == i
next 10
end
20
end
").to_i.should eq(30)
end
it "codegens next without expressions" do
run("
struct Nil; def to_i; 0; end; end
def foo
yield
end
foo do
if 1 == 1
1
else
next
end
end.to_i
").to_i.should eq(1)
end