Skip to content
This repository

comparing arrays of multiple hashes with different keys and/or values using the =~ operator results in ambiguous error #81

Closed
chrisbloom7 opened this Issue · 2 comments

2 participants

Chris Bloom David Chelimsky
Chris Bloom

Seriously, it's that specific of an error. Bear with me while I copy/paste this really big test suite.

# spec/hash_comparison_spec.rb
require 'spec_helper'

describe "hash comparison tests" do
  context "comparing single hashes with same keys, different values" do
    let(:me)  { { :foo => 'bar' } }
    let(:me2) { { :foo => 'baz' } }

    it "should fail gracefully when using ==" do
      me.should == me2
    end

    it "should fail gracefully when using eq()" do
      me.should eq(me2)
    end

    it "should fail gracefully when using =~" do
      me.should =~ me2
    end
  end

  context "comparing single hashes with different keys, different values" do
    let(:me)  { { :foo => 'bar' } }
    let(:me2) { { :bar => 'baz' } }

    it "should fail gracefully when using ==" do
      me.should == me2
    end

    it "should fail gracefully when using eq()" do
      me.should eq(me2)
    end

    it "should fail gracefully when using =~" do
      me.should =~ me2
    end
  end

  context "comparing an array of single hashes with same keys, different values" do
    let(:me)  { [{ :foo => 'bar' }] }
    let(:me2) { [{ :foo => 'baz' }] }

    it "should fail gracefully when using ==" do
      me.should == me2
    end

    it "should fail gracefully when using eq()" do
      me.should eq(me2)
    end

    it "should fail gracefully when using =~" do
      me.should =~ me2
    end
  end

  context "comparing an array of single hashes with different keys, different values" do
    let(:me)  { [{ :foo => 'bar' }] }
    let(:me2) { [{ :bar => 'baz' }] }

    it "should fail gracefully when using ==" do
      me.should == me2
    end

    it "should fail gracefully when using eq()" do
      me.should eq(me2)
    end

    it "should fail gracefully when using =~" do
      me.should =~ me2
    end
  end

  context "comparing an array of multiple hashes with same keys, same values" do
    let(:me3) { [{ :foo => 'bar' }, { :biz => 'bang' }] }
    let(:me4) { [{ :foo => 'bar' }, { :biz => 'bang' }] }

    it "should pass when using ==" do
      me3.should == me4
    end

    it "should pass when using eq()" do
      me3.should eq(me4)
    end

    it "should pass when using =~" do
      me3.should =~ me4
    end
  end

  context "comparing an array of multiple hashes with same keys, different values" do
    let(:me3) { [{ :foo => 'bar' }, { :biz => 'bang' }] }
    let(:me4) { [{ :foo => 'bang' }, { :biz => 'bar' }] }

    it "should fail gracefully when using ==" do
      me3.should == me4
    end

    it "should fail gracefully when using eq()" do
      me3.should eq(me4)
    end

    it "should fail and show me an ambiguous error when using =~" do
      me3.should =~ me4
    end
  end

  context "comparing an array of hashes with different keys, different values" do
    let(:me3) { [{ :foo => 'bar' }, { :biz => 'bang' }] }
    let(:me4) { [{ :bar => 'foo' }, { :bang => 'biz' }] }

    it "should fail gracefully when using ==" do
      me3.should == me4
    end

    it "should fail gracefully when using eq()" do
      me3.should eq(me4)
    end

    it "should fail and show me an ambiguous error when using =~" do
      me3.should =~ me4
    end
  end
end

And the Really Big Output

$ rspec spec/hash_comparison_spec.rb --format documentation

hash comparison tests
  comparing single hashes with same keys, different values
    should fail gracefully when using == (FAILED - 1)
    should fail gracefully when using eq() (FAILED - 2)
    should fail gracefully when using =~ (FAILED - 3)
  comparing single hashes with different keys, different values
    should fail gracefully when using == (FAILED - 4)
    should fail gracefully when using eq() (FAILED - 5)
    should fail gracefully when using =~ (FAILED - 6)
  comparing an array of single hashes with same keys, different values
    should fail gracefully when using == (FAILED - 7)
    should fail gracefully when using eq() (FAILED - 8)
    should fail gracefully when using =~ (FAILED - 9)
  comparing an array of single hashes with different keys, different values
    should fail gracefully when using == (FAILED - 10)
    should fail gracefully when using eq() (FAILED - 11)
    should fail gracefully when using =~ (FAILED - 12)
  comparing an array of multiple hashes with same keys, same values
    should pass when using ==
    should pass when using eq()
    should pass when using =~
  comparing an array of multiple hashes with same keys, different values
    should fail gracefully when using == (FAILED - 13)
    should fail gracefully when using eq() (FAILED - 14)
    should fail and show me an ambiguous error when using =~ (FAILED - 15)
  comparing an array of hashes with different keys, different values
    should fail gracefully when using == (FAILED - 16)
    should fail gracefully when using eq() (FAILED - 17)
    should fail and show me an ambiguous error when using =~ (FAILED - 18)

Failures:

  1) hash comparison tests comparing single hashes with same keys, different values should fail gracefully when using ==
     Failure/Error: me.should == me2
       expected: {:foo=>"baz"}
            got: {:foo=>"bar"} (using ==)
       Diff:
       @@ -1,2 +1,2 @@
       -{:foo=>"baz"}
       +{:foo=>"bar"}
     # ./spec/hash_comparison_spec.rb:9:in `block (3 levels) in <top (required)>'

  2) hash comparison tests comparing single hashes with same keys, different values should fail gracefully when using eq()
     Failure/Error: me.should eq(me2)

       expected {:foo=>"baz"}
            got {:foo=>"bar"}

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -{:foo=>"baz"}
       +{:foo=>"bar"}
     # ./spec/hash_comparison_spec.rb:13:in `block (3 levels) in <top (required)>'

  3) hash comparison tests comparing single hashes with same keys, different values should fail gracefully when using =~
     Failure/Error: me.should =~ me2
       expected: {:foo=>"baz"}
            got: {:foo=>"bar"} (using =~)
       Diff:
       @@ -1,2 +1,2 @@
       -{:foo=>"baz"}
       +{:foo=>"bar"}
     # ./spec/hash_comparison_spec.rb:17:in `block (3 levels) in <top (required)>'

  4) hash comparison tests comparing single hashes with different keys, different values should fail gracefully when using ==
     Failure/Error: me.should == me2
       expected: {:bar=>"baz"}
            got: {:foo=>"bar"} (using ==)
       Diff:
       @@ -1,2 +1,2 @@
       -{:bar=>"baz"}
       +{:foo=>"bar"}
     # ./spec/hash_comparison_spec.rb:26:in `block (3 levels) in <top (required)>'

  5) hash comparison tests comparing single hashes with different keys, different values should fail gracefully when using eq()
     Failure/Error: me.should eq(me2)

       expected {:bar=>"baz"}
            got {:foo=>"bar"}

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -{:bar=>"baz"}
       +{:foo=>"bar"}
     # ./spec/hash_comparison_spec.rb:30:in `block (3 levels) in <top (required)>'

  6) hash comparison tests comparing single hashes with different keys, different values should fail gracefully when using =~
     Failure/Error: me.should =~ me2
       expected: {:bar=>"baz"}
            got: {:foo=>"bar"} (using =~)
       Diff:
       @@ -1,2 +1,2 @@
       -{:bar=>"baz"}
       +{:foo=>"bar"}
     # ./spec/hash_comparison_spec.rb:34:in `block (3 levels) in <top (required)>'

  7) hash comparison tests comparing an array of single hashes with same keys, different values should fail gracefully when using ==
     Failure/Error: me.should == me2
       expected: [{:foo=>"baz"}]
            got: [{:foo=>"bar"}] (using ==)
       Diff:
       @@ -1,2 +1,2 @@
       -[{:foo=>"baz"}]
       +[{:foo=>"bar"}]
     # ./spec/hash_comparison_spec.rb:43:in `block (3 levels) in <top (required)>'

  8) hash comparison tests comparing an array of single hashes with same keys, different values should fail gracefully when using eq()
     Failure/Error: me.should eq(me2)

       expected [{:foo=>"baz"}]
            got [{:foo=>"bar"}]

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -[{:foo=>"baz"}]
       +[{:foo=>"bar"}]
     # ./spec/hash_comparison_spec.rb:47:in `block (3 levels) in <top (required)>'

  9) hash comparison tests comparing an array of single hashes with same keys, different values should fail gracefully when using =~
     Failure/Error: me.should =~ me2
       expected collection contained:  [{:foo=>"baz"}]
       actual collection contained:    [{:foo=>"bar"}]
       the missing elements were:      [{:foo=>"baz"}]
       the extra elements were:        [{:foo=>"bar"}]
     # ./spec/hash_comparison_spec.rb:51:in `block (3 levels) in <top (required)>'

  10) hash comparison tests comparing an array of single hashes with different keys, different values should fail gracefully when using ==
     Failure/Error: me.should == me2
       expected: [{:bar=>"baz"}]
            got: [{:foo=>"bar"}] (using ==)
       Diff:
       @@ -1,2 +1,2 @@
       -[{:bar=>"baz"}]
       +[{:foo=>"bar"}]
     # ./spec/hash_comparison_spec.rb:60:in `block (3 levels) in <top (required)>'

  11) hash comparison tests comparing an array of single hashes with different keys, different values should fail gracefully when using eq()
     Failure/Error: me.should eq(me2)

       expected [{:bar=>"baz"}]
            got [{:foo=>"bar"}]

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -[{:bar=>"baz"}]
       +[{:foo=>"bar"}]
     # ./spec/hash_comparison_spec.rb:64:in `block (3 levels) in <top (required)>'

  12) hash comparison tests comparing an array of single hashes with different keys, different values should fail gracefully when using =~
     Failure/Error: me.should =~ me2
       expected collection contained:  [{:bar=>"baz"}]
       actual collection contained:    [{:foo=>"bar"}]
       the missing elements were:      [{:bar=>"baz"}]
       the extra elements were:        [{:foo=>"bar"}]
     # ./spec/hash_comparison_spec.rb:68:in `block (3 levels) in <top (required)>'

  13) hash comparison tests comparing an array of multiple hashes with same keys, different values should fail gracefully when using ==
     Failure/Error: me3.should == me4
       expected: [{:foo=>"bang"}, {:biz=>"bar"}]
            got: [{:foo=>"bar"}, {:biz=>"bang"}] (using ==)
       Diff:
       @@ -1,2 +1,2 @@
       -[{:foo=>"bang"}, {:biz=>"bar"}]
       +[{:foo=>"bar"}, {:biz=>"bang"}]
     # ./spec/hash_comparison_spec.rb:94:in `block (3 levels) in <top (required)>'

  14) hash comparison tests comparing an array of multiple hashes with same keys, different values should fail gracefully when using eq()
     Failure/Error: me3.should eq(me4)

       expected [{:foo=>"bang"}, {:biz=>"bar"}]
            got [{:foo=>"bar"}, {:biz=>"bang"}]

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -[{:foo=>"bang"}, {:biz=>"bar"}]
       +[{:foo=>"bar"}, {:biz=>"bang"}]
     # ./spec/hash_comparison_spec.rb:98:in `block (3 levels) in <top (required)>'

  15) hash comparison tests comparing an array of multiple hashes with same keys, different values should fail and show me an ambiguous error when using =~
     Failure/Error: me3.should =~ me4
     ArgumentError:
       comparison of Hash with Hash failed
     # ./spec/hash_comparison_spec.rb:102:in `block (3 levels) in <top (required)>'

  16) hash comparison tests comparing an array of hashes with different keys, different values should fail gracefully when using ==
     Failure/Error: me3.should == me4
       expected: [{:bar=>"foo"}, {:bang=>"biz"}]
            got: [{:foo=>"bar"}, {:biz=>"bang"}] (using ==)
       Diff:
       @@ -1,2 +1,2 @@
       -[{:bar=>"foo"}, {:bang=>"biz"}]
       +[{:foo=>"bar"}, {:biz=>"bang"}]
     # ./spec/hash_comparison_spec.rb:111:in `block (3 levels) in <top (required)>'

  17) hash comparison tests comparing an array of hashes with different keys, different values should fail gracefully when using eq()
     Failure/Error: me3.should eq(me4)

       expected [{:bar=>"foo"}, {:bang=>"biz"}]
            got [{:foo=>"bar"}, {:biz=>"bang"}]

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -[{:bar=>"foo"}, {:bang=>"biz"}]
       +[{:foo=>"bar"}, {:biz=>"bang"}]
     # ./spec/hash_comparison_spec.rb:115:in `block (3 levels) in <top (required)>'

  18) hash comparison tests comparing an array of hashes with different keys, different values should fail and show me an ambiguous error when using =~
     Failure/Error: me3.should =~ me4
     ArgumentError:
       comparison of Hash with Hash failed
     # ./spec/hash_comparison_spec.rb:119:in `block (3 levels) in <top (required)>'

Finished in 1.35 seconds
21 examples, 18 failures

Failed examples:

rspec ./spec/hash_comparison_spec.rb:8 # hash comparison tests comparing single hashes with same keys, different values should fail gracefully when using ==
rspec ./spec/hash_comparison_spec.rb:12 # hash comparison tests comparing single hashes with same keys, different values should fail gracefully when using eq()
rspec ./spec/hash_comparison_spec.rb:16 # hash comparison tests comparing single hashes with same keys, different values should fail gracefully when using =~
rspec ./spec/hash_comparison_spec.rb:25 # hash comparison tests comparing single hashes with different keys, different values should fail gracefully when using ==
rspec ./spec/hash_comparison_spec.rb:29 # hash comparison tests comparing single hashes with different keys, different values should fail gracefully when using eq()
rspec ./spec/hash_comparison_spec.rb:33 # hash comparison tests comparing single hashes with different keys, different values should fail gracefully when using =~
rspec ./spec/hash_comparison_spec.rb:42 # hash comparison tests comparing an array of single hashes with same keys, different values should fail gracefully when using ==
rspec ./spec/hash_comparison_spec.rb:46 # hash comparison tests comparing an array of single hashes with same keys, different values should fail gracefully when using eq()
rspec ./spec/hash_comparison_spec.rb:50 # hash comparison tests comparing an array of single hashes with same keys, different values should fail gracefully when using =~
rspec ./spec/hash_comparison_spec.rb:59 # hash comparison tests comparing an array of single hashes with different keys, different values should fail gracefully when using ==
rspec ./spec/hash_comparison_spec.rb:63 # hash comparison tests comparing an array of single hashes with different keys, different values should fail gracefully when using eq()
rspec ./spec/hash_comparison_spec.rb:67 # hash comparison tests comparing an array of single hashes with different keys, different values should fail gracefully when using =~
rspec ./spec/hash_comparison_spec.rb:93 # hash comparison tests comparing an array of multiple hashes with same keys, different values should fail gracefully when using ==
rspec ./spec/hash_comparison_spec.rb:97 # hash comparison tests comparing an array of multiple hashes with same keys, different values should fail gracefully when using eq()
rspec ./spec/hash_comparison_spec.rb:101 # hash comparison tests comparing an array of multiple hashes with same keys, different values should fail and show me an ambiguous error when using =~
rspec ./spec/hash_comparison_spec.rb:110 # hash comparison tests comparing an array of hashes with different keys, different values should fail gracefully when using ==
rspec ./spec/hash_comparison_spec.rb:114 # hash comparison tests comparing an array of hashes with different keys, different values should fail gracefully when using eq()
rspec ./spec/hash_comparison_spec.rb:118 # hash comparison tests comparing an array of hashes with different keys, different values should fail and show me an ambiguous error when using =~

You will notice that all of the errors except for #15 and #18 contain diffs, while those other two report only ArgumentError: comparison of Hash with Hash failed

Chris Bloom

Whoops, had some test descriptions mixed up in the OP. Fixed.

David Chelimsky
Owner

FYI - #83 is the same issue. These examples can serve as specs for that fix. Should be able to get this fixed for 2.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.