File: chain.rb

package info (click to toggle)
ruby-rspec 3.12.0c0e1m1s0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,752 kB
  • sloc: ruby: 69,818; sh: 1,861; makefile: 99
file content (111 lines) | stat: -rw-r--r-- 2,997 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
module RSpec
  module Mocks
    # @private
    module AnyInstance
      # @private
      class Chain
        def initialize(recorder, *args, &block)
          @recorder          = recorder
          @expectation_args  = args
          @expectation_block = block
          @argument_list_matcher = ArgumentListMatcher::MATCH_ALL
        end

        # @private
        #
        # Provides convenience methods for recording customizations on message
        # expectations.
        module Customizations
          # @macro [attach] record
          #   @method $1(*args, &block)
          #   Records the `$1` message for playback against an instance that
          #   invokes a method stubbed or mocked using `any_instance`.
          #
          #   @see RSpec::Mocks::MessageExpectation#$1
          #
          def self.record(method_name)
            define_method(method_name) do |*args, &block|
              record(method_name, *args, &block)
            end
          end

          record :and_return
          record :and_raise
          record :and_throw
          record :and_yield
          record :and_call_original
          record :and_wrap_original
          record :with
          record :once
          record :twice
          record :thrice
          record :exactly
          record :times
          record :time
          record :never
          record :at_least
          record :at_most
        end

        include Customizations

        # @private
        def playback!(instance)
          message_expectation = create_message_expectation_on(instance)
          messages.inject(message_expectation) do |object, message|
            object.__send__(*message.first, &message.last)
          end
        end

        # @private
        def constrained_to_any_of?(*constraints)
          constraints.any? do |constraint|
            messages.any? do |message|
              message.first.first == constraint
            end
          end
        end

        # @private
        def matches_args?(*args)
          @argument_list_matcher.args_match?(*args)
        end

        # @private
        def expectation_fulfilled!
          @expectation_fulfilled = true
        end

        def never
          AnyInstance.error_generator.raise_double_negation_error("expect_any_instance_of(MyClass)") if negated?
          super
        end

        def with(*args, &block)
          @argument_list_matcher = ArgumentListMatcher.new(*args)
          super
        end

      private

        def negated?
          messages.any? { |(message, *_), _| message == :never }
        end

        def messages
          @messages ||= []
        end

        def last_message
          messages.last.first.first unless messages.empty?
        end

        def record(rspec_method_name, *args, &block)
          verify_invocation_order(rspec_method_name, *args, &block)
          messages << [args.unshift(rspec_method_name), block]
          self
        end
      end
    end
  end
end