Skip to content

Commit 77ebfbf

Browse files
herwinweregon
authored andcommitted
Ruby 3.4 allows Strings as Fiber storage keys
But not anything else that responds to #to_sym. It does accept objects that respond to #to_str. This expands upon the ruby_bug block from upstream MRI. This also restores the previously removed "can't use invalid keys" test, but with the String key removed. Reference: https://bugs.ruby-lang.org/issues/20978
1 parent cc693a3 commit 77ebfbf

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

core/fiber/storage_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@
9090
key = :"#{self.class.name}#.#{self.object_id}"
9191
Fiber.new { Fiber[key] = 42; Fiber[key] }.resume.should == 42
9292
end
93+
94+
it "can't use invalid keys" do
95+
invalid_keys = [Object.new, 12]
96+
invalid_keys.each do |key|
97+
-> { Fiber[key] }.should raise_error(TypeError)
98+
end
99+
end
93100
end
94101

95102
ruby_bug "#20978", "3.2"..."3.4" do
@@ -98,6 +105,23 @@
98105
def key.to_str; "Foo"; end
99106
Fiber.new { Fiber[key] = 42; Fiber["Foo"] }.resume.should == 42
100107
end
108+
109+
it "converts a String key into a Symbol" do
110+
Fiber.new { Fiber["key"] = 42; Fiber[:key] }.resume.should == 42
111+
Fiber.new { Fiber[:key] = 42; Fiber["key"] }.resume.should == 42
112+
end
113+
114+
it "can use any object that responds to #to_str as a key" do
115+
key = mock("key")
116+
key.should_receive(:to_str).twice.and_return("key")
117+
Fiber.new { Fiber[key] = 42; Fiber[key] }.resume.should == 42
118+
end
119+
end
120+
121+
it "does not call #to_sym on the key" do
122+
key = mock("key")
123+
key.should_not_receive(:to_sym)
124+
-> { Fiber[key] }.should raise_error(TypeError)
101125
end
102126

103127
it "can access the storage of the parent fiber" do

0 commit comments

Comments
 (0)