Class: Matchi::Change::From::To
- Inherits:
-
Object
- Object
- Matchi::Change::From::To
- Defined in:
- lib/matchi/change/from/to.rb
Overview
Value transition matcher that verifies both initial and final states of an operation.
This matcher ensures that a value not only changes to an expected final state but also starts from a specific initial state. This is particularly useful when testing state transitions where both the starting and ending conditions are important, such as in workflow systems, state machines, or data transformations.
Instance Method Summary collapse
-
#initialize(expected_init, expected_new_value, &state) ⇒ To
constructor
Initialize the matcher with expected initial and final values.
-
#match? { ... } ⇒ Boolean
Verifies both initial and final states during a transition.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize(expected_init, expected_new_value, &state) ⇒ To
Initialize the matcher with expected initial and final values.
73 74 75 76 77 78 79 |
# File 'lib/matchi/change/from/to.rb', line 73 def initialize(expected_init, expected_new_value, &state) raise ::ArgumentError, "a block must be provided" unless block_given? @expected_init = expected_init @expected = expected_new_value @state = state end |
Instance Method Details
#match? { ... } ⇒ Boolean
Verifies both initial and final states during a transition.
This method first checks if the initial state matches the expected value, then executes the provided block and verifies the final state. The match fails if either the initial or final state doesn’t match expectations.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/matchi/change/from/to.rb', line 105 def match? raise ::ArgumentError, "a block must be provided" unless block_given? value_before = @state.call return false unless @expected_init == value_before yield value_after = @state.call @expected == value_after end |
#to_s ⇒ String
Returns a human-readable description of the matcher.
126 127 128 |
# File 'lib/matchi/change/from/to.rb', line 126 def to_s "change from #{@expected_init.inspect} to #{@expected.inspect}" end |