@api private Provides the implementation for `yield_with_args`. Not intended to be instantiated directly.
# File lib/rspec/matchers/built_in/yield.rb, line 247 def initialize(*args) @expected = args end
@private
# File lib/rspec/matchers/built_in/yield.rb, line 275 def description desc = "yield with args" desc << "(#{expected_arg_description})" unless @expected.empty? desc end
@private
# File lib/rspec/matchers/built_in/yield.rb, line 260 def does_not_match?(block) !matches?(block) && @probe.has_block? end
@private
# File lib/rspec/matchers/built_in/yield.rb, line 265 def failure_message "expected given block to yield with arguments, but #{positive_failure_reason}" end
@private
# File lib/rspec/matchers/built_in/yield.rb, line 270 def failure_message_when_negated "expected given block not to yield with arguments, but #{negative_failure_reason}" end
@private
# File lib/rspec/matchers/built_in/yield.rb, line 252 def matches?(block) @probe = YieldProbe.probe(block) return false unless @probe.has_block? @actual = @probe.single_yield_args @probe.yielded_once?(:yield_with_args) && args_match? end
@private
# File lib/rspec/matchers/built_in/yield.rb, line 282 def supports_block_expectations? true end
# File lib/rspec/matchers/built_in/yield.rb, line 325 def all_args_match? values_match?(@expected, @actual) end
# File lib/rspec/matchers/built_in/yield.rb, line 310 def args_match? if @expected.empty? # expect {...}.to yield_with_args @positive_args_failure = "yielded with no arguments" if @actual.empty? return !@actual.empty? end unless match = all_args_match? @positive_args_failure = "yielded with unexpected arguments" + "\nexpected: #{surface_descriptions_in(@expected).inspect}" + "\n got: #{@actual.inspect}" end match end
# File lib/rspec/matchers/built_in/yield.rb, line 294 def expected_arg_description @expected.map { |e| description_of e }.join(", ") end
# File lib/rspec/matchers/built_in/yield.rb, line 298 def negative_failure_reason if !@probe.has_block? "was not a block" elsif all_args_match? "yielded with expected arguments" + "\nexpected not: #{surface_descriptions_in(@expected).inspect}" + "\n got: #{@actual.inspect}" else "did" end end
# File lib/rspec/matchers/built_in/yield.rb, line 288 def positive_failure_reason return "was not a block" unless @probe.has_block? return "did not yield" if @probe.num_yields.zero? @positive_args_failure end