Class: Matchi::Change::FromTo

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/change/from_to.rb

Overview

Passes when the observed value is the old value before the block and the new value after it, both compared like Eq. The block is always evaluated; a wrong initial value is a failure of the expectation, like a wrong final value.

Examples:

s = +"a"
Matchi::Change.new(s, :to_s).from("a").to("b").match? { s.replace("b") } # => true

Instance Method Summary collapse

Constructor Details

#initialize(old_value, new_value, &state) ⇒ FromTo

Returns a new instance of FromTo.

Parameters:

  • old_value (#eql?) —

    the expected value before the block

  • new_value (#eql?) —

    the expected value after the block

Yield Returns:

  • (Object) —

    the observed value

Raises:

  • (ArgumentError) —

    when no block is given



20
21
22
23
24
25
26
# File 'lib/matchi/change/from_to.rb', line 20

def initialize(old_value, new_value, &state)
  raise ::ArgumentError, "a block must be provided" unless block_given?

  @old_value = old_value
  @new_value = new_value
  @state = state
end

Instance Method Details

#match? ⇒ Boolean

Yield Returns:

  • (Object) —

    ignored, the block is evaluated for its effect

Returns:

  • (Boolean)

Raises:

  • (ArgumentError) —

    when no block is given



31
32
33
34
35
36
37
38
# File 'lib/matchi/change/from_to.rb', line 31

def match?
  raise ::ArgumentError, "a block must be provided" unless block_given?

  initial = Eq.eql?(@old_value, @state.call)
  yield

  initial && Eq.eql?(@new_value, @state.call)
end

#to_s ⇒ String

Returns:

  • (String)


41
42
43
# File 'lib/matchi/change/from_to.rb', line 41

def to_s
  "change from #{@old_value.inspect} to #{@new_value.inspect}"
end