Class: Minicrest::Not

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

Overview

Combinator that negates a matcher’s result.

Wraps another matcher and inverts its matching logic. When the wrapped matcher succeeds, Not fails, and vice versa.

Examples:

Basic usage

does_not(equals(5)).matches?(3) # => true (3 != 5)
does_not(equals(5)).matches?(5) # => false (5 == 5)

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(matcher) ⇒ Not

Creates a new negating matcher.

Parameters:

  • matcher (Matcher)

    the matcher to negate



16
17
18
19
# File 'lib/minicrest/combinators.rb', line 16

def initialize(matcher)
  super()
  @matcher = matcher
end

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    negated description



32
33
34
# File 'lib/minicrest/combinators.rb', line 32

def description
  "not #{@matcher.description}"
end

#failure_message(actual) ⇒ String

Returns the failure message when the negated match fails.

This happens when the wrapped matcher succeeds (but we wanted it to fail).

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    the wrapped matcher’s negated failure message



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

def failure_message(actual)
  @matcher.negated_failure_message(actual)
end

#matches?(actual) ⇒ Boolean

Checks if actual does NOT match the wrapped matcher.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if the wrapped matcher returns false



25
26
27
# File 'lib/minicrest/combinators.rb', line 25

def matches?(actual)
  !@matcher.matches?(actual)
end

#negated_failure_message(actual) ⇒ String

Returns the failure message when a double-negated match fails.

This happens when not(not(matcher)) is used and the inner matcher fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    the wrapped matcher’s regular failure message



52
53
54
# File 'lib/minicrest/combinators.rb', line 52

def negated_failure_message(actual)
  @matcher.failure_message(actual)
end