Sha256: 171bdf0bb2e9f7c5ce37f907d2e6d20b28afee69d8433055aa831e166e548a5b

Contents?: true

Size: 625 Bytes

Versions: 52

Compression:

Stored size: 625 Bytes

Contents

# checks if actual contains all the key-value pairs that expected does,
# ignoring order for any child arrays
RSpec::Matchers.define :contain_hash do |expected|
  match do |actual|
    contains_hash?(expected, actual)
  end
end


def contains_hash?(expected, actual)
  expected.all? do |key, value|
    unordered_match(actual[key], value)
  end
end

def unordered_match(expected, actual)
  case
  when [expected, actual].all?{|val| val.is_a? Array }
    expected.all?{|el| actual.include? el }
  when [expected, actual].all?{|val| val.is_a? Hash }
    contains_hash?(expected, actual)
  else
    expected == actual
  end
end

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
pact_broker-2.15.0 spec/support/rspec_match_hash.rb
pact_broker-2.14.0 spec/support/rspec_match_hash.rb
pact_broker-2.13.1 spec/support/rspec_match_hash.rb
pact_broker-2.13.0 spec/support/rspec_match_hash.rb
pact_broker-2.12.0 spec/support/rspec_match_hash.rb
pact_broker-2.11.0 spec/support/rspec_match_hash.rb
pact_broker-2.10.0 spec/support/rspec_match_hash.rb
pact_broker-2.9.0 spec/support/rspec_match_hash.rb
pact_broker-2.9.0.beta.5 spec/support/rspec_match_hash.rb
pact_broker-2.8.0.beta.5 spec/support/rspec_match_hash.rb
pact_broker-2.8.0.beta.4 spec/support/rspec_match_hash.rb
pact_broker-2.7.0.beta.3 spec/support/rspec_match_hash.rb
pact_broker-2.7.0.beta.2 spec/support/rspec_match_hash.rb
pact_broker-2.7.0.beta.1 spec/support/rspec_match_hash.rb
pact_broker-2.6.0 spec/support/rspec_match_hash.rb
pact_broker-2.5.1 spec/support/rspec_match_hash.rb
pact_broker-2.5.0 spec/support/rspec_match_hash.rb
pact_broker-2.4.2 spec/support/rspec_match_hash.rb
pact_broker-2.4.1 spec/support/rspec_match_hash.rb
pact_broker-2.4.0 spec/support/rspec_match_hash.rb