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 99
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 140
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 133
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 160
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 126
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 166
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 172
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 152
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 105
def once
  exactly(1)
  self
end
supports_block_expectations?() click to toggle source

@private

# File lib/rspec/matchers/built_in/yield.rb, line 177
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 119
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 147
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 112
def twice
  exactly(2)
  self
end

Private Instance Methods

failure_reason() click to toggle source
# File lib/rspec/matchers/built_in/yield.rb, line 193
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 208
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 200
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 183
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