Class: Minicrest::StringMatcher Private

Inherits:
Matcher
  • Object
show all
Defined in:
lib/minicrest/string_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 check string properties.

Direct Known Subclasses

EndsWith, StartsWith

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(substring, method, label) ⇒ StringMatcher

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 string matcher.

Parameters:

  • substring (String)

    the expected substring

  • method (Symbol)

    the method to call on the string (:start_with?, :end_with?)

  • label (String)

    a human-readable label (e.g., “start with”)



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

def initialize(substring, method, label)
  super()
  @substring = substring
  @method = method
  @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/string_matcher.rb', line 37

def description
  "a string #{@label} #{@substring.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
48
49
50
# File 'lib/minicrest/string_matcher.rb', line 45

def failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect}
          to #{@label} #{@substring.inspect}
  MSG
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 condition.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if actual matches



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

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

  actual.public_send(@method, @substring)
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)

    failure message



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

def negated_failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect}
          not to #{@label} #{@substring.inspect}
    but it does
  MSG
end