Skip to content
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

Closed
tomekn opened this issue Nov 29, 2017 · 4 comments

Comments

@tomekn
Copy link

tomekn commented Nov 29, 2017

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

screen

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

@myronmarston
Copy link
Member

RSpec performs a textual diff using diff-lcs. In this case, the textual representation of the two BigDecimal objects is different, even though they are equal. I agree it's an undesirable behavior but as long as we perform textual diffs there's not much we can do about it.

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).

@benoittgt
Copy link
Member

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.

@JonRowe
Copy link
Member

JonRowe commented Jun 13, 2019

Where did we get to with your differ @benoittgt? That would solve this right?

@benoittgt
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants