Class: Minicrest::SomeItems
- Inherits:
-
CollectionItemMatcher
- Object
- Matcher
- CollectionItemMatcher
- Minicrest::SomeItems
- Defined in:
- lib/minicrest/some_items.rb
Overview
Matcher that checks if at least one item in a collection matches a given matcher.
Instance Method Summary collapse
-
#description ⇒ String
Returns a description of what this matcher expects.
-
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
-
#matches?(actual) ⇒ Boolean
Checks if at least one item matches the item matcher.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
Methods inherited from CollectionItemMatcher
Methods inherited from Matcher
Constructor Details
This class inherits a constructor from Minicrest::CollectionItemMatcher
Instance Method Details
#description ⇒ String
Returns a description of what this matcher expects.
26 27 28 |
# File 'lib/minicrest/some_items.rb', line 26 def description "some items are #{@item_matcher.description}" end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
34 35 36 37 38 39 40 41 |
# File 'lib/minicrest/some_items.rb', line 34 def (actual) return "expected a collection, but got #{actual.inspect}" unless collection?(actual) <<~MSG.chomp expected some items to be #{@item_matcher.description} but no items matched MSG end |
#matches?(actual) ⇒ Boolean
Checks if at least one item matches the item matcher.
17 18 19 20 21 |
# File 'lib/minicrest/some_items.rb', line 17 def matches?(actual) return false unless collection?(actual) actual.any? { |item| @item_matcher.matches?(item) } end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/minicrest/some_items.rb', line 47 def (actual) return "expected a collection, but got #{actual.inspect}" unless collection?(actual) matching_index = actual.find_index { |item| @item_matcher.matches?(item) } matching_item = actual.to_a[matching_index] <<~MSG.chomp expected no items to be #{@item_matcher.description} but item at index #{matching_index} matched: #{matching_item.inspect} MSG end |