Class: Matchi::Matcher::Base

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

Overview

Abstract matcher class.

Direct Known Subclasses

BeAnInstanceOf, 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.



24
25
26
# File 'lib/matchi/matcher/base.rb', line 24

def expected
  @expected
end

Class Method Details

.to_symSymbol

Returns a symbol identifying the matcher.

Examples:

The readable definition of a FooBar matcher class.

matcher_class = Matchi::Matcher::FooBar
matcher_class.to_sym # => "foo_bar"

Returns:

  • (Symbol)

    A symbol identifying the matcher.



14
15
16
17
18
19
20
21
# File 'lib/matchi/matcher/base.rb', line 14

def self.to_sym
  name.split("::")
      .fetch(-1)
      .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.

Examples:

The human-readable representation of a FooBar matcher instance.

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

Returns:

  • (String)

    The human-readable representation of the matcher.



33
34
35
# File 'lib/matchi/matcher/base.rb', line 33

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

#matches?Boolean

Abstract matcher class.

Examples:

Test the equivalence between two “foo” strings.

eql = Matchi::Matcher::Eql.new("foo")
eql.matches? { "foo" } # => true

Yield Returns:

  • (#object_id)

    The actual value to compare to the expected one.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

    Override this method inside a matcher.



47
48
49
# File 'lib/matchi/matcher/base.rb', line 47

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

#to_sString

Returns a string representing the matcher instance.

Examples:

The readable definition of a FooBar matcher instance.

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

Returns:

  • (String)

    A string representing the matcher instance.



58
59
60
# File 'lib/matchi/matcher/base.rb', line 58

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