File: respond_to.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (200 lines) | stat: -rw-r--r-- 6,926 bytes parent folder | download | duplicates (2)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
RSpec::Support.require_rspec_support "method_signature_verifier"

module RSpec
  module Matchers
    module BuiltIn
      # @api private
      # Provides the implementation for `respond_to`.
      # Not intended to be instantiated directly.
      class RespondTo < BaseMatcher
        def initialize(*names)
          @names = names
          @expected_arity = nil
          @expected_keywords = []
          @ignoring_method_signature_failure = false
          @unlimited_arguments = nil
          @arbitrary_keywords = nil
        end

        # @api public
        # Specifies the number of expected arguments.
        #
        # @example
        #   expect(obj).to respond_to(:message).with(3).arguments
        def with(n)
          @expected_arity = n
          self
        end

        # @api public
        # Specifies keyword arguments, if any.
        #
        # @example
        #   expect(obj).to respond_to(:message).with_keywords(:color, :shape)
        # @example with an expected number of arguments
        #   expect(obj).to respond_to(:message).with(3).arguments.and_keywords(:color, :shape)
        def with_keywords(*keywords)
          @expected_keywords = keywords
          self
        end
        alias :and_keywords :with_keywords

        # @api public
        # Specifies that the method accepts any keyword, i.e. the method has
        #   a splatted keyword parameter of the form **kw_args.
        #
        # @example
        #   expect(obj).to respond_to(:message).with_any_keywords
        def with_any_keywords
          @arbitrary_keywords = true
          self
        end
        alias :and_any_keywords :with_any_keywords

        # @api public
        # Specifies that the number of arguments has no upper limit, i.e. the
        #   method has a splatted parameter of the form *args.
        #
        # @example
        #   expect(obj).to respond_to(:message).with_unlimited_arguments
        def with_unlimited_arguments
          @unlimited_arguments = true
          self
        end
        alias :and_unlimited_arguments :with_unlimited_arguments

        # @api public
        # No-op. Intended to be used as syntactic sugar when using `with`.
        #
        # @example
        #   expect(obj).to respond_to(:message).with(3).arguments
        def argument
          self
        end
        alias :arguments :argument

        # @private
        def matches?(actual)
          find_failing_method_names(actual, :reject).empty?
        end

        # @private
        def does_not_match?(actual)
          find_failing_method_names(actual, :select).empty?
        end

        # @api private
        # @return [String]
        def failure_message
          "expected #{actual_formatted} to respond to #{@failing_method_names.map { |name| description_of(name) }.join(', ')}#{with_arity}"
        end

        # @api private
        # @return [String]
        def failure_message_when_negated
          failure_message.sub(/to respond to/, 'not to respond to')
        end

        # @api private
        # @return [String]
        def description
          "respond to #{pp_names}#{with_arity}"
        end

        # @api private
        # Used by other matchers to suppress a check
        def ignoring_method_signature_failure!
          @ignoring_method_signature_failure = true
        end

      private

        def find_failing_method_names(actual, filter_method)
          @actual = actual
          @failing_method_names = @names.__send__(filter_method) do |name|
            @actual.respond_to?(name) && matches_arity?(actual, name)
          end
        end

        def matches_arity?(actual, name)
          ArityCheck.new(@expected_arity, @expected_keywords, @arbitrary_keywords, @unlimited_arguments).matches?(actual, name)
        rescue NameError
          return true if @ignoring_method_signature_failure
          raise ArgumentError, "The #{matcher_name} matcher requires that " \
                               "the actual object define the method(s) in " \
                               "order to check arity, but the method " \
                               "`#{name}` is not defined. Remove the arity " \
                               "check or define the method to continue."
        end

        def with_arity
          str = ''.dup
          str << " with #{with_arity_string}" if @expected_arity
          str << " #{str.length == 0 ? 'with' : 'and'} #{with_keywords_string}" if @expected_keywords && @expected_keywords.count > 0
          str << " #{str.length == 0 ? 'with' : 'and'} unlimited arguments" if @unlimited_arguments
          str << " #{str.length == 0 ? 'with' : 'and'} any keywords" if @arbitrary_keywords
          str
        end

        def with_arity_string
          "#{@expected_arity} argument#{@expected_arity == 1 ? '' : 's'}"
        end

        def with_keywords_string
          kw_str = case @expected_keywords.count
                   when 1
                     @expected_keywords.first.inspect
                   when 2
                     @expected_keywords.map(&:inspect).join(' and ')
                   else
                     "#{@expected_keywords[0...-1].map(&:inspect).join(', ')}, and #{@expected_keywords.last.inspect}"
                   end

          "keyword#{@expected_keywords.count == 1 ? '' : 's'} #{kw_str}"
        end

        def pp_names
          @names.length == 1 ? "##{@names.first}" : description_of(@names)
        end

        # @private
        class ArityCheck
          def initialize(expected_arity, expected_keywords, arbitrary_keywords, unlimited_arguments)
            expectation = Support::MethodSignatureExpectation.new

            if expected_arity.is_a?(Range)
              expectation.min_count = expected_arity.min
              expectation.max_count = expected_arity.max
            else
              expectation.min_count = expected_arity
            end

            expectation.keywords = expected_keywords
            expectation.expect_unlimited_arguments = unlimited_arguments
            expectation.expect_arbitrary_keywords  = arbitrary_keywords
            @expectation = expectation
          end

          def matches?(actual, name)
            return true if @expectation.empty?
            verifier_for(actual, name).with_expectation(@expectation).valid?
          end

          def verifier_for(actual, name)
            Support::StrictSignatureVerifier.new(method_signature_for(actual, name))
          end

          def method_signature_for(actual, name)
            method_handle = Support.method_handle_for(actual, name)

            if name == :new && method_handle.owner === ::Class && ::Class === actual
              Support::MethodSignature.new(actual.instance_method(:initialize))
            else
              Support::MethodSignature.new(method_handle)
            end
          end
        end
      end
    end
  end
end