Class: Matchi::Change::To

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

Overview

Final state matcher that verifies if a method returns an expected value after a change.

This matcher focuses on the final state of an object, verifying that a method call returns an expected value after executing a block of code. Unlike the full from/to matcher, it only cares about the end result, not the initial state.

Examples:

Basic string transformation

text = "hello"
matcher = Matchi::Change::To.new("HELLO") { text.to_s }
matcher.match? { text.upcase! }   # => true
matcher.match? { text.reverse! }  # => false

Number calculations

counter = 0
matcher = Matchi::Change::To.new(5) { counter }
matcher.match? { counter = 5 }    # => true
matcher.match? { counter += 1 }   # => false

With object attributes

class User
  attr_accessor :status
  def initialize(status)
    @status = status
  end
end

user = User.new(:pending)
matcher = Matchi::Change::To.new(:active) { user.status }
matcher.match? { user.status = :active }  # => true

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(expected, &state) ⇒ To

Initialize the matcher with an expected new value and a state block.

Examples:

With simple value

To.new("test") { object.value }

With complex calculation

To.new(100) { object.items.count }

Parameters:

  • expected (#eql?)

    The expected final value

  • state (Proc)

    Block that retrieves the value to check

Raises:

  • (ArgumentError)

    if no state block is provided



54
55
56
57
58
59
# File 'lib/matchi/change/to.rb', line 54

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

  @expected = expected
  @state = state
end

Instance Method Details

#match? { ... } ⇒ Boolean

Checks if the state block returns the expected value after executing the provided block.

This method executes the provided block and then checks if the state block returns the expected value. It only cares about the final state, not any intermediate values or the initial state.

Examples:

Basic usage

text = "hello"
matcher = To.new("HELLO") { text.to_s }
matcher.match? { text.upcase! }  # => true

With method chaining

array = [1, 2, 3]
matcher = To.new(6) { array.sum }
matcher.match? { array.map! { |x| x * 2 } }  # => false

Yields:

  • Block that should cause the state change

Yield Returns:

  • (Object)

    The result of the block (not used)

Returns:

  • (Boolean)

    true if the final state matches the expected value

Raises:

  • (ArgumentError)

    if no block is provided



85
86
87
88
89
90
91
92
# File 'lib/matchi/change/to.rb', line 85

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

  yield
  value_after = @state.call

  @expected.eql?(value_after)
end

#to_sString

Returns a human-readable description of the matcher.

Examples:

To.new("test").to_s # => 'change to "test"'

Returns:

  • (String)

    A string describing what this matcher verifies



102
103
104
# File 'lib/matchi/change/to.rb', line 102

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