Class: Matchi::Equal

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

Overview

Identity matcher.

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ Equal

Initialize the matcher with an object.

Examples:

The number 42 matcher.

Matchi::Equal.new(42)

Parameters:

  • expected (#equal?)

    An expected object.



10
11
12
# File 'lib/matchi/equal.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 equal to :foo?

equal = Matchi::Equal.new(:foo)
equal.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/equal.rb', line 23

def matches?
  @expected.equal?(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/equal.rb', line 46

def to_h
  { Equal: [@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/equal.rb', line 34

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