class RSpec::Matchers::BuiltIn::Compound::NestedEvaluator
This is necessary so that the ‘expect` block is only executed once.
}.to bar
expect { x }.to foo
expect {
`expect { x }.to foo.and bar` becomes:
For block expectations, we need to nest them instead, so that
expect(x).to bar
expect(x).to foo
`expect(x).to foo.and bar`, this becomes:
Normally, we evaluate the matching sequentially. For an expression like
def self.matcher_expects_call_stack_jump?(matcher)
def self.matcher_expects_call_stack_jump?(matcher) matcher.expects_call_stack_jump? rescue NoMethodError false end
def initialize(actual, matcher_1, matcher_2)
def initialize(actual, matcher_1, matcher_2) @actual = actual @matcher_1 = matcher_1 @matcher_2 = matcher_2 @match_results = {} inner, outer = order_block_matchers @match_results[outer] = outer.matches?(Proc.new do |*args| @match_results[inner] = inner.matches?(inner_matcher_block(args)) end) end
def inner_matcher_block(outer_args)
When such a matcher is used as the outer matcher, we need to forward the
Some block matchers (such as `yield_xyz`) pass args to the `expect` block.
def inner_matcher_block(outer_args) return @actual if outer_args.empty? Proc.new do |*inner_args| unless inner_args.empty? raise ArgumentError, "(#{@matcher_1.description}) and " \ "(#{@matcher_2.description}) cannot be combined in a compound expectation " \ "since both matchers pass arguments to the block." end @actual.call(*outer_args) end end
def matcher_matches?(matcher)
def matcher_matches?(matcher) @match_results.fetch(matcher) do raise ArgumentError, "Your #{matcher.description} has no match " \ "results, this can occur when an unexpected call stack or " \ "local jump occurs. Perhaps one of your matchers needs to " \ "declare `expects_call_stack_jump?` as `true`?" end end
def order_block_matchers
This method figures out which matcher should be the inner matcher and which
`raise_error` logic, so only the former case will work properly.
get executed because the `raise "boom"` line would jump to the `rescue` in the
In the latter case, the after-block logic in the `change` matcher would never
}.to raise_error("boom")
}.to change { x }.by(1)
raise "boom"
x += 1
expect {
expect {
...rather than:
}.to change { x }.by(1)
}.to raise_error("boom")
raise "boom"
x += 1
expect {
expect {
For example, we need it to be this:
up the call stack, we need to order things so that it is the inner matcher.
For a matcher like `raise_error` or `throw_symbol`, where the block will jump
def order_block_matchers return @matcher_1, @matcher_2 unless self.class.matcher_expects_call_stack_jump?(@matcher_2) return @matcher_2, @matcher_1 unless self.class.matcher_expects_call_stack_jump?(@matcher_1) raise ArgumentError, "(#{@matcher_1.description}) and " \ "(#{@matcher_2.description}) cannot be combined in a compound expectation " \ "because they both expect a call stack jump." end