Class: Matchi::Match

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

Overview

*Regular expressions* matcher.

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ Match

Initialize the matcher with an instance of Regexp.

Examples:

require "matchi/match"

Matchi::Match.new(/^foo$/)

Parameters:

  • expected (#match)

    A regular expression.

Raises:

  • (::ArgumentError)


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

def initialize(expected)
  raise ::ArgumentError, "expected must respond to match?" unless expected.respond_to?(:match?)

  @expected = expected
end

Instance Method Details

#match?Boolean

Boolean comparison between the actual value and the expected value.

Examples:

require "matchi/match"

matcher = Matchi::Match.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)


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

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

  @expected.match?(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/match.rb', line 41

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