Skip to content

Commit 74a1904

Browse files
committed
Actually verify that non-string to_s is discarded
The old spec only verified that a dynamic string would be successfully created when an interpolated object returned a non-string value from to_s. This enhances the spec to also verify that the non-string value will be discarded in favor of the generic "hashy" inspect string.
1 parent a18e040 commit 74a1904

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

spec/ruby/language/string_spec.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,14 @@ class << obj
134134
end
135135

136136
it "uses an internal representation when #to_s doesn't return a String" do
137-
obj = mock('to_s')
138-
obj.stub!(:to_s).and_return(42)
139-
140-
# See rubyspec commit 787c132d by yugui. There is value in
141-
# ensuring that this behavior works. So rather than removing
142-
# this spec completely, the only thing that can be asserted
143-
# is that if you interpolate an object that fails to return
144-
# a String, you will still get a String and not raise an
145-
# exception.
146-
"#{obj}".should be_an_instance_of(String)
137+
# don't use mocking so we have a predictable string representation below (#<Object...>)
138+
obj = Object.new
139+
def obj.to_s
140+
42
141+
end
142+
143+
# ensure the interpolation succeeds and the content included does not include the non-string value.
144+
"#{obj}".should =~ /#<Object:0x[0-9a-fA-F]+>/
147145
end
148146

149147
it "allows a dynamic string to parse a nested do...end block as an argument to a call without parens, interpolated" do

0 commit comments

Comments
 (0)