class RSpec::SleepingKingStudios::Matchers::Core::HavePropertyMatcher
Matcher for testing whether an object has a specific property, e.g. responds to property and property= and has the specified value for property.
@since 1.0.0
Public Class Methods
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb, line 21 def initialize expected, allow_private: false @expected = expected.intern @allow_private = allow_private end
@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.
Public Instance Methods
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb, line 28 def allow_private? !!@allow_private end
@return [Boolean] True if the matcher matches private reader methods,
otherwise false.
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb, line 33 def description value_message = value_to_string "have property :#{@expected}#{@value_set ? " with value #{value_message}" : ''}" end
(see BaseMatcher#description
)
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb, line 39 def does_not_match? actual @actual = actual matches_property?(:none?) end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb, line 73 def failure_message methods = [] methods << ":#{@expected}" unless @matches_reader methods << ":#{@expected}=" unless @matches_writer message = "expected #{@actual.inspect} to respond to :#{@expected} and :#{@expected}=" message << " and return #{value_to_string}" if @value_set errors = [] errors << "did not respond to #{methods.join " or "}" unless methods.empty? if @matches_reader errors << "returned #{@actual.send(@expected).inspect}" unless @matches_reader_value || !@value_set end # if message << ", but #{errors.join(" and ")}" message end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb, line 93 def failure_message_when_negated methods = [] methods << ":#{@expected}" if @matches_reader methods << ":#{@expected}=" if @matches_writer message = "expected #{@actual.inspect} not to respond to :#{@expected} or :#{@expected}=" message << " and return #{value_to_string}" if @value_set errors = [] errors << "responded to #{methods.join " and "}" unless methods.empty? errors << "returned #{@actual.send(@expected).inspect}" if @matches_reader_value message << ", but #{errors.join(" and ")}" message end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb, line 53 def matches? actual super matches_property?(:all?) end
Checks if the object responds to expected and expected=. Additionally, if a value expectation is set, compares the result of calling :expected to the value.
@param [Object] actual The object to check.
@return [Boolean] true If the object responds to expected and
#expected= and matches the value expectation (if any); otherwise false.
RSpec::SleepingKingStudios::Matchers::BaseMatcher#matches?
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb, line 65 def with value @value = value @value_set = true self end
Sets a value expectation. The matcher will compare the value to the result of calling property.
@param [Object] value The value to set and then compare.
@return [HavePropertyMatcher] self
Private Instance Methods
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb, line 111 def matches_property? filter [ responds_to_reader?(:allow_private => allow_private?), responds_to_writer?(:allow_private => allow_private?), matches_reader_value?(:allow_private => allow_private?) ].send(filter) { |bool| bool } end