Class: Matchi::Change::ByAtMost

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

Overview

*Change by at most* matcher.

Instance Method Summary collapse

Constructor Details

#initialize(expected, &state) ⇒ ByAtMost

Initialize the matcher with an object and a block.

Examples:

require "matchi/change/by_at_most"

object = []

Matchi::Change::ByAtMost.new(1) { object.length }

Parameters:

  • expected (#object_id)

    An expected delta.

  • state (Proc)

    A block of code to execute to get the state of the object.

Raises:

  • (::ArgumentError)


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

def initialize(expected, &state)
  raise ::ArgumentError, "expected must be a Numeric" unless expected.is_a?(::Numeric)
  raise ::ArgumentError, "a block must be provided" unless block_given?
  raise ::ArgumentError, "expected must be non-negative" if expected.negative?

  @expected = expected
  @state    = state
end

Instance Method Details

#match?Boolean

Boolean comparison on the expected change by comparing the value before and after the code execution.

Examples:

require "matchi/change/by_at_most"

object = []

matcher = Matchi::Change::ByAtMost.new(1) { object.length }
matcher.match? { object << "foo" } # => true

Yield Returns:

  • (#object_id)

    The block of code to execute.

Returns:

  • (Boolean)

    Comparison between the value before and after the code execution.

Raises:

  • (::ArgumentError)


43
44
45
46
47
48
49
50
51
# File 'lib/matchi/change/by_at_most.rb', line 43

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

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

  @expected >= (value_after - value_before)
end

#to_sString

Returns a string representing the matcher.

Returns:

  • (String)

    a human-readable description of the matcher



56
57
58
# File 'lib/matchi/change/by_at_most.rb', line 56

def to_s
  "change by at most #{@expected.inspect}"
end