-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Differ wrongly highlights BigDecimals in a hash where another element doesn't match. #335
Comments
RSpec performs a textual diff using diff-lcs. In this case, the textual representation of the two I think that to fix this we'd probably have to write our own differ from scratch, which is not something we're willing to take on and maintain (nor do we have the time to do so). |
Closing the issue because of inactivity. I think at the moment you can deal with this with custom matchers. Here is an example: # run with `ruby rspec_bigdecimal.rb`
require "bundler/inline"
require 'bigdecimal'
gemfile(true) do
source "https://rubygems.org"
gem "rspec"
end
require 'rspec/autorun'
RSpec::Matchers.define :a_big_decimal_of do |expected|
match do |actual|
BigDecimal(actual.to_s) == BigDecimal(expected.to_s)
end
end
RSpec::Matchers.define :a_big_decimal_close_to do |expected|
match do |actual|
BigDecimal(actual.to_s).round(3) == BigDecimal(expected.to_s).round(3)
end
end
RSpec.describe "BigDecimal" do
it "validate big decimales" do
hash_with_bigdecimal = {
hello: "world",
decimal: BigDecimal("10"),
decimal_rounded: BigDecimal("10.123456789")
}
expect(hash_with_bigdecimal).to include({
decimal: a_big_decimal_of("10"),
decimal_rounded: a_big_decimal_close_to("10.123")
})
end
end If wanted we can maybe add a matcher. |
Where did we get to with your differ @benoittgt? That would solve this right? |
If you speak about #365 I see it as more dedicated to complicated structure like hash, very long string, deeply nested array. But this case is something the new differ should manage. |
Hi!
I've encountered an issue with the differ that feels wrong.
Let's take this class as an example...
Simple enough, let's test that our hash has the right values.
Green - all good!
Now let's change the expectation by altering the hello string, note that the bigdecimals are unchanged...
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
The text was updated successfully, but these errors were encountered: