2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
describe "Fiber.new" do
|
|
|
|
it "creates a fiber from the given block" do
|
|
|
|
fiber = Fiber.new {}
|
|
|
|
fiber.resume
|
|
|
|
fiber.should be_an_instance_of(Fiber)
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
it "creates a fiber from a subclass" do
|
|
|
|
class MyFiber < Fiber
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2019-04-28 17:20:11 -04:00
|
|
|
fiber = MyFiber.new {}
|
|
|
|
fiber.resume
|
|
|
|
fiber.should be_an_instance_of(MyFiber)
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
it "raises an ArgumentError if called without a block" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { Fiber.new }.should raise_error(ArgumentError)
|
2019-04-28 17:20:11 -04:00
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
it "does not invoke the block" do
|
|
|
|
invoked = false
|
|
|
|
fiber = Fiber.new { invoked = true }
|
|
|
|
invoked.should be_false
|
|
|
|
fiber.resume
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-04-28 17:20:11 -04:00
|
|
|
it "closes over lexical environments" do
|
|
|
|
o = Object.new
|
|
|
|
def o.f
|
|
|
|
a = 1
|
|
|
|
f = Fiber.new { a = 2 }
|
|
|
|
f.resume
|
|
|
|
a
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2019-04-28 17:20:11 -04:00
|
|
|
o.f.should == 2
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|