Class: Minicrest::NoItems

Inherits:
CollectionItemMatcher show all
Defined in:
lib/minicrest/no_items.rb

Overview

Matcher that checks if no items in a collection match a given matcher.

Examples:

Basic usage

no_items(descends_from(String)).matches?([1, 2, 3])  # => true
no_items(descends_from(String)).matches?([1, 'two', 3])  # => false

See Also:

Instance Method Summary collapse

Methods inherited from CollectionItemMatcher

#collection?, #initialize

Methods inherited from Matcher

#&, #|

Constructor Details

This class inherits a constructor from Minicrest::CollectionItemMatcher

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    description



26
27
28
# File 'lib/minicrest/no_items.rb', line 26

def description
  "no items are #{@item_matcher.description}"
end

#failure_message(actual) ⇒ String

Returns the failure message when the match fails.

Parameters:

  • actual (Enumerable)

    the collection that was checked

Returns:

  • (String)

    failure message showing matching item



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/minicrest/no_items.rb', line 34

def failure_message(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

#matches?(actual) ⇒ Boolean

Checks if no items match the item matcher.

Parameters:

  • actual (Enumerable)

    the collection to check

Returns:

  • (Boolean)

    true if no items match



17
18
19
20
21
# File 'lib/minicrest/no_items.rb', line 17

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

  actual.none? { |item| @item_matcher.matches?(item) }
end

#negated_failure_message(actual) ⇒ String

Returns the failure message when a negated match fails.

Parameters:

  • actual (Enumerable)

    the collection that was checked

Returns:

  • (String)

    message indicating no items matched



50
51
52
53
54
55
56
57
# File 'lib/minicrest/no_items.rb', line 50

def negated_failure_message(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