Class: RSpec::SleepingKingStudios::Matchers::BuiltIn::IncludeMatcher

Inherits:
Matchers::BuiltIn::Include
  • Object
show all
Includes:
Description
Defined in:
lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb

Overview

Extensions to the built-in RSpec #include matcher.

Constant Summary

Constants included from Description

Description::DEFAULT_EXPECTED_ITEMS

Instance Method Summary collapse

Constructor Details

#initialize(*expected) {|item| ... } ⇒ IncludeMatcher

Returns a new instance of IncludeMatcher.

Parameters:

  • expected (Array<Hash, Proc, Object>)

    the items expected to be matched by the actual object

Yields:

  • If a block is provided, the block is converted to a proc and appended to the item expectations.

Yield Parameters:

  • item (Object)

    An item from the actual object; yield(item) should return true if and only if the item matches the desired predicate.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 19

def initialize(*expected, &block) # rubocop:disable Metrics/MethodLength
  if block_given?
    SleepingKingStudios::Tools::CoreTools
      .deprecate('IncludeMatcher with a block')

    expected << block
  end

  if expected.empty? && !allow_empty_matcher?
    raise ArgumentError,
      'must specify an item expectation',
      caller
  end

  super(*expected)
end

Instance Method Details

#descriptionObject



37
38
39
40
41
42
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 37

def description
  desc = super

  # Replace processed block expectation stub with proper description.
  desc.gsub ':__block_comparison__', 'an item matching the block'
end

#does_not_match?(actual) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 56

def does_not_match?(actual) # rubocop:disable Naming/PredicatePrefix
  @actual = actual

  perform_match(actual, &:!)
end

#failure_messageObject

Message for when the object does not match, but was expected to. Make sure to always call #matches? first to set up the matcher state.



63
64
65
66
67
68
69
70
71
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 63

def failure_message
  message = super.sub ':__block_comparison__', 'an item matching the block'

  unless actual.respond_to?(:include?) || message =~ /does not respond to/
    message << ', but it does not respond to `include?`'
  end

  message
end

#failure_message_when_negatedObject

Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.



74
75
76
77
78
79
80
81
82
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 74

def failure_message_when_negated
  message = super.sub ':__block_comparison__', 'an item matching the block'

  unless actual.respond_to?(:include?) || message =~ /does not respond to/
    message << ', but it does not respond to `include?`'
  end

  message
end

#matches?(actual) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 47

def matches?(actual)
  @actual = actual

  perform_match(actual) { |v| v }
end