Skip to content

Commit

Permalink
Merge pull request #616 from rspec/ruby-34-fixes
Browse files Browse the repository at this point in the history
Fix hash formatting in specs on Ruby 3.4
  • Loading branch information
JonRowe committed Oct 19, 2024
1 parent a658003 commit a3dda29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
4 changes: 4 additions & 0 deletions spec/rspec/support/differ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ def inspect; "<BrokenObject>"; end
|
EOD

expected_diff.gsub!('=>',' => ') if RUBY_VERSION.to_f > 3.3

diff = differ.diff(expected,actual)
expect(diff).to be_diffed_as(expected_diff)
end
Expand Down Expand Up @@ -375,6 +377,7 @@ def inspect; "<BrokenObject>"; end
|+"c" => {"key_1"=>#{formatted_time}},
|
EOD
expected_diff.gsub!('"=>','" => ') if RUBY_VERSION.to_f > 3.3

left_side_hash = {'c' => {'key_1' => time}}
right_side_hash = {'b' => {'key_1' => time}}
Expand Down Expand Up @@ -408,6 +411,7 @@ def inspect; "<BrokenObject>"; end
|+[{"a"=>#{formatted_time}}, "c"]
|
EOD
expected_diff.gsub!('=>',' => ') if RUBY_VERSION.to_f > 3.3

left_side_array = [{'a' => time}, 'c']
right_side_array = [{'b' => time}, 'c']
Expand Down
24 changes: 17 additions & 7 deletions spec/rspec/support/object_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Support
# We can't count on the ordering of the hash on 1.8.7...
expect(formatted).to include(%Q{"key"=>#{formatted_time}}, %Q{#{formatted_time}=>"value"}, %Q{"nested"=>{"key"=>#{formatted_time}}})
else
expect(formatted).to eq(%Q{{"key"=>#{formatted_time}, #{formatted_time}=>"value", "nested"=>{"key"=>#{formatted_time}}}})
expect(formatted).to eq_hash_syntax(%Q{{"key"=>#{formatted_time}, #{formatted_time}=>"value", "nested"=>{"key"=>#{formatted_time}}}})
end
end
end
Expand All @@ -39,7 +39,7 @@ module Support

it 'sorts keys to ensure objects are always displayed the same way' do
formatted = ObjectFormatter.format(input)
expect(formatted).to eq expected
expect(formatted).to eq_hash_syntax expected
end
end
end
Expand Down Expand Up @@ -269,7 +269,7 @@ def self.to_s
let(:formatted_time) { ObjectFormatter.format(time) }

it 'formats the recursive element as {...} and other elements with custom formatting' do
expect(output).to eq("{{...}=>#{formatted_time}}")
expect(output).to eq_hash_syntax("{{...}=>#{formatted_time}}")
end
end

Expand All @@ -289,7 +289,7 @@ def self.to_s
let(:formatted_time) { ObjectFormatter.format(time) }

it 'formats the recursive element as {...} and other elements with custom formatting' do
expect(output).to eq("{#{formatted_time}=>{...}}")
expect(output).to eq_hash_syntax("{#{formatted_time}=>{...}}")
end
end

Expand All @@ -305,7 +305,7 @@ def self.to_s
end

it 'formats the recursive element as [...]' do
expect(output).to eq('[{:recursive_array=>[...]}]')
expect(output).to eq_hash_syntax('[{:recursive_array=>[...]}]')
end
end

Expand All @@ -321,7 +321,7 @@ def self.to_s
end

it 'formats the recursive element as {...}' do
expect(output).to eq('{:array=>[:next_is_recursive_hash, {...}]}')
expect(output).to eq_hash_syntax('{:array=>[:next_is_recursive_hash, {...}]}')
end
end

Expand All @@ -336,7 +336,7 @@ def self.to_s
end

it 'does not omit them' do
expect(output).to eq('[{:key=>"value"}, {:key=>"value"}]')
expect(output).to eq_hash_syntax('[{:key=>"value"}, {:key=>"value"}]')
end
end

Expand Down Expand Up @@ -370,6 +370,16 @@ def inspect
expect(formatter.format('Test String Of A Longer Length')).to eq('"Test String Of A Longer Length"')
end
end

if RUBY_VERSION.to_f > 3.3
def eq_hash_syntax(string)
eq string.gsub('=>', ' => ')
end
else
def eq_hash_syntax(string)
eq string
end
end
end
end
end

0 comments on commit a3dda29

Please sign in to comment.