Class: Minicrest::ComparisonMatcher Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Base class for matchers that compare a value against an expected value using an operator.

Direct Known Subclasses

IsGreaterThan, IsGreaterThanOrEqualTo, IsLessThan

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(expected, operator, label) ⇒ ComparisonMatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new comparison matcher.

Parameters:

  • expected (Object)

    the value to compare against

  • operator (Symbol)

    the comparison operator (e.g., :>, :<, :>=, :<=)

  • label (String)

    a human-readable label for the comparison (e.g., “greater than”)



15
16
17
18
19
20
# File 'lib/minicrest/comparison_matcher.rb', line 15

def initialize(expected, operator, label)
  super()
  @expected = expected
  @operator = operator
  @label = label
end

Instance Method Details

#descriptionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a description of what this matcher expects.

Returns:

  • (String)

    description



37
38
39
# File 'lib/minicrest/comparison_matcher.rb', line 37

def description
  "#{@label} #{@expected.inspect}"
end

#failure_message(actual) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the failure message when the match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    failure message



45
46
47
# File 'lib/minicrest/comparison_matcher.rb', line 45

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

#matches?(actual) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if actual matches the expected value using the operator.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if actual.send(@operator, @expected)



26
27
28
29
30
31
32
# File 'lib/minicrest/comparison_matcher.rb', line 26

def matches?(actual)
  return false unless actual.respond_to?(@operator)

  actual.public_send(@operator, @expected)
rescue ArgumentError, NoMethodError
  false
end

#negated_failure_message(actual) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the failure message when a negated match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    message indicating unexpected comparison match



53
54
55
# File 'lib/minicrest/comparison_matcher.rb', line 53

def negated_failure_message(actual)
  "expected #{actual.inspect} not to be #{@label} #{@expected.inspect}, but it was"
end