class Mocha::Cardinality

Constants

INFINITY

Attributes

maximum[R]
required[R]

Public Class Methods

at_least(count) click to toggle source
# File lib/mocha/cardinality.rb, line 10
def at_least(count)
  new(count, INFINITY)
end
at_most(count) click to toggle source
# File lib/mocha/cardinality.rb, line 14
def at_most(count)
  new(0, count)
end
exactly(count) click to toggle source
# File lib/mocha/cardinality.rb, line 6
def exactly(count)
  new(count, count)
end
new(required, maximum) click to toggle source
# File lib/mocha/cardinality.rb, line 26
def initialize(required, maximum)
  @required = required
  @maximum = maximum
  @invocations = []
end
times(range_or_count) click to toggle source
# File lib/mocha/cardinality.rb, line 18
def times(range_or_count)
  case range_or_count
  when Range then new(range_or_count.first, range_or_count.last)
  else new(range_or_count, range_or_count)
  end
end

Public Instance Methods

<<(invocation) click to toggle source
# File lib/mocha/cardinality.rb, line 32
def <<(invocation)
  @invocations << invocation
end
actual_invocations() click to toggle source
# File lib/mocha/cardinality.rb, line 82
def actual_invocations
  @invocations.map(&:full_description).join
end
allowed_any_number_of_times?() click to toggle source
# File lib/mocha/cardinality.rb, line 52
def allowed_any_number_of_times?
  required.zero? && infinite?(maximum)
end
anticipated_times() click to toggle source

rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity

# File lib/mocha/cardinality.rb, line 61
def anticipated_times
  if allowed_any_number_of_times?
    'allowed any number of times'
  elsif required.zero? && maximum.zero?
    "expected #{times(maximum)}"
  elsif required == maximum
    "expected exactly #{times(required)}"
  elsif infinite?(maximum)
    "expected at least #{times(required)}"
  elsif required.zero?
    "expected at most #{times(maximum)}"
  else
    "expected between #{required} and #{times(maximum)}"
  end
end
invocations_allowed?() click to toggle source
# File lib/mocha/cardinality.rb, line 36
def invocations_allowed?
  @invocations.size < maximum
end
invoked_times() click to toggle source

rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity

# File lib/mocha/cardinality.rb, line 78
def invoked_times
  "invoked #{times(@invocations.size)}"
end
needs_verifying?() click to toggle source
# File lib/mocha/cardinality.rb, line 44
def needs_verifying?
  !allowed_any_number_of_times?
end
satisfied?() click to toggle source
# File lib/mocha/cardinality.rb, line 40
def satisfied?
  @invocations.size >= required
end
used?() click to toggle source
# File lib/mocha/cardinality.rb, line 56
def used?
  @invocations.any? || maximum.zero?
end
verified?() click to toggle source
# File lib/mocha/cardinality.rb, line 48
def verified?
  (@invocations.size >= required) && (@invocations.size <= maximum)
end

Protected Instance Methods

infinite?(number) click to toggle source
# File lib/mocha/cardinality.rb, line 99
def infinite?(number)
  number.respond_to?(:infinite?) && number.infinite?
end
times(number) click to toggle source
# File lib/mocha/cardinality.rb, line 90
def times(number)
  case number
  when 0 then 'never'
  when 1 then 'once'
  when 2 then 'twice'
  else "#{number} times"
  end
end