lib/rspec/matchers/built_in/yield.rb in rspec-expectations-3.8.4 vs lib/rspec/matchers/built_in/yield.rb in rspec-expectations-3.8.5

- old
+ new

@@ -8,11 +8,10 @@ # yield matchers is used. Provides information about # the yield behavior of the object-under-test. class YieldProbe def self.probe(block, &callback) probe = new(block, &callback) - return probe unless probe.has_block? probe.probe end attr_accessor :num_yields, :yielded_args @@ -22,14 +21,10 @@ @used = false self.num_yields = 0 self.yielded_args = [] end - def has_block? - Proc === @block - end - def probe assert_valid_expect_block! @block.call(self) assert_used! self @@ -150,18 +145,16 @@ end # @private def matches?(block) @probe = YieldProbe.probe(block) - return false unless @probe.has_block? - @probe.num_yields.__send__(@expectation_type, @expected_yields_count) end # @private def does_not_match?(block) - !matches?(block) && @probe.has_block? + !matches?(block) end # @api private # @return [String] def failure_message @@ -177,10 +170,15 @@ # @private def supports_block_expectations? true end + # @private + def supports_value_expectations? + false + end + private def set_expected_yields_count(relativity, n) @expectation_type = relativity @expected_yields_count = case n @@ -190,11 +188,10 @@ when :thrice then 3 end end def failure_reason - return ' but was not a block' unless @probe.has_block? return '' unless @expected_yields_count " #{human_readable_expectation_type}#{human_readable_count(@expected_yields_count)}" \ " but yielded #{human_readable_count(@probe.num_yields)}" end @@ -220,17 +217,16 @@ # Not intended to be instantiated directly. class YieldWithNoArgs < BaseMatcher # @private def matches?(block) @probe = YieldProbe.probe(block) - return false unless @probe.has_block? @probe.yielded_once?(:yield_with_no_args) && @probe.single_yield_args.empty? end # @private def does_not_match?(block) - !matches?(block) && @probe.has_block? + !matches?(block) end # @private def failure_message "expected given block to yield with no arguments, but #{positive_failure_reason}" @@ -244,20 +240,23 @@ # @private def supports_block_expectations? true end + # @private + def supports_value_expectations? + false + end + private def positive_failure_reason - return 'was not a block' unless @probe.has_block? return 'did not yield' if @probe.num_yields.zero? "yielded with arguments: #{description_of @probe.single_yield_args}" end def negative_failure_reason - return 'was not a block' unless @probe.has_block? 'did' end end # @api private @@ -274,18 +273,17 @@ @probe = YieldProbe.new(block) do @actual = @probe.single_yield_args @actual_formatted = actual_formatted @args_matched_when_yielded &&= args_currently_match? end - return false unless @probe.has_block? @probe.probe @probe.yielded_once?(:yield_with_args) && @args_matched_when_yielded end # @private def does_not_match?(block) - !matches?(block) && @probe.has_block? + !matches?(block) end # @private def failure_message "expected given block to yield with arguments, but #{positive_failure_reason}" @@ -306,26 +304,28 @@ # @private def supports_block_expectations? true end + # @private + def supports_value_expectations? + false + end + private 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 def expected_arg_description @expected.map { |e| description_of e }.join(', ') end def negative_failure_reason - if !@probe.has_block? - 'was not a block' - elsif @args_matched_when_yielded && !@expected.empty? + if @args_matched_when_yielded && !@expected.empty? 'yielded with expected arguments' \ "\nexpected not: #{surface_descriptions_in(@expected).inspect}" \ "\n got: #{@actual_formatted}" else 'did' @@ -373,16 +373,15 @@ @actual << arg_or_args args_matched_when_yielded &&= values_match?(@expected[yield_count], arg_or_args) yield_count += 1 end - return false unless @probe.has_block? args_matched_when_yielded && yield_count == @expected.length end def does_not_match?(block) - !matches?(block) && @probe.has_block? + !matches?(block) end # @private def failure_message 'expected given block to yield successively with arguments, ' \ @@ -403,26 +402,27 @@ # @private def supports_block_expectations? true end + # @private + def supports_value_expectations? + false + end + private def expected_arg_description @expected.map { |e| description_of e }.join(', ') end def positive_failure_reason - return 'was not a block' unless @probe.has_block? - 'yielded with unexpected arguments' \ "\nexpected: #{surface_descriptions_in(@expected).inspect}" \ "\n got: [#{@actual_formatted.join(", ")}]" end def negative_failure_reason - return 'was not a block' unless @probe.has_block? - 'yielded with expected arguments' \ "\nexpected not: #{surface_descriptions_in(@expected).inspect}" \ "\n got: [#{@actual_formatted.join(", ")}]" end end