Skip to content

Commit 4c90b51

Browse files
authoredAug 21, 2023
Merge pull request #1056 from herwinw/env_to_str
Add missing checks for to_str calls in ENV specs
2 parents 30be6b9 + 8c01090 commit 4c90b51

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed
 

‎core/env/delete_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141
ENV["foo"].should == nil
4242
end
4343

44+
it "removes the variable coerced with #to_str" do
45+
ENV["foo"] = "bar"
46+
k = mock('key')
47+
k.should_receive(:to_str).and_return("foo")
48+
ENV.delete(k)
49+
ENV["foo"].should == nil
50+
end
51+
4452
it "raises TypeError if the argument is not a String and does not respond to #to_str" do
4553
-> { ENV.delete(Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String")
4654
end

‎core/env/shared/include.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
ENV.send(@method, "foo").should == false
1818
end
1919

20+
it "coerces the key with #to_str" do
21+
ENV["foo"] = "bar"
22+
k = mock('key')
23+
k.should_receive(:to_str).and_return("foo")
24+
ENV.send(@method, k).should == true
25+
end
26+
2027
it "raises TypeError if the argument is not a String and does not respond to #to_str" do
2128
-> { ENV.send(@method, Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String")
2229
end

‎core/env/shared/key.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
}
2222
end
2323

24+
it "coerces the key element with #to_str" do
25+
ENV["foo"] = "bar"
26+
k = mock('key')
27+
k.should_receive(:to_str).and_return("bar")
28+
suppress_warning {
29+
ENV.send(@method, k).should == "foo"
30+
}
31+
end
32+
2433
it "raises TypeError if the argument is not a String and does not respond to #to_str" do
2534
-> {
2635
suppress_warning {

‎core/env/shared/value.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
ENV.send(@method, "foo").should == false
1717
end
1818

19+
it "coerces the value element with #to_str" do
20+
ENV["foo"] = "bar"
21+
v = mock('value')
22+
v.should_receive(:to_str).and_return("bar")
23+
ENV.send(@method, v).should == true
24+
end
25+
1926
it "returns nil if the argument is not a String and does not respond to #to_str" do
2027
ENV.send(@method, Object.new).should == nil
2128
end

‎core/env/slice_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
ENV.slice("foo", "boo", "bar").should == {"foo" => "0", "bar" => "1"}
2222
end
2323

24+
it "returns the values for the keys coerced with #to_str, but keeps the original objects as result keys" do
25+
foo = mock('key 1')
26+
foo.should_receive(:to_str).and_return("foo")
27+
boo = mock('key 2')
28+
boo.should_receive(:to_str).and_return("boo")
29+
bar = mock('key 3')
30+
bar.should_receive(:to_str).and_return("bar")
31+
ENV.slice(foo, boo, bar).should == {foo => "0", bar => "1"}
32+
end
33+
2434
it "raises TypeError if any argument is not a String and does not respond to #to_str" do
2535
-> { ENV.slice(Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String")
2636
end

0 commit comments

Comments
 (0)