Skip to content

Commit

Permalink
Update spec/ for colon-style hash inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Jul 24, 2024
1 parent 17d5651 commit a2f04ae
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
26 changes: 15 additions & 11 deletions spec/ruby/core/hash/shared/to_s.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

pairs = []
h.each do |key, value|
pairs << key.inspect + '=>' + value.inspect
if Symbol === key
pairs << key.to_s + ': ' + value.inspect
else
pairs << key.inspect + ' => ' + value.inspect
end
end

str = '{' + pairs.join(', ') + '}'
Expand All @@ -20,30 +24,30 @@
key.should_receive(:inspect).and_return('key')
val.should_receive(:inspect).and_return('val')

{ key => val }.send(@method).should == '{key=>val}'
{ key => val }.send(@method).should == '{key => val}'
end

it "does not call #to_s on a String returned from #inspect" do
str = +"abc"
str.should_not_receive(:to_s)

{ a: str }.send(@method).should == '{:a=>"abc"}'
{ a: str }.send(@method).should == '{a: "abc"}'
end

it "calls #to_s on the object returned from #inspect if the Object isn't a String" do
obj = mock("Hash#inspect/to_s calls #to_s")
obj.should_receive(:inspect).and_return(obj)
obj.should_receive(:to_s).and_return("abc")

{ a: obj }.send(@method).should == "{:a=>abc}"
{ a: obj }.send(@method).should == "{a: abc}"
end

it "does not call #to_str on the object returned from #inspect when it is not a String" do
obj = mock("Hash#inspect/to_s does not call #to_str")
obj.should_receive(:inspect).and_return(obj)
obj.should_not_receive(:to_str)

{ a: obj }.send(@method).should =~ /^\{:a=>#<MockObject:0x[0-9a-f]+>\}$/
{ a: obj }.send(@method).should =~ /^\{a: #<MockObject:0x[0-9a-f]+>\}$/
end

it "does not call #to_str on the object returned from #to_s when it is not a String" do
Expand All @@ -52,7 +56,7 @@
obj.should_receive(:to_s).and_return(obj)
obj.should_not_receive(:to_str)

{ a: obj }.send(@method).should =~ /^\{:a=>#<MockObject:0x[0-9a-f]+>\}$/
{ a: obj }.send(@method).should =~ /^\{a: #<MockObject:0x[0-9a-f]+>\}$/
end

it "does not swallow exceptions raised by #to_s" do
Expand All @@ -66,24 +70,24 @@
it "handles hashes with recursive values" do
x = {}
x[0] = x
x.send(@method).should == '{0=>{...}}'
x.send(@method).should == '{0 => {...}}'

x = {}
y = {}
x[0] = y
y[1] = x
x.send(@method).should == "{0=>{1=>{...}}}"
y.send(@method).should == "{1=>{0=>{...}}}"
x.send(@method).should == "{0 => {1 => {...}}}"
y.send(@method).should == "{1 => {0 => {...}}}"
end

it "does not raise if inspected result is not default external encoding" do
utf_16be = mock("utf_16be")
utf_16be.should_receive(:inspect).and_return(%<"utf_16be \u3042">.encode(Encoding::UTF_16BE))

{a: utf_16be}.send(@method).should == '{:a=>"utf_16be \u3042"}'
{a: utf_16be}.send(@method).should == '{a: "utf_16be \u3042"}'
end

it "works for keys and values whose #inspect return a frozen String" do
{ true => false }.to_s.should == "{true=>false}"
{ true => false }.to_s.should == "{true => false}"
end
end
2 changes: 1 addition & 1 deletion spec/ruby/core/string/modulo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def universal.to_f() 0.0 end
("%1$p" % [10, 5]).should == "10"
("%-22p" % 10).should == "10 "
("%*p" % [10, 10]).should == " 10"
("%p" % {capture: 1}).should == "{:capture=>1}"
("%p" % {capture: 1}).should == "{capture: 1}"
("%p" % "str").should == "\"str\""
end

Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/language/pattern_matching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
case {a: 0, b: 1}
in a: 1, b: 1
end
}.should raise_error(NoMatchingPatternError, /\{:a=>0, :b=>1\}/)
}.should raise_error(NoMatchingPatternError, /\{a: 0, b: 1\}/)
end

it "raises NoMatchingPatternError if no pattern matches and evaluates the expression only once" do
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/library/net-http/http/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

it "sends Content-Type: application/x-www-form-urlencoded by default" do
response = Net::HTTP.post(URI("http://localhost:#{NetHTTPSpecs.port}/request/header"), "test=test")
response.body.should include('"Content-Type"=>"application/x-www-form-urlencoded"')
response.body.should include('"Content-Type" => "application/x-www-form-urlencoded"')
end

it "does not support HTTP Basic Auth" do
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/library/net-http/http/send_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

@methods.each do |method|
response = @http.send_request(method, "/request/header", "test=test", "referer" => referer)
response.body.should include('"Referer"=>"' + referer + '"')
response.body.should include('"Referer" => "' + referer + '"')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/library/pp/pp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
hash = { 'key' => 42 }
-> {
PP.pp hash
}.should output('{"key"=>42}' + "\n")
}.should output('{"key" => 42}' + "\n")
end
end
2 changes: 1 addition & 1 deletion spec/ruby/optional/capi/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ def inspect
end

it "tries to convert the passed argument to a string by calling #to_s" do
@s.rb_String({"bar" => "foo"}).should == '{"bar"=>"foo"}'
@s.rb_String({"bar" => "foo"}).should == '{"bar" => "foo"}'
end
end

Expand Down

0 comments on commit a2f04ae

Please sign in to comment.