@api private Provides the implementation for `yield_control`. Not intended to be instantiated directly.
# File lib/rspec/matchers/built_in/yield.rb, line 82 def initialize @expectation_type = nil @expected_yields_count = nil end
@api public Specifies the minimum number of times the method is expected to yield
# File lib/rspec/matchers/built_in/yield.rb, line 117 def at_least(number) set_expected_yields_count(:>=, number) self end
@api public Specifies the maximum number of times the method is expected to yield
# File lib/rspec/matchers/built_in/yield.rb, line 110 def at_most(number) set_expected_yields_count(:<=, number) self end
@private
# File lib/rspec/matchers/built_in/yield.rb, line 141 def does_not_match?(block) !matches?(block) && @probe.has_block? end
@api public Specifies that the method is expected to yield the given number of times.
# File lib/rspec/matchers/built_in/yield.rb, line 103 def exactly(number) set_expected_yields_count(:==, number) self end
@api private @return [String]
# File lib/rspec/matchers/built_in/yield.rb, line 147 def failure_message 'expected given block to yield control' + failure_reason end
@api private @return [String]
# File lib/rspec/matchers/built_in/yield.rb, line 153 def failure_message_when_negated 'expected given block not to yield control' + failure_reason end
@private
# File lib/rspec/matchers/built_in/yield.rb, line 129 def matches?(block) @probe = YieldProbe.probe(block) return false unless @probe.has_block? if @expectation_type @probe.num_yields.__send__(@expectation_type, @expected_yields_count) else @probe.yielded_once?(:yield_control) end end
@api public Specifies that the method is expected to yield once.
# File lib/rspec/matchers/built_in/yield.rb, line 89 def once exactly(1) self end
@private
# File lib/rspec/matchers/built_in/yield.rb, line 158 def supports_block_expectations? true end
@api public No-op. Provides syntactic sugar.
# File lib/rspec/matchers/built_in/yield.rb, line 124 def times self end
@api public Specifies that the method is expected to yield once.
# File lib/rspec/matchers/built_in/yield.rb, line 96 def twice exactly(2) self end
# File lib/rspec/matchers/built_in/yield.rb, line 173 def failure_reason return " but was not a block" unless @probe.has_block? return '' unless @expected_yields_count " #{human_readable_expecation_type}#{human_readable_count}" end
# File lib/rspec/matchers/built_in/yield.rb, line 187 def human_readable_count case @expected_yields_count when 1 then "once" when 2 then "twice" else "#{@expected_yields_count} times" end end
# File lib/rspec/matchers/built_in/yield.rb, line 179 def human_readable_expecation_type case @expectation_type when :<= then 'at most ' when :>= then 'at least ' else '' end end
# File lib/rspec/matchers/built_in/yield.rb, line 164 def set_expected_yields_count(relativity, n) @expectation_type = relativity @expected_yields_count = case n when Numeric then n when :once then 1 when :twice then 2 end end