class Mocha::Cardinality

Constants

INFINITY

Attributes

maximum[R]
required[R]

Public Class Methods

new(required = 0, maximum = INFINITY) click to toggle source
# File lib/mocha/cardinality.rb, line 5
def initialize(required = 0, maximum = INFINITY)
  update(required, maximum)
  @invocations = []
end

Public Instance Methods

<<(invocation) click to toggle source
# File lib/mocha/cardinality.rb, line 29
def <<(invocation)
  @invocations << invocation
end
actual_invocations() click to toggle source
# File lib/mocha/cardinality.rb, line 79
def actual_invocations
  @invocations.map(&:full_description).join
end
allowed_any_number_of_times?() click to toggle source
# File lib/mocha/cardinality.rb, line 49
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 58
def anticipated_times
  if allowed_any_number_of_times?
    'allowed any number of times'
  elsif required.zero? && maximum.zero?
    "expected #{count(maximum)}"
  elsif required == maximum
    "expected exactly #{count(required)}"
  elsif infinite?(maximum)
    "expected at least #{count(required)}"
  elsif required.zero?
    "expected at most #{count(maximum)}"
  else
    "expected between #{required} and #{count(maximum)}"
  end
end
at_least(count) click to toggle source
# File lib/mocha/cardinality.rb, line 14
def at_least(count)
  update(count, INFINITY)
end
at_most(count) click to toggle source
# File lib/mocha/cardinality.rb, line 18
def at_most(count)
  update(0, count)
end
exactly(count) click to toggle source
# File lib/mocha/cardinality.rb, line 10
def exactly(count)
  update(count, count)
end
invocations_allowed?() click to toggle source
# File lib/mocha/cardinality.rb, line 33
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 75
def invoked_times
  "invoked #{count(@invocations.size)}"
end
needs_verifying?() click to toggle source
# File lib/mocha/cardinality.rb, line 41
def needs_verifying?
  !allowed_any_number_of_times?
end
satisfied?() click to toggle source
# File lib/mocha/cardinality.rb, line 37
def satisfied?
  @invocations.size >= required
end
times(range_or_count) click to toggle source
# File lib/mocha/cardinality.rb, line 22
def times(range_or_count)
  case range_or_count
  when Range then update(range_or_count.first, range_or_count.last)
  else update(range_or_count, range_or_count)
  end
end
used?() click to toggle source
# File lib/mocha/cardinality.rb, line 53
def used?
  @invocations.any? || maximum.zero?
end
verified?() click to toggle source
# File lib/mocha/cardinality.rb, line 45
def verified?
  (@invocations.size >= required) && (@invocations.size <= maximum)
end

Protected Instance Methods

count(number) click to toggle source
# File lib/mocha/cardinality.rb, line 87
def count(number)
  case number
  when 0 then 'never'
  when 1 then 'once'
  when 2 then 'twice'
  else "#{number} times"
  end
end
infinite?(number) click to toggle source
# File lib/mocha/cardinality.rb, line 102
def infinite?(number)
  number.respond_to?(:infinite?) && number.infinite?
end
update(required, maximum) click to toggle source
# File lib/mocha/cardinality.rb, line 96
def update(required, maximum)
  @required = required
  @maximum = maximum
  self
end