Class: Matchi::Eql

Inherits:
BasicObject
Defined in:
lib/matchi/eql.rb

Overview

Equivalence matcher.

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ Eql

Initialize the matcher with an object.

Examples:

The string ‘foo’ matcher.

Matchi::Eql.new('foo')

Parameters:

  • expected (#eql?)

    An expected equivalent object.



10
11
12
# File 'lib/matchi/eql.rb', line 10

def initialize(expected)
  @expected = expected
end

Instance Method Details

#matches?Boolean

Boolean comparison between the actual value and the expected value.

Examples:

Is it equivalent to ‘foo’?

eql = Matchi::Eql.new('foo')
eql.matches? { 'foo' } # => true

Yield Returns:

  • (#object_id)

    the actual value to compare to the expected one.

Returns:

  • (Boolean)

    Comparison between actual and expected values.



23
24
25
# File 'lib/matchi/eql.rb', line 23

def matches?
  @expected.eql?(yield)
end

#to_hHash

Returns a hash of one key-value pair with a key corresponding to the

matcher and a value corresponding to its initialize parameters.

Examples:

A FooBar matcher serialized into a hash.

matcher = Matchi::FooBar.new(42)
matcher.to_h # => { FooBar: 42 }

Returns:

  • (Hash)

    A hash of one key-value pair.



46
47
48
# File 'lib/matchi/eql.rb', line 46

def to_h
  { Eql: [@expected] }
end

#to_sString

Returns a string representing the matcher.

Examples:

The readable definition of a FooBar matcher.

matcher = Matchi::FooBar.new(42)
matcher.to_s # => "foo_bar 42"

Returns:

  • (String)

    A string representing the matcher.



34
35
36
# File 'lib/matchi/eql.rb', line 34

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