class Mocha::Cardinality

Constants

INFINITY

Attributes

maximum[R]
required[R]

Public Class Methods

new(required, maximum) click to toggle source
# File lib/mocha/cardinality.rb, line 30
def initialize(required, maximum)
  @required, @maximum = required, maximum
end

Public Instance Methods

allowed_any_number_of_times?() click to toggle source
# File lib/mocha/cardinality.rb, line 50
def allowed_any_number_of_times?
  required == 0 && infinite?(maximum)
end
at_least(count) click to toggle source
# File lib/mocha/cardinality.rb, line 13
def at_least(count)
  new(count, INFINITY)
end
at_most(count) click to toggle source
# File lib/mocha/cardinality.rb, line 17
def at_most(count)
  new(0, count)
end
exactly(count) click to toggle source
# File lib/mocha/cardinality.rb, line 9
def exactly(count)
  new(count, count)
end
invocations_allowed?(invocation_count) click to toggle source
# File lib/mocha/cardinality.rb, line 34
def invocations_allowed?(invocation_count)
  invocation_count < maximum
end
mocha_inspect() click to toggle source
# File lib/mocha/cardinality.rb, line 58
def mocha_inspect
  if allowed_any_number_of_times?
    "allowed any number of times"
  else
    if required == 0 && maximum == 0
      "expected never"
    elsif required == maximum
      "expected exactly #{times(required)}"
    elsif infinite?(maximum)
      "expected at least #{times(required)}"
    elsif required == 0
      "expected at most #{times(maximum)}"
    else
      "expected between #{required} and #{times(maximum)}"
    end
  end
end
needs_verifying?() click to toggle source
# File lib/mocha/cardinality.rb, line 42
def needs_verifying?
  !allowed_any_number_of_times?
end
satisfied?(invocations_so_far) click to toggle source
# File lib/mocha/cardinality.rb, line 38
def satisfied?(invocations_so_far)
  invocations_so_far >= required
end
times(range_or_count) click to toggle source
# File lib/mocha/cardinality.rb, line 21
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
used?(invocation_count) click to toggle source
# File lib/mocha/cardinality.rb, line 54
def used?(invocation_count)
  (invocation_count > 0) || (maximum == 0)
end
verified?(invocation_count) click to toggle source
# File lib/mocha/cardinality.rb, line 46
def verified?(invocation_count)
  (invocation_count >= required) && (invocation_count <= maximum)
end

Protected Instance Methods

infinite?(number) click to toggle source
# File lib/mocha/cardinality.rb, line 89
def infinite?(number)
  number.respond_to?(:infinite?) && number.infinite?
end