Class: Minicrest::SomeEntry

Inherits:
HashEntryMatcher show all
Defined in:
lib/minicrest/hash_entry_matchers.rb

Overview

Matcher that checks if at least one entry in a hash matches a given condition.

Instance Method Summary collapse

Methods inherited from HashEntryMatcher

#hash?, #initialize

Methods inherited from Matcher

#&, #|

Constructor Details

This class inherits a constructor from Minicrest::HashEntryMatcher

Instance Method Details

#descriptionObject



76
77
78
# File 'lib/minicrest/hash_entry_matchers.rb', line 76

def description
  'at least one entry matches condition'
end

#failure_message(actual) ⇒ Object



80
81
82
83
84
# File 'lib/minicrest/hash_entry_matchers.rb', line 80

def failure_message(actual)
  return "expected a Hash, but got #{actual.inspect}" unless hash?(actual)

  'expected at least one entry to match, but none did'
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/minicrest/hash_entry_matchers.rb', line 70

def matches?(actual)
  return false unless hash?(actual)

  actual.any? { |k, v| entry_matches?(k, v) }
end

#negated_failure_message(actual) ⇒ Object



86
87
88
89
90
91
# File 'lib/minicrest/hash_entry_matchers.rb', line 86

def negated_failure_message(actual)
  return "expected a Hash, but got #{actual.inspect}" unless hash?(actual)

  matching_entry = actual.find { |k, v| entry_matches?(k, v) }
  "expected no entries to match, but entry #{matching_entry.inspect} did"
end