Class: Minicrest::And

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

Overview

Combinator that requires both matchers to succeed (logical AND).

Examples:

(greater_than(0) & less_than(10)).matches?(5) # => true
(greater_than(0) & less_than(10)).matches?(15) # => false

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(left, right) ⇒ And

Creates a new AND combinator.

Parameters:



67
68
69
70
71
# File 'lib/minicrest/combinators.rb', line 67

def initialize(left, right)
  super()
  @left = left
  @right = right
end

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    combined description



84
85
86
# File 'lib/minicrest/combinators.rb', line 84

def description
  "(#{@left.description} and #{@right.description})"
end

#failure_message(actual) ⇒ String

Returns the failure message when the match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    message indicating which matcher(s) failed



92
93
94
95
96
97
# File 'lib/minicrest/combinators.rb', line 92

def failure_message(actual)
  messages = []
  messages << @left.failure_message(actual) unless @left.matches?(actual)
  messages << @right.failure_message(actual) unless @right.matches?(actual)
  messages.join("\n  AND\n")
end

#matches?(actual) ⇒ Boolean

Checks if actual matches both matchers.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if both matchers match



77
78
79
# File 'lib/minicrest/combinators.rb', line 77

def matches?(actual)
  @left.matches?(actual) && @right.matches?(actual)
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 match of both



103
104
105
106
107
108
109
110
# File 'lib/minicrest/combinators.rb', line 103

def negated_failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect} not to match both conditions:
      #{@left.description}
      #{@right.description}
    but it matched both
  MSG
end