Class: Minicrest::Empty

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

Overview

Matcher that checks if a value is empty.

Works with any object that responds to empty?: - Strings - Arrays - Hashes - Sets - Any Enumerable

Examples:

Basic usage

empty.matches?('')     # => true
empty.matches?([])     # => true
empty.matches?({})     # => true
empty.matches?('hi')   # => false
empty.matches?([1])    # => false

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    description



31
32
33
# File 'lib/minicrest/empty.rb', line 31

def description
  'empty'
end

#failure_message(actual) ⇒ String

Returns the failure message when the match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    failure message



39
40
41
42
43
# File 'lib/minicrest/empty.rb', line 39

def failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect} to be empty, but had size #{actual.size}
  MSG
end

#matches?(actual) ⇒ Boolean

Checks if actual is empty.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if actual.empty?



24
25
26
# File 'lib/minicrest/empty.rb', line 24

def matches?(actual)
  actual.empty?
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)

    message indicating unexpected emptiness



49
50
51
52
53
# File 'lib/minicrest/empty.rb', line 49

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