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.0.0.beta.2 spec/support/rspec_match_hash.rb
pact_broker-2.0.0.beta.1 spec/support/rspec_match_hash.rb
pact_broker-1.17.0 spec/support/rspec_match_hash.rb
pact_broker-1.16.0 spec/support/rspec_match_hash.rb
pact_broker-1.15.0 spec/support/rspec_match_hash.rb
pact_broker-1.14.0 spec/support/rspec_match_hash.rb
pact_broker-1.13.0 spec/support/rspec_match_hash.rb
pact_broker-1.12.0 spec/support/rspec_match_hash.rb
pact_broker-1.11.2 spec/support/rspec_match_hash.rb
pact_broker-1.11.1 spec/support/rspec_match_hash.rb
pact_broker-1.11.0 spec/support/rspec_match_hash.rb
pact_broker-1.10.0 spec/support/rspec_match_hash.rb