Class: RSpec::SleepingKingStudios::Matchers::Core::HaveReaderMatcher

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

Overview

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

Since:

  • 1.0.0

Constant Summary

Constants included from Description

Description::DEFAULT_EXPECTED_ITEMS

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual

Instance Method Summary collapse

Constructor Details

#initialize(expected, allow_private: false) ⇒ HaveReaderMatcher

Returns a new instance of HaveReaderMatcher.

Parameters:

  • expected (String, Symbol)

    The property to check for on the actual object.

  • allow_private (Boolean) (defaults to: false)

    If true, then the matcher will match a protected or private reader method. Defaults to false.

Since:

  • 1.0.0



20
21
22
23
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 20

def initialize expected, allow_private: false
  @expected      = expected.intern
  @allow_private = allow_private
end

Instance Method Details

#allow_private?Boolean

Returns True if the matcher matches private reader methods, otherwise false.

Returns:

  • (Boolean)

    True if the matcher matches private reader methods, otherwise false.

Since:

  • 1.0.0



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

def allow_private?
  !!@allow_private
end

#descriptionObject

Since:

  • 1.0.0



32
33
34
35
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 32

def description
  value_message = value_to_string
  "have reader :#{@expected}#{@value_set ? " with value #{value_message}" : ''}"
end

#does_not_match?(actual) ⇒ Boolean

Inverse of #matches? method.

Parameters:

  • actual (Object)

    the object to test against the matcher

Returns:

  • (Boolean)

    false if the object matches, otherwise true

See Also:

Since:

  • 1.0.0



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

def does_not_match? actual
  super

  matches_reader?(:none?)
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:

  • 1.0.0



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 72

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

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

  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:

  • 1.0.0



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 86

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

  errors = []
  errors << "responded to :#{@expected}" if @matches_reader
  errors << "returned #{@actual.send(@expected).inspect}" if @matches_reader_value

  message << ", but #{errors.join(" and ")}"
  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:

  • 1.0.0



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

def matches? actual
  super

  matches_reader?(:all?)
end

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

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:

  • 1.0.0



64
65
66
67
68
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb', line 64

def with value
  @value = value
  @value_set = true
  self
end