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, #teardown

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(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    class Bar; end

    module Foo
      class Bar
      end

      class Baz
        class Something
        end
      end
    end
  RUBY

  entries = @index["::Foo::Baz::Something"] #: as !nil
  refute_empty(entries)
  assert_equal("Foo::Baz::Something", entries.first&.name)
end

#test_actual_nestingObject



2248
2249
2250
2251
2252
2253
# File 'lib/ruby_indexer/test/index_test.rb', line 2248

def test_actual_nesting
  assert_equal(["Foo"], Index.actual_nesting([], "Foo"))
  assert_equal(["TopLevel", "Foo"], Index.actual_nesting(["First", "::TopLevel"], "Foo"))
  assert_equal(["TopLevel", "Another", "Foo"], Index.actual_nesting(["::TopLevel", "Another"], "Foo"))
  assert_equal(["TopLevel"], Index.actual_nesting(["First", "::TopLevel"], nil))
end

#test_ancestors_linearization_complex_include_duplicationObject



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

def test_ancestors_linearization_complex_include_duplication
  index(<<~RUBY)
    module A; end
    module B
      include A
    end
    module C
      include B
    end

    class Foo
      include A
      include C
    end
  RUBY

  assert_equal(
    [
      "Foo",
      "C",
      "B",
      "A",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Foo"),
  )
end

#test_ancestors_linearization_complex_prepend_duplicationObject



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

def test_ancestors_linearization_complex_prepend_duplication
  index(<<~RUBY)
    module A; end
    module B
      prepend A
    end
    module C
      prepend B
    end

    class Foo
      prepend A
      prepend C
    end
  RUBY

  assert_equal(
    [
      "A",
      "B",
      "C",
      "Foo",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Foo"),
  )
end

#test_basic_object_superclass_resolutionObject



1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
# File 'lib/ruby_indexer/test/index_test.rb', line 1336

def test_basic_object_superclass_resolution
  index(<<~RUBY)
    module Foo
      class BasicObject; end

      class Bar; end
      class Baz < BasicObject; end
    end
  RUBY

  assert_equal(["Foo::Bar", "Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Foo::Bar"))
  assert_equal(
    ["Foo::Baz", "Foo::BasicObject", "Object", "Kernel", "BasicObject"],
    @index.linearized_ancestors_of("Foo::Baz"),
  )
end

#test_build_non_redundant_nameObject



2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
# File 'lib/ruby_indexer/test/index_test.rb', line 2116

def test_build_non_redundant_name
  assert_equal(
    "Namespace::Foo::Constants",
    @index.send(
      :build_non_redundant_full_name,
      "Namespace::Foo::Constants",
      ["Namespace", "Foo", "InnerNamespace"],
    ),
  )

  assert_equal(
    "Namespace::Foo::Constants",
    @index.send(
      :build_non_redundant_full_name,
      "Namespace::Foo::Constants",
      ["Namespace", "Foo"],
    ),
  )

  assert_equal(
    "Namespace::Foo::Constants",
    @index.send(
      :build_non_redundant_full_name,
      "Foo::Constants",
      ["Namespace", "Foo"],
    ),
  )

  assert_equal(
    "Bar::Namespace::Foo::Constants",
    @index.send(
      :build_non_redundant_full_name,
      "Namespace::Foo::Constants",
      ["Bar"],
    ),
  )

  assert_equal(
    "First::Namespace::Foo::Constants",
    @index.send(
      :build_non_redundant_full_name,
      "Namespace::Foo::Constants",
      ["First", "Namespace", "Foo", "InnerNamespace"],
    ),
  )
end

#test_class_variable_completion_from_singleton_contextObject



2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
# File 'lib/ruby_indexer/test/index_test.rb', line 2219

def test_class_variable_completion_from_singleton_context
  index(<<~RUBY)
    class Foo
      @@hello = 123

      def self.do
      end
    end
  RUBY

  candidates = @index.class_variable_completion_candidates("@@", "Foo::<Class:Foo>")
  refute_empty(candidates)

  assert_equal("@@hello", candidates.first&.name)
end

#test_completion_does_not_duplicate_methods_overridden_by_aliasesObject



1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
# File 'lib/ruby_indexer/test/index_test.rb', line 1667

def test_completion_does_not_duplicate_methods_overridden_by_aliases
  index(<<~RUBY)
    class Foo
      def bar; end
    end

    class Baz < Foo
      alias bar to_s
    end
  RUBY

  entries = @index.method_completion_candidates("bar", "Baz")
  assert_equal(["bar"], entries.map(&:name))
  assert_equal("Baz", entries.first&.owner&.name)
end

#test_completion_does_not_duplicate_overridden_methodsObject



1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
# File 'lib/ruby_indexer/test/index_test.rb', line 1651

def test_completion_does_not_duplicate_overridden_methods
  index(<<~RUBY)
    class Foo
      def bar; end
    end

    class Baz < Foo
      def bar; end
    end
  RUBY

  entries = @index.method_completion_candidates("bar", "Baz")
  assert_equal(["bar"], entries.map(&:name))
  assert_equal("Baz", entries.first&.owner&.name)
end

#test_completion_does_not_include_unresolved_aliasesObject



1609
1610
1611
1612
1613
1614
1615
1616
1617
# File 'lib/ruby_indexer/test/index_test.rb', line 1609

def test_completion_does_not_include_unresolved_aliases
  index(<<~RUBY)
    class Foo
      alias_method :bar, :missing
    end
  RUBY

  assert_empty(@index.method_completion_candidates("bar", "Foo"))
end

#test_constant_completion_candidates_all_possible_constantsObject



1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
# File 'lib/ruby_indexer/test/index_test.rb', line 1991

def test_constant_completion_candidates_all_possible_constants
  index(<<~RUBY)
    XQRK = 3

    module Bar
      XQRK = 2
    end

    module Foo
      XQRK = 1
    end

    module Namespace
      XQRK = 0

      class Baz
        include Foo
        include Bar
      end
    end
  RUBY

  result = @index.constant_completion_candidates("X", ["Namespace", "Baz"])

  result.each do |entries|
    name = entries.first&.name
    assert(entries.all? { |e| e.name == name })
  end

  assert_equal(["Namespace::XQRK", "Bar::XQRK", "XQRK"], result.map { |entries| entries.first&.name })

  result = @index.constant_completion_candidates("::X", ["Namespace", "Baz"])
  assert_equal(["XQRK"], result.map { |entries| entries.first&.name })
end

#test_constant_completion_candidates_for_empty_nameObject



2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
# File 'lib/ruby_indexer/test/index_test.rb', line 2041

def test_constant_completion_candidates_for_empty_name
  index(<<~RUBY)
    module Foo
      Bar = 1
    end

    class Baz
      include Foo
    end
  RUBY

  result = @index.constant_completion_candidates("Baz::", [])
  assert_includes(result.map { |entries| entries.first&.name }, "Foo::Bar")
end

#test_constant_completion_does_not_confuse_uppercase_methodsObject



2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
# File 'lib/ruby_indexer/test/index_test.rb', line 2026

def test_constant_completion_does_not_confuse_uppercase_methods
  index(<<~RUBY)
    class Foo
      def Qux
      end
    end
  RUBY

  candidates = @index.constant_completion_candidates("Q", [])
  refute_includes(candidates.flat_map { |entries| entries.map(&:name) }, "Qux")

  candidates = @index.constant_completion_candidates("Qux", [])
  assert_equal(0, candidates.length)
end

#test_constant_nameObject



2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
# File 'lib/ruby_indexer/test/index_test.rb', line 2255

def test_constant_name
  node = Prism.parse("class var::Foo; end").value.statements.body.first.constant_path
  assert_nil(Index.constant_name(node))

  node = Prism.parse("class ; end").value.statements.body.first.constant_path
  assert_nil(Index.constant_name(node))

  node = Prism.parse("class method_call; end").value.statements.body.first.constant_path
  assert_nil(Index.constant_name(node))

  node = Prism.parse("class Foo; end").value.statements.body.first.constant_path
  assert_equal("Foo", Index.constant_name(node))

  node = Prism.parse(<<~RUBY).value.statements.body.first.constant_path
    class class Foo
    end
    end
  RUBY
  assert_nil(Index.constant_name(node))
end

#test_decorated_parametersObject



1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
# File 'lib/ruby_indexer/test/index_test.rb', line 1683

def test_decorated_parameters
  index(<<~RUBY)
    class Foo
      def bar(a, b = 1, c: 2)
      end
    end
  RUBY

  methods = @index.resolve_method("bar", "Foo") #: as !nil
  refute_nil(methods)

  entry = methods.first #: as Entry::Method
  assert_equal("(a, b = <default>, c: <default>)", entry.decorated_parameters)
end

#test_decorated_parameters_when_method_has_no_parametersObject



1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
# File 'lib/ruby_indexer/test/index_test.rb', line 1698

def test_decorated_parameters_when_method_has_no_parameters
  index(<<~RUBY)
    class Foo
      def bar
      end
    end
  RUBY

  methods = @index.resolve_method("bar", "Foo") #: as !nil
  refute_nil(methods)

  entry = methods.first #: as Entry::Method
  assert_equal("()", entry.decorated_parameters)
end

#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(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    class Foo
    end
  RUBY

  entries = @index["Foo"] #: as !nil
  assert_equal(1, entries.length)

  @index.delete(URI::Generic.from_path(path: "/fake/path/foo.rb"))
  entries = @index["Foo"]
  assert_nil(entries)
end

#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(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    class Foo
    end
  RUBY
  @index.index_single(URI::Generic.from_path(path: "/fake/path/other_foo.rb"), <<~RUBY)
    class Foo
    end
  RUBY

  entries = @index["Foo"] #: as !nil
  assert_equal(2, entries.length)

  @index.delete(URI::Generic.from_path(path: "/fake/path/other_foo.rb"))
  entries = @index["Foo"] #: as !nil
  assert_equal(1, entries.length)
end

#test_duplicate_prepend_includeObject



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

def test_duplicate_prepend_include
  index(<<~RUBY)
    module A; end

    class Foo
      prepend A
      include A
    end

    class Bar
      include A
      prepend A
    end
  RUBY

  assert_equal(
    [
      "A",
      "Foo",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Foo"),
  )

  assert_equal(
    [
      "A",
      "Bar",
      "A",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Bar"),
  )
end

#test_entries_forObject



1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
# File 'lib/ruby_indexer/test/index_test.rb', line 1967

def test_entries_for
  index(<<~RUBY)
    class Foo; end

    module Bar
      def my_def; end
      def self.my_singleton_def; end
    end
  RUBY

  entries = @index.entries_for("file:///fake/path/foo.rb", Entry) #: as !nil
  assert_equal(["Foo", "Bar", "my_def", "Bar::<Class:Bar>", "my_singleton_def"], entries.map(&:name))

  entries = @index.entries_for("file:///fake/path/foo.rb", RubyIndexer::Entry::Namespace) #: as !nil
  assert_equal(["Foo", "Bar", "Bar::<Class:Bar>"], entries.map(&:name))

  entries = @index.entries_for("file:///fake/path/foo.rb") #: as !nil
  assert_equal(["Foo", "Bar", "my_def", "Bar::<Class:Bar>", "my_singleton_def"], entries.map(&:name))
end

#test_entries_for_returns_nil_if_no_matchesObject



1987
1988
1989
# File 'lib/ruby_indexer/test/index_test.rb', line 1987

def test_entries_for_returns_nil_if_no_matches
  assert_nil(@index.entries_for("non_existing_file.rb", Entry::Namespace))
end

#test_extend_selfObject



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

def test_extend_self
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    module Foo
      def bar
      end

      extend self

      def baz
      end
    end
  RUBY

  ["bar", "baz"].product(["Foo", "Foo::<Class:Foo>"]).each do |method, receiver|
    entry = @index.resolve_method(method, receiver)&.first #: as !nil
    refute_nil(entry)
    assert_equal(method, entry.name)
  end

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

#test_first_unqualified_constObject



1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
# File 'lib/ruby_indexer/test/index_test.rb', line 1619

def test_first_unqualified_const
  index(<<~RUBY)
    module Foo
      class Bar; end
    end

    module Baz
      class Bar; end
    end
  RUBY

  entry = @index.first_unqualified_const("Bar")&.first #: as !nil
  assert_equal("Foo::Bar", entry.name)
end

#test_first_unqualified_const_prefers_exact_matchesObject



1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
# File 'lib/ruby_indexer/test/index_test.rb', line 1634

def test_first_unqualified_const_prefers_exact_matches
  index(<<~RUBY)
    module Foo
      class ParseResultType
      end
    end

    module Namespace
      class Type
      end
    end
  RUBY

  entry = @index.first_unqualified_const("Type")&.first #: as !nil
  assert_equal("Namespace::Type", entry.name)
end

#test_follow_alias_namespaceObject



2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
# File 'lib/ruby_indexer/test/index_test.rb', line 2056

def test_follow_alias_namespace
  index(<<~RUBY)
    module First
      module Second
        class Foo
        end
      end
    end

    module Namespace
      Second = First::Second
    end
  RUBY

  real_namespace = @index.follow_aliased_namespace("Namespace::Second")
  assert_equal("First::Second", real_namespace)
end

#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(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    class Zws; end

    module Qtl
      class Zws
      end

      class Zwo
        class Something
        end
      end
    end
  RUBY

  result = @index.fuzzy_search("Zws")
  assert_equal(2, result.length)
  assert_equal(["Zws", "Qtl::Zwo::Something"], result.map(&:name))

  result = @index.fuzzy_search("qtlzwssomeking")
  assert_equal(5, result.length)
  assert_equal(["Qtl::Zwo::Something", "Qtl::Zws", "Qtl::Zwo", "Qtl", "Zws"], result.map(&:name))

  result = @index.fuzzy_search("QltZwo")
  assert_equal(4, result.length)
  assert_equal(["Qtl::Zwo", "Qtl::Zws", "Qtl::Zwo::Something", "Qtl"], result.map(&:name))
end

#test_handle_change_clears_ancestor_cache_if_parent_class_changedObject



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
906
907
908
909
# File 'lib/ruby_indexer/test/index_test.rb', line 877

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"), <<~RUBY)
        class Foo
        end

        class Bar < Foo
        end
      RUBY

      uri = URI::Generic.from_path(path: File.join(dir, "foo.rb"))
      @index.index_file(uri)

      assert_equal(["Bar", "Foo", "Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Bar"))

      # Remove include to invalidate the ancestor tree
      File.write(File.join(dir, "foo.rb"), <<~RUBY)
        class Foo
        end

        class Bar
        end
      RUBY

      path = uri.full_path #: as !nil
      @index.handle_change(uri, File.read(path))
      assert_empty(@index.instance_variable_get(:@ancestors))
      assert_equal(["Bar", "Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Bar"))
    end
  end
end

#test_handle_change_clears_ancestor_cache_if_tree_changedObject



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

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"), <<~RUBY)
        module Foo
        end

        class Bar
          include Foo
        end
      RUBY

      uri = URI::Generic.from_path(path: File.join(dir, "foo.rb"))
      @index.index_file(uri)

      assert_equal(["Bar", "Foo", "Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Bar"))

      # Remove include to invalidate the ancestor tree
      File.write(File.join(dir, "foo.rb"), <<~RUBY)
        module Foo
        end

        class Bar
        end
      RUBY

      path = uri.full_path #: as !nil
      @index.handle_change(uri, File.read(path))
      assert_empty(@index.instance_variable_get(:@ancestors))
      assert_equal(["Bar", "Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Bar"))
    end
  end
end

#test_handle_change_does_not_clear_ancestor_cache_if_tree_not_changedObject



839
840
841
842
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
875
# File 'lib/ruby_indexer/test/index_test.rb', line 839

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"), <<~RUBY)
        module Foo
        end

        class Bar
          include Foo
        end
      RUBY

      uri = URI::Generic.from_path(path: File.join(dir, "foo.rb"))
      @index.index_file(uri)

      assert_equal(["Bar", "Foo", "Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Bar"))

      # Remove include to invalidate the ancestor tree
      File.write(File.join(dir, "foo.rb"), <<~RUBY)
        module Foo
        end

        class Bar
          include Foo

          def baz; end
        end
      RUBY

      path = uri.full_path #: as !nil
      @index.handle_change(uri, File.read(path))
      refute_empty(@index.instance_variable_get(:@ancestors))
      assert_equal(["Bar", "Foo", "Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Bar"))
    end
  end
end

#test_index_can_handle_entries_from_untitled_schemeObject



2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
# File 'lib/ruby_indexer/test/index_test.rb', line 2171

def test_index_can_handle_entries_from_untitled_scheme
  uri = URI("untitled:Untitled-1")

  index(<<~RUBY, uri: uri)
    class Foo
    end
  RUBY

  entry = @index["Foo"]&.first #: as !nil
  refute_nil(entry, "Expected indexer to be able to handle unsaved URIs")
  assert_equal("untitled:Untitled-1", entry.uri.to_s)
  assert_equal("Untitled-1", entry.file_name)
  assert_nil(entry.file_path)

  @index.handle_change(uri, <<~RUBY)
    # I added this comment!
    class Foo
    end
  RUBY

  entry = @index["Foo"]&.first #: as !nil
  refute_nil(entry, "Expected indexer to be able to handle unsaved URIs")
  assert_equal("I added this comment!", entry.comments)
end

#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(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    class Bar; end

    module Foo
      class Bar
      end

      class Baz
        class Something
        end
      end
    end
  RUBY

  entries = @index.resolve("Something", ["Foo", "Baz"]) #: as !nil
  refute_empty(entries)
  assert_equal("Foo::Baz::Something", entries.first&.name)

  entries = @index.resolve("Bar", ["Foo"]) #: as !nil
  refute_empty(entries)
  assert_equal("Foo::Bar", entries.first&.name)

  entries = @index.resolve("Bar", ["Foo", "Baz"]) #: as !nil
  refute_empty(entries)
  assert_equal("Foo::Bar", entries.first&.name)

  entries = @index.resolve("Foo::Bar", ["Foo", "Baz"]) #: as !nil
  refute_empty(entries)
  assert_equal("Foo::Bar", entries.first&.name)

  assert_nil(@index.resolve("DoesNotExist", ["Foo"]))
end

#test_index_single_does_not_fail_for_non_existing_fileObject



364
365
366
367
368
# File 'lib/ruby_indexer/test/index_test.rb', line 364

def test_index_single_does_not_fail_for_non_existing_file
  @index.index_file(URI::Generic.from_path(path: "/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
128
129
130
131
# File 'lib/ruby_indexer/test/index_test.rb', line 122

def test_index_single_ignores_directories
  path = "#{Dir.pwd}/lib/this_is_a_dir.rb"
  FileUtils.mkdir(path)

  begin
    @index.index_file(URI::Generic.from_path(path: path))
  ensure
    FileUtils.rm_r(path)
  end
end

#test_indexing_prism_fixtures_succeedsObject



349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/ruby_indexer/test/index_test.rb', line 349

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("#{Dir.pwd}/test/fixtures/prism/test/prism/fixtures/**/*.txt")

  fixtures.each do |fixture|
    uri = URI::Generic.from_path(path: fixture)
    @index.index_file(uri)
  end

  refute_empty(@index)
end

#test_instance_variable_completion_in_singleton_contextsObject



1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
# File 'lib/ruby_indexer/test/index_test.rb', line 1452

def test_instance_variable_completion_in_singleton_contexts
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    module Foo
      class Bar
        @a = 123

        class << self
          def hello
            @b = 123
          end

          @c = 123
        end
      end
    end
  RUBY

  entries = @index.instance_variable_completion_candidates("@", "Foo::Bar::<Class:Bar>").map(&:name)
  assert_includes(entries, "@a")
  assert_includes(entries, "@b")
end

#test_instance_variable_completion_returns_class_variables_tooObject



2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
# File 'lib/ruby_indexer/test/index_test.rb', line 2196

def test_instance_variable_completion_returns_class_variables_too
  index(<<~RUBY)
    class Parent
      @@abc = 123
    end

    class Child < Parent
      @@adf = 123

      def self.do
      end
    end
  RUBY

  adf, abc = @index.instance_variable_completion_candidates("@", "Child::<Class:Child>")

  refute_nil(abc)
  refute_nil(adf)

  assert_equal("@@abc", abc&.name)
  assert_equal("@@adf", adf&.name)
end

#test_instance_variables_completions_from_different_owners_with_conflicting_namesObject



1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
# File 'lib/ruby_indexer/test/index_test.rb', line 1249

def test_instance_variables_completions_from_different_owners_with_conflicting_names
  index(<<~RUBY)
    class Foo
      def initialize
        @bar = 1
      end
    end

    class Bar
      def initialize
        @bar = 2
      end
    end
  RUBY

  entry = @index.instance_variable_completion_candidates("@", "Bar").first #: as !nil
  assert_equal("@bar", entry.name)
  assert_equal("Bar", entry.owner&.name)
end

#test_linearized_ancestorsObject



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

def test_linearized_ancestors
  index(<<~RUBY)
    module A; end
    module B; end
    module C; end

    module D
      include A
    end

    module E
      prepend B
    end

    module F
      include C
      include A
    end

    class Bar
      prepend F
    end

    class Foo < Bar
      include E
      prepend D
    end
  RUBY

  # Object, Kernel and BasicObject are intentionally commented out for now until we develop a strategy for indexing
  # declarations made in C code
  assert_equal(
    [
      "D",
      "A",
      "Foo",
      "B",
      "E",
      "F",
      "A",
      "C",
      "Bar",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Foo"),
  )
end

#test_linearized_ancestors_basic_orderingObject



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

def test_linearized_ancestors_basic_ordering
  index(<<~RUBY)
    module A; end
    module B; end

    class Foo
      prepend A
      prepend B
    end

    class Bar
      include A
      include B
    end
  RUBY

  assert_equal(
    [
      "B",
      "A",
      "Foo",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Foo"),
  )

  assert_equal(
    [
      "Bar",
      "B",
      "A",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Bar"),
  )
end

#test_linearized_ancestors_duplicatesObject



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

def test_linearized_ancestors_duplicates
  index(<<~RUBY)
    module A; end
    module B
      include A
    end

    class Foo
      include B
      include A
    end

    class Bar
      prepend B
      prepend A
    end
  RUBY

  assert_equal(
    [
      "Foo",
      "B",
      "A",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Foo"),
  )

  assert_equal(
    [
      "B",
      "A",
      "Bar",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Bar"),
  )
end

#test_linearizing_a_module_singleton_classObject



1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
# File 'lib/ruby_indexer/test/index_test.rb', line 1867

def test_linearizing_a_module_singleton_class
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    module A; end
  RUBY

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

#test_linearizing_a_singleton_class_with_no_attachedObject



1884
1885
1886
1887
1888
# File 'lib/ruby_indexer/test/index_test.rb', line 1884

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_classes_with_overridden_parentsObject



731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# File 'lib/ruby_indexer/test/index_test.rb', line 731

def test_linearizing_ancestors_for_classes_with_overridden_parents
  index(<<~RUBY)
    # Find the re-open of a class first, without specifying a parent
    class Child
    end

    # Now, find the actual definition of the class, which includes a parent
    class Parent; end
    class Child < Parent
    end
  RUBY

  assert_equal(
    [
      "Child",
      "Parent",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Child"),
  )
end

#test_linearizing_ancestors_for_non_existing_namespacesObject



665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/ruby_indexer/test/index_test.rb', line 665

def test_linearizing_ancestors_for_non_existing_namespaces
  index(<<~RUBY)
    def Bar(a); end
  RUBY

  assert_raises(Index::NonExistingNamespaceError) do
    @index.linearized_ancestors_of("Foo")
  end

  assert_raises(Index::NonExistingNamespaceError) do
    @index.linearized_ancestors_of("Bar")
  end
end

#test_linearizing_ancestors_handles_circular_parent_classObject



565
566
567
568
569
570
571
572
# File 'lib/ruby_indexer/test/index_test.rb', line 565

def test_linearizing_ancestors_handles_circular_parent_class
  index(<<~RUBY)
    class Foo < Foo
    end
  RUBY

  assert_equal(["Foo"], @index.linearized_ancestors_of("Foo"))
end

#test_linearizing_ancestors_is_cachedObject



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/ruby_indexer/test/index_test.rb', line 504

def test_linearizing_ancestors_is_cached
  index(<<~RUBY)
    module C; end
    module A; end
    module B
      include A
    end

    class Foo
      include B
      include A
    end
  RUBY

  @index.linearized_ancestors_of("Foo")
  ancestors = @index.instance_variable_get(:@ancestors)
  assert(ancestors.key?("Foo"))
  assert(ancestors.key?("A"))
  assert(ancestors.key?("B"))
  refute(ancestors.key?("C"))
end

#test_linearizing_ancestors_that_need_to_be_resolvedObject



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

def test_linearizing_ancestors_that_need_to_be_resolved
  index(<<~RUBY)
    module Foo
      module Baz
      end
      module Qux
      end

      class Something; end

      class Bar < Something
        include Baz
        prepend Qux
      end
    end
  RUBY

  assert_equal(
    [
      "Foo::Qux",
      "Foo::Bar",
      "Foo::Baz",
      "Foo::Something",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Foo::Bar"),
  )
end

#test_linearizing_circular_aliased_dependencyObject



716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/ruby_indexer/test/index_test.rb', line 716

def test_linearizing_circular_aliased_dependency
  index(<<~RUBY)
    module A
    end

    ALIAS = A

    module A
      include ALIAS
    end
  RUBY

  assert_equal(["A", "ALIAS"], @index.linearized_ancestors_of("A"))
end

#test_linearizing_circular_ancestorsObject



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
707
708
709
710
711
712
713
714
# File 'lib/ruby_indexer/test/index_test.rb', line 679

def test_linearizing_circular_ancestors
  index(<<~RUBY)
    module M1
      include M2
    end

    module M2
      include M1
    end

    module A1
      include A2
    end

    module A2
      include A3
    end

    module A3
      include A1
    end

    class Foo < Foo
      include Foo
    end

    module Bar
      include Bar
    end
  RUBY

  assert_equal(["M2", "M1"], @index.linearized_ancestors_of("M2"))
  assert_equal(["A3", "A1", "A2"], @index.linearized_ancestors_of("A3"))
  assert_equal(["Foo"], @index.linearized_ancestors_of("Foo"))
  assert_equal(["Bar"], @index.linearized_ancestors_of("Bar"))
end

#test_linearizing_singleton_ancestorsObject



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
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
# File 'lib/ruby_indexer/test/index_test.rb', line 1796

def test_linearizing_singleton_ancestors
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    module First
    end

    module Second
      include First
    end

    module Foo
      class Bar
        class << self
          class Baz
            extend Second

            class << self
              include First
            end
          end
        end
      end
    end
  RUBY

  assert_equal(
    [
      "Foo::Bar::<Class:Bar>::Baz::<Class:Baz>",
      "Second",
      "First",
      "Object::<Class:Object>",
      "BasicObject::<Class:BasicObject>",
      "Class",
      "Module",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Foo::Bar::<Class:Bar>::Baz::<Class:Baz>"),
  )
end

#test_linearizing_singleton_ancestors_of_singleton_when_class_has_parentObject



1713
1714
1715
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
1745
1746
1747
# File 'lib/ruby_indexer/test/index_test.rb', line 1713

def test_linearizing_singleton_ancestors_of_singleton_when_class_has_parent
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    class Foo; end

    class Bar < Foo
    end

    class Baz < Bar
      class << self
        class << self
        end
      end
    end
  RUBY

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

#test_linearizing_singleton_ancestors_when_class_has_parentObject



1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
# File 'lib/ruby_indexer/test/index_test.rb', line 1837

def test_linearizing_singleton_ancestors_when_class_has_parent
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    class Foo; end

    class Bar < Foo
    end

    class Baz < Bar
      class << self
      end
    end
  RUBY

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

#test_linearizing_singleton_objectObject



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

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



1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
# File 'lib/ruby_indexer/test/index_test.rb', line 1890

def test_linearizing_singleton_parent_class_with_namespace
  index(<<~RUBY)
    class ActiveRecord::Base; end

    class User < ActiveRecord::Base
    end
  RUBY

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

#test_object_superclass_indexing_and_resolution_with_reopened_basic_object_classObject



1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
# File 'lib/ruby_indexer/test/index_test.rb', line 1307

def test_object_superclass_indexing_and_resolution_with_reopened_basic_object_class
  index(<<~RUBY)
    class BasicObject; end
  RUBY

  entries = @index["BasicObject"] #: as !nil
  assert_equal(2, entries.length)
  reopened_entry = entries.last #: as Entry::Class
  assert_nil(reopened_entry.parent_class)
  assert_equal(["BasicObject"], @index.linearized_ancestors_of("BasicObject"))
end

#test_object_superclass_indexing_and_resolution_with_reopened_object_classObject



1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
# File 'lib/ruby_indexer/test/index_test.rb', line 1295

def test_object_superclass_indexing_and_resolution_with_reopened_object_class
  index(<<~RUBY)
    class Object; end
  RUBY

  entries = @index["Object"] #: as !nil
  assert_equal(2, entries.length)
  reopened_entry = entries.last #: as Entry::Class
  assert_equal("::BasicObject", reopened_entry.parent_class)
  assert_equal(["Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Object"))
end

#test_object_superclass_resolutionObject



1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
# File 'lib/ruby_indexer/test/index_test.rb', line 1319

def test_object_superclass_resolution
  index(<<~RUBY)
    module Foo
      class Object; end

      class Bar; end
      class Baz < Object; end
    end
  RUBY

  assert_equal(["Foo::Bar", "Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Foo::Bar"))
  assert_equal(
    ["Foo::Baz", "Foo::Object", "Object", "Kernel", "BasicObject"],
    @index.linearized_ancestors_of("Foo::Baz"),
  )
end

#test_only_aliases_for_the_right_owner_are_resolvedObject



1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
# File 'lib/ruby_indexer/test/index_test.rb', line 1582

def test_only_aliases_for_the_right_owner_are_resolved
  index(<<~RUBY)
    class Foo
      attr_reader :name
      alias_method :decorated_name, :name
    end

    class Bar
      alias_method :decorated_name, :to_s
    end
  RUBY

  methods = @index.resolve_method("decorated_name", "Foo") #: as !nil
  refute_nil(methods)

  entry = methods.first #: as Entry::MethodAlias
  assert_kind_of(Entry::MethodAlias, entry)

  target = entry.target
  assert_equal("name", target.name)
  assert_kind_of(Entry::Accessor, target)
  assert_equal("Foo", target.owner&.name)

  other_decorated_name = @index["decorated_name"]&.find { |e| e.is_a?(Entry::UnresolvedMethodAlias) }
  assert_kind_of(Entry::UnresolvedMethodAlias, other_decorated_name)
end

#test_prefix_search_for_methodsObject



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/ruby_indexer/test/index_test.rb', line 333

def test_prefix_search_for_methods
  index(<<~RUBY)
    module Foo
      module Bar
        def qzx; end
      end
    end
  RUBY

  entries = @index.prefix_search("qz")
  refute_empty(entries)

  entry = entries.first&.first #: as !nil
  assert_equal("qzx", entry.name)
end

#test_prevents_multiple_calls_to_index_allObject



2163
2164
2165
2166
2167
2168
2169
# File 'lib/ruby_indexer/test/index_test.rb', line 2163

def test_prevents_multiple_calls_to_index_all
  @index.index_all

  assert_raises(Index::IndexNotEmptyError) do
    @index.index_all
  end
end

#test_resolve_class_variable_in_singleton_contextObject



2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
# File 'lib/ruby_indexer/test/index_test.rb', line 2235

def test_resolve_class_variable_in_singleton_context
  index(<<~RUBY)
    class Foo
      @@hello = 123
    end
  RUBY

  candidates = @index.resolve_class_variable("@@hello", "Foo::<Class:Foo>") #: as !nil
  refute_empty(candidates)

  assert_equal("@@hello", candidates.first&.name)
end

#test_resolve_method_attributeObject



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/ruby_indexer/test/index_test.rb', line 258

def test_resolve_method_attribute
  index(<<~RUBY)
    class Foo
      attr_reader :bar
    end
  RUBY

  entries = @index.resolve_method("bar", "Foo") #: as !nil
  assert_equal("bar", entries.first&.name)
  owner = entries.first&.owner #: as !nil
  assert_equal("Foo", owner.name)
end

#test_resolve_method_inherited_onlyObject



297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/ruby_indexer/test/index_test.rb', line 297

def test_resolve_method_inherited_only
  index(<<~RUBY)
    class Bar
      def baz; end
    end

    class Foo < Bar
      def baz; end
    end
  RUBY

  entry = @index.resolve_method("baz", "Foo", inherited_only: true)&.first #: as !nil
  assert_equal("Bar", entry.owner&.name)
end

#test_resolve_method_inherited_only_for_prepended_moduleObject



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/ruby_indexer/test/index_test.rb', line 312

def test_resolve_method_inherited_only_for_prepended_module
  index(<<~RUBY)
    module Bar
      def baz
        super
      end
    end

    class Foo
      prepend Bar

      def baz; end
    end
  RUBY

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

#test_resolve_method_with_class_name_conflictObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/ruby_indexer/test/index_test.rb', line 242

def test_resolve_method_with_class_name_conflict
  index(<<~RUBY)
    class Array
    end

    class Foo
      def Array(*args); end
    end
  RUBY

  entries = @index.resolve_method("Array", "Foo") #: as !nil
  assert_equal("Array", entries.first&.name)
  owner = entries.first&.owner #: as !nil
  assert_equal("Foo", owner.name)
end

#test_resolve_method_with_known_receiverObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ruby_indexer/test/index_test.rb', line 227

def test_resolve_method_with_known_receiver
  index(<<~RUBY)
    module Foo
      module Bar
        def baz; end
      end
    end
  RUBY

  entries = @index.resolve_method("baz", "Foo::Bar") #: as !nil
  assert_equal("baz", entries.first&.name)
  owner = entries.first&.owner #: as !nil
  assert_equal("Foo::Bar", owner.name)
end

#test_resolve_method_with_two_definitionsObject



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

def test_resolve_method_with_two_definitions
  index(<<~RUBY)
    class Foo
      # Hello from first `bar`
      def bar; end
    end

    class Foo
      # Hello from second `bar`
      def bar; end
    end
  RUBY

  first_entry, second_entry = @index.resolve_method("bar", "Foo") #: as !nil

  assert_equal("bar", first_entry&.name)
  owner = first_entry&.owner #: as !nil
  assert_equal("Foo", owner.name)
  assert_includes(first_entry&.comments, "Hello from first `bar`")

  assert_equal("bar", second_entry&.name)
  owner = second_entry&.owner #: as !nil
  assert_equal("Foo", owner.name)
  assert_includes(second_entry&.comments, "Hello from second `bar`")
end

#test_resolve_normalizes_top_level_namesObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ruby_indexer/test/index_test.rb', line 166

def test_resolve_normalizes_top_level_names
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb", load_path_entry: "/fake"), <<~RUBY)
    class Bar; end

    module Foo
      class Bar; end
    end
  RUBY

  entries = @index.resolve("::Foo::Bar", []) #: as !nil
  refute_nil(entries)

  assert_equal("Foo::Bar", entries.first&.name)

  entries = @index.resolve("::Bar", ["Foo"]) #: as !nil
  refute_nil(entries)

  assert_equal("Bar", entries.first&.name)
end

#test_resolving_a_qualified_referenceObject



1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
# File 'lib/ruby_indexer/test/index_test.rb', line 1269

def test_resolving_a_qualified_reference
  index(<<~RUBY)
    class Base
      module Third
        CONST = 1
      end
    end

    class Foo
      module Third
        CONST = 2
      end

      class Second < Base
      end
    end
  RUBY

  foo_entry = @index.resolve("Third::CONST", ["Foo"])&.first #: as !nil
  assert_equal(9, foo_entry.location.start_line)
end

#test_resolving_alias_to_existing_constant_from_inner_namespaceObject



2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
# File 'lib/ruby_indexer/test/index_test.rb', line 2092

def test_resolving_alias_to_existing_constant_from_inner_namespace
  index(<<~RUBY)
    module Parent
      CONST = 123
    end

    module First
      module Namespace
        class Foo
          include Parent

          module InnerNamespace
            Constants = Namespace::Foo::CONST
          end
        end
      end
    end
  RUBY

  entry = @index.resolve("Namespace::Foo::CONST", ["First", "Namespace", "Foo", "InnerNamespace"])&.first #: as !nil
  assert_equal("Parent::CONST", entry.name)
  assert_instance_of(Entry::Constant, entry)
end

#test_resolving_alias_to_non_existing_namespaceObject



2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
# File 'lib/ruby_indexer/test/index_test.rb', line 2074

def test_resolving_alias_to_non_existing_namespace
  index(<<~RUBY)
    module Namespace
      class Foo
        module InnerNamespace
          Constants = Namespace::Foo::Constants
        end
      end
    end
  RUBY

  entry = @index.resolve("Constants", ["Namespace", "Foo", "InnerNamespace"])&.first
  assert_instance_of(Entry::UnresolvedConstantAlias, entry)

  entry = @index.resolve("Namespace::Foo::Constants", ["Namespace", "Foo", "InnerNamespace"])&.first
  assert_nil(entry)
end

#test_resolving_aliases_to_non_existing_constants_with_conflicting_namesObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ruby_indexer/test/index_test.rb', line 186

def test_resolving_aliases_to_non_existing_constants_with_conflicting_names
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb", load_path_entry: "/fake"), <<~RUBY)
    class Bar
    end

    module Foo
      class Bar < self
        BAZ = ::Bar::BAZ
      end
    end
  RUBY

  entry = @index.resolve("BAZ", ["Foo", "Bar"])&.first
  refute_nil(entry)

  assert_instance_of(Entry::UnresolvedConstantAlias, entry)
end

#test_resolving_an_inherited_methodObject



755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
# File 'lib/ruby_indexer/test/index_test.rb', line 755

def test_resolving_an_inherited_method
  index(<<~RUBY)
    module Foo
      def baz; end
    end

    class Bar
      def qux; end
    end

    class Wow < Bar
      include Foo
    end
  RUBY

  entry = @index.resolve_method("baz", "Wow")&.first #: as !nil
  assert_equal("baz", entry.name)
  assert_equal("Foo", entry.owner&.name)

  entry = @index.resolve_method("qux", "Wow")&.first #: as !nil
  assert_equal("qux", entry.name)
  assert_equal("Bar", entry.owner&.name)
end

#test_resolving_an_inherited_method_lands_on_first_matchObject



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

def test_resolving_an_inherited_method_lands_on_first_match
  index(<<~RUBY)
    module Foo
      def qux; end
    end

    class Bar
      def qux; end
    end

    class Wow < Bar
      prepend Foo

      def qux; end
    end
  RUBY

  entries = @index.resolve_method("qux", "Wow") #: as !nil
  assert_equal(1, entries.length)

  entry = entries.first #: as !nil
  assert_equal("qux", entry.name)
  assert_equal("Foo", entry.owner&.name)
end

#test_resolving_circular_aliasObject



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/ruby_indexer/test/index_test.rb', line 1043

def test_resolving_circular_alias
  index(<<~RUBY)
    module Namespace
      FOO = BAR
      BAR = FOO
    end
  RUBY

  foo_entry = @index.resolve("FOO", ["Namespace"])&.first #: as !nil
  assert_equal(2, foo_entry.location.start_line)
  assert_instance_of(Entry::ConstantAlias, foo_entry)

  bar_entry = @index.resolve("BAR", ["Namespace"])&.first #: as !nil
  assert_equal(3, bar_entry.location.start_line)
  assert_instance_of(Entry::ConstantAlias, bar_entry)
end

#test_resolving_circular_alias_three_levelsObject



1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'lib/ruby_indexer/test/index_test.rb', line 1060

def test_resolving_circular_alias_three_levels
  index(<<~RUBY)
    module Namespace
      FOO = BAR
      BAR = BAZ
      BAZ = FOO
    end
  RUBY

  foo_entry = @index.resolve("FOO", ["Namespace"])&.first #: as !nil
  assert_equal(2, foo_entry.location.start_line)
  assert_instance_of(Entry::ConstantAlias, foo_entry)

  bar_entry = @index.resolve("BAR", ["Namespace"])&.first #: as !nil
  assert_equal(3, bar_entry.location.start_line)
  assert_instance_of(Entry::ConstantAlias, bar_entry)

  baz_entry = @index.resolve("BAZ", ["Namespace"])&.first #: as !nil
  assert_equal(4, baz_entry.location.start_line)
  assert_instance_of(Entry::ConstantAlias, baz_entry)
end

#test_resolving_circular_method_aliasesObject



1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
# File 'lib/ruby_indexer/test/index_test.rb', line 1552

def test_resolving_circular_method_aliases
  index(<<~RUBY)
    class Foo
      alias bar bar
    end
  RUBY

  # It's not possible to resolve an alias that points to itself
  methods = @index.resolve_method("bar", "Foo")
  assert_nil(methods)

  entry = @index["bar"]&.first
  assert_kind_of(Entry::UnresolvedMethodAlias, entry)
end

#test_resolving_circular_method_aliases_on_class_reopenObject



1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
# File 'lib/ruby_indexer/test/index_test.rb', line 1947

def test_resolving_circular_method_aliases_on_class_reopen
  index(<<~RUBY)
    class Foo
      alias bar ==
      def ==(other) = true
    end

    class Foo
      alias == bar
    end
  RUBY

  method = @index.resolve_method("==", "Foo")&.first #: as Entry::Method
  assert_kind_of(Entry::Method, method)
  assert_equal("==", method.name)

  candidates = @index.method_completion_candidates("=", "Foo")
  assert_equal(["==", "==="], candidates.map(&:name))
end

#test_resolving_constants_favors_ancestors_over_top_levelObject



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/ruby_indexer/test/index_test.rb', line 1019

def test_resolving_constants_favors_ancestors_over_top_level
  index(<<~RUBY)
    module Value1
      CONST = 1
    end

    module Value2
      CONST = 2
    end

    CONST = 3
    module First
      include Value1

      module Second
        include Value2
      end
    end
  RUBY

  entry = @index.resolve("CONST", ["First", "Second"])&.first #: as !nil
  assert_equal(6, entry.location.start_line)
end

#test_resolving_constants_in_aliased_namespaceObject



1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'lib/ruby_indexer/test/index_test.rb', line 1082

def test_resolving_constants_in_aliased_namespace
  index(<<~RUBY)
    module Original
      module Something
        CONST = 123
      end
    end

    module Other
      ALIAS = Original::Something
    end

    module Third
      Other::ALIAS::CONST
    end
  RUBY

  entry = @index.resolve("Other::ALIAS::CONST", ["Third"])&.first #: as !nil
  assert_kind_of(Entry::Constant, entry)
  assert_equal("Original::Something::CONST", entry.name)
end

#test_resolving_constants_in_singleton_contextsObject



1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
# File 'lib/ruby_indexer/test/index_test.rb', line 1397

def test_resolving_constants_in_singleton_contexts
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    module Foo
      class Bar
        CONST = 3

        class << self
          CONST = 2

          class Baz
            CONST = 1

            class << self
            end
          end
        end
      end
    end
  RUBY

  entry = @index.resolve("CONST", ["Foo", "Bar", "<Class:Bar>", "Baz", "<Class:Baz>"])&.first #: as !nil
  refute_nil(entry)
  assert_equal(9, entry.location.start_line)
end

#test_resolving_inherited_aliased_namespaceObject



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# File 'lib/ruby_indexer/test/index_test.rb', line 942

def test_resolving_inherited_aliased_namespace
  index(<<~RUBY)
    module Bar
      TARGET = 123
    end

    module Foo
      CONST = Bar
    end

    module Namespace
      class Bar
        include Foo
      end
    end
  RUBY

  entry = @index.resolve("Foo::CONST::TARGET", [])&.first #: as !nil
  assert_equal(2, entry.location.start_line)

  entry = @index.resolve("Namespace::Bar::CONST::TARGET", [])&.first #: as !nil
  assert_equal(2, entry.location.start_line)
end

#test_resolving_inherited_constantsObject



911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/ruby_indexer/test/index_test.rb', line 911

def test_resolving_inherited_constants
  index(<<~RUBY)
    module Foo
      CONST = 1
    end

    module Baz
      CONST = 2
    end

    module Qux
      include Foo
    end

    module Namespace
      CONST = 3

      include Baz

      class Bar
        include Qux
      end
    end

    CONST = 4
  RUBY

  entry = @index.resolve("CONST", ["Namespace", "Bar"])&.first #: as !nil
  assert_equal(14, entry.location.start_line)
end

#test_resolving_instance_variables_in_singleton_contextsObject



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

def test_resolving_instance_variables_in_singleton_contexts
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    module Foo
      class Bar
        @a = 123

        class << self
          def hello
            @b = 123
          end

          @c = 123
        end
      end
    end
  RUBY

  entry = @index.resolve_instance_variable("@a", "Foo::Bar::<Class:Bar>")&.first #: as !nil
  refute_nil(entry)
  assert_equal("@a", entry.name)

  entry = @index.resolve_instance_variable("@b", "Foo::Bar::<Class:Bar>")&.first #: as !nil
  refute_nil(entry)
  assert_equal("@b", entry.name)

  entry = @index.resolve_instance_variable("@c", "Foo::Bar::<Class:Bar>::<Class:<Class:Bar>>")&.first #: as !nil
  refute_nil(entry)
  assert_equal("@c", entry.name)
end

#test_resolving_method_aliasesObject



1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
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
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
# File 'lib/ruby_indexer/test/index_test.rb', line 1498

def test_resolving_method_aliases
  index(<<~RUBY)
    class Foo
      def bar(a, b, c)
      end

      alias double_alias bar
    end

    class Bar < Foo
      def hello(b); end

      alias baz bar
      alias_method :qux, :hello
      alias double double_alias
    end
  RUBY

  # baz
  methods = @index.resolve_method("baz", "Bar") #: as !nil
  refute_nil(methods)

  entry = methods.first #: as Entry::MethodAlias
  assert_kind_of(Entry::MethodAlias, entry)
  assert_equal("bar", entry.target.name)
  assert_equal("Foo", entry.target.owner&.name)

  # qux
  methods = @index.resolve_method("qux", "Bar") #: as !nil
  refute_nil(methods)

  entry = methods.first #: as Entry::MethodAlias
  assert_kind_of(Entry::MethodAlias, entry)
  assert_equal("hello", entry.target.name)
  assert_equal("Bar", entry.target.owner&.name)

  # double
  methods = @index.resolve_method("double", "Bar") #: as !nil
  refute_nil(methods)

  entry = methods.first #: as Entry::MethodAlias
  assert_kind_of(Entry::MethodAlias, entry)

  target = entry.target #: as Entry::MethodAlias
  assert_equal("double_alias", target.name)
  assert_kind_of(Entry::MethodAlias, target)
  assert_equal("Foo", target.owner&.name)

  final_target = target.target
  assert_equal("bar", final_target.name)
  assert_kind_of(Entry::Method, final_target)
  assert_equal("Foo", final_target.owner&.name)
end

#test_resolving_method_inside_singleton_contextObject



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

def test_resolving_method_inside_singleton_context
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb"), <<~RUBY)
    module Foo
      class Bar
        class << self
          class Baz
            class << self
              def found_me!; end
            end
          end
        end
      end
    end
  RUBY

  entry = @index.resolve_method("found_me!", "Foo::Bar::<Class:Bar>::Baz::<Class:Baz>")&.first #: as !nil
  refute_nil(entry)
  assert_equal("found_me!", entry.name)
end

#test_resolving_non_existing_self_referential_constant_aliasObject



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

def test_resolving_non_existing_self_referential_constant_alias
  index(<<~RUBY)
    module Foo
      SomeClass = ::SomeClass
      UNRESOLVED = SomeClass::CONSTANT
    end
  RUBY

  entry = @index.resolve("Foo::UNRESOLVED", [])&.first #: as Entry::UnresolvedConstantAlias
  assert_kind_of(Entry::UnresolvedConstantAlias, entry)
  assert_equal(3, entry.location.start_line)
  assert_equal("SomeClass::CONSTANT", entry.target)

  entry = @index.resolve("SomeClass::CONSTANT", ["Foo"])
  refute(entry)
end

#test_resolving_prepended_constantsObject



987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/ruby_indexer/test/index_test.rb', line 987

def test_resolving_prepended_constants
  index(<<~RUBY)
    module Included
      CONST = 123
    end

    module Prepended
      CONST = 321
    end

    class Foo
      include Included
      prepend Prepended
    end

    class Bar
      CONST = 456
      include Included
      prepend Prepended
    end
  RUBY

  entry = @index.resolve("CONST", ["Foo"])&.first #: as !nil
  assert_equal(6, entry.location.start_line)

  entry = @index.resolve("Foo::CONST", [])&.first #: as !nil
  assert_equal(6, entry.location.start_line)

  entry = @index.resolve("Bar::CONST", [])&.first #: as !nil
  assert_equal(15, entry.location.start_line)
end

#test_resolving_qualified_referencesObject



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

def test_resolving_qualified_references
  index(<<~RUBY)
    module Namespace
      class Entry
        CONST = 1
      end
    end

    module Namespace
      class Index
      end
    end
  RUBY

  foo_entry = @index.resolve("Entry::CONST", ["Namespace", "Index"])&.first #: as !nil
  assert_equal(3, foo_entry.location.start_line)
end

#test_resolving_references_with_only_top_level_declarationObject



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

def test_resolving_references_with_only_top_level_declaration
  index(<<~RUBY)
    CONST = 1

    module Foo; end

    module Namespace
      class Index
        include Foo
      end
    end
  RUBY

  foo_entry = @index.resolve("CONST", ["Namespace", "Index"])&.first #: as !nil
  assert_equal(1, foo_entry.location.start_line)
end

#test_resolving_references_with_redundant_namespacesObject



1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'lib/ruby_indexer/test/index_test.rb', line 1130

def test_resolving_references_with_redundant_namespaces
  index(<<~RUBY)
    module Bar
      CONST = 1
    end

    module A
      CONST = 2

      module B
        CONST = 3

        class Foo
          include Bar
        end

        A::B::Foo::CONST
      end
    end
  RUBY

  foo_entry = @index.resolve("A::B::Foo::CONST", ["A", "B"])&.first #: as !nil
  assert_equal(2, foo_entry.location.start_line)
end

#test_resolving_same_constant_from_different_scopesObject



966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'lib/ruby_indexer/test/index_test.rb', line 966

def test_resolving_same_constant_from_different_scopes
  index(<<~RUBY)
    module Namespace
      CONST = 123

      class Parent
        CONST = 321
      end

      class Child < Parent
      end
    end
  RUBY

  entry = @index.resolve("CONST", ["Namespace", "Child"])&.first #: as !nil
  assert_equal(2, entry.location.start_line)

  entry = @index.resolve("Namespace::Child::CONST", [])&.first #: as !nil
  assert_equal(5, entry.location.start_line)
end

#test_resolving_self_referential_constant_aliasObject



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

def test_resolving_self_referential_constant_alias
  index(<<~RUBY)
    module A
      module B
        class C
        end
      end
    end

    module A
      module D
        B = B::C
      end
    end
  RUBY

  entry = @index.resolve("A::D::B", [])&.first #: as Entry::ConstantAlias

  assert_kind_of(RubyIndexer::Entry::ConstantAlias, entry)
  assert_equal(10, entry.location.start_line)
  assert_equal("A::B::C", entry.target)
end

#test_resolving_top_level_aliasesObject



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
# File 'lib/ruby_indexer/test/index_test.rb', line 1104

def test_resolving_top_level_aliases
  index(<<~RUBY)
    class Foo
      CONST = 123
    end

    FOO = Foo
    FOO::CONST
  RUBY

  entry = @index.resolve("FOO::CONST", [])&.first #: as !nil
  assert_kind_of(Entry::Constant, entry)
  assert_equal("Foo::CONST", entry.name)
end

#test_resolving_top_level_compact_referenceObject



1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/ruby_indexer/test/index_test.rb', line 1119

def test_resolving_top_level_compact_reference
  index(<<~RUBY)
    class Foo::Bar
    end
  RUBY

  foo_entry = @index.resolve("Foo::Bar", [])&.first #: as !nil
  assert_equal(1, foo_entry.location.start_line)
  assert_instance_of(Entry::Class, foo_entry)
end

#test_resolving_unindexed_constant_with_no_nestingObject



1291
1292
1293
# File 'lib/ruby_indexer/test/index_test.rb', line 1291

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

#test_resolving_unqualified_referencesObject



1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
# File 'lib/ruby_indexer/test/index_test.rb', line 1213

def test_resolving_unqualified_references
  index(<<~RUBY)
    module Foo
      CONST = 1
    end

    module Namespace
      CONST = 2

      class Index
        include Foo
      end
    end
  RUBY

  foo_entry = @index.resolve("CONST", ["Namespace", "Index"])&.first #: as !nil
  assert_equal(6, foo_entry.location.start_line)
end

#test_searching_for_entries_based_on_prefixObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ruby_indexer/test/index_test.rb', line 146

def test_searching_for_entries_based_on_prefix
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb", load_path_entry: "/fake"), <<~RUBY)
    class Foo::Bizw
    end
  RUBY
  @index.index_single(URI::Generic.from_path(path: "/fake/path/other_foo.rb", load_path_entry: "/fake"), <<~RUBY)
    class Foo::Bizw
    end

    class Foo::Bizt
    end
  RUBY

  results = @index.prefix_search("Foo", []).map { |entries| entries.map(&:name) }
  assert_equal([["Foo::Bizt"], ["Foo::Bizw", "Foo::Bizw"]], results)

  results = @index.prefix_search("Biz", ["Foo"]).map { |entries| entries.map(&:name) }
  assert_equal([["Foo::Bizt"], ["Foo::Bizw", "Foo::Bizw"]], results)
end

#test_searching_for_require_pathsObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ruby_indexer/test/index_test.rb', line 133

def test_searching_for_require_paths
  @index.index_single(URI::Generic.from_path(path: "/fake/path/foo.rb", load_path_entry: "/fake"), <<~RUBY)
    class Foo
    end
  RUBY
  @index.index_single(URI::Generic.from_path(path: "/fake/path/other_foo.rb", load_path_entry: "/fake"), <<~RUBY)
    class Foo
    end
  RUBY

  assert_equal(["path/other_foo", "path/foo"], @index.search_require_paths("path").map(&:require_path))
end

#test_singleton_nesting_is_correctly_split_during_linearizationObject



1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
# File 'lib/ruby_indexer/test/index_test.rb', line 1914

def test_singleton_nesting_is_correctly_split_during_linearization
  index(<<~RUBY)
    module Bar; end

    module Foo
      class Namespace::Parent
        extend Bar
      end
    end

    module Foo
      class Child < Namespace::Parent
      end
    end
  RUBY

  assert_equal(
    [
      "Foo::Child::<Class:Child>",
      "Foo::Namespace::Parent::<Class:Parent>",
      "Bar",
      "Object::<Class:Object>",
      "BasicObject::<Class:BasicObject>",
      "Class",
      "Module",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("Foo::Child::<Class:Child>"),
  )
end

#test_singletons_are_excluded_from_fuzzy_searchObject



1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
# File 'lib/ruby_indexer/test/index_test.rb', line 1485

def test_singletons_are_excluded_from_fuzzy_search
  index(<<~RUBY)
    class Zwq
      class << self
      end
    end
  RUBY

  results = @index.fuzzy_search("Zwq")
  assert_equal(1, results.length)
  assert_equal("Zwq", results.first&.name)
end

#test_singletons_are_excluded_from_prefix_searchObject



1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
# File 'lib/ruby_indexer/test/index_test.rb', line 1474

def test_singletons_are_excluded_from_prefix_search
  index(<<~RUBY)
    class Zwq
      class << self
      end
    end
  RUBY

  assert_empty(@index.prefix_search("Zwq::<C"))
end

#test_top_level_basic_object_superclass_resolutionObject



1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
# File 'lib/ruby_indexer/test/index_test.rb', line 1365

def test_top_level_basic_object_superclass_resolution
  index(<<~RUBY)
    module Foo
      class BasicObject; end

      class Bar < ::BasicObject; end
    end
  RUBY

  assert_equal(["Foo::Bar", "BasicObject"], @index.linearized_ancestors_of("Foo::Bar"))
end

#test_top_level_object_superclass_resolutionObject



1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
# File 'lib/ruby_indexer/test/index_test.rb', line 1353

def test_top_level_object_superclass_resolution
  index(<<~RUBY)
    module Foo
      class Object; end

      class Bar < ::Object; end
    end
  RUBY

  assert_equal(["Foo::Bar", "Object", "Kernel", "BasicObject"], @index.linearized_ancestors_of("Foo::Bar"))
end

#test_unresolvable_method_aliasesObject



1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
# File 'lib/ruby_indexer/test/index_test.rb', line 1567

def test_unresolvable_method_aliases
  index(<<~RUBY)
    class Foo
      alias bar baz
    end
  RUBY

  # `baz` does not exist, so resolving `bar` is not possible
  methods = @index.resolve_method("bar", "Foo")
  assert_nil(methods)

  entry = @index["bar"]&.first
  assert_kind_of(Entry::UnresolvedMethodAlias, entry)
end

#test_visitor_does_not_visit_unnecessary_nodesObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/ruby_indexer/test/index_test.rb', line 204

def test_visitor_does_not_visit_unnecessary_nodes
  concats = (0...10_000).map do |i|
    <<~STRING
      "string#{i}" \\
    STRING
  end.join

  index(<<~RUBY)
    module Foo
      local_var = #{concats}
        "final"
      @class_instance_var = #{concats}
        "final"
      @@class_var = #{concats}
        "final"
      $global_var = #{concats}
        "final"
      CONST = #{concats}
        "final"
    end
  RUBY
end