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
  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 # if

  super *expected
end

Instance Method Details

#descriptionObject



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

def description
  desc = super

  # Format hash expectations.
  desc = desc.gsub(/(\S)=>(\S)/, '\1 => \2')

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

  desc
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)


61
62
63
64
65
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 61

def does_not_match?(actual)
  @actual = actual

  perform_match(actual) { |v| !v }
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.



68
69
70
71
72
73
74
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 68

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

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

  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.



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

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

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

  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)


52
53
54
55
56
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 52

def matches?(actual)
  @actual = actual

  perform_match(actual) { |v| v }
end