1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 128 129 130 131 132 133 134
|
class NextSpecs
def self.yielding_method(expected)
yield.should == expected
:method_return_value
end
def self.yielding
yield
end
# The methods below are defined to spec the behavior of the next statement
# while specifically isolating whether the statement is in an Iter block or
# not. In a normal spec example, the code is always nested inside a block.
# Rather than rely on that implicit context in this case, the context is
# made explicit because of the interaction of next in a loop nested inside
# an Iter block.
def self.while_next(arg)
x = true
while x
begin
ScratchPad << :begin
x = false
if arg
next 42
else
next
end
ensure
ScratchPad << :ensure
end
end
end
def self.while_within_iter(arg)
yielding do
x = true
while x
begin
ScratchPad << :begin
x = false
if arg
next 42
else
next
end
ensure
ScratchPad << :ensure
end
end
end
end
def self.until_next(arg)
x = false
until x
begin
ScratchPad << :begin
x = true
if arg
next 42
else
next
end
ensure
ScratchPad << :ensure
end
end
end
def self.until_within_iter(arg)
yielding do
x = false
until x
begin
ScratchPad << :begin
x = true
if arg
next 42
else
next
end
ensure
ScratchPad << :ensure
end
end
end
end
def self.loop_next(arg)
x = 1
loop do
break if x == 2
begin
ScratchPad << :begin
x += 1
if arg
next 42
else
next
end
ensure
ScratchPad << :ensure
end
end
end
def self.loop_within_iter(arg)
yielding do
x = 1
loop do
break if x == 2
begin
ScratchPad << :begin
x += 1
if arg
next 42
else
next
end
ensure
ScratchPad << :ensure
end
end
end
end
class Block
def method(v)
yield v
end
end
end
|