Class: Matchi::Eq

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

Overview

Equivalence matcher.

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ Eq

Initialize the matcher with an object.

Examples:

require "matchi/eq"

Matchi::Eq.new("foo")

Parameters:

  • expected (#eql?)

    An expected equivalent object.



14
15
16
# File 'lib/matchi/eq.rb', line 14

def initialize(expected)
  @expected = expected
end

Instance Method Details

#match?Boolean

Boolean comparison between the actual value and the expected value.

Examples:

require "matchi/eq"

matcher = Matchi::Eq.new("foo")
matcher.match? { "foo" } # => true

Yield Returns:

  • (#object_id)

    The actual value to compare to the expected one.

Returns:

  • (Boolean)

    Comparison between actual and expected values.

Raises:

  • (::ArgumentError)


30
31
32
33
34
# File 'lib/matchi/eq.rb', line 30

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

  @expected.eql?(yield)
end

#to_sString

Returns a string representing the matcher.

Returns:

  • (String)

    a human-readable description of the matcher



39
40
41
# File 'lib/matchi/eq.rb', line 39

def to_s
  "eq #{@expected.inspect}"
end