class RSpec::Matchers::BuiltIn::YieldControl
@api private Provides the implementation for `yield_control`. Not intended to be instantiated directly.
Public Class Methods
new()
click to toggle source
# File lib/rspec/matchers/built_in/yield.rb, line 100 def initialize at_least(:once) end
Public Instance Methods
at_least(number)
click to toggle source
@api public Specifies the minimum number of times the method is expected to yield
# File lib/rspec/matchers/built_in/yield.rb, line 141 def at_least(number) set_expected_yields_count(:>=, number) self end
at_most(number)
click to toggle source
@api public Specifies the maximum number of times the method is expected to yield
# File lib/rspec/matchers/built_in/yield.rb, line 134 def at_most(number) set_expected_yields_count(:<=, number) self end
does_not_match?(block)
click to toggle source
@private
# File lib/rspec/matchers/built_in/yield.rb, line 161 def does_not_match?(block) !matches?(block) && @probe.has_block? end
exactly(number)
click to toggle source
@api public Specifies that the method is expected to yield the given number of times.
# File lib/rspec/matchers/built_in/yield.rb, line 127 def exactly(number) set_expected_yields_count(:==, number) self end
failure_message()
click to toggle source
@api private @return [String]
# File lib/rspec/matchers/built_in/yield.rb, line 167 def failure_message 'expected given block to yield control' + failure_reason end
failure_message_when_negated()
click to toggle source
@api private @return [String]
# File lib/rspec/matchers/built_in/yield.rb, line 173 def failure_message_when_negated 'expected given block not to yield control' + failure_reason end
matches?(block)
click to toggle source
@private
# File lib/rspec/matchers/built_in/yield.rb, line 153 def matches?(block) @probe = YieldProbe.probe(block) return false unless @probe.has_block? @probe.num_yields.__send__(@expectation_type, @expected_yields_count) end
once()
click to toggle source
@api public Specifies that the method is expected to yield once.
# File lib/rspec/matchers/built_in/yield.rb, line 106 def once exactly(1) self end
supports_block_expectations?()
click to toggle source
@private
# File lib/rspec/matchers/built_in/yield.rb, line 178 def supports_block_expectations? true end
thrice()
click to toggle source
@api public Specifies that the method is expected to yield thrice.
# File lib/rspec/matchers/built_in/yield.rb, line 120 def thrice exactly(3) self end
times()
click to toggle source
@api public No-op. Provides syntactic sugar.
# File lib/rspec/matchers/built_in/yield.rb, line 148 def times self end
twice()
click to toggle source
@api public Specifies that the method is expected to yield twice.
# File lib/rspec/matchers/built_in/yield.rb, line 113 def twice exactly(2) self end
Private Instance Methods
failure_reason()
click to toggle source
# File lib/rspec/matchers/built_in/yield.rb, line 194 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
human_readable_count(count)
click to toggle source
# File lib/rspec/matchers/built_in/yield.rb, line 209 def human_readable_count(count) case count when 1 then 'once' when 2 then 'twice' else "#{count} times" end end
human_readable_expectation_type()
click to toggle source
# File lib/rspec/matchers/built_in/yield.rb, line 201 def human_readable_expectation_type case @expectation_type when :<= then 'at most ' when :>= then 'at least ' else '' end end
set_expected_yields_count(relativity, n)
click to toggle source
# File lib/rspec/matchers/built_in/yield.rb, line 184 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 when :thrice then 3 end end