Module: Matchi::MatchersBase

Overview

Common matcher methods.

Instance Method Summary collapse

Instance Method Details

#matches?Boolean

Abstract matcher class.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

    Override me inside a Matcher subclass please.



9
10
11
# File 'lib/matchi/matchers_base.rb', line 9

def matches?
  raise NotImplementedError, 'The matcher MUST respond to matches? method.'
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::Matchers::FooBar::Matcher.new(42)
matcher.to_h # => { FooBar: 42 }

Returns:

  • (Hash)

    A hash of one key-value pair.



37
38
39
# File 'lib/matchi/matchers_base.rb', line 37

def to_h
  { matcher_name.to_sym => (defined?(@expected) ? Array(@expected) : []) }
end

#to_sString

Returns a string representing the matcher.

Examples:

The readable definition of a FooBar matcher.

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

Returns:

  • (String)

    A string representing the matcher.



20
21
22
23
24
25
26
27
# File 'lib/matchi/matchers_base.rb', line 20

def to_s
  s = matcher_name
      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .downcase

  defined?(@expected) ? [s, @expected.inspect].join(' ') : s
end