Class: Matchi::Change

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/change.rb,
lib/matchi/change/by.rb,
lib/matchi/change/to.rb,
lib/matchi/change/from.rb,
lib/matchi/change/from/to.rb,
lib/matchi/change/by_at_most.rb,
lib/matchi/change/by_at_least.rb

Overview

Wraps the target of a change matcher.

Defined Under Namespace

Classes: By, ByAtLeast, ByAtMost, From, To

Instance Method Summary collapse

Constructor Details

#initialize(object, method) ⇒ Change

Initialize a wrapper of the change matcher with an object and the name of one of its methods.

Examples:

require "matchi/change"

Matchi::Change.new("foo", :to_s)

Parameters:

  • object (#object_id)

    An object.

  • method (Symbol)

    The name of a method.

Raises:

  • (::ArgumentError)


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

def initialize(object, method, ...)
  raise ::ArgumentError, "method must be a Symbol" unless method.is_a?(::Symbol)
  raise ::ArgumentError, "object must respond to method" unless object.respond_to?(method)

  @state = -> { object.send(method, ...) }
end

Instance Method Details

#by(delta) ⇒ #match?

Specifies the delta of the expected change.

Examples:

require "matchi/change"

object = []

change_wrapper = Matchi::Change.new(object, :length)
change_wrapper.by(1)

Parameters:

  • delta (#object_id)

    The delta of the expected change.

Returns:

  • (#match?)

    A *change by* matcher.



76
77
78
# File 'lib/matchi/change.rb', line 76

def by(delta)
  By.new(delta, &@state)
end

#by_at_least(minimum_delta) ⇒ #match?

Specifies a minimum delta of the expected change.

Examples:

require "matchi/change"

object = []

change_wrapper = Matchi::Change.new(object, :length)
change_wrapper.by_at_least(1)

Parameters:

  • minimum_delta (#object_id)

    The minimum delta of the expected change.

Returns:

  • (#match?)

    A *change by at least* matcher.



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

def by_at_least(minimum_delta)
  ByAtLeast.new(minimum_delta, &@state)
end

#by_at_most(maximum_delta) ⇒ #match?

Specifies a maximum delta of the expected change.

Examples:

require "matchi/change"

object = []

change_wrapper = Matchi::Change.new(object, :length)
change_wrapper.by_at_most(1)

Parameters:

  • maximum_delta (#object_id)

    The maximum delta of the expected change.

Returns:

  • (#match?)

    A *change by at most* matcher.



59
60
61
# File 'lib/matchi/change.rb', line 59

def by_at_most(maximum_delta)
  ByAtMost.new(maximum_delta, &@state)
end

#from(old_value) ⇒ #match?

Specifies the original value.

Examples:

require "matchi/change"

change_wrapper = Matchi::Change.new("foo", :to_s)
change_wrapper.from("foo")

Parameters:

  • old_value (#object_id)

    The original value.

Returns:

  • (#match?)

    A *change from* wrapper.



91
92
93
# File 'lib/matchi/change.rb', line 91

def from(old_value)
  From.new(old_value, &@state)
end

#to(new_value) ⇒ #match?

Specifies the new value to expect.

Examples:

require "matchi/change"

change_wrapper = Matchi::Change.new("foo", :to_s)
change_wrapper.to("FOO")

Parameters:

  • new_value (#object_id)

    The new value to expect.

Returns:

  • (#match?)

    A *change to* matcher.



106
107
108
# File 'lib/matchi/change.rb', line 106

def to(new_value)
  To.new(new_value, &@state)
end