Class: Minicrest::Matcher

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

Overview

Base class for all matchers.

Matchers encapsulate a matching condition and provide descriptive messages for both successful and failed matches.

To create a custom matcher, subclass this and implement: - #matches?(actual) - returns true if the actual value matches - #description - returns a description of what the matcher expects - #failure_message(actual) - returns message when match fails - #negated_failure_message(actual) - returns message when negated match fails

Examples:

Creating a custom matcher

class GreaterThan < Matcher
  def initialize(expected)
    @expected = expected
  end

  def matches?(actual)
    actual > @expected
  end

  def description
    "a value greater than #{@expected.inspect}"
  end

  def failure_message(actual)
    "expected #{actual.inspect} to be greater than #{@expected.inspect}"
  end

  def negated_failure_message(actual)
    "expected #{actual.inspect} not to be greater than #{@expected.inspect}"
  end
end

Instance Method Summary collapse

Instance Method Details

#&(other) ⇒ And

Combines this matcher with another using logical AND.

Parameters:

  • other (Matcher)

    another matcher

Returns:

  • (And)

    a combined matcher that requires both to match



79
80
81
# File 'lib/minicrest/matcher.rb', line 79

def &(other)
  And.new(self, other)
end

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    human-readable description

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/minicrest/matcher.rb', line 49

def description
  raise NotImplementedError, "#{self.class} must implement #description"
end

#failure_message(actual) ⇒ String

Returns the failure message when the match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    human-readable failure message



57
58
59
60
61
62
# File 'lib/minicrest/matcher.rb', line 57

def failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect}
          to be #{description}
  MSG
end

#matches?(actual) ⇒ Boolean

Checks if the actual value matches this matcher’s condition.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if the value matches

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/minicrest/matcher.rb', line 42

def matches?(actual)
  raise NotImplementedError, "#{self.class} must implement #matches?"
end

#negated_failure_message(actual) ⇒ String

Returns the failure message when a negated match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    human-readable failure message for negated case



68
69
70
71
72
73
# File 'lib/minicrest/matcher.rb', line 68

def negated_failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect}
          not to be #{description}
  MSG
end

#|(other) ⇒ Or

Combines this matcher with another using logical OR.

Parameters:

  • other (Matcher)

    another matcher

Returns:

  • (Or)

    a combined matcher that requires either to match



87
88
89
# File 'lib/minicrest/matcher.rb', line 87

def |(other)
  Or.new(self, other)
end