Class: RSpec::SleepingKingStudios::Matchers::Core::HaveChangedMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb

Overview

Matcher for testing the change in a value.

Since:

  • 2.4.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

#initializeHaveChangedMatcher

Returns a new instance of HaveChangedMatcher.

Since:

  • 2.4.0



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

def initialize
  super

  @expected_initial_value = DEFAULT_VALUE
  @expected_current_value = DEFAULT_VALUE
end

Instance Method Details

#by(difference) ⇒ HaveChangedMatcher

Creates an difference expectation between the initial and current values. The matcher will subtract the current value from the initial value and compare the result with the specified value.

Parameters:

  • difference (Object)

    The expected difference between the initial value and the current value.

Returns:

Since:

  • 2.4.0



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

def by(difference)
  @expected_difference = difference

  self
end

#descriptionObject

Since:

  • 2.4.0



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb', line 37

def description
  desc = 'have changed'

  unless @expected_initial_value == DEFAULT_VALUE
    desc << " from #{@expected_initial_value.inspect}"
  end

  if @expected_difference
    desc << " by #{@expected_difference.inspect}"
  end

  unless @expected_current_value == DEFAULT_VALUE
    desc << " to #{@expected_current_value.inspect}"
  end

  desc
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:

  • 2.4.0



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb', line 56

def does_not_match?(actual)
  @actual = actual

  unless actual.is_a?(RSpec::SleepingKingStudios::Support::ValueSpy)
    raise ArgumentError, 'You must pass a value spy to `expect`.'
  end

  unless @expected_current_value == DEFAULT_VALUE
    raise NotImplementedError,
      "`expect().not_to have_changed().to()` is not supported"
  end

  if @expected_difference
    raise NotImplementedError,
      "`expect().not_to have_changed().by()` is not supported"
  end

  match_initial_value? && !value_has_changed?
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:

  • 2.4.0



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb', line 77

def failure_message
  unless @match_initial_value.nil? || @match_initial_value
    return "expected #{value_spy.description} to have initially " \
      "been #{@expected_initial_value.inspect}, but was " \
      "#{value_spy.initial_inspect}"
  end

  message = "expected #{value_spy.description} to have changed"

  if @expected_difference
    message << " by #{@expected_difference.inspect}"
  end

  unless @expected_current_value == DEFAULT_VALUE
    message << " to #{@expected_current_value.inspect}"
  end

  unless @match_difference.nil? || @match_difference
    return message << ", but was changed by #{@actual_difference.inspect}"
  end

  unless @match_current_value.nil? || @match_current_value
    return message << ", but is now #{current_value.inspect}"
  end

  message << ", but is still #{current_value.inspect}"

  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:

  • 2.4.0



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb', line 108

def failure_message_when_negated
  unless @match_initial_value.nil? || @match_initial_value
    return "expected #{value_spy.description} to have initially " \
      "been #{@expected_initial_value.inspect}, but was " \
      "#{value_spy.initial_inspect}"
  end

  message = "expected #{value_spy.description} not to have changed"

  message <<
    ", but did change from #{value_spy.initial_inspect} to " <<
    current_value.inspect

  message
end

#from(value) ⇒ HaveChangedMatcher

Creates an expectation on the initial value. The matcher will compare the initial value from the value spy with the specified value.

Parameters:

  • value (Object)

    The expected initial value.

Returns:

Since:

  • 2.4.0



130
131
132
133
134
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb', line 130

def from(value)
  @expected_initial_value = value

  self
end

#matches?(actual) ⇒ Boolean

Checks if the observed value has changed.

Parameters:

Returns:

  • (Boolean)

    True if the observed value has changed, otherwise false.

Raises:

  • ArgumentError unless the actual object is a value spy.

Since:

  • 2.4.0



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb', line 144

def matches?(actual)
  super

  unless actual.is_a?(RSpec::SleepingKingStudios::Support::ValueSpy)
    raise ArgumentError, 'You must pass a value spy to `expect`.'
  end

  match_initial_value? &&
    value_has_changed? &&
    match_current_value? &&
    match_difference?
end

#to(value) ⇒ HaveChangedMatcher

Creates an expectation on the current value. The matcher will compare the current value from the value spy with the specified value.

Parameters:

  • value (Object)

    The expected current value.

Returns:

Since:

  • 2.4.0



163
164
165
166
167
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb', line 163

def to(value)
  @expected_current_value = value

  self
end