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

#test_enhancing_indexing_configuration_dslObject



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

def test_enhancing_indexing_configuration_dsl
  enhancement_class = Class.new do
    include Enhancement

    def on_call_node(index, owner, node, file_path)
      return unless 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)

      location = association_name.location

      index.add(Entry::Method.new(
        T.must(association_name.value),
        file_path,
        location,
        location,
        [],
        [],
        Entry::Visibility::PUBLIC,
        owner,
      ))
    end
  end

  @index.register_enhancement(enhancement_class.new)
  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



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

def test_enhancing_indexing_included_hook
  enhancement_class = Class.new do
    include Enhancement

    def on_call_node(index, owner, node, file_path)
      return unless owner
      return unless node.name == :extend

      arguments = node.arguments&.arguments
      return unless arguments

      location = node.location

      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"

        index.register_included_hook(owner.name) 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

        index.add(Entry::Method.new(
          "new_method",
          file_path,
          location,
          location,
          [],
          [Entry::Signature.new([Entry::RequiredParameter.new(name: :a)])],
          Entry::Visibility::PUBLIC,
          owner,
        ))
      rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError,
             Prism::ConstantPathNode::MissingNodesInConstantPathError
        # Do nothing
      end
    end
  end

  @index.register_enhancement(enhancement_class.new)
  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_enhancementObject



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

def test_error_handling_in_enhancement
  enhancement_class = Class.new do
    include Enhancement

    def on_call_node(index, owner, node, file_path)
      raise "Error"
    end

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

  @index.register_enhancement(enhancement_class.new)

  _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(%r{Indexing error in /fake/path/foo\\.rb with 'TestEnhancement' enhancement}, stderr)\n  # The module should still be indexed\n  assert_entry(\"ActiveSupport::Concern\", Entry::Module, \"/fake/path/foo.rb:1-2:5-5\")\nend\n")