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
  @expectation_type = @expected_yields_count = nil
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?
  return @probe.num_yields > 0 unless @expectation_type
  @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

count_constraint_to_number(n) click to toggle source
# File lib/rspec/matchers/built_in/yield.rb, line 191
def count_constraint_to_number(n)
  case n
  when Numeric then n
  when :once then 1
  when :twice then 2
  when :thrice then 3
  else
    raise ArgumentError, "Expected a number, :once, :twice or :thrice," \
      " but got #{n}"
  end
end
failure_reason() click to toggle source
# File lib/rspec/matchers/built_in/yield.rb, line 203
def failure_reason
  return ' but was not a block' unless @probe.has_block?
  "#{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 217
def human_readable_count(count)
  case count
  when nil then ''
  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 209
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)
  raise "Multiple count constraints are not supported" if @expectation_type

  @expectation_type = relativity
  @expected_yields_count = count_constraint_to_number(n)
end