File: receive_message_chain.rb

package info (click to toggle)
ruby-rspec 3.9.0c2e2m1s3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,612 kB
  • sloc: ruby: 67,456; sh: 1,572; makefile: 98
file content (82 lines) | stat: -rw-r--r-- 2,438 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
RSpec::Support.require_rspec_mocks 'matchers/expectation_customization'

module RSpec
  module Mocks
    module Matchers
      # @private
      class ReceiveMessageChain
        include Matcher

        def initialize(chain, &block)
          @chain = chain
          @block = block
          @recorded_customizations  = []
        end

        [:with, :and_return, :and_throw, :and_raise, :and_yield, :and_call_original].each do |msg|
          define_method(msg) do |*args, &block|
            @recorded_customizations << ExpectationCustomization.new(msg, args, block)
            self
          end
        end

        def name
          "receive_message_chain"
        end

        def description
          "receive message chain #{formatted_chain}"
        end

        def setup_allowance(subject, &block)
          chain = StubChain.stub_chain_on(subject, *@chain, &(@block || block))
          replay_customizations(chain)
        end

        def setup_any_instance_allowance(subject, &block)
          proxy = ::RSpec::Mocks.space.any_instance_proxy_for(subject)
          chain = proxy.stub_chain(*@chain, &(@block || block))
          replay_customizations(chain)
        end

        def setup_any_instance_expectation(subject, &block)
          proxy = ::RSpec::Mocks.space.any_instance_proxy_for(subject)
          chain = proxy.expect_chain(*@chain, &(@block || block))
          replay_customizations(chain)
        end

        def setup_expectation(subject, &block)
          chain = ExpectChain.expect_chain_on(subject, *@chain, &(@block || block))
          replay_customizations(chain)
        end

        def setup_negative_expectation(*_args)
          raise NegationUnsupportedError,
                "`expect(...).not_to receive_message_chain` is not supported " \
                "since it doesn't really make sense. What would it even mean?"
        end

        alias matches? setup_expectation
        alias does_not_match? setup_negative_expectation

      private

        def replay_customizations(chain)
          @recorded_customizations.each do |customization|
            customization.playback_onto(chain)
          end
        end

        def formatted_chain
          @formatted_chain ||= @chain.map do |part|
            if Hash === part
              part.keys.first.to_s
            else
              part.to_s
            end
          end.join(".")
        end
      end
    end
  end
end