Class: Matchi::Be

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

Overview

Identity matcher: passes when the actual value is the very same object as the expected one (equal?).

Examples:

Matchi::Be.new(:foo).match? { :foo } # => true
Matchi::Be.new("a").match? { "a".dup } # => false (another object)

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ Be

Returns a new instance of Be.

Parameters:

  • expected (Object) —

    the expected object



12
13
14
# File 'lib/matchi/be.rb', line 12

def initialize(expected)
  @expected = expected
end

Instance Method Details

#match? ⇒ Boolean

Yield Returns:

  • (Object) —

    the actual value

Returns:

  • (Boolean)

Raises:

  • (ArgumentError) —

    when no block is given



19
20
21
22
23
# File 'lib/matchi/be.rb', line 19

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

  @expected.equal?(yield)
end

#to_s ⇒ String

Returns:

  • (String)


26
27
28
# File 'lib/matchi/be.rb', line 26

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