File: sequence.rb

package info (click to toggle)
ruby-mocha 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 12,324; javascript: 499; makefile: 14
file content (48 lines) | stat: -rw-r--r-- 1,044 bytes parent folder | download
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
# frozen_string_literal: true

module Mocha
  # Used to constrain the order in which expectations can occur.
  #
  # @see API#sequence
  # @see Expectation#in_sequence
  class Sequence
    # @private
    class InSequenceOrderingConstraint
      def initialize(sequence, index)
        @sequence = sequence
        @index = index
      end

      def allows_invocation_now?
        @sequence.satisfied_to_index?(@index)
      end

      def mocha_inspect
        "in sequence #{@sequence.mocha_inspect}"
      end
    end

    # @private
    def initialize(name)
      @name = name
      @expectations = []
    end

    # @private
    def constrain_as_next_in_sequence(expectation)
      index = @expectations.length
      @expectations << expectation
      expectation.add_ordering_constraint(InSequenceOrderingConstraint.new(self, index))
    end

    # @private
    def satisfied_to_index?(index)
      @expectations[0...index].all?(&:satisfied?)
    end

    # @private
    def mocha_inspect
      @name.mocha_inspect.to_s
    end
  end
end