Class: RubyIndexer::IndexTest

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

Instance Method Summary collapse

Methods inherited from TestCase

#setup

Instance Method Details

#test_accessing_with_colon_colon_prefixObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby_indexer/test/index_test.rb', line 74

def test_accessing_with_colon_colon_prefix
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    class Bar; end\n\n    module Foo\n      class Bar\n      end\n\n      class Baz\n        class Something\n        end\n      end\n    end\n  RUBY\n\n  entries = @index[\"::Foo::Baz::Something\"]\n  refute_empty(entries)\n  assert_equal(\"Foo::Baz::Something\", entries.first.name)\nend\n")

#test_ancestors_linearization_complex_include_duplicationObject



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

def test_ancestors_linearization_complex_include_duplication
  index("    module A; end\n    module B\n      include A\n    end\n    module C\n      include B\n    end\n\n    class Foo\n      include A\n      include C\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"Foo\",\n      \"C\",\n      \"B\",\n      \"A\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Foo\"),\n  )\nend\n")

#test_ancestors_linearization_complex_prepend_duplicationObject



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

def test_ancestors_linearization_complex_prepend_duplication
  index("    module A; end\n    module B\n      prepend A\n    end\n    module C\n      prepend B\n    end\n\n    class Foo\n      prepend A\n      prepend C\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"A\",\n      \"B\",\n      \"C\",\n      \"Foo\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Foo\"),\n  )\nend\n")

#test_basic_object_superclass_resolutionObject



1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
# File 'lib/ruby_indexer/test/index_test.rb', line 1261

def test_basic_object_superclass_resolution
  index("    module Foo\n      class BasicObject; end\n\n      class Bar; end\n      class Baz < BasicObject; end\n    end\n  RUBY\n\n  assert_equal([\"Foo::Bar\", \"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Foo::Bar\"))\n  assert_equal(\n    [\"Foo::Baz\", \"Foo::BasicObject\", \"Object\", \"Kernel\", \"BasicObject\"],\n    @index.linearized_ancestors_of(\"Foo::Baz\"),\n  )\nend\n")

#test_completion_does_not_duplicate_methods_overridden_by_aliasesObject



1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
# File 'lib/ruby_indexer/test/index_test.rb', line 1576

def test_completion_does_not_duplicate_methods_overridden_by_aliases
  index("    class Foo\n      def bar; end\n    end\n\n    class Baz < Foo\n      alias bar to_s\n    end\n  RUBY\n\n  entries = @index.method_completion_candidates(\"bar\", \"Baz\")\n  assert_equal([\"bar\"], entries.map(&:name))\n  assert_equal(\"Baz\", T.must(entries.first.owner).name)\nend\n")

#test_completion_does_not_duplicate_overridden_methodsObject



1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
# File 'lib/ruby_indexer/test/index_test.rb', line 1560

def test_completion_does_not_duplicate_overridden_methods
  index("    class Foo\n      def bar; end\n    end\n\n    class Baz < Foo\n      def bar; end\n    end\n  RUBY\n\n  entries = @index.method_completion_candidates(\"bar\", \"Baz\")\n  assert_equal([\"bar\"], entries.map(&:name))\n  assert_equal(\"Baz\", T.must(entries.first.owner).name)\nend\n")

#test_completion_does_not_include_unresolved_aliasesObject



1535
1536
1537
1538
1539
1540
1541
1542
1543
# File 'lib/ruby_indexer/test/index_test.rb', line 1535

def test_completion_does_not_include_unresolved_aliases
  index("    class Foo\n      alias_method :bar, :missing\n    end\n  RUBY\n\n  assert_empty(@index.method_completion_candidates(\"bar\", \"Foo\"))\nend\n")

#test_decorated_parametersObject



1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
# File 'lib/ruby_indexer/test/index_test.rb', line 1592

def test_decorated_parameters
  index("    class Foo\n      def bar(a, b = 1, c: 2)\n      end\n    end\n  RUBY\n\n  methods = @index.resolve_method(\"bar\", \"Foo\")\n  refute_nil(methods)\n\n  entry = T.must(methods.first)\n\n  assert_equal(\"(a, b = <default>, c: <default>)\", entry.decorated_parameters)\nend\n")

#test_decorated_parameters_when_method_has_no_parametersObject



1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
# File 'lib/ruby_indexer/test/index_test.rb', line 1608

def test_decorated_parameters_when_method_has_no_parameters
  index("    class Foo\n      def bar\n      end\n    end\n  RUBY\n\n  methods = @index.resolve_method(\"bar\", \"Foo\")\n  refute_nil(methods)\n\n  entry = T.must(methods.first)\n\n  assert_equal(\"()\", entry.decorated_parameters)\nend\n")

#test_deleting_all_entries_for_a_classObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_indexer/test/index_test.rb', line 26

def test_deleting_all_entries_for_a_class
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    class Foo\n    end\n  RUBY\n\n  entries = @index[\"Foo\"]\n  assert_equal(1, entries.length)\n\n  @index.delete(IndexablePath.new(nil, \"/fake/path/foo.rb\"))\n  entries = @index[\"Foo\"]\n  assert_nil(entries)\nend\n")

#test_deleting_one_entry_for_a_classObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_indexer/test/index_test.rb', line 8

def test_deleting_one_entry_for_a_class
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    class Foo\n    end\n  RUBY\n  @index.index_single(IndexablePath.new(nil, \"/fake/path/other_foo.rb\"), <<~RUBY)\n    class Foo\n    end\n  RUBY\n\n  entries = @index[\"Foo\"]\n  assert_equal(2, entries.length)\n\n  @index.delete(IndexablePath.new(nil, \"/fake/path/other_foo.rb\"))\n  entries = @index[\"Foo\"]\n  assert_equal(1, entries.length)\nend\n")

#test_duplicate_prepend_includeObject



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

def test_duplicate_prepend_include
  index("    module A; end\n\n    class Foo\n      prepend A\n      include A\n    end\n\n    class Bar\n      include A\n      prepend A\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"A\",\n      \"Foo\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Foo\"),\n  )\n\n  assert_equal(\n    [\n      \"A\",\n      \"Bar\",\n      \"A\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Bar\"),\n  )\nend\n")

#test_first_unqualified_constObject



1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
# File 'lib/ruby_indexer/test/index_test.rb', line 1545

def test_first_unqualified_const
  index("    module Foo\n      class Bar; end\n    end\n\n    module Baz\n      class Bar; end\n    end\n  RUBY\n\n  entry = T.must(@index.first_unqualified_const(\"Bar\")&.first)\n  assert_equal(\"Foo::Bar\", entry.name)\nend\n")

#test_fuzzy_searchObject



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

def test_fuzzy_search
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    class Zws; end\n\n    module Qtl\n      class Zws\n      end\n\n      class Zwo\n        class Something\n        end\n      end\n    end\n  RUBY\n\n  result = @index.fuzzy_search(\"Zws\")\n  assert_equal(2, result.length)\n  assert_equal([\"Zws\", \"Qtl::Zwo::Something\"], result.map(&:name))\n\n  result = @index.fuzzy_search(\"qtlzwssomeking\")\n  assert_equal(5, result.length)\n  assert_equal([\"Qtl::Zwo::Something\", \"Qtl::Zws\", \"Qtl::Zwo\", \"Qtl\", \"Zws\"], result.map(&:name))\n\n  result = @index.fuzzy_search(\"QltZwo\")\n  assert_equal(4, result.length)\n  assert_equal([\"Qtl::Zwo\", \"Qtl::Zws\", \"Qtl::Zwo::Something\", \"Qtl\"], result.map(&:name))\nend\n")

#test_handle_change_clears_ancestor_cache_if_parent_class_changedObject



843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
# File 'lib/ruby_indexer/test/index_test.rb', line 843

def test_handle_change_clears_ancestor_cache_if_parent_class_changed
  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      # Write the original file
      File.write(File.join(dir, "foo.rb"), "        class Foo\n        end\n\n        class Bar < Foo\n        end\n      RUBY\n\n      indexable_path = IndexablePath.new(nil, File.join(dir, \"foo.rb\"))\n      @index.index_single(indexable_path)\n\n      assert_equal([\"Bar\", \"Foo\", \"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Bar\"))\n\n      # Remove include to invalidate the ancestor tree\n      File.write(File.join(dir, \"foo.rb\"), <<~RUBY)\n        class Foo\n        end\n\n        class Bar\n        end\n      RUBY\n\n      @index.handle_change(indexable_path)\n      assert_empty(@index.instance_variable_get(:@ancestors))\n      assert_equal([\"Bar\", \"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Bar\"))\n    end\n  end\nend\n")

#test_handle_change_clears_ancestor_cache_if_tree_changedObject



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File 'lib/ruby_indexer/test/index_test.rb', line 772

def test_handle_change_clears_ancestor_cache_if_tree_changed
  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      # Write the original file
      File.write(File.join(dir, "foo.rb"), "        module Foo\n        end\n\n        class Bar\n          include Foo\n        end\n      RUBY\n\n      indexable_path = IndexablePath.new(nil, File.join(dir, \"foo.rb\"))\n      @index.index_single(indexable_path)\n\n      assert_equal([\"Bar\", \"Foo\", \"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Bar\"))\n\n      # Remove include to invalidate the ancestor tree\n      File.write(File.join(dir, \"foo.rb\"), <<~RUBY)\n        module Foo\n        end\n\n        class Bar\n        end\n      RUBY\n\n      @index.handle_change(indexable_path)\n      assert_empty(@index.instance_variable_get(:@ancestors))\n      assert_equal([\"Bar\", \"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Bar\"))\n    end\n  end\nend\n")

#test_handle_change_does_not_clear_ancestor_cache_if_tree_not_changedObject



806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# File 'lib/ruby_indexer/test/index_test.rb', line 806

def test_handle_change_does_not_clear_ancestor_cache_if_tree_not_changed
  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      # Write the original file
      File.write(File.join(dir, "foo.rb"), "        module Foo\n        end\n\n        class Bar\n          include Foo\n        end\n      RUBY\n\n      indexable_path = IndexablePath.new(nil, File.join(dir, \"foo.rb\"))\n      @index.index_single(indexable_path)\n\n      assert_equal([\"Bar\", \"Foo\", \"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Bar\"))\n\n      # Remove include to invalidate the ancestor tree\n      File.write(File.join(dir, \"foo.rb\"), <<~RUBY)\n        module Foo\n        end\n\n        class Bar\n          include Foo\n\n          def baz; end\n        end\n      RUBY\n\n      @index.handle_change(indexable_path)\n      refute_empty(@index.instance_variable_get(:@ancestors))\n      assert_equal([\"Bar\", \"Foo\", \"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Bar\"))\n    end\n  end\nend\n")

#test_index_resolveObject



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

def test_index_resolve
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    class Bar; end\n\n    module Foo\n      class Bar\n      end\n\n      class Baz\n        class Something\n        end\n      end\n    end\n  RUBY\n\n  entries = @index.resolve(\"Something\", [\"Foo\", \"Baz\"])\n  refute_empty(entries)\n  assert_equal(\"Foo::Baz::Something\", entries.first.name)\n\n  entries = @index.resolve(\"Bar\", [\"Foo\"])\n  refute_empty(entries)\n  assert_equal(\"Foo::Bar\", entries.first.name)\n\n  entries = @index.resolve(\"Bar\", [\"Foo\", \"Baz\"])\n  refute_empty(entries)\n  assert_equal(\"Foo::Bar\", entries.first.name)\n\n  entries = @index.resolve(\"Foo::Bar\", [\"Foo\", \"Baz\"])\n  refute_empty(entries)\n  assert_equal(\"Foo::Bar\", entries.first.name)\n\n  assert_nil(@index.resolve(\"DoesNotExist\", [\"Foo\"]))\nend\n")

#test_index_single_does_not_fail_for_non_existing_fileObject



356
357
358
359
360
# File 'lib/ruby_indexer/test/index_test.rb', line 356

def test_index_single_does_not_fail_for_non_existing_file
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"))
  entries_after_indexing = @index.names
  assert_equal(@default_indexed_entries.keys, entries_after_indexing)
end

#test_index_single_ignores_directoriesObject



122
123
124
125
126
127
# File 'lib/ruby_indexer/test/index_test.rb', line 122

def test_index_single_ignores_directories
  FileUtils.mkdir("lib/this_is_a_dir.rb")
  @index.index_single(IndexablePath.new(nil, "lib/this_is_a_dir.rb"))
ensure
  FileUtils.rm_r("lib/this_is_a_dir.rb")
end

#test_indexing_prism_fixtures_succeedsObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/ruby_indexer/test/index_test.rb', line 341

def test_indexing_prism_fixtures_succeeds
  unless Dir.exist?("test/fixtures/prism/test/prism/fixtures")
    raise "Prism fixtures not found. Run `git submodule update --init` to fetch them."
  end

  fixtures = Dir.glob("test/fixtures/prism/test/prism/fixtures/**/*.txt")

  fixtures.each do |fixture|
    indexable_path = IndexablePath.new("", fixture)
    @index.index_single(indexable_path)
  end

  refute_empty(@index)
end

#test_instance_variable_completion_in_singleton_contextsObject



1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
# File 'lib/ruby_indexer/test/index_test.rb', line 1378

def test_instance_variable_completion_in_singleton_contexts
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    module Foo\n      class Bar\n        @a = 123\n\n        class << self\n          def hello\n            @b = 123\n          end\n\n          @c = 123\n        end\n      end\n    end\n  RUBY\n\n  entries = @index.instance_variable_completion_candidates(\"@\", \"Foo::Bar::<Class:Bar>\").map(&:name)\n  assert_includes(entries, \"@a\")\n  assert_includes(entries, \"@b\")\nend\n")

#test_instance_variables_completions_from_different_owners_with_conflicting_namesObject



1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
# File 'lib/ruby_indexer/test/index_test.rb', line 1174

def test_instance_variables_completions_from_different_owners_with_conflicting_names
  index("    class Foo\n      def initialize\n        @bar = 1\n      end\n    end\n\n    class Bar\n      def initialize\n        @bar = 2\n      end\n    end\n  RUBY\n\n  entry = T.must(@index.instance_variable_completion_candidates(\"@\", \"Bar\")&.first)\n  assert_equal(\"@bar\", entry.name)\n  assert_equal(\"Bar\", T.must(entry.owner).name)\nend\n")

#test_linearized_ancestorsObject



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

def test_linearized_ancestors
  index("    module A; end\n    module B; end\n    module C; end\n\n    module D\n      include A\n    end\n\n    module E\n      prepend B\n    end\n\n    module F\n      include C\n      include A\n    end\n\n    class Bar\n      prepend F\n    end\n\n    class Foo < Bar\n      include E\n      prepend D\n    end\n  RUBY\n\n  # Object, Kernel and BasicObject are intentionally commented out for now until we develop a strategy for indexing\n  # declarations made in C code\n  assert_equal(\n    [\n      \"D\",\n      \"A\",\n      \"Foo\",\n      \"B\",\n      \"E\",\n      \"F\",\n      \"A\",\n      \"C\",\n      \"Bar\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Foo\"),\n  )\nend\n")

#test_linearized_ancestors_basic_orderingObject



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

def test_linearized_ancestors_basic_ordering
  index("    module A; end\n    module B; end\n\n    class Foo\n      prepend A\n      prepend B\n    end\n\n    class Bar\n      include A\n      include B\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"B\",\n      \"A\",\n      \"Foo\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Foo\"),\n  )\n\n  assert_equal(\n    [\n      \"Bar\",\n      \"B\",\n      \"A\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Bar\"),\n  )\nend\n")

#test_linearized_ancestors_duplicatesObject



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

def test_linearized_ancestors_duplicates
  index("    module A; end\n    module B\n      include A\n    end\n\n    class Foo\n      include B\n      include A\n    end\n\n    class Bar\n      prepend B\n      prepend A\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"Foo\",\n      \"B\",\n      \"A\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Foo\"),\n  )\n\n  assert_equal(\n    [\n      \"B\",\n      \"A\",\n      \"Bar\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Bar\"),\n  )\nend\n")

#test_linearizing_a_module_singleton_classObject



1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
# File 'lib/ruby_indexer/test/index_test.rb', line 1746

def test_linearizing_a_module_singleton_class
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    module A; end\n  RUBY\n\n  assert_equal(\n    [\n      \"A::<Class:A>\",\n      \"Module\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"A::<Class:A>\"),\n  )\nend\n")

#test_linearizing_a_singleton_class_with_no_attachedObject



1763
1764
1765
1766
1767
# File 'lib/ruby_indexer/test/index_test.rb', line 1763

def test_linearizing_a_singleton_class_with_no_attached
  assert_raises(Index::NonExistingNamespaceError) do
    @index.linearized_ancestors_of("A::<Class:A>")
  end
end

#test_linearizing_ancestors_for_non_existing_namespacesObject



657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/ruby_indexer/test/index_test.rb', line 657

def test_linearizing_ancestors_for_non_existing_namespaces
  index("    def Bar(a); end\n  RUBY\n\n  assert_raises(Index::NonExistingNamespaceError) do\n    @index.linearized_ancestors_of(\"Foo\")\n  end\n\n  assert_raises(Index::NonExistingNamespaceError) do\n    @index.linearized_ancestors_of(\"Bar\")\n  end\nend\n")

#test_linearizing_ancestors_handles_circular_parent_classObject



557
558
559
560
561
562
563
564
# File 'lib/ruby_indexer/test/index_test.rb', line 557

def test_linearizing_ancestors_handles_circular_parent_class
  index("    class Foo < Foo\n    end\n  RUBY\n\n  assert_equal([\"Foo\"], @index.linearized_ancestors_of(\"Foo\"))\nend\n")

#test_linearizing_ancestors_is_cachedObject



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/ruby_indexer/test/index_test.rb', line 496

def test_linearizing_ancestors_is_cached
  index("    module C; end\n    module A; end\n    module B\n      include A\n    end\n\n    class Foo\n      include B\n      include A\n    end\n  RUBY\n\n  @index.linearized_ancestors_of(\"Foo\")\n  ancestors = @index.instance_variable_get(:@ancestors)\n  assert(ancestors.key?(\"Foo\"))\n  assert(ancestors.key?(\"A\"))\n  assert(ancestors.key?(\"B\"))\n  refute(ancestors.key?(\"C\"))\nend\n")

#test_linearizing_ancestors_that_need_to_be_resolvedObject



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

def test_linearizing_ancestors_that_need_to_be_resolved
  index("    module Foo\n      module Baz\n      end\n      module Qux\n      end\n\n      class Something; end\n\n      class Bar < Something\n        include Baz\n        prepend Qux\n      end\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"Foo::Qux\",\n      \"Foo::Bar\",\n      \"Foo::Baz\",\n      \"Foo::Something\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Foo::Bar\"),\n  )\nend\n")

#test_linearizing_circular_aliased_dependencyObject



708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/ruby_indexer/test/index_test.rb', line 708

def test_linearizing_circular_aliased_dependency
  index("    module A\n    end\n\n    ALIAS = A\n\n    module A\n      include ALIAS\n    end\n  RUBY\n\n  assert_equal([\"A\", \"ALIAS\"], @index.linearized_ancestors_of(\"A\"))\nend\n")

#test_linearizing_circular_ancestorsObject



671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/ruby_indexer/test/index_test.rb', line 671

def test_linearizing_circular_ancestors
  index("    module M1\n      include M2\n    end\n\n    module M2\n      include M1\n    end\n\n    module A1\n      include A2\n    end\n\n    module A2\n      include A3\n    end\n\n    module A3\n      include A1\n    end\n\n    class Foo < Foo\n      include Foo\n    end\n\n    module Bar\n      include Bar\n    end\n  RUBY\n\n  assert_equal([\"M2\", \"M1\"], @index.linearized_ancestors_of(\"M2\"))\n  assert_equal([\"A3\", \"A1\", \"A2\"], @index.linearized_ancestors_of(\"A3\"))\n  assert_equal([\"Foo\"], @index.linearized_ancestors_of(\"Foo\"))\n  assert_equal([\"Bar\"], @index.linearized_ancestors_of(\"Bar\"))\nend\n")

#test_linearizing_singleton_ancestorsObject



1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
# File 'lib/ruby_indexer/test/index_test.rb', line 1675

def test_linearizing_singleton_ancestors
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    module First\n    end\n\n    module Second\n      include First\n    end\n\n    module Foo\n      class Bar\n        class << self\n          class Baz\n            extend Second\n\n            class << self\n              include First\n            end\n          end\n        end\n      end\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"Foo::Bar::<Class:Bar>::Baz::<Class:Baz>\",\n      \"Second\",\n      \"First\",\n      \"Object::<Class:Object>\",\n      \"BasicObject::<Class:BasicObject>\",\n      \"Class\",\n      \"Module\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Foo::Bar::<Class:Bar>::Baz::<Class:Baz>\"),\n  )\nend\n")

#test_linearizing_singleton_ancestors_of_singleton_when_class_has_parentObject



1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
# File 'lib/ruby_indexer/test/index_test.rb', line 1624

def test_linearizing_singleton_ancestors_of_singleton_when_class_has_parent
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    class Foo; end\n\n    class Bar < Foo\n    end\n\n    class Baz < Bar\n      class << self\n        class << self\n        end\n      end\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"Baz::<Class:Baz>::<Class:<Class:Baz>>\",\n      \"Bar::<Class:Bar>::<Class:<Class:Bar>>\",\n      \"Foo::<Class:Foo>::<Class:<Class:Foo>>\",\n      \"Object::<Class:Object>::<Class:<Class:Object>>\",\n      \"BasicObject::<Class:BasicObject>::<Class:<Class:BasicObject>>\",\n      \"Class::<Class:Class>\",\n      \"Module::<Class:Module>\",\n      \"Object::<Class:Object>\",\n      \"BasicObject::<Class:BasicObject>\",\n      \"Class\",\n      \"Module\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Baz::<Class:Baz>::<Class:<Class:Baz>>\"),\n  )\nend\n")

#test_linearizing_singleton_ancestors_when_class_has_parentObject



1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
# File 'lib/ruby_indexer/test/index_test.rb', line 1716

def test_linearizing_singleton_ancestors_when_class_has_parent
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    class Foo; end\n\n    class Bar < Foo\n    end\n\n    class Baz < Bar\n      class << self\n      end\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"Baz::<Class:Baz>\",\n      \"Bar::<Class:Bar>\",\n      \"Foo::<Class:Foo>\",\n      \"Object::<Class:Object>\",\n      \"BasicObject::<Class:BasicObject>\",\n      \"Class\",\n      \"Module\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Baz::<Class:Baz>\"),\n  )\nend\n")

#test_linearizing_singleton_objectObject



1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
# File 'lib/ruby_indexer/test/index_test.rb', line 1660

def test_linearizing_singleton_object
  assert_equal(
    [
      "Object::<Class:Object>",
      "BasicObject::<Class:BasicObject>",
      "Class",
      "Module",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Object::<Class:Object>"),
  )
end

#test_linearizing_singleton_parent_class_with_namespaceObject



1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
# File 'lib/ruby_indexer/test/index_test.rb', line 1769

def test_linearizing_singleton_parent_class_with_namespace
  index("    class ActiveRecord::Base; 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      \"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  )\nend\n")

#test_object_superclass_indexing_and_resolution_with_reopened_basic_object_classObject



1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
# File 'lib/ruby_indexer/test/index_test.rb', line 1232

def test_object_superclass_indexing_and_resolution_with_reopened_basic_object_class
  index("    class BasicObject; end\n  RUBY\n\n  entries = @index[\"BasicObject\"]\n  assert_equal(2, entries.length)\n  reopened_entry = entries.last\n  assert_nil(reopened_entry.parent_class)\n  assert_equal([\"BasicObject\"], @index.linearized_ancestors_of(\"BasicObject\"))\nend\n")

#test_object_superclass_indexing_and_resolution_with_reopened_object_classObject



1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
# File 'lib/ruby_indexer/test/index_test.rb', line 1220

def test_object_superclass_indexing_and_resolution_with_reopened_object_class
  index("    class Object; end\n  RUBY\n\n  entries = @index[\"Object\"]\n  assert_equal(2, entries.length)\n  reopened_entry = entries.last\n  assert_equal(\"::BasicObject\", reopened_entry.parent_class)\n  assert_equal([\"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Object\"))\nend\n")

#test_object_superclass_resolutionObject



1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
# File 'lib/ruby_indexer/test/index_test.rb', line 1244

def test_object_superclass_resolution
  index("    module Foo\n      class Object; end\n\n      class Bar; end\n      class Baz < Object; end\n    end\n  RUBY\n\n  assert_equal([\"Foo::Bar\", \"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Foo::Bar\"))\n  assert_equal(\n    [\"Foo::Baz\", \"Foo::Object\", \"Object\", \"Kernel\", \"BasicObject\"],\n    @index.linearized_ancestors_of(\"Foo::Baz\"),\n  )\nend\n")

#test_only_aliases_for_the_right_owner_are_resolvedObject



1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
# File 'lib/ruby_indexer/test/index_test.rb', line 1508

def test_only_aliases_for_the_right_owner_are_resolved
  index("    class Foo\n      attr_reader :name\n      alias_method :decorated_name, :name\n    end\n\n    class Bar\n      alias_method :decorated_name, :to_s\n    end\n  RUBY\n\n  methods = @index.resolve_method(\"decorated_name\", \"Foo\")\n  refute_nil(methods)\n\n  entry = T.must(methods.first)\n  assert_kind_of(Entry::MethodAlias, entry)\n\n  target = entry.target\n  assert_equal(\"name\", target.name)\n  assert_kind_of(Entry::Accessor, target)\n  assert_equal(\"Foo\", T.must(target.owner).name)\n\n  other_decorated_name = T.must(@index[\"decorated_name\"].find { |e| e.is_a?(Entry::UnresolvedMethodAlias) })\n  assert_kind_of(Entry::UnresolvedMethodAlias, other_decorated_name)\nend\n")

#test_prefix_search_for_methodsObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/ruby_indexer/test/index_test.rb', line 325

def test_prefix_search_for_methods
  index("    module Foo\n      module Bar\n        def qzx; end\n      end\n    end\n  RUBY\n\n  entries = @index.prefix_search(\"qz\")\n  refute_empty(entries)\n\n  entry = T.must(T.must(entries.first).first)\n  assert_equal(\"qzx\", entry.name)\nend\n")

#test_resolve_method_attributeObject



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ruby_indexer/test/index_test.rb', line 252

def test_resolve_method_attribute
  index("    class Foo\n      attr_reader :bar\n    end\n  RUBY\n\n  entries = T.must(@index.resolve_method(\"bar\", \"Foo\"))\n  assert_equal(\"bar\", entries.first.name)\n  assert_equal(\"Foo\", T.must(entries.first.owner).name)\nend\n")

#test_resolve_method_inherited_onlyObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/ruby_indexer/test/index_test.rb', line 288

def test_resolve_method_inherited_only
  index("    class Bar\n      def baz; end\n    end\n\n    class Foo < Bar\n      def baz; end\n    end\n  RUBY\n\n  entry = T.must(@index.resolve_method(\"baz\", \"Foo\", inherited_only: true).first)\n\n  assert_equal(\"Bar\", T.must(entry.owner).name)\nend\n")

#test_resolve_method_inherited_only_for_prepended_moduleObject



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/index_test.rb', line 304

def test_resolve_method_inherited_only_for_prepended_module
  index("    module Bar\n      def baz\n        super\n      end\n    end\n\n    class Foo\n      prepend Bar\n\n      def baz; end\n    end\n  RUBY\n\n  # This test is just to document the fact that we don't yet support resolving inherited methods for modules that\n  # are prepended. The only way to support this is to find all namespaces that have the module a subtype, so that we\n  # can show the results for everywhere the module has been prepended.\n  assert_nil(@index.resolve_method(\"baz\", \"Bar\", inherited_only: true))\nend\n")

#test_resolve_method_with_class_name_conflictObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ruby_indexer/test/index_test.rb', line 237

def test_resolve_method_with_class_name_conflict
  index("    class Array\n    end\n\n    class Foo\n      def Array(*args); end\n    end\n  RUBY\n\n  entries = T.must(@index.resolve_method(\"Array\", \"Foo\"))\n  assert_equal(\"Array\", entries.first.name)\n  assert_equal(\"Foo\", T.must(entries.first.owner).name)\nend\n")

#test_resolve_method_with_known_receiverObject



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ruby_indexer/test/index_test.rb', line 223

def test_resolve_method_with_known_receiver
  index("    module Foo\n      module Bar\n        def baz; end\n      end\n    end\n  RUBY\n\n  entries = T.must(@index.resolve_method(\"baz\", \"Foo::Bar\"))\n  assert_equal(\"baz\", entries.first.name)\n  assert_equal(\"Foo::Bar\", T.must(entries.first.owner).name)\nend\n")

#test_resolve_method_with_two_definitionsObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ruby_indexer/test/index_test.rb', line 264

def test_resolve_method_with_two_definitions
  index("    class Foo\n      # Hello from first `bar`\n      def bar; end\n    end\n\n    class Foo\n      # Hello from second `bar`\n      def bar; end\n    end\n  RUBY\n\n  first_entry, second_entry = T.must(@index.resolve_method(\"bar\", \"Foo\"))\n\n  assert_equal(\"bar\", first_entry.name)\n  assert_equal(\"Foo\", T.must(first_entry.owner).name)\n  assert_includes(first_entry.comments, \"Hello from first `bar`\")\n\n  assert_equal(\"bar\", second_entry.name)\n  assert_equal(\"Foo\", T.must(second_entry.owner).name)\n  assert_includes(second_entry.comments, \"Hello from second `bar`\")\nend\n")

#test_resolve_normalizes_top_level_namesObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ruby_indexer/test/index_test.rb', line 162

def test_resolve_normalizes_top_level_names
  @index.index_single(IndexablePath.new("/fake", "/fake/path/foo.rb"), "    class Bar; end\n\n    module Foo\n      class Bar; end\n    end\n  RUBY\n\n  entries = @index.resolve(\"::Foo::Bar\", [])\n  refute_nil(entries)\n\n  assert_equal(\"Foo::Bar\", entries.first.name)\n\n  entries = @index.resolve(\"::Bar\", [\"Foo\"])\n  refute_nil(entries)\n\n  assert_equal(\"Bar\", entries.first.name)\nend\n")

#test_resolving_a_qualified_referenceObject



1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
# File 'lib/ruby_indexer/test/index_test.rb', line 1194

def test_resolving_a_qualified_reference
  index("    class Base\n      module Third\n        CONST = 1\n      end\n    end\n\n    class Foo\n      module Third\n        CONST = 2\n      end\n\n      class Second < Base\n      end\n    end\n  RUBY\n\n  foo_entry = T.must(@index.resolve(\"Third::CONST\", [\"Foo\"])&.first)\n  assert_equal(9, foo_entry.location.start_line)\nend\n")

#test_resolving_aliases_to_non_existing_constants_with_conflicting_namesObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ruby_indexer/test/index_test.rb', line 182

def test_resolving_aliases_to_non_existing_constants_with_conflicting_names
  @index.index_single(IndexablePath.new("/fake", "/fake/path/foo.rb"), "    class Bar\n    end\n\n    module Foo\n      class Bar < self\n        BAZ = ::Bar::BAZ\n      end\n    end\n  RUBY\n\n  entry = @index.resolve(\"BAZ\", [\"Foo\", \"Bar\"]).first\n  refute_nil(entry)\n\n  assert_instance_of(Entry::UnresolvedConstantAlias, entry)\nend\n")

#test_resolving_an_inherited_methodObject



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/ruby_indexer/test/index_test.rb', line 723

def test_resolving_an_inherited_method
  index("    module Foo\n      def baz; end\n    end\n\n    class Bar\n      def qux; end\n    end\n\n    class Wow < Bar\n      include Foo\n    end\n  RUBY\n\n  entry = T.must(@index.resolve_method(\"baz\", \"Wow\")&.first)\n  assert_equal(\"baz\", entry.name)\n  assert_equal(\"Foo\", T.must(entry.owner).name)\n\n  entry = T.must(@index.resolve_method(\"qux\", \"Wow\")&.first)\n  assert_equal(\"qux\", entry.name)\n  assert_equal(\"Bar\", T.must(entry.owner).name)\nend\n")

#test_resolving_an_inherited_method_lands_on_first_matchObject



747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/ruby_indexer/test/index_test.rb', line 747

def test_resolving_an_inherited_method_lands_on_first_match
  index("    module Foo\n      def qux; end\n    end\n\n    class Bar\n      def qux; end\n    end\n\n    class Wow < Bar\n      prepend Foo\n\n      def qux; end\n    end\n  RUBY\n\n  entries = T.must(@index.resolve_method(\"qux\", \"Wow\"))\n  assert_equal(1, entries.length)\n\n  entry = T.must(entries.first)\n  assert_equal(\"qux\", entry.name)\n  assert_equal(\"Foo\", T.must(entry.owner).name)\nend\n")

#test_resolving_circular_aliasObject



1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/ruby_indexer/test/index_test.rb', line 1008

def test_resolving_circular_alias
  index("    module Namespace\n      FOO = BAR\n      BAR = FOO\n    end\n  RUBY\n\n  foo_entry = T.must(@index.resolve(\"FOO\", [\"Namespace\"])&.first)\n  assert_equal(2, foo_entry.location.start_line)\n  assert_instance_of(Entry::ConstantAlias, foo_entry)\n\n  bar_entry = T.must(@index.resolve(\"BAR\", [\"Namespace\"])&.first)\n  assert_equal(3, bar_entry.location.start_line)\n  assert_instance_of(Entry::ConstantAlias, bar_entry)\nend\n")

#test_resolving_circular_alias_three_levelsObject



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
# File 'lib/ruby_indexer/test/index_test.rb', line 1025

def test_resolving_circular_alias_three_levels
  index("    module Namespace\n      FOO = BAR\n      BAR = BAZ\n      BAZ = FOO\n    end\n  RUBY\n\n  foo_entry = T.must(@index.resolve(\"FOO\", [\"Namespace\"])&.first)\n  assert_equal(2, foo_entry.location.start_line)\n  assert_instance_of(Entry::ConstantAlias, foo_entry)\n\n  bar_entry = T.must(@index.resolve(\"BAR\", [\"Namespace\"])&.first)\n  assert_equal(3, bar_entry.location.start_line)\n  assert_instance_of(Entry::ConstantAlias, bar_entry)\n\n  baz_entry = T.must(@index.resolve(\"BAZ\", [\"Namespace\"])&.first)\n  assert_equal(4, baz_entry.location.start_line)\n  assert_instance_of(Entry::ConstantAlias, baz_entry)\nend\n")

#test_resolving_circular_method_aliasesObject



1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
# File 'lib/ruby_indexer/test/index_test.rb', line 1478

def test_resolving_circular_method_aliases
  index("    class Foo\n      alias bar bar\n    end\n  RUBY\n\n  # It's not possible to resolve an alias that points to itself\n  methods = @index.resolve_method(\"bar\", \"Foo\")\n  assert_nil(methods)\n\n  entry = T.must(@index[\"bar\"].first)\n  assert_kind_of(Entry::UnresolvedMethodAlias, entry)\nend\n")

#test_resolving_constants_favors_ancestors_over_top_levelObject



984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/ruby_indexer/test/index_test.rb', line 984

def test_resolving_constants_favors_ancestors_over_top_level
  index("    module Value1\n      CONST = 1\n    end\n\n    module Value2\n      CONST = 2\n    end\n\n    CONST = 3\n    module First\n      include Value1\n\n      module Second\n        include Value2\n      end\n    end\n  RUBY\n\n  entry = T.must(@index.resolve(\"CONST\", [\"First\", \"Second\"])&.first)\n  assert_equal(6, entry.location.start_line)\nend\n")

#test_resolving_constants_in_aliased_namespaceObject



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# File 'lib/ruby_indexer/test/index_test.rb', line 1047

def test_resolving_constants_in_aliased_namespace
  index("    module Original\n      module Something\n        CONST = 123\n      end\n    end\n\n    module Other\n      ALIAS = Original::Something\n    end\n\n    module Third\n      Other::ALIAS::CONST\n    end\n  RUBY\n\n  entry = T.must(@index.resolve(\"Other::ALIAS::CONST\", [\"Third\"])&.first)\n  assert_kind_of(Entry::Constant, entry)\n  assert_equal(\"Original::Something::CONST\", entry.name)\nend\n")

#test_resolving_constants_in_singleton_contextsObject



1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
# File 'lib/ruby_indexer/test/index_test.rb', line 1323

def test_resolving_constants_in_singleton_contexts
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    module Foo\n      class Bar\n        CONST = 3\n\n        class << self\n          CONST = 2\n\n          class Baz\n            CONST = 1\n\n            class << self\n            end\n          end\n        end\n      end\n    end\n  RUBY\n\n  entry = @index.resolve(\"CONST\", [\"Foo\", \"Bar\", \"<Class:Bar>\", \"Baz\", \"<Class:Baz>\"])&.first\n  refute_nil(entry)\n  assert_equal(9, T.must(entry).location.start_line)\nend\n")

#test_resolving_inherited_alised_namespaceObject



907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# File 'lib/ruby_indexer/test/index_test.rb', line 907

def test_resolving_inherited_alised_namespace
  index("    module Bar\n      TARGET = 123\n    end\n\n    module Foo\n      CONST = Bar\n    end\n\n    module Namespace\n      class Bar\n        include Foo\n      end\n    end\n  RUBY\n\n  entry = T.must(@index.resolve(\"Foo::CONST::TARGET\", [])&.first)\n  assert_equal(2, entry.location.start_line)\n\n  entry = T.must(@index.resolve(\"Namespace::Bar::CONST::TARGET\", [])&.first)\n  assert_equal(2, entry.location.start_line)\nend\n")

#test_resolving_inherited_constantsObject



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
# File 'lib/ruby_indexer/test/index_test.rb', line 876

def test_resolving_inherited_constants
  index("    module Foo\n      CONST = 1\n    end\n\n    module Baz\n      CONST = 2\n    end\n\n    module Qux\n      include Foo\n    end\n\n    module Namespace\n      CONST = 3\n\n      include Baz\n\n      class Bar\n        include Qux\n      end\n    end\n\n    CONST = 4\n  RUBY\n\n  entry = T.must(@index.resolve(\"CONST\", [\"Namespace\", \"Bar\"])&.first)\n  assert_equal(14, entry.location.start_line)\nend\n")

#test_resolving_instance_variables_in_singleton_contextsObject



1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
# File 'lib/ruby_indexer/test/index_test.rb', line 1348

def test_resolving_instance_variables_in_singleton_contexts
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    module Foo\n      class Bar\n        @a = 123\n\n        class << self\n          def hello\n            @b = 123\n          end\n\n          @c = 123\n        end\n      end\n    end\n  RUBY\n\n  entry = @index.resolve_instance_variable(\"@a\", \"Foo::Bar::<Class:Bar>\")&.first\n  refute_nil(entry)\n  assert_equal(\"@a\", T.must(entry).name)\n\n  entry = @index.resolve_instance_variable(\"@b\", \"Foo::Bar::<Class:Bar>\")&.first\n  refute_nil(entry)\n  assert_equal(\"@b\", T.must(entry).name)\n\n  entry = @index.resolve_instance_variable(\"@c\", \"Foo::Bar::<Class:Bar>::<Class:<Class:Bar>>\")&.first\n  refute_nil(entry)\n  assert_equal(\"@c\", T.must(entry).name)\nend\n")

#test_resolving_method_aliasesObject



1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
# File 'lib/ruby_indexer/test/index_test.rb', line 1424

def test_resolving_method_aliases
  index("    class Foo\n      def bar(a, b, c)\n      end\n\n      alias double_alias bar\n    end\n\n    class Bar < Foo\n      def hello(b); end\n\n      alias baz bar\n      alias_method :qux, :hello\n      alias double double_alias\n    end\n  RUBY\n\n  # baz\n  methods = @index.resolve_method(\"baz\", \"Bar\")\n  refute_nil(methods)\n\n  entry = T.must(methods.first)\n  assert_kind_of(Entry::MethodAlias, entry)\n  assert_equal(\"bar\", entry.target.name)\n  assert_equal(\"Foo\", T.must(entry.target.owner).name)\n\n  # qux\n  methods = @index.resolve_method(\"qux\", \"Bar\")\n  refute_nil(methods)\n\n  entry = T.must(methods.first)\n  assert_kind_of(Entry::MethodAlias, entry)\n  assert_equal(\"hello\", entry.target.name)\n  assert_equal(\"Bar\", T.must(entry.target.owner).name)\n\n  # double\n  methods = @index.resolve_method(\"double\", \"Bar\")\n  refute_nil(methods)\n\n  entry = T.must(methods.first)\n  assert_kind_of(Entry::MethodAlias, entry)\n\n  target = entry.target\n  assert_equal(\"double_alias\", target.name)\n  assert_kind_of(Entry::MethodAlias, target)\n  assert_equal(\"Foo\", T.must(target.owner).name)\n\n  final_target = target.target\n  assert_equal(\"bar\", final_target.name)\n  assert_kind_of(Entry::Method, final_target)\n  assert_equal(\"Foo\", T.must(final_target.owner).name)\nend\n")

#test_resolving_method_inside_singleton_contextObject



1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
# File 'lib/ruby_indexer/test/index_test.rb', line 1302

def test_resolving_method_inside_singleton_context
  @index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), "    module Foo\n      class Bar\n        class << self\n          class Baz\n            class << self\n              def found_me!; end\n            end\n          end\n        end\n      end\n    end\n  RUBY\n\n  entry = @index.resolve_method(\"found_me!\", \"Foo::Bar::<Class:Bar>::Baz::<Class:Baz>\")&.first\n  refute_nil(entry)\n\n  assert_equal(\"found_me!\", T.must(entry).name)\nend\n")

#test_resolving_prepended_constantsObject



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'lib/ruby_indexer/test/index_test.rb', line 952

def test_resolving_prepended_constants
  index("    module Included\n      CONST = 123\n    end\n\n    module Prepended\n      CONST = 321\n    end\n\n    class Foo\n      include Included\n      prepend Prepended\n    end\n\n    class Bar\n      CONST = 456\n      include Included\n      prepend Prepended\n    end\n  RUBY\n\n  entry = T.must(@index.resolve(\"CONST\", [\"Foo\"])&.first)\n  assert_equal(6, entry.location.start_line)\n\n  entry = T.must(@index.resolve(\"Foo::CONST\", [])&.first)\n  assert_equal(6, entry.location.start_line)\n\n  entry = T.must(@index.resolve(\"Bar::CONST\", [])&.first)\n  assert_equal(15, entry.location.start_line)\nend\n")

#test_resolving_qualified_referencesObject



1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'lib/ruby_indexer/test/index_test.rb', line 1120

def test_resolving_qualified_references
  index("    module Namespace\n      class Entry\n        CONST = 1\n      end\n    end\n\n    module Namespace\n      class Index\n      end\n    end\n  RUBY\n\n  foo_entry = T.must(@index.resolve(\"Entry::CONST\", [\"Namespace\", \"Index\"])&.first)\n  assert_equal(3, foo_entry.location.start_line)\nend\n")

#test_resolving_references_with_only_top_level_declarationObject



1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
# File 'lib/ruby_indexer/test/index_test.rb', line 1157

def test_resolving_references_with_only_top_level_declaration
  index("    CONST = 1\n\n    module Foo; end\n\n    module Namespace\n      class Index\n        include Foo\n      end\n    end\n  RUBY\n\n  foo_entry = T.must(@index.resolve(\"CONST\", [\"Namespace\", \"Index\"])&.first)\n  assert_equal(1, foo_entry.location.start_line)\nend\n")

#test_resolving_references_with_redundant_namespacesObject



1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/ruby_indexer/test/index_test.rb', line 1095

def test_resolving_references_with_redundant_namespaces
  index("    module Bar\n      CONST = 1\n    end\n\n    module A\n      CONST = 2\n\n      module B\n        CONST = 3\n\n        class Foo\n          include Bar\n        end\n\n        A::B::Foo::CONST\n      end\n    end\n  RUBY\n\n  foo_entry = T.must(@index.resolve(\"A::B::Foo::CONST\", [\"A\", \"B\"])&.first)\n  assert_equal(2, foo_entry.location.start_line)\nend\n")

#test_resolving_same_constant_from_different_scopesObject



931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'lib/ruby_indexer/test/index_test.rb', line 931

def test_resolving_same_constant_from_different_scopes
  index("    module Namespace\n      CONST = 123\n\n      class Parent\n        CONST = 321\n      end\n\n      class Child < Parent\n      end\n    end\n  RUBY\n\n  entry = T.must(@index.resolve(\"CONST\", [\"Namespace\", \"Child\"])&.first)\n  assert_equal(2, entry.location.start_line)\n\n  entry = T.must(@index.resolve(\"Namespace::Child::CONST\", [])&.first)\n  assert_equal(5, entry.location.start_line)\nend\n")

#test_resolving_top_level_aliasesObject



1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
# File 'lib/ruby_indexer/test/index_test.rb', line 1069

def test_resolving_top_level_aliases
  index("    class Foo\n      CONST = 123\n    end\n\n    FOO = Foo\n    FOO::CONST\n  RUBY\n\n  entry = T.must(@index.resolve(\"FOO::CONST\", [])&.first)\n  assert_kind_of(Entry::Constant, entry)\n  assert_equal(\"Foo::CONST\", entry.name)\nend\n")

#test_resolving_top_level_compact_referenceObject



1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'lib/ruby_indexer/test/index_test.rb', line 1084

def test_resolving_top_level_compact_reference
  index("    class Foo::Bar\n    end\n  RUBY\n\n  foo_entry = T.must(@index.resolve(\"Foo::Bar\", [])&.first)\n  assert_equal(1, foo_entry.location.start_line)\n  assert_instance_of(Entry::Class, foo_entry)\nend\n")

#test_resolving_unindexed_constant_with_no_nestingObject



1216
1217
1218
# File 'lib/ruby_indexer/test/index_test.rb', line 1216

def test_resolving_unindexed_constant_with_no_nesting
  assert_nil(@index.resolve("RSpec", []))
end

#test_resolving_unqualified_referencesObject



1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
# File 'lib/ruby_indexer/test/index_test.rb', line 1138

def test_resolving_unqualified_references
  index("    module Foo\n      CONST = 1\n    end\n\n    module Namespace\n      CONST = 2\n\n      class Index\n        include Foo\n      end\n    end\n  RUBY\n\n  foo_entry = T.must(@index.resolve(\"CONST\", [\"Namespace\", \"Index\"])&.first)\n  assert_equal(6, foo_entry.location.start_line)\nend\n")

#test_searching_for_entries_based_on_prefixObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ruby_indexer/test/index_test.rb', line 142

def test_searching_for_entries_based_on_prefix
  @index.index_single(IndexablePath.new("/fake", "/fake/path/foo.rb"), "    class Foo::Bizw\n    end\n  RUBY\n  @index.index_single(IndexablePath.new(\"/fake\", \"/fake/path/other_foo.rb\"), <<~RUBY)\n    class Foo::Bizw\n    end\n\n    class Foo::Bizt\n    end\n  RUBY\n\n  results = @index.prefix_search(\"Foo\", []).map { |entries| entries.map(&:name) }\n  assert_equal([[\"Foo::Bizw\", \"Foo::Bizw\"], [\"Foo::Bizt\"]], results)\n\n  results = @index.prefix_search(\"Biz\", [\"Foo\"]).map { |entries| entries.map(&:name) }\n  assert_equal([[\"Foo::Bizw\", \"Foo::Bizw\"], [\"Foo::Bizt\"]], results)\nend\n")

#test_searching_for_require_pathsObject



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ruby_indexer/test/index_test.rb', line 129

def test_searching_for_require_paths
  @index.index_single(IndexablePath.new("/fake", "/fake/path/foo.rb"), "    class Foo\n    end\n  RUBY\n  @index.index_single(IndexablePath.new(\"/fake\", \"/fake/path/other_foo.rb\"), <<~RUBY)\n    class Foo\n    end\n  RUBY\n\n  assert_equal([\"path/foo\", \"path/other_foo\"], @index.search_require_paths(\"path\").map(&:require_path))\nend\n")

#test_singleton_nesting_is_correctly_split_during_linearizationObject



1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
# File 'lib/ruby_indexer/test/index_test.rb', line 1793

def test_singleton_nesting_is_correctly_split_during_linearization
  index("    module Bar; end\n\n    module Foo\n      class Namespace::Parent\n        extend Bar\n      end\n    end\n\n    module Foo\n      class Child < Namespace::Parent\n      end\n    end\n  RUBY\n\n  assert_equal(\n    [\n      \"Foo::Child::<Class:Child>\",\n      \"Foo::Namespace::Parent::<Class:Parent>\",\n      \"Bar\",\n      \"Object::<Class:Object>\",\n      \"BasicObject::<Class:BasicObject>\",\n      \"Class\",\n      \"Module\",\n      \"Object\",\n      \"Kernel\",\n      \"BasicObject\",\n    ],\n    @index.linearized_ancestors_of(\"Foo::Child::<Class:Child>\"),\n  )\nend\n")

#test_singletons_are_excluded_from_fuzzy_searchObject



1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
# File 'lib/ruby_indexer/test/index_test.rb', line 1411

def test_singletons_are_excluded_from_fuzzy_search
  index("    class Zwq\n      class << self\n      end\n    end\n  RUBY\n\n  results = @index.fuzzy_search(\"Zwq\")\n  assert_equal(1, results.length)\n  assert_equal(\"Zwq\", results.first.name)\nend\n")

#test_singletons_are_excluded_from_prefix_searchObject



1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
# File 'lib/ruby_indexer/test/index_test.rb', line 1400

def test_singletons_are_excluded_from_prefix_search
  index("    class Zwq\n      class << self\n      end\n    end\n  RUBY\n\n  assert_empty(@index.prefix_search(\"Zwq::<C\"))\nend\n")

#test_top_level_basic_object_superclass_resolutionObject



1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
# File 'lib/ruby_indexer/test/index_test.rb', line 1290

def test_top_level_basic_object_superclass_resolution
  index("    module Foo\n      class BasicObject; end\n\n      class Bar < ::BasicObject; end\n    end\n  RUBY\n\n  assert_equal([\"Foo::Bar\", \"BasicObject\"], @index.linearized_ancestors_of(\"Foo::Bar\"))\nend\n")

#test_top_level_object_superclass_resolutionObject



1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
# File 'lib/ruby_indexer/test/index_test.rb', line 1278

def test_top_level_object_superclass_resolution
  index("    module Foo\n      class Object; end\n\n      class Bar < ::Object; end\n    end\n  RUBY\n\n  assert_equal([\"Foo::Bar\", \"Object\", \"Kernel\", \"BasicObject\"], @index.linearized_ancestors_of(\"Foo::Bar\"))\nend\n")

#test_unresolable_method_aliasesObject



1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
# File 'lib/ruby_indexer/test/index_test.rb', line 1493

def test_unresolable_method_aliases
  index("    class Foo\n      alias bar baz\n    end\n  RUBY\n\n  # `baz` does not exist, so resolving `bar` is not possible\n  methods = @index.resolve_method(\"bar\", \"Foo\")\n  assert_nil(methods)\n\n  entry = T.must(@index[\"bar\"].first)\n  assert_kind_of(Entry::UnresolvedMethodAlias, entry)\nend\n")

#test_visitor_does_not_visit_unnecessary_nodesObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/ruby_indexer/test/index_test.rb', line 200

def test_visitor_does_not_visit_unnecessary_nodes
  concats = (0...10_000).map do |i|
    "      \"string\#{i}\" \\\\\n    STRING\n  end.join\n\n  index(<<~RUBY)\n    module Foo\n      local_var = \#{concats}\n        \"final\"\n      @class_instance_var = \#{concats}\n        \"final\"\n      @@class_var = \#{concats}\n        \"final\"\n      $global_var = \#{concats}\n        \"final\"\n      CONST = \#{concats}\n        \"final\"\n    end\n  RUBY\nend\n"