Class: Matchi::Matcher::Base

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

Overview

Abstract matcher class.

Direct Known Subclasses

BeFalse, BeNil, BeTrue, Eql, Equal, Match, RaiseException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#expected#object_id (readonly)

Returns Any value to give to the matcher.

Returns:

  • (#object_id)

    Any value to give to the matcher.



17
18
19
# File 'lib/matchi/matcher/base.rb', line 17

def expected
  @expected
end

Class Method Details

.to_symSymbol

Returns A symbol identifying the matcher.

Returns:

  • (Symbol)

    A symbol identifying the matcher.



8
9
10
11
12
13
14
# File 'lib/matchi/matcher/base.rb', line 8

def self.to_sym
  name.delete_prefix("Matchi::Matcher::")
      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .downcase
      .to_sym
end

Instance Method Details

#inspectString

A string containing a human-readable representation of the matcher.

Returns:

  • (String)

    The human-readable representation of the matcher.



22
23
24
# File 'lib/matchi/matcher/base.rb', line 22

def inspect
  "#{self.class}(#{expected&.inspect})"
end

#matches?Boolean

Abstract matcher class.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

    Override me inside a matcher.



29
30
31
# File 'lib/matchi/matcher/base.rb', line 29

def matches?
  raise ::NotImplementedError, "matcher MUST respond to this method."
end

#to_sString

Returns a string representing the matcher.

Examples:

The readable definition of a FooBar matcher.

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

Returns:

  • (String)

    A string representing the matcher.



40
41
42
# File 'lib/matchi/matcher/base.rb', line 40

def to_s
  [self.class.to_sym, expected&.inspect].compact.join(" ")
end