Class: RSpec::SleepingKingStudios::Matchers::Core::HavePredicateMatcher

Inherits:
BaseMatcher
  • Object
show all
Includes:
Shared::MatchProperty
Defined in:
lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb

Overview

Matcher for testing whether an object has a specific predicate, e.g. responds to :property?.

Since:

  • 2.2.0

Constant Summary

Constants included from Description

Description::DEFAULT_EXPECTED_ITEMS

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual

Instance Method Summary collapse

Methods inherited from BaseMatcher

#does_not_match?

Constructor Details

#initialize(expected) ⇒ HavePredicateMatcher

Returns a new instance of HavePredicateMatcher.

Parameters:

  • expected (String, Symbol)

    The predicate to check for on the actual object.

Since:

  • 2.2.0



25
26
27
28
29
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 25

def initialize expected
  @expected = expected.to_s.gsub(/\?$/, '').intern

  apply_boolean_expectation if strict_matching?
end

Instance Method Details

#descriptionString

Generates a description of the matcher expectation.

Returns:

  • (String)

    The matcher description.

Since:

  • 2.2.0



19
20
21
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 19

def description
  "have predicate :#{@expected}"
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.

Since:

  • 2.2.0



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 63

def failure_message
  message = "expected #{@actual.inspect} to respond to :#{@expected}?"
  message << " and return #{value_to_string}" if @value_set

  if !@matches_predicate
    message << ", but did not respond to :#{@expected}?"
  elsif !@matches_predicate_value
    message << ", but returned #{@actual.send(:"#{@expected}?").inspect}"
  end # if-elsif

  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.

Since:

  • 2.2.0



77
78
79
80
81
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 77

def failure_message_when_negated
  message = "expected #{@actual.inspect} not to respond to :#{@expected}?"
  message << " and return #{value_to_string}" if @value_set
  message
end

#matches?(actual) ⇒ Boolean

Checks if the object responds to #expected?. Additionally, if a value expectation is set, compares the value of #expected to the specified value.

Parameters:

  • actual (Object)

    The object to check.

Returns:

  • (Boolean)

    true If the object responds to #expected and matches the value expectation (if any); otherwise false.

Since:

  • 2.2.0



39
40
41
42
43
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 39

def matches? actual
  super

  responds_to_predicate? && matches_predicate_value?
end

#with_value(value) ⇒ HaveReaderMatcher Also known as: with

Sets a value expectation. The matcher will compare the value from #property? with the specified value.

Parameters:

  • value (Object)

    The value to compare.

Returns:

Since:

  • 2.2.0



51
52
53
54
55
56
57
58
59
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_predicate_matcher.rb', line 51

def with_value value
  if strict_matching? && !(value === true || value === false)
    raise ArgumentError.new 'predicate must return true or false'
  end # if

  @value = value
  @value_set = true
  self
end