Class: Matchi::Satisfy

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

Overview

Satisfy matcher.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Satisfy

Initialize the matcher with a block.

Examples:

require "matchi/satisfy"

Matchi::Satisfy.new { |value| value == 42 }

Parameters:

  • block (Proc)

    A block of code.

Raises:

  • (::ArgumentError)


14
15
16
17
18
# File 'lib/matchi/satisfy.rb', line 14

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

  @expected = block
end

Instance Method Details

#match?Boolean

Boolean comparison between the actual value and the expected value.

Examples:

require "matchi/satisfy"

matcher = Matchi::Satisfy.new { |value| value == 42 }
matcher.match? { 42 } # => true

Yield Returns:

  • (#object_id)

    The actual value to compare to the expected one.

Returns:

  • (Boolean)

    Comparison between actual and expected values.

Raises:

  • (::ArgumentError)


32
33
34
35
36
# File 'lib/matchi/satisfy.rb', line 32

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

  @expected.call(yield)
end

#to_sString

Returns a string representing the matcher.

Returns:

  • (String)

    a human-readable description of the matcher



41
42
43
# File 'lib/matchi/satisfy.rb', line 41

def to_s
  "satisfy &block"
end