From 8c01090119ad6a2c02a2897c3f600f27408b9c94 Mon Sep 17 00:00:00 2001 From: Herwin Date: Sun, 20 Aug 2023 17:50:00 +0200 Subject: [PATCH] Add missing checks for to_str calls in ENV specs A number of specs did mention the calls to to_str, but did not test the full path. --- core/env/delete_spec.rb | 8 ++++++++ core/env/shared/include.rb | 7 +++++++ core/env/shared/key.rb | 9 +++++++++ core/env/shared/value.rb | 7 +++++++ core/env/slice_spec.rb | 10 ++++++++++ 5 files changed, 41 insertions(+) diff --git a/core/env/delete_spec.rb b/core/env/delete_spec.rb index 3d69e4b022..f28ac97911 100644 --- a/core/env/delete_spec.rb +++ b/core/env/delete_spec.rb @@ -41,6 +41,14 @@ ENV["foo"].should == nil end + it "removes the variable coerced with #to_str" do + ENV["foo"] = "bar" + k = mock('key') + k.should_receive(:to_str).and_return("foo") + ENV.delete(k) + ENV["foo"].should == nil + end + it "raises TypeError if the argument is not a String and does not respond to #to_str" do -> { ENV.delete(Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String") end diff --git a/core/env/shared/include.rb b/core/env/shared/include.rb index 3efcd523d6..70aa555301 100644 --- a/core/env/shared/include.rb +++ b/core/env/shared/include.rb @@ -17,6 +17,13 @@ ENV.send(@method, "foo").should == false end + it "coerces the key with #to_str" do + ENV["foo"] = "bar" + k = mock('key') + k.should_receive(:to_str).and_return("foo") + ENV.send(@method, k).should == true + end + it "raises TypeError if the argument is not a String and does not respond to #to_str" do -> { ENV.send(@method, Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String") end diff --git a/core/env/shared/key.rb b/core/env/shared/key.rb index 93396d2aca..945cb99ac4 100644 --- a/core/env/shared/key.rb +++ b/core/env/shared/key.rb @@ -21,6 +21,15 @@ } end + it "coerces the key element with #to_str" do + ENV["foo"] = "bar" + k = mock('key') + k.should_receive(:to_str).and_return("bar") + suppress_warning { + ENV.send(@method, k).should == "foo" + } + end + it "raises TypeError if the argument is not a String and does not respond to #to_str" do -> { suppress_warning { diff --git a/core/env/shared/value.rb b/core/env/shared/value.rb index bef96b5fef..c2b5025465 100644 --- a/core/env/shared/value.rb +++ b/core/env/shared/value.rb @@ -16,6 +16,13 @@ ENV.send(@method, "foo").should == false end + it "coerces the value element with #to_str" do + ENV["foo"] = "bar" + v = mock('value') + v.should_receive(:to_str).and_return("bar") + ENV.send(@method, v).should == true + end + it "returns nil if the argument is not a String and does not respond to #to_str" do ENV.send(@method, Object.new).should == nil end diff --git a/core/env/slice_spec.rb b/core/env/slice_spec.rb index e3b6020391..959239d2b2 100644 --- a/core/env/slice_spec.rb +++ b/core/env/slice_spec.rb @@ -21,6 +21,16 @@ ENV.slice("foo", "boo", "bar").should == {"foo" => "0", "bar" => "1"} end + it "returns the values for the keys coerced with #to_str, but keeps the original objects as result keys" do + foo = mock('key 1') + foo.should_receive(:to_str).and_return("foo") + boo = mock('key 2') + boo.should_receive(:to_str).and_return("boo") + bar = mock('key 3') + bar.should_receive(:to_str).and_return("bar") + ENV.slice(foo, boo, bar).should == {foo => "0", bar => "1"} + end + it "raises TypeError if any argument is not a String and does not respond to #to_str" do -> { ENV.slice(Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String") end