-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathclasses_and_modules_test.rb
670 lines (534 loc) · 17.8 KB
/
classes_and_modules_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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
289
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# typed: true
# frozen_string_literal: true
require_relative "test_case"
module RubyIndexer
class ClassesAndModulesTest < TestCase
def test_empty_statements_class
index(<<~RUBY)
class Foo
end
RUBY
assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
end
def test_conditional_class
index(<<~RUBY)
class Foo
end if condition
RUBY
assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
end
def test_class_with_statements
index(<<~RUBY)
class Foo
def something; end
end
RUBY
assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:2-3")
end
def test_colon_colon_class
index(<<~RUBY)
class ::Foo
end
RUBY
assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
end
def test_colon_colon_class_inside_class
index(<<~RUBY)
class Bar
class ::Foo
end
end
RUBY
assert_entry("Bar", Entry::Class, "/fake/path/foo.rb:0-0:3-3")
assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:1-2:2-5")
end
def test_namespaced_class
index(<<~RUBY)
class Foo::Bar
end
RUBY
assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
end
def test_dynamically_namespaced_class
index(<<~RUBY)
class self::Bar
end
RUBY
assert_entry("self::Bar", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
end
def test_dynamically_namespaced_class_does_not_affect_other_classes
index(<<~RUBY)
class Foo
class self::Bar
end
class Bar
end
end
RUBY
refute_entry("self::Bar")
assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:6-3")
assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:4-2:5-5")
end
def test_empty_statements_module
index(<<~RUBY)
module Foo
end
RUBY
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
end
def test_conditional_module
index(<<~RUBY)
module Foo
end if condition
RUBY
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
end
def test_module_with_statements
index(<<~RUBY)
module Foo
def something; end
end
RUBY
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:2-3")
end
def test_colon_colon_module
index(<<~RUBY)
module ::Foo
end
RUBY
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
end
def test_namespaced_module
index(<<~RUBY)
module Foo::Bar
end
RUBY
assert_entry("Foo::Bar", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
end
def test_dynamically_namespaced_module
index(<<~RUBY)
module self::Bar
end
RUBY
assert_entry("self::Bar", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
end
def test_dynamically_namespaced_module_does_not_affect_other_modules
index(<<~RUBY)
module Foo
class self::Bar
end
module Bar
end
end
RUBY
assert_entry("Foo::self::Bar", Entry::Class, "/fake/path/foo.rb:1-2:2-5")
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:6-3")
assert_entry("Foo::Bar", Entry::Module, "/fake/path/foo.rb:4-2:5-5")
end
def test_nested_modules_and_classes_with_multibyte_characters
index(<<~RUBY)
module A動物
class Bねこ; end
end
RUBY
assert_entry("A動物", Entry::Module, "/fake/path/foo.rb:0-0:2-3")
assert_entry("A動物::Bねこ", Entry::Class, "/fake/path/foo.rb:1-2:1-16")
end
def test_nested_modules_and_classes
index(<<~RUBY)
module Foo
class Bar
end
module Baz
class Qux
class Something
end
end
end
end
RUBY
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:10-3")
assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:1-2:2-5")
assert_entry("Foo::Baz", Entry::Module, "/fake/path/foo.rb:4-2:9-5")
assert_entry("Foo::Baz::Qux", Entry::Class, "/fake/path/foo.rb:5-4:8-7")
assert_entry("Foo::Baz::Qux::Something", Entry::Class, "/fake/path/foo.rb:6-6:7-9")
end
def test_deleting_from_index_based_on_file_path
index(<<~RUBY)
class Foo
end
RUBY
assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
@index.delete(URI::Generic.from_path(path: "/fake/path/foo.rb"))
refute_entry("Foo")
assert_no_indexed_entries
end
def test_comments_can_be_attached_to_a_class
index(<<~RUBY)
# This is method comment
def foo; end
# This is a Foo comment
# This is another Foo comment
class Foo
# This should not be attached
end
# Ignore me
# This Bar comment has 1 line padding
class Bar; end
RUBY
foo_entry = @index["Foo"].first
assert_equal("This is a Foo comment\nThis is another Foo comment", foo_entry.comments)
bar_entry = @index["Bar"].first
assert_equal("This Bar comment has 1 line padding", bar_entry.comments)
end
def test_skips_comments_containing_invalid_encodings
index(<<~RUBY)
# comment \xBA
class Foo
end
RUBY
assert(@index["Foo"].first)
end
def test_comments_can_be_attached_to_a_namespaced_class
index(<<~RUBY)
# This is a Foo comment
# This is another Foo comment
class Foo
# This is a Bar comment
class Bar; end
end
RUBY
foo_entry = @index["Foo"].first
assert_equal("This is a Foo comment\nThis is another Foo comment", foo_entry.comments)
bar_entry = @index["Foo::Bar"].first
assert_equal("This is a Bar comment", bar_entry.comments)
end
def test_comments_can_be_attached_to_a_reopened_class
index(<<~RUBY)
# This is a Foo comment
class Foo; end
# This is another Foo comment
class Foo; end
RUBY
first_foo_entry = @index["Foo"][0]
assert_equal("This is a Foo comment", first_foo_entry.comments)
second_foo_entry = @index["Foo"][1]
assert_equal("This is another Foo comment", second_foo_entry.comments)
end
def test_comments_removes_the_leading_pound_and_space
index(<<~RUBY)
# This is a Foo comment
class Foo; end
#This is a Bar comment
class Bar; end
RUBY
first_foo_entry = @index["Foo"][0]
assert_equal("This is a Foo comment", first_foo_entry.comments)
second_foo_entry = @index["Bar"][0]
assert_equal("This is a Bar comment", second_foo_entry.comments)
end
def test_private_class_and_module_indexing
index(<<~RUBY)
class A
class B; end
private_constant(:B)
module C; end
private_constant("C")
class D; end
end
RUBY
b_const = @index["A::B"].first
assert_predicate(b_const, :private?)
c_const = @index["A::C"].first
assert_predicate(c_const, :private?)
d_const = @index["A::D"].first
assert_equal(Entry::Visibility::PUBLIC, d_const.visibility)
end
def test_keeping_track_of_super_classes
index(<<~RUBY)
class Foo < Bar
end
class Baz
end
module Something
class Baz
end
class Qux < ::Baz
end
end
class FinalThing < Something::Baz
end
RUBY
foo = T.must(@index["Foo"].first)
assert_equal("Bar", foo.parent_class)
baz = T.must(@index["Baz"].first)
assert_equal("::Object", baz.parent_class)
qux = T.must(@index["Something::Qux"].first)
assert_equal("::Baz", qux.parent_class)
final_thing = T.must(@index["FinalThing"].first)
assert_equal("Something::Baz", final_thing.parent_class)
end
def test_keeping_track_of_included_modules
index(<<~RUBY)
class Foo
# valid syntaxes that we can index
include A1
self.include A2
include A3, A4
self.include A5, A6
# valid syntaxes that we cannot index because of their dynamic nature
include some_variable_or_method_call
self.include some_variable_or_method_call
def something
include A7 # We should not index this because of this dynamic nature
end
# Valid inner class syntax definition with its own modules included
class Qux
include Corge
self.include Corge
include Baz
include some_variable_or_method_call
end
end
class ConstantPathReferences
include Foo::Bar
self.include Foo::Bar2
include dynamic::Bar
include Foo::
end
RUBY
foo = T.must(@index["Foo"][0])
assert_equal(["A1", "A2", "A3", "A4", "A5", "A6"], foo.mixin_operation_module_names)
qux = T.must(@index["Foo::Qux"][0])
assert_equal(["Corge", "Corge", "Baz"], qux.mixin_operation_module_names)
constant_path_references = T.must(@index["ConstantPathReferences"][0])
assert_equal(["Foo::Bar", "Foo::Bar2"], constant_path_references.mixin_operation_module_names)
end
def test_keeping_track_of_prepended_modules
index(<<~RUBY)
class Foo
# valid syntaxes that we can index
prepend A1
self.prepend A2
prepend A3, A4
self.prepend A5, A6
# valid syntaxes that we cannot index because of their dynamic nature
prepend some_variable_or_method_call
self.prepend some_variable_or_method_call
def something
prepend A7 # We should not index this because of this dynamic nature
end
# Valid inner class syntax definition with its own modules prepended
class Qux
prepend Corge
self.prepend Corge
prepend Baz
prepend some_variable_or_method_call
end
end
class ConstantPathReferences
prepend Foo::Bar
self.prepend Foo::Bar2
prepend dynamic::Bar
prepend Foo::
end
RUBY
foo = T.must(@index["Foo"][0])
assert_equal(["A1", "A2", "A3", "A4", "A5", "A6"], foo.mixin_operation_module_names)
qux = T.must(@index["Foo::Qux"][0])
assert_equal(["Corge", "Corge", "Baz"], qux.mixin_operation_module_names)
constant_path_references = T.must(@index["ConstantPathReferences"][0])
assert_equal(["Foo::Bar", "Foo::Bar2"], constant_path_references.mixin_operation_module_names)
end
def test_keeping_track_of_extended_modules
index(<<~RUBY)
class Foo
# valid syntaxes that we can index
extend A1
self.extend A2
extend A3, A4
self.extend A5, A6
# valid syntaxes that we cannot index because of their dynamic nature
extend some_variable_or_method_call
self.extend some_variable_or_method_call
def something
extend A7 # We should not index this because of this dynamic nature
end
# Valid inner class syntax definition with its own modules prepended
class Qux
extend Corge
self.extend Corge
extend Baz
extend some_variable_or_method_call
end
end
class ConstantPathReferences
extend Foo::Bar
self.extend Foo::Bar2
extend dynamic::Bar
extend Foo::
end
RUBY
foo = T.must(@index["Foo::<Class:Foo>"][0])
assert_equal(["A1", "A2", "A3", "A4", "A5", "A6"], foo.mixin_operation_module_names)
qux = T.must(@index["Foo::Qux::<Class:Qux>"][0])
assert_equal(["Corge", "Corge", "Baz"], qux.mixin_operation_module_names)
constant_path_references = T.must(@index["ConstantPathReferences::<Class:ConstantPathReferences>"][0])
assert_equal(["Foo::Bar", "Foo::Bar2"], constant_path_references.mixin_operation_module_names)
end
def test_tracking_singleton_classes
index(<<~RUBY)
class Foo; end
class Foo
# Some extra comments
class << self
end
end
RUBY
foo = T.must(@index["Foo::<Class:Foo>"].first)
assert_equal(4, foo.location.start_line)
assert_equal("Some extra comments", foo.comments)
end
def test_dynamic_singleton_class_blocks
index(<<~RUBY)
class Foo
# Some extra comments
class << bar
end
end
RUBY
singleton = T.must(@index["Foo::<Class:bar>"].first)
# Even though this is not correct, we consider any dynamic singleton class block as a regular singleton class.
# That pattern cannot be properly analyzed statically and assuming that it's always a regular singleton simplifies
# the implementation considerably.
assert_equal(3, singleton.location.start_line)
assert_equal("Some extra comments", singleton.comments)
end
def test_namespaces_inside_singleton_blocks
index(<<~RUBY)
class Foo
class << self
class Bar
end
end
end
RUBY
assert_entry("Foo::<Class:Foo>::Bar", Entry::Class, "/fake/path/foo.rb:2-4:3-7")
end
def test_name_location_points_to_constant_path_location
index(<<~RUBY)
class Foo
def foo; end
end
module Bar
def bar; end
end
RUBY
foo = T.must(@index["Foo"].first)
refute_equal(foo.location, foo.name_location)
name_location = foo.name_location
assert_equal(1, name_location.start_line)
assert_equal(1, name_location.end_line)
assert_equal(6, name_location.start_column)
assert_equal(9, name_location.end_column)
bar = T.must(@index["Bar"].first)
refute_equal(bar.location, bar.name_location)
name_location = bar.name_location
assert_equal(5, name_location.start_line)
assert_equal(5, name_location.end_line)
assert_equal(7, name_location.start_column)
assert_equal(10, name_location.end_column)
end
def test_indexing_namespaces_inside_top_level_references
index(<<~RUBY)
module ::Foo
class Bar
end
end
RUBY
# We want to explicitly verify that we didn't introduce the leading `::` by accident, but `Index#[]` deletes the
# prefix when we use `refute_entry`
entries = @index.instance_variable_get(:@entries)
refute(entries.key?("::Foo"))
refute(entries.key?("::Foo::Bar"))
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:3-3")
assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:1-2:2-5")
end
def test_indexing_singletons_inside_top_level_references
index(<<~RUBY)
module ::Foo
class Bar
class << self
end
end
end
RUBY
# We want to explicitly verify that we didn't introduce the leading `::` by accident, but `Index#[]` deletes the
# prefix when we use `refute_entry`
entries = @index.instance_variable_get(:@entries)
refute(entries.key?("::Foo"))
refute(entries.key?("::Foo::Bar"))
refute(entries.key?("::Foo::Bar::<Class:Bar>"))
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:5-3")
assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:1-2:4-5")
assert_entry("Foo::Bar::<Class:Bar>", Entry::SingletonClass, "/fake/path/foo.rb:2-4:3-7")
end
def test_indexing_namespaces_inside_nested_top_level_references
index(<<~RUBY)
class Baz
module ::Foo
class Bar
end
class ::Qux
end
end
end
RUBY
refute_entry("Baz::Foo")
refute_entry("Baz::Foo::Bar")
assert_entry("Baz", Entry::Class, "/fake/path/foo.rb:0-0:8-3")
assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:1-2:7-5")
assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:2-4:3-7")
assert_entry("Qux", Entry::Class, "/fake/path/foo.rb:5-4:6-7")
end
def test_lazy_comment_fetching_uses_correct_line_breaks_for_rendering
uri = URI::Generic.from_path(
load_path_entry: "#{Dir.pwd}/lib",
path: "#{Dir.pwd}/lib/ruby_lsp/node_context.rb",
)
@index.index_file(uri, collect_comments: false)
entry = @index["RubyLsp::NodeContext"].first
assert_equal(<<~COMMENTS.chomp, entry.comments)
This class allows listeners to access contextual information about a node in the AST, such as its parent,
its namespace nesting, and the surrounding CallNode (e.g. a method call).
COMMENTS
end
def test_lazy_comment_fetching_does_not_fail_if_file_gets_deleted
uri = URI::Generic.from_path(
load_path_entry: "#{Dir.pwd}/lib",
path: "#{Dir.pwd}/lib/ruby_lsp/does_not_exist.rb",
)
@index.index_single(uri, <<~RUBY, collect_comments: false)
class Foo
end
RUBY
entry = @index["Foo"].first
assert_empty(entry.comments)
end
def test_singleton_inside_compact_namespace
index(<<~RUBY)
module Foo::Bar
class << self
def baz; end
end
end
RUBY
# Verify we didn't index the incorrect name
assert_nil(@index["Foo::Bar::<Class:Foo::Bar>"])
# Verify we indexed the correct name
assert_entry("Foo::Bar::<Class:Bar>", Entry::SingletonClass, "/fake/path/foo.rb:1-2:3-5")
method = @index["baz"]&.first
assert_equal("Foo::Bar::<Class:Bar>", method.owner.name)
end
end
end