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

Suggestion: Alternate Hash differ output #211

Open
hackling opened this issue Jun 3, 2015 · 3 comments
Open

Suggestion: Alternate Hash differ output #211

hackling opened this issue Jun 3, 2015 · 3 comments

Comments

@hackling
Copy link
Contributor

hackling commented Jun 3, 2015

I've have a custom matcher that I use when comparing hashes/ model objects.

The differ functionality is good, but when keys move around, especially when they aren't symbols and strings, it can sometimes be a little hard to get the information you are looking for. This becomes particularly useful when dealing with much large hashes or Rails model objects, as it only shows you the key value pairs that are incorrect, rather than all of them.

Here is an example of the current differ output and the output from my custom matcher. If there is interest in including it, I can share the rest of the code etc / potentially work on adding it as a feature.

example_spec.rb

require 'spec_helper'

describe 'diff output for tags' do
  let(:hash_1) do
    {
      :akey_1 => :value_1,
      :bkey_2 => :value_2,
      :bkey_3 => :value_3,
      :bkey_4 => :value_4,
    }
  end
  let(:hash_2) do
    {
      :bkey_2 => :value_2,
      :akey_1 => :value_3,
      :akey_4 => :value_5,
      :bkey_3 => :value_3,
    }
  end

  it 'outputs using the standard differ' do
    expect(hash_1).to eq hash_2
  end

  it 'outputs using the new differ' do
    expect(hash_1).to match_hash hash_2
  end
end

output.txt

  1) diff output for tags outputs using the standard differ
     Failure/Error: expect(hash_1).to eq hash_2

       expected: {:bkey_2=>:value_2, :akey_1=>:value_3, :akey_4=>:value_5, :bkey_3=>:value_3}
            got: {:akey_1=>:value_1, :bkey_2=>:value_2, :bkey_3=>:value_3, :bkey_4=>:value_4}

       (compared using ==)

       Diff:

       @@ -1,5 +1,5 @@
       -:akey_1 => :value_3,
       -:akey_4 => :value_5,
       +:akey_1 => :value_1,
        :bkey_2 => :value_2,
        :bkey_3 => :value_3,
       +:bkey_4 => :value_4,
     # ./spec/example_spec.rb:22:in `block (2 levels) in <top (required)>'

  2) diff output for tags outputs using the new differ
     Failure/Error: expect(hash_1).to match_hash hash_2
       key: :akey_1 expected: :value_3 got: :value_1
       key: :akey_4 expected: :value_5 got: <missing>
       key: :bkey_4 expected: <missing> got: :value_4
     # ./spec/example_spec.rb:26:in `block (2 levels) in <top (required)>'

Finished in 0.75406 seconds (files took 0.01402 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./spec/example_spec.rb:21 # diff output for tags outputs using the standard differ
rspec ./spec/example_spec.rb:25 # diff output for tags outputs using the new differ

Rerun all failed examples:

rspec ./spec/example_spec.rb:{21,25}

An example with a much larger hash

@myronmarston
Copy link
Member

Interesting. How does your differ handle large values that don't fit neatly on one line (e.g. objects with long inspect output or multi-line strings)? What about when the difference is in some nested hash?

@hackling
Copy link
Contributor Author

Sorry, I've been away on holiday.

Currently it does nothing to handle really large output. That's not to say that it couldn't be updated to do so.
I also have an include_hash matcher as well, which is the more useful version that I use, that checks to see if the content of a smaller hash is included inside a larger one, allowing you to only check for specific key/value pairs instead against the whole hash, or slicing out a bunch of data beforehand.

@ttsui
Copy link

ttsui commented Jul 31, 2018

@hackling Is your custom matcher available anywhere? I'm encountering the same difficulty reading the builtin diff output.

@benoittgt benoittgt mentioned this issue Feb 6, 2019
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

3 participants