Class: Minicrest::All

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

Overview

Combinator that requires all matchers to succeed.

Examples:

all_of(equals(5), does_not(equal(nil))).matches?(5) # => true
all_of(equals(5), equals(6)).matches?(5) # => false

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(*matchers) ⇒ All

Creates a new ALL combinator.

Parameters:

  • matchers (Array<Matcher>)

    matchers that must all match



184
185
186
187
# File 'lib/minicrest/combinators.rb', line 184

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

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    combined description



200
201
202
# File 'lib/minicrest/combinators.rb', line 200

def description
  "all 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) failed



208
209
210
211
212
213
214
215
216
# File 'lib/minicrest/combinators.rb', line 208

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

#matches?(actual) ⇒ Boolean

Checks if actual matches all matchers.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if all matchers match



193
194
195
# File 'lib/minicrest/combinators.rb', line 193

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



222
223
224
225
226
227
# File 'lib/minicrest/combinators.rb', line 222

def negated_failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect} not to match all conditions, but it matched all:
      #{@matchers.map(&:description).join("\n  ")}
  MSG
end