class RSpec::Matchers::BuiltIn::Compound

@api private Base class for `and` and `or` compound matchers. rubocop:disable ClassLength

Attributes

evaluator[R]

@private

matcher_1[R]

@private

matcher_2[R]

@private

Public Class Methods

new(matcher_1, matcher_2) click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 11
def initialize(matcher_1, matcher_2)
  @matcher_1 = matcher_1
  @matcher_2 = matcher_2
end

Public Instance Methods

description() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/compound.rb, line 26
def description
  singleline_message(matcher_1.description, matcher_2.description)
end
diffable?() click to toggle source

@api private @return [Boolean]

# File lib/rspec/matchers/built_in/compound.rb, line 42
def diffable?
  matcher_is_diffable?(matcher_1) || matcher_is_diffable?(matcher_2)
end
does_not_match?(_actual) click to toggle source

@private

# File lib/rspec/matchers/built_in/compound.rb, line 17
def does_not_match?(_actual)
  raise NotImplementedError, "`expect(...).not_to matcher.#{conjunction} matcher` "              "is not supported, since it creates a bit of an ambiguity. Instead, define negated versions "              "of whatever matchers you wish to negate with `RSpec::Matchers.define_negated_matcher` and "              "use `expect(...).to matcher.#{conjunction} matcher`."
end
expected() click to toggle source

@api private @return [RSpec::Matchers::ExpectedsForMultipleDiffs]

# File lib/rspec/matchers/built_in/compound.rb, line 48
def expected
  return nil unless evaluator
  ::RSpec::Matchers::ExpectedsForMultipleDiffs.for_many_matchers(diffable_matcher_list)
end
expects_call_stack_jump?() click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 35
def expects_call_stack_jump?
  NestedEvaluator.matcher_expects_call_stack_jump?(matcher_1) ||
  NestedEvaluator.matcher_expects_call_stack_jump?(matcher_2)
end
supports_block_expectations?() click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 30
def supports_block_expectations?
  matcher_supports_block_expectations?(matcher_1) &&
  matcher_supports_block_expectations?(matcher_2)
end

Protected Instance Methods

diffable_matcher_list() click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 55
def diffable_matcher_list
  list = []
  list.concat(diffable_matcher_list_for(matcher_1)) unless matcher_1_matches?
  list.concat(diffable_matcher_list_for(matcher_2)) unless matcher_2_matches?
  list
end

Private Instance Methods

compound_failure_message() click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 86
def compound_failure_message
  message_1 = matcher_1.failure_message
  message_2 = matcher_2.failure_message

  if multiline?(message_1) || multiline?(message_2)
    multiline_message(message_1, message_2)
  else
    singleline_message(message_1, message_2)
  end
end
diffable_matcher_list_for(matcher) click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 133
def diffable_matcher_list_for(matcher)
  return [] unless matcher_is_diffable?(matcher)
  return matcher.diffable_matcher_list if Compound === matcher
  [matcher]
end
indent_multiline_message(message) click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 80
def indent_multiline_message(message)
  message.lines.map do |line|
    line =~ /\S/ ? '   ' + line : line
  end.join
end
initialize_copy(other) click to toggle source
Calls superclass method
# File lib/rspec/matchers/built_in/compound.rb, line 64
def initialize_copy(other)
  @matcher_1 = @matcher_1.clone
  @matcher_2 = @matcher_2.clone
  super
end
match(_expected, actual) click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 70
def match(_expected, actual)
  evaluator_klass = if supports_block_expectations? && Proc === actual
                      NestedEvaluator
                    else
                      SequentialEvaluator
                    end

  @evaluator = evaluator_klass.new(actual, matcher_1, matcher_2)
end
matcher_1_matches?() click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 113
def matcher_1_matches?
  evaluator.matcher_matches?(matcher_1)
end
matcher_2_matches?() click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 117
def matcher_2_matches?
  evaluator.matcher_matches?(matcher_2)
end
matcher_is_diffable?(matcher) click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 127
def matcher_is_diffable?(matcher)
  matcher.diffable?
rescue NoMethodError
  false
end
matcher_supports_block_expectations?(matcher) click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 121
def matcher_supports_block_expectations?(matcher)
  matcher.supports_block_expectations?
rescue NoMethodError
  false
end
multiline?(message) click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 105
def multiline?(message)
  message.lines.count > 1
end
multiline_message(message_1, message_2) click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 97
def multiline_message(message_1, message_2)
  [
    indent_multiline_message(message_1.sub(/\n+\z/, '')),
    "...#{conjunction}:",
    indent_multiline_message(message_2.sub(/\A\n+/, ''))
  ].join("\n\n")
end
singleline_message(message_1, message_2) click to toggle source
# File lib/rspec/matchers/built_in/compound.rb, line 109
def singleline_message(message_1, message_2)
  [message_1, conjunction, message_2].join(' ')
end