Class: RSpec::SleepingKingStudios::Matchers::Core::HavePropertyMatcher

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

Overview

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

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) ⇒ HavePropertyMatcher

Returns a new instance of HavePropertyMatcher.

Parameters:

  • expected (String, Symbol)

    The property to check for on the actual object.

Since:

  • 1.0.0



24
25
26
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 24

def initialize expected
  @expected = expected.intern
end

Instance Method Details

#descriptionObject

Since:

  • 1.0.0



17
18
19
20
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 17

def description
  value_message = value_to_string
  "have property :#{@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



29
30
31
32
33
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 29

def does_not_match? actual
  super

  matches_property?(: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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 63

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

#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



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

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

#matches?(actual) ⇒ Boolean

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.

Parameters:

  • actual (Object)

    The object to check.

Returns:

  • (Boolean)

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

Since:

  • 1.0.0



43
44
45
46
47
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 43

def matches? actual
  super

  matches_property?(:all?)
end

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

Sets a value expectation. The matcher will compare the value to the result of calling #property.

Parameters:

  • value (Object)

    The value to set and then compare.

Returns:

Since:

  • 1.0.0



55
56
57
58
59
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_property_matcher.rb', line 55

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