Class: Mocha::Cardinality

Inherits:
Object show all
Defined in:
lib/mocha/cardinality.rb

Constant Summary collapse

INFINITY =
1 / 0.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(required, maximum) ⇒ Cardinality

Returns a new instance of Cardinality.



30
31
32
# File 'lib/mocha/cardinality.rb', line 30

def initialize(required, maximum)
  @required, @maximum = required, maximum
end

Class Method Details

.at_least(count) ⇒ Object



13
14
15
# File 'lib/mocha/cardinality.rb', line 13

def at_least(count)
  new(count, INFINITY)
end

.at_most(count) ⇒ Object



17
18
19
# File 'lib/mocha/cardinality.rb', line 17

def at_most(count)
  new(0, count)
end

.exactly(count) ⇒ Object



9
10
11
# File 'lib/mocha/cardinality.rb', line 9

def exactly(count)
  new(count, count)
end

.times(range_or_count) ⇒ Object



21
22
23
24
25
26
# 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

Instance Method Details

#allowed_any_number_of_times?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/mocha/cardinality.rb', line 50

def allowed_any_number_of_times?
  required == 0 && infinite?(maximum)
end

#invocations_allowed?(invocation_count) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/mocha/cardinality.rb', line 34

def invocations_allowed?(invocation_count)
  invocation_count < maximum
end

#mocha_inspectObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/mocha/cardinality.rb', line 42

def needs_verifying?
  !allowed_any_number_of_times?
end

#satisfied?(invocations_so_far) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/mocha/cardinality.rb', line 38

def satisfied?(invocations_so_far)
  invocations_so_far >= required
end

#used?(invocation_count) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/mocha/cardinality.rb', line 54

def used?(invocation_count)
  (invocation_count > 0) || (maximum == 0)
end

#verified?(invocation_count) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/mocha/cardinality.rb', line 46

def verified?(invocation_count)
  (invocation_count >= required) && (invocation_count <= maximum)
end