123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Matchers::BuiltIn::Compound Private

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
And, Or
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, BaseMatcher
Instance Chain:
Inherits: RSpec::Matchers::BuiltIn::BaseMatcher
Defined in: rspec-expectations/lib/rspec/matchers/built_in/compound.rb

Overview

Base class for and and or compound matchers.

Constant Summary

BaseMatcher - Inherited

UNDEFINED

Class Method Summary

BaseMatcher - Inherited

.matcher_name, .new,
.underscore

Borrowed from ActiveSupport.

Instance Attribute Summary

BaseMatcher - Inherited

#actual,
#diffable?

::RSpec::Matchers are not diffable by default.

#expected, #expects_call_stack_jump?, #matcher_name, #matcher_name=, #rescued_exception,
#supports_block_expectations?

Most matchers are value matchers (i.e. meant to work with ‘expect(value)`) rather than block matchers (i.e. meant to work with `expect { }`), so this defaults to false.

#supports_value_expectations?

BaseMatcher::StringEncodingFormatting - Included

Instance Method Summary

BaseMatcher - Inherited

#actual_formatted,
#description

Generates a description using ::RSpec::Matchers::EnglishPhrasing.

#expected_formatted,
#match_unless_raises

Used to wrap a block of code that will indicate failure by raising one of the named exceptions.

#matches?

Indicates if the match is successful.

BaseMatcher::DefaultFailureMessages - Included

#failure_message

Provides a good generic failure message.

#failure_message_when_negated

Provides a good generic negative failure message.

BaseMatcher::StringEncodingFormatting - Included

#format_encoding

Formats a String’s encoding as a human readable string :nocov:

BaseMatcher::HashFormatting - Included

#improve_hash_formatting

‘{ :a => 5, :b => 2 }.inspect` produces:

::RSpec::Matchers::Composable - Included

#&

Alias for Composable#and.

#===

Delegates to #matches?.

#and

Creates a compound and expectation.

#description_of

Returns the description of the given object in a way that is aware of composed matchers.

#or

Creates a compound or expectation.

#values_match?

This provides a generic way to fuzzy-match an expected value against an actual value.

#|

Alias for Composable#or.

#should_enumerate?

We should enumerate arrays as long as they are not recursive.

#surface_descriptions_in

Transforms the given data structure (typically a hash or array) into a new data structure that, when #inspect is called on it, will provide descriptions of any contained matchers rather than the normal #inspect output.

#unreadable_io?,
#with_matchers_cloned

Historically, a single matcher instance was only checked against a single value.

Instance Attribute Details

#diffable?Boolean (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 49

def diffable?
  matcher_is_diffable?(matcher_1) || matcher_is_diffable?(matcher_2)
end

#evaluator (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 8

attr_reader :matcher_1, :matcher_2, :evaluator

#expects_call_stack_jump?Boolean (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 42

def expects_call_stack_jump?
  NestedEvaluator.matcher_expects_call_stack_jump?(matcher_1) ||
  NestedEvaluator.matcher_expects_call_stack_jump?(matcher_2)
end

#matcher_1 (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 8

attr_reader :matcher_1, :matcher_2, :evaluator

#matcher_1_matches?Boolean (readonly, private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 102

def matcher_1_matches?
  evaluator.matcher_matches?(matcher_1)
end

#matcher_2 (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 8

attr_reader :matcher_1, :matcher_2, :evaluator

#matcher_2_matches?Boolean (readonly, private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 106

def matcher_2_matches?
  evaluator.matcher_matches?(matcher_2)
end

#supports_block_expectations?Boolean (readonly)

[ GitHub ]

  
# File 'rspec-expectations/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

#supports_value_expectations?Boolean (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 36

def supports_value_expectations?
  matcher_supports_value_expectations?(matcher_1) &&
  matcher_supports_value_expectations?(matcher_2)
end

Instance Method Details

#compound_failure_message (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 96

def compound_failure_message
  "#{indent_multiline_message(matcher_1.failure_message.sub(/\n+\z/, ''))}" \
  "\n\n...#{conjunction}:" \
  "\n\n#{indent_multiline_message(matcher_2.failure_message.sub(/\A\n+/, ''))}"
end

#descriptionString

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 25

def description
  "#{matcher_1.description} #{conjunction} #{matcher_2.description}"
end

#diffable_matcher_list (protected)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 62

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

#diffable_matcher_list_for(matcher) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 128

def diffable_matcher_list_for(matcher)
  return [] unless matcher_is_diffable?(matcher)
  return matcher.diffable_matcher_list if Compound === matcher
  [matcher]
end

#does_not_match?(_actual) ⇒ Boolean

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 16

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

#expectedRSpec::Matchers::MultiMatcherDiff

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 55

def expected
  return nil unless evaluator
  ::RSpec::Matchers::MultiMatcherDiff.for_many_matchers(diffable_matcher_list)
end

#indent_multiline_message(message) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 90

def indent_multiline_message(message)
  message.lines.map do |line|
    line =~ /\S/ ? '   ' + line : line
  end.join
end

#initialize_copy(other) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 71

def initialize_copy(other)
  @matcher_1 = @matcher_1.clone
  @matcher_2 = @matcher_2.clone
  super
end

#match(_expected, actual) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 77

def match(_expected, actual)
  evaluator_klass = if supports_block_expectations? && Proc === actual
                      NestedEvaluator
                    elsif supports_value_expectations?
                      SequentialEvaluator
                    else
                      # Can't raise an ArgumentError in this context, as it's rescued
                      raise "Block and value matchers can't be combined in a compound expectation (#{matcher_1.description}, #{matcher_2.description})"
                    end

  @evaluator = evaluator_klass.new(actual, matcher_1, matcher_2)
end

#matcher_is_diffable?(matcher) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 122

def matcher_is_diffable?(matcher)
  matcher.diffable?
rescue NoMethodError
  false
end

#matcher_supports_block_expectations?(matcher) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 110

def matcher_supports_block_expectations?(matcher)
  matcher.supports_block_expectations?
rescue NoMethodError
  false
end

#matcher_supports_value_expectations?(matcher) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/compound.rb', line 116

def matcher_supports_value_expectations?(matcher)
  matcher.supports_value_expectations?
rescue NoMethodError
  true
end