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
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 19

def initialize *expected, &block
  expected << block if block_given?

  super *expected
end

Instance Method Details

#descriptionObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 26

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)


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

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.



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

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.



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

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)


41
42
43
44
45
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/include_matcher.rb', line 41

def matches?(actual)
  @actual = actual

  perform_match(actual) { |v| v }
end