Class: RSpec::SleepingKingStudios::Matchers::Core::HaveWriterMatcher

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

Overview

Matcher for testing whether an object has a specific property writer, e.g. responds to :property= and updates the state.

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

Methods inherited from BaseMatcher

#does_not_match?

Constructor Details

#initialize(expected, allow_private: false) ⇒ HaveWriterMatcher

Returns a new instance of HaveWriterMatcher.

Parameters:

  • expected (String, Symbol)

    the property to check for on the actual object

Since:

  • 1.0.0



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

def initialize expected, allow_private: false
  @expected      = expected.to_s.gsub(/=$/,'').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



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

def allow_private?
  !!@allow_private
end

#descriptionString

Generates a description of the matcher expectation.

Returns:

  • (String)

    The matcher description.

Since:

  • 1.0.0



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

def description
  "have writer :#{@expected}"
end

#failure_messageObject

See Also:

Since:

  • 1.0.0



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

def failure_message
  "expected #{@actual.inspect} to respond to :#{@expected}="\
  ", but did not respond to :#{@expected}="
end

#failure_message_when_negatedObject



58
59
60
61
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_writer_matcher.rb', line 58

def failure_message_when_negated
  "expected #{@actual.inspect} not to respond to :#{@expected}="\
  ", but responded to :#{@expected}="
end

#matches?(actual) ⇒ Boolean

Checks if the object responds to :expected=. Additionally, if a value expectation is set, assigns the value via :expected= and compares the subsequent value to the specified value using :expected or the block provided to #with.

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



45
46
47
48
49
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_writer_matcher.rb', line 45

def matches? actual
  super

  responds_to_writer?(:allow_private => allow_private?)
end