Differ wrongly highlights BigDecimals in a hash where another element doesn't match. #335
Description
Hi!
I've encountered an issue with the differ that feels wrong.
Let's take this class as an example...
require 'bigdecimal'
class HashGenerator
def self.generate
{
hello: 'world',
decimal1: BigDecimal.new("10"),
decimal2: BigDecimal.new("29.32")
}
end
end
Simple enough, let's test that our hash has the right values.
require 'hash_generator'
require 'bigdecimal'
RSpec.describe HashGenerator, '#self.generate' do
it 'returns the expected hash' do
expected_hash = {
hello: 'world',
decimal1: BigDecimal.new("10"),
decimal2: BigDecimal.new("29.32")
}
expect(HashGenerator.generate).to eq(expected_hash)
end
end
Green - all good!
Now let's change the expectation by altering the hello string, note that the bigdecimals are unchanged...
require 'hash_generator'
require 'bigdecimal'
RSpec.describe HashGenerator, '#self.generate' do
it 'returns the expected hash' do
expected_hash = {
hello: 'SOME DIFFERENT STRING',
decimal1: BigDecimal.new("10"),
decimal2: BigDecimal.new("29.32")
}
expect(HashGenerator.generate).to eq(expected_hash)
end
end
The test fails as expected - but the diff also highlights the bigdecimals as a problem. I understand this is because the BigDecimal inspect
contains the object id and the differ probably looks at the stringified version of the hash when it's doing its thing?
Anyway, a more complex permutation of this has led me down a wild goose chase yesterday and I thought I'd raise it here. I'm happy to work on a solution if there is consensus that this is undesired behaviour.
Thanks,
Tom