class RSpec::SleepingKingStudios::Matchers::Core::HaveReaderMatcher

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

@since 1.0.0

Public Class Methods

new(expected, allow_private: false) click to toggle source

@param [String, Symbol] expected The property to check for on the actual

object.

@param [Boolean] allow_private If true, then the matcher will match a

protected or private reader method. Defaults to false.
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 19
def initialize expected, allow_private: false
  @expected      = expected.intern
  @allow_private = allow_private
end

Public Instance Methods

allow_private?() click to toggle source

@return [Boolean] True if the matcher matches private reader methods,

otherwise false.
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 26
def allow_private?
  !!@allow_private
end
description() click to toggle source

(see BaseMatcher#description)

# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 31
def description
  value_message = value_to_string
  "have reader :#{@expected}#{@value_set ? " with value #{value_message}" : ''}"
end
does_not_match?(actual) click to toggle source

(see BaseMatcher#does_not_match?)

# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 37
def does_not_match? actual
  super

  matches_reader?(:none?)
end
failure_message() click to toggle source

(see BaseMatcher#failure_message)

# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 71
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_negated() click to toggle source

(see BaseMatcher#failure_message_when_negated)

# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 85
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) click to toggle source

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

@param [Object] actual The object to check.

@return [Boolean] true If the object responds to expected and matches

the value expectation (if any); otherwise false.
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 51
def matches? actual
  super

  matches_reader?(:all?)
end
with(value) click to toggle source

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

@param [Object] value The value to compare.

@return [HaveReaderMatcher] self

# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 63
def with value
  @value = value
  @value_set = true
  self
end
Also aliased as: with_value
with_value(value)
Alias for: with

Private Instance Methods

matches_reader?(filter) click to toggle source
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 99
def matches_reader? filter
  [ responds_to_reader?(:allow_private => allow_private?),
    matches_reader_value?(:allow_private => allow_private?)
  ].send(filter) { |bool| bool }
end