2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
describe "Kernel.loop" do
|
|
|
|
it "is a private method" do
|
|
|
|
Kernel.should have_private_instance_method(:loop)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls block until it is terminated by a break" do
|
|
|
|
i = 0
|
|
|
|
loop do
|
|
|
|
i += 1
|
|
|
|
break if i == 10
|
|
|
|
end
|
|
|
|
|
|
|
|
i.should == 10
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns value passed to break" do
|
|
|
|
loop do
|
|
|
|
break 123
|
|
|
|
end.should == 123
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil if no value passed to break" do
|
|
|
|
loop do
|
|
|
|
break
|
|
|
|
end.should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an enumerator if no block given" do
|
|
|
|
enum = loop
|
|
|
|
enum.instance_of?(Enumerator).should be_true
|
|
|
|
cnt = 0
|
|
|
|
enum.each do |*args|
|
|
|
|
raise "Args should be empty #{args.inspect}" unless args.empty?
|
|
|
|
cnt += 1
|
|
|
|
break cnt if cnt >= 42
|
|
|
|
end.should == 42
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rescues StopIteration" do
|
|
|
|
loop do
|
|
|
|
raise StopIteration
|
|
|
|
end
|
|
|
|
42.should == 42
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rescues StopIteration's subclasses" do
|
|
|
|
finish = Class.new StopIteration
|
|
|
|
loop do
|
|
|
|
raise finish
|
|
|
|
end
|
|
|
|
42.should == 42
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not rescue other errors" do
|
2019-07-27 06:40:09 -04:00
|
|
|
->{ loop do raise StandardError end }.should raise_error( StandardError )
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2018-04-28 15:50:06 -04:00
|
|
|
it "returns StopIteration#result, the result value of a finished iterator" do
|
|
|
|
e = Enumerator.new { |y|
|
|
|
|
y << 1
|
|
|
|
y << 2
|
|
|
|
:stopped
|
|
|
|
}
|
|
|
|
loop { e.next }.should == :stopped
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "when no block is given" do
|
|
|
|
describe "returned Enumerator" do
|
|
|
|
describe "size" do
|
|
|
|
it "returns Float::INFINITY" do
|
|
|
|
loop.size.should == Float::INFINITY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|