Class: Minicrest::None

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

Overview

Combinator that requires none of the matchers to succeed.

Examples:

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

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(*matchers) ⇒ None

Creates a new NONE combinator.

Parameters:

  • matchers (Array<Matcher>)

    matchers that must all fail



239
240
241
242
# File 'lib/minicrest/combinators.rb', line 239

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

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    combined description



255
256
257
# File 'lib/minicrest/combinators.rb', line 255

def description
  "none 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 which matcher(s) matched



263
264
265
266
267
268
269
270
271
# File 'lib/minicrest/combinators.rb', line 263

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

#matches?(actual) ⇒ Boolean

Checks if actual matches none of the matchers.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if no matchers match



248
249
250
# File 'lib/minicrest/combinators.rb', line 248

def matches?(actual)
  @matchers.none? { |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 unexpected match of none



277
278
279
280
281
282
283
# File 'lib/minicrest/combinators.rb', line 277

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