Class: Minicrest::Blank

Inherits:
Matcher show all
Defined in:
lib/minicrest/blank.rb

Overview

Matcher that checks if a string is blank (empty or whitespace-only).

Examples:

Basic usage

blank.matches?("")      # => true
blank.matches?("   ")   # => true
blank.matches?("hello") # => false

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    description of blank expectation



26
27
28
# File 'lib/minicrest/blank.rb', line 26

def description
  'a blank string'
end

#failure_message(actual) ⇒ String

Returns the failure message when the match fails.

Parameters:

  • actual (String)

    the string that was checked

Returns:

  • (String)

    message showing expected blank



34
35
36
37
38
39
# File 'lib/minicrest/blank.rb', line 34

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

#matches?(actual) ⇒ Boolean

Checks if actual is a blank string.

Parameters:

  • actual (String)

    the string to check

Returns:

  • (Boolean)

    true if actual is empty or whitespace-only



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

def matches?(actual)
  return false unless actual.respond_to?(:strip)

  actual.strip.empty?
rescue NoMethodError
  false
end

#negated_failure_message(actual) ⇒ String

Returns the failure message when a negated match fails.

Parameters:

  • actual (String)

    the string that was checked

Returns:

  • (String)

    message indicating unexpected blank match



45
46
47
48
49
50
51
# File 'lib/minicrest/blank.rb', line 45

def negated_failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect}
          not to be blank
    but it is
  MSG
end