Class: Matchi::Change::To

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

Overview

Passes when the observed value is not the expected value before the block and is the expected value after it: the value must change to it. The block is always evaluated.

Examples:

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

Instance Method Summary collapse

Constructor Details

#initialize(expected, &state) ⇒ To

Returns a new instance of To.

Parameters:

  • expected (#eql?) —

    the expected value after the block

Yield Returns:

  • (Object) —

    the observed value

Raises:

  • (ArgumentError) —

    when no block is given



18
19
20
21
22
23
# File 'lib/matchi/change/to.rb', line 18

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

  @expected = expected
  @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



28
29
30
31
32
33
34
35
# File 'lib/matchi/change/to.rb', line 28

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

  already = Eq.eql?(@expected, @state.call)
  yield

  !already && Eq.eql?(@expected, @state.call)
end

#to_s ⇒ String

Returns:

  • (String)


38
39
40
# File 'lib/matchi/change/to.rb', line 38

def to_s
  "change to #{@expected.inspect}"
end