File: cardinality.rb

package info (click to toggle)
ruby-mocha 0.11.3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,300 kB
  • sloc: ruby: 9,935; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 1,962 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module Mocha

  class Cardinality

    INFINITY = 1 / 0.0

    class << self

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

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

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

      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

    end

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

    def invocations_allowed?(invocation_count)
      invocation_count < maximum
    end

    def satisfied?(invocations_so_far)
      invocations_so_far >= required
    end

    def needs_verifying?
      !allowed_any_number_of_times?
    end

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

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

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

    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

    protected

    attr_reader :required, :maximum

    def times(number)
      case number
        when 0 then "no times"
        when 1 then "once"
        when 2 then "twice"
        else "#{number} times"
      end
    end

    def infinite?(number)
      number.respond_to?(:infinite?) && number.infinite?
    end

  end

end