Skip to content

Commit

Permalink
Add missing checks for to_str calls in ENV specs
Browse files Browse the repository at this point in the history
A number of specs did mention the calls to to_str, but did not test the
full path.
  • Loading branch information
herwinw committed Aug 21, 2023
1 parent 30be6b9 commit 8c01090
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/env/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions core/env/shared/include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions core/env/shared/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions core/env/shared/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions core/env/slice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c01090

Please sign in to comment.