Skip to content

Commit fe7ccac

Browse files
authored
Merge pull request #1120 from herwinw/thread_fetch
Add specs for Thread#fetch with a block
2 parents 56e16b7 + c6ea864 commit fe7ccac

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

core/thread/fetch_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@
2929
end
3030
end
3131

32+
describe 'with a block' do
33+
it 'returns the value of the fiber-local variable if value has been assigned' do
34+
th = Thread.new { Thread.current[:cat] = 'meow' }
35+
th.join
36+
th.fetch(:cat) { true }.should == 'meow'
37+
end
38+
39+
it "returns the block value if fiber-local variable hasn't been assigned" do
40+
th = Thread.new {}
41+
th.join
42+
th.fetch(:cat) { true }.should == true
43+
end
44+
45+
it "does not call the block if value has been assigned" do
46+
th = Thread.new { Thread.current[:cat] = 'meow' }
47+
th.join
48+
var = :not_updated
49+
th.fetch(:cat) { var = :updated }.should == 'meow'
50+
var.should == :not_updated
51+
end
52+
53+
it "uses the block if a default is given and warns about it" do
54+
th = Thread.new {}
55+
th.join
56+
-> {
57+
th.fetch(:cat, false) { true }.should == true
58+
}.should complain(/warning: block supersedes default value argument/)
59+
end
60+
end
61+
3262
it 'raises an ArgumentError when not passed one or two arguments' do
3363
-> { Thread.current.fetch() }.should raise_error(ArgumentError)
3464
-> { Thread.current.fetch(1, 2, 3) }.should raise_error(ArgumentError)

0 commit comments

Comments
 (0)