-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindexing_enhancement_test.rb
72 lines (59 loc) · 2.3 KB
/
indexing_enhancement_test.rb
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
# typed: true
# frozen_string_literal: true
require "test_helper"
module RubyLsp
module Rails
class IndexingEnhancementTest < ActiveSupport::TestCase
class << self
# For these tests, it's convenient to have the index fully populated with Rails information, but we don't have
# to reindex on every single example or that will be too slow
def populated_index
@index ||= begin
index = RubyIndexer::Index.new
index.register_enhancement(IndexingEnhancement.new)
index.index_all
index
end
end
end
def setup
@index = self.class.populated_index
end
test "ClassMethods module inside concerns are automatically extended" do
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY)
class Post < ActiveRecord::Base
end
RUBY
ancestors = @index.linearized_ancestors_of("Post::<Class:Post>")
assert_includes(ancestors, "ActiveRecord::Associations::ClassMethods")
assert_includes(ancestors, "ActiveRecord::Store::ClassMethods")
assert_includes(ancestors, "ActiveRecord::AttributeMethods::ClassMethods")
end
test "associations" do
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY)
class Post < ActiveRecord::Base
has_one :content
belongs_to :author
has_many :comments
has_and_belongs_to_many :tags
end
RUBY
assert_declaration_on_line("content", "Post", 2)
assert_declaration_on_line("content=", "Post", 2)
assert_declaration_on_line("author", "Post", 3)
assert_declaration_on_line("author=", "Post", 3)
assert_declaration_on_line("comments", "Post", 4)
assert_declaration_on_line("comments=", "Post", 4)
assert_declaration_on_line("tags", "Post", 5)
assert_declaration_on_line("tags=", "Post", 5)
end
private
def assert_declaration_on_line(method_name, class_name, line)
association_entries = @index.resolve_method(method_name, class_name)
refute_nil(association_entries)
association = association_entries.first
assert_equal(line, association.location.start_line)
end
end
end
end