Sha256: e219b81bdbacd413d00f656848a73d5152e8e4e746613fcffd6127c01d0ae9c6

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

RSpec::Matchers.define :include_keys do |*expected|
  
  match do |actual|
    check_all_present(actual, expected) == []
  end

  failure_message_for_should do |actual|
    "expected key #{check_all_present(actual, expected).first} but did not see it in #{actual.keys.map(&:to_sym)}"
   end

   def check_all_present actual, expected
     keys_we_have = actual.keys.map(&:to_sym)
     expected = [expected] unless expected.is_a?(Array)
     remainder = expected.map(&:to_sym) - keys_we_have
   end
end

RSpec::Matchers.define :match_object do |object, *expected_matching_keys|

  match do |actual|
    check_specified_keys_match(actual, object, expected_matching_keys) == []
  end

  failure_message_for_should do |actual|
    offending_key = check_specified_keys_match(actual, object, expected_matching_keys).first
    "expected match for #{offending_key} but it did not. Expected: #{object.send(offending_key).inspect} but got #{actual[offending_key].inspect}"
  end

  def check_specified_keys_match actual, object, expected_matches
    expected_matches = [expected_matches] unless expected_matches.is_a?(Array)
    expected_matches.map(&:to_sym).map { |key| key unless (object.send(key) == actual[key]) }.compact
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
arid_cache-1.4.4 spec/support/matchers.rb
arid_cache-1.4.3 spec/support/matchers.rb
arid_cache-1.4.2 spec/support/matchers.rb
arid_cache-1.4.1 spec/support/matchers.rb
arid_cache-1.4.0 spec/support/matchers.rb