Class: RSpec::SleepingKingStudios::Matchers::Core::HaveChangedMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::SleepingKingStudios::Matchers::Core::HaveChangedMatcher
- Defined in:
- lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb
Overview
Matcher for testing the change in a value.
Constant Summary
Constants included from Description
Description::DEFAULT_EXPECTED_ITEMS
Instance Attribute Summary
Attributes inherited from BaseMatcher
Instance Method Summary collapse
-
#by(difference) ⇒ HaveChangedMatcher
Creates an difference expectation between the initial and current values.
- #description ⇒ Object
-
#does_not_match?(actual) ⇒ Boolean
Inverse of #matches? method.
-
#failure_message ⇒ Object
Message for when the object does not match, but was expected to.
-
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to.
-
#from(value) ⇒ HaveChangedMatcher
Creates an expectation on the initial value.
-
#initialize ⇒ HaveChangedMatcher
constructor
A new instance of HaveChangedMatcher.
-
#matches?(actual) ⇒ Boolean
Checks if the observed value has changed.
-
#to(value) ⇒ HaveChangedMatcher
Creates an expectation on the current value.
Constructor Details
#initialize ⇒ HaveChangedMatcher
Returns a new instance of HaveChangedMatcher.
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.
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 |
#description ⇒ Object
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.
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_message ⇒ Object
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.
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 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 = "expected #{value_spy.description} to have changed" if @expected_difference << " by #{@expected_difference.inspect}" end unless @expected_current_value == DEFAULT_VALUE << " to #{@expected_current_value.inspect}" end unless @match_difference.nil? || @match_difference return << ", but was changed by #{@actual_difference.inspect}" end unless @match_current_value.nil? || @match_current_value return << ", but is now #{current_value.inspect}" end << ", but is still #{current_value.inspect}" end |
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.
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 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 = "expected #{value_spy.description} not to have changed" << ", but did change from #{value_spy.initial_inspect} to " << current_value.inspect 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.
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.
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.
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 |