File: base_method_dispatch.rb

package info (click to toggle)
ruby-rr 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,424 kB
  • sloc: ruby: 11,405; makefile: 7
file content (101 lines) | stat: -rw-r--r-- 2,931 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
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
module RR
  module MethodDispatches
    class BaseMethodDispatch
      extend Forwardable
      include Space::Reader

      attr_reader :args, :kwargs, :block, :double

      def call
        raise NotImplementedError
      end

    protected
      def find_double_to_attempt
        matches = DoubleMatches.new(doubles).find_all_matches(args, kwargs)

        unless matches.exact_terminal_doubles_to_attempt.empty?
          return matches.exact_terminal_doubles_to_attempt.first
        end

        unless matches.exact_non_terminal_doubles_to_attempt.empty?
          return matches.exact_non_terminal_doubles_to_attempt.last
        end

        unless matches.wildcard_terminal_doubles_to_attempt.empty?
          return matches.wildcard_terminal_doubles_to_attempt.first
        end

        unless matches.wildcard_non_terminal_doubles_to_attempt.empty?
          return matches.wildcard_non_terminal_doubles_to_attempt.last
        end

        unless matches.matching_doubles.empty?
          return matches.matching_doubles.first # This will raise a TimesCalledError
        end

        return nil
      end

      def call_yields
        if definition.yields_value
          if block
            block.call(*definition.yields_value)
          else
            raise ArgumentError, "A Block must be passed into the method call when using yields"
          end
        end
      end

      if KeywordArguments.fully_supported?
        def call_original_method_missing
          subject.__send__(
            MethodMissingDispatch.original_method_missing_alias_name,
            method_name,
            *args,
            **kwargs,
            &block
          )
        end
      else
        def call_original_method_missing
          subject.__send__(
            MethodMissingDispatch.original_method_missing_alias_name,
            method_name,
            *args,
            &block
          )
        end
      end

      def implementation_is_original_method?
        double.implementation_is_original_method?
      end

      def extract_subject_from_return_value(return_value)
        case return_value
        when DoubleDefinitions::DoubleDefinition
          return_value.root_subject
        when DoubleDefinitions::DoubleDefinitionCreateBlankSlate
          return_value.__double_definition_create__.root_subject
        else
          return_value
        end
      end

      def double_not_found_error
        message =
          "On subject #{subject},\n" <<
          "unexpected method invocation:\n" <<
          "  #{Double.formatted_name(method_name, args, kwargs)}\n" <<
          "expected invocations:\n" <<
          Double.list_message_part(doubles)
        raise RR::Errors.build_error(:DoubleNotFoundError, message)
      end

      def_delegators :definition, :after_call_proc
      def_delegators :double, :definition
      def_delegators :double_injection, :doubles
    end
  end
end