Class: Minicrest::Or

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

Overview

Combinator that requires at least one matcher to succeed (logical OR).

Examples:

(equals(1) | equals(2)).matches?(1) # => true
(equals(1) | equals(2)).matches?(3) # => false

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(left, right) ⇒ Or

Creates a new OR combinator.

Parameters:



123
124
125
126
127
# File 'lib/minicrest/combinators.rb', line 123

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



140
141
142
# File 'lib/minicrest/combinators.rb', line 140

def description
  "(#{@left.description} or #{@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 neither matcher matched



148
149
150
151
152
153
154
155
156
157
# File 'lib/minicrest/combinators.rb', line 148

def failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect} to match at least one of:
      #{@left.description}
      #{@right.description}
    but it matched neither:
      #{@left.failure_message(actual)}
      #{@right.failure_message(actual)}
  MSG
end

#matches?(actual) ⇒ Boolean

Checks if actual matches at least one matcher.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if either matcher matches



133
134
135
# File 'lib/minicrest/combinators.rb', line 133

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 which matcher(s) matched



163
164
165
166
167
168
169
170
171
172
# File 'lib/minicrest/combinators.rb', line 163

def negated_failure_message(actual)
  matched = []
  matched << @left.description if @left.matches?(actual)
  matched << @right.description if @right.matches?(actual)

  <<~MSG.chomp
    expected #{actual.inspect} not to match either condition, but it matched:
      #{matched.join("\n  ")}
  MSG
end