Class: Matchi::Change

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/change.rb,
lib/matchi/change/to.rb,
lib/matchi/change/from.rb,
lib/matchi/change/delta.rb,
lib/matchi/change/from_to.rb

Overview

State change matcher: passes when evaluating the block changes the value returned by a public method of an object.

The observed method is called with public_send before and after the block, and the two values are compared with eql?. The observed method must return a value, not the mutated object itself: when it returns the very same unfrozen object twice, the comparison cannot tell whether that object changed, and match? raises instead of answering false. attr_reader :items with items << x is that case; observe items.length or use #to, which compares with the expected value. A frozen object returned twice counts as an unchanged value.

The refinements #by, #by_at_least, #by_at_most, #from and #to build more specific matchers. Change, by, by_at_least and by_at_most compare values produced by the observed object, so they depend on its honesty, like Predicate. to and from … to compare with the expected value, like Eq. Whatever the observed method raises propagates.

Examples:

list = []
Matchi::Change.new(list, :length).match? { list << 1 }       # => true
Matchi::Change.new(list, :length).by(1).match? { list << 1 } # => true

Defined Under Namespace

Classes: Delta, From, FromTo, To

Instance Method Summary collapse

Constructor Details

#initialize(object, method, *args, **kwargs, &block) ⇒ Change

Returns a new instance of Change.

Parameters:

  • object (Object) —

    the object to observe

  • method (Symbol, String) —

    a public method of that object

  • args (Array) —

    positional arguments for the method

  • kwargs (Hash) —

    keyword arguments for the method

  • block (Proc, nil) —

    a block for the method

Raises:

  • (ArgumentError) —

    when method is neither a Symbol nor a String



39
40
41
42
43
44
45
# File 'lib/matchi/change.rb', line 39

def initialize(object, method, *args, **kwargs, &block)
  raise ::ArgumentError, "method must be a Symbol or a String (got: #{method.inspect})" unless name?(method)

  @object = object
  @method = method.to_sym
  @state = -> { ::Kernel.instance_method(:public_send).bind_call(object, @method, *args, **kwargs, &block) }
end

Instance Method Details

#by(delta) ⇒ Matchi::Change::Delta

Parameters:

  • delta (Numeric) —

    the exact expected difference, negative allowed

Returns:

Raises:

  • (ArgumentError) —

    when delta is not a finite real Numeric



73
74
75
# File 'lib/matchi/change.rb', line 73

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

#by_at_least(delta) ⇒ Matchi::Change::Delta

Parameters:

  • delta (Numeric) —

    the smallest acceptable increase, zero or positive

Returns:

Raises:

  • (ArgumentError) —

    when delta is not a finite real non-negative Numeric



80
81
82
# File 'lib/matchi/change.rb', line 80

def by_at_least(delta)
  Delta.new(:by_at_least, delta, &@state)
end

#by_at_most(delta) ⇒ Matchi::Change::Delta

Parameters:

  • delta (Numeric) —

    the largest acceptable increase, zero or positive

Returns:

Raises:

  • (ArgumentError) —

    when delta is not a finite real non-negative Numeric



87
88
89
# File 'lib/matchi/change.rb', line 87

def by_at_most(delta)
  Delta.new(:by_at_most, delta, &@state)
end

#from(old_value) ⇒ Matchi::Change::From

Returns a builder; call to on it.

Parameters:

  • old_value (#eql?) —

    the expected value before the block

Returns:



93
94
95
# File 'lib/matchi/change.rb', line 93

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

#match? ⇒ Boolean

Yield Returns:

  • (Object) —

    ignored, the block is evaluated for its effect

Returns:

  • (Boolean)

Raises:

  • (ArgumentError) —

    when no block is given

  • (NoMethodError) —

    when the object has no such public method

  • (TypeError) —

    when the observed method returns the same unfrozen object twice



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/matchi/change.rb', line 52

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

  value_before = @state.call
  yield
  value_after = @state.call

  return !value_before.eql?(value_after) unless value_before.equal?(value_after)
  return false if ::Kernel.instance_method(:frozen?).bind_call(value_before)

  raise ::TypeError, "#{self} returned the same unfrozen object twice: observe a value, not the object"
end

#to(new_value) ⇒ Matchi::Change::To

Parameters:

  • new_value (#eql?) —

    the expected value after the block

Returns:



99
100
101
# File 'lib/matchi/change.rb', line 99

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

#to_s ⇒ String

Returns the observed class and method, such as change Array#length.

Returns:

  • (String) —

    the observed class and method, such as change Array#length



66
67
68
# File 'lib/matchi/change.rb', line 66

def to_s
  "change #{Constant.label(::Kernel.instance_method(:class).bind_call(@object))}##{@method}"
end