Class: Matchi::Change::ByAtLeast

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

Overview

*Change by at least* matcher.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected, &state) ⇒ ByAtLeast

Initialize the matcher with an object and a block.

Examples:

require "matchi/change/by_at_least"

object = []

Matchi::Change::ByAtLeast.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.



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

def initialize(expected, &state)
  @expected = expected
  @state    = state
end

Instance Attribute Details

#expected#object_id (readonly)

Returns An expected delta.

Returns:

  • (#object_id)

    An expected delta.



8
9
10
# File 'lib/matchi/change/by_at_least.rb', line 8

def expected
  @expected
end

Instance Method Details

#inspectObject

A string containing a human-readable representation of the matcher.



53
54
55
# File 'lib/matchi/change/by_at_least.rb', line 53

def inspect
  "#{self.class}(#{expected.inspect})"
end

#matches?Boolean

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

Examples:

require "matchi/change/by_at_least"

object = []

matcher = Matchi::Change::ByAtLeast.new(1) { object.length }

matcher.expected                     # => 1
matcher.matches? { 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.



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

def matches?
  value_before = @state.call
  yield
  value_after = @state.call

  expected <= (value_after - value_before)
end

#to_sObject

Returns a string representing the matcher.



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

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