Class: Minicrest::Some

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

Overview

Combinator that requires at least one matcher to succeed.

Examples:

some_of(equals(5), equals(6)).matches?(5) # => true
some_of(equals(5), equals(6)).matches?(7) # => false

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(*matchers) ⇒ Some

Creates a new SOME combinator.

Parameters:

  • matchers (Array<Matcher>)

    matchers where at least one must match



295
296
297
298
# File 'lib/minicrest/combinators.rb', line 295

def initialize(*matchers)
  super()
  @matchers = matchers.flatten
end

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    combined description



311
312
313
# File 'lib/minicrest/combinators.rb', line 311

def description
  "some of: #{@matchers.map(&:description).join(', ')}"
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 no matchers matched



319
320
321
322
323
324
325
326
# File 'lib/minicrest/combinators.rb', line 319

def failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect} to match at least one of:
      #{@matchers.map(&:description).join("\n  ")}
    but matched none:
      #{@matchers.map { |m| m.failure_message(actual) }.join("\n  ")}
  MSG
end

#matches?(actual) ⇒ Boolean

Checks if actual matches at least one matcher.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if any matcher matches



304
305
306
# File 'lib/minicrest/combinators.rb', line 304

def matches?(actual)
  @matchers.any? { |m| m.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



332
333
334
335
336
337
338
# File 'lib/minicrest/combinators.rb', line 332

def negated_failure_message(actual)
  matched = @matchers.select { |m| m.matches?(actual) }
  <<~MSG.chomp
    expected #{actual.inspect} to match none of the conditions, but matched:
      #{matched.map(&:description).join("\n  ")}
  MSG
end