Class: RubyIndexer::EnhancementTest

Inherits:
TestCase
  • Object
show all
Defined in:
lib/ruby_indexer/test/enhancements_test.rb

Instance Method Summary collapse

Methods inherited from TestCase

#setup

Instance Method Details

#teardownObject



8
9
10
11
# File 'lib/ruby_indexer/test/enhancements_test.rb', line 8

def teardown
  super
  Enhancement.clear
end

#test_advancing_namespace_stack_from_enhancementObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/ruby_indexer/test/enhancements_test.rb', line 215

def test_advancing_namespace_stack_from_enhancement
  Class.new(Enhancement) do
    def on_call_node_enter(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
      owner = @listener.current_owner
      return unless owner

      case call_node.name
      when :class_methods
        @listener.add_module("ClassMethods", call_node.location, call_node.location)
      when :extend
        arguments = call_node.arguments&.arguments
        return unless arguments

        arguments.each do |node|
          next unless node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)

          module_name = node.full_name
          next unless module_name == "ActiveSupport::Concern"

          @listener.register_included_hook do |index, base|
            class_methods_name = "#{owner.name}::ClassMethods"

            if index.indexed?(class_methods_name)
              singleton = index.existing_or_new_singleton_class(base.name)
              singleton.mixin_operations << Entry::Include.new(class_methods_name)
            end
          end
        end
      end
    end

    def on_call_node_leave(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
      return unless call_node.name == :class_methods

      @listener.pop_namespace_stack
    end
  end

  index("    module ActiveSupport\n      module Concern\n      end\n    end\n\n    module MyConcern\n      extend ActiveSupport::Concern\n\n      class_methods do\n        def foo; end\n      end\n    end\n\n    class User\n      include MyConcern\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"User::<Class:User>\",\n      \"MyConcern::ClassMethods\",\n      \"Object::<Class:Object>\",\n      \"BasicObject::<Class:BasicObject>\",\n      \"Class\",\n      \"Module\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"User::<Class:User>\"),\n  )\n\n  refute_nil(@index.resolve_method(\"foo\", \"User::<Class:User>\"))\nend\n")

#test_creating_anonymous_classes_from_enhancementObject



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/ruby_indexer/test/enhancements_test.rb', line 290

def test_creating_anonymous_classes_from_enhancement
  Class.new(Enhancement) do
    def on_call_node_enter(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
      case call_node.name
      when :context
        arguments = call_node.arguments&.arguments
        first_argument = arguments&.first
        return unless first_argument.is_a?(Prism::StringNode)

        @listener.add_class(
          "<RSpec:#{first_argument.content}>",
          call_node.location,
          first_argument.location,
        )
      when :subject
        @listener.add_method("subject", call_node.location, [])
      end
    end

    def on_call_node_leave(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
      return unless call_node.name == :context

      @listener.pop_namespace_stack
    end
  end

  index("    context \"does something\" do\n      subject { call_whatever }\n    end\n  RUBY\n\n  refute_nil(@index.resolve_method(\"subject\", \"<RSpec:does something>\"))\nend\n")

#test_enhancing_indexing_configuration_dslObject



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
# File 'lib/ruby_indexer/test/enhancements_test.rb', line 96

def test_enhancing_indexing_configuration_dsl
  Class.new(Enhancement) do
    def on_call_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
      return unless @listener.current_owner

      name = node.name
      return unless name == :has_many

      arguments = node.arguments&.arguments
      return unless arguments

      association_name = arguments.first
      return unless association_name.is_a?(Prism::SymbolNode)

      @listener.add_method(
        association_name.value, #: as !nil
        association_name.location,
        [],
      )
    end
  end

  index("    module ActiveSupport\n      module Concern\n        def self.extended(base)\n          base.class_eval(\"def new_method(a); end\")\n        end\n      end\n    end\n\n    module ActiveRecord\n      module Associations\n        extend ActiveSupport::Concern\n\n        module ClassMethods\n          def belongs_to(something); end\n        end\n      end\n\n      class Base\n        include Associations\n      end\n    end\n\n    class User < ActiveRecord::Base\n      has_many :posts\n    end\n  RUBY\n\n  assert_entry(\"posts\", Entry::Method, \"/fake/path/foo.rb:23-11:23-17\")\nend\n")

#test_enhancing_indexing_included_hookObject



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
# File 'lib/ruby_indexer/test/enhancements_test.rb', line 13

def test_enhancing_indexing_included_hook
  Class.new(Enhancement) do
    def on_call_node_enter(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
      owner = @listener.current_owner
      return unless owner
      return unless call_node.name == :extend

      arguments = call_node.arguments&.arguments
      return unless arguments

      arguments.each do |node|
        next unless node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)

        module_name = node.full_name
        next unless module_name == "ActiveSupport::Concern"

        @listener.register_included_hook do |index, base|
          class_methods_name = "#{owner.name}::ClassMethods"

          if index.indexed?(class_methods_name)
            singleton = index.existing_or_new_singleton_class(base.name)
            singleton.mixin_operations << Entry::Include.new(class_methods_name)
          end
        end

        @listener.add_method(
          "new_method",
          call_node.location,
          [Entry::Signature.new([Entry::RequiredParameter.new(name: :a)])],
        )
      rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError,
             Prism::ConstantPathNode::MissingNodesInConstantPathError
        # Do nothing
      end
    end
  end

  index("    module ActiveSupport\n      module Concern\n        def self.extended(base)\n          base.class_eval(\"def new_method(a); end\")\n        end\n      end\n    end\n\n    module ActiveRecord\n      module Associations\n        extend ActiveSupport::Concern\n\n        module ClassMethods\n          def belongs_to(something); end\n        end\n      end\n\n      class Base\n        include Associations\n      end\n    end\n\n    class User < ActiveRecord::Base\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"User::<Class:User>\",\n      \"ActiveRecord::Base::<Class:Base>\",\n      \"ActiveRecord::Associations::ClassMethods\",\n      \"Object::<Class:Object>\",\n      \"BasicObject::<Class:BasicObject>\",\n      \"Class\",\n      \"Module\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"User::<Class:User>\"),\n  )\n\n  assert_entry(\"new_method\", Entry::Method, \"/fake/path/foo.rb:10-4:10-33\")\nend\n")

#test_error_handling_in_on_call_node_enter_enhancementObject



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
# File 'lib/ruby_indexer/test/enhancements_test.rb', line 149

def test_error_handling_in_on_call_node_enter_enhancement
  Class.new(Enhancement) do
    def on_call_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
      raise "Error"
    end

    class << self
      def name
        "TestEnhancement"
      end
    end
  end

  _stdout, stderr = capture_io do
    index("      module ActiveSupport\n        module Concern\n          def self.extended(base)\n            base.class_eval(\"def new_method(a); end\")\n          end\n        end\n      end\n    RUBY\n  end\n\n  assert_match(\n    %r{Indexing error in file:///fake/path/foo\\.rb with 'TestEnhancement' on call node enter enhancement},\n    stderr,\n  )\n  # The module should still be indexed\n  assert_entry(\"ActiveSupport::Concern\", Entry::Module, \"/fake/path/foo.rb:1-2:5-5\")\nend\n")

#test_error_handling_in_on_call_node_leave_enhancementObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ruby_indexer/test/enhancements_test.rb', line 182

def test_error_handling_in_on_call_node_leave_enhancement
  Class.new(Enhancement) do
    def on_call_node_leave(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
      raise "Error"
    end

    class << self
      def name
        "TestEnhancement"
      end
    end
  end

  _stdout, stderr = capture_io do
    index("      module ActiveSupport\n        module Concern\n          def self.extended(base)\n            base.class_eval(\"def new_method(a); end\")\n          end\n        end\n      end\n    RUBY\n  end\n\n  assert_match(\n    %r{Indexing error in file:///fake/path/foo\\.rb with 'TestEnhancement' on call node leave enhancement},\n    stderr,\n  )\n  # The module should still be indexed\n  assert_entry(\"ActiveSupport::Concern\", Entry::Module, \"/fake/path/foo.rb:1-2:5-5\")\nend\n")