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

    module Foo
      class Bar
      end

      class Baz
        class Something
        end
      end
    end
  RUBY

  entries = @index["::Foo::Baz::Something"]
  refute_empty(entries)
  assert_equal("Foo::Baz::Something", entries.first.name)
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(IndexablePath.new(nil, "/fake/path/foo.rb"), <<~RUBY)
    class Foo
    end
  RUBY

  entries = @index["Foo"]
  assert_equal(1, entries.length)

  @index.delete(IndexablePath.new(nil, "/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(IndexablePath.new(nil, "/fake/path/foo.rb"), <<~RUBY)
    class Foo
    end
  RUBY
  @index.index_single(IndexablePath.new(nil, "/fake/path/other_foo.rb"), <<~RUBY)
    class Foo
    end
  RUBY

  entries = @index["Foo"]
  assert_equal(2, entries.length)

  @index.delete(IndexablePath.new(nil, "/fake/path/other_foo.rb"))
  entries = @index["Foo"]
  assert_equal(1, entries.length)
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(IndexablePath.new(nil, "/fake/path/foo.rb"), <<~RUBY)
    class Bar; end

    module Foo
      class Bar
      end

      class Baz
        class Something
        end
      end
    end
  RUBY

  result = @index.fuzzy_search("Bar")
  assert_equal(1, result.length)
  assert_equal(@index["Bar"].first, result.first)

  result = @index.fuzzy_search("foobarsomeking")
  assert_equal(5, result.length)
  assert_equal(["Foo::Baz::Something", "Foo::Bar", "Foo::Baz", "Foo", "Bar"], result.map(&:name))

  result = @index.fuzzy_search("FooBaz")
  assert_equal(4, result.length)
  assert_equal(["Foo::Baz", "Foo::Bar", "Foo", "Foo::Baz::Something"], result.map(&:name))
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(IndexablePath.new(nil, "/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"])
  refute_empty(entries)
  assert_equal("Foo::Baz::Something", entries.first.name)

  entries = @index.resolve("Bar", ["Foo"])
  refute_empty(entries)
  assert_equal("Foo::Bar", entries.first.name)

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

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

  assert_nil(@index.resolve("DoesNotExist", ["Foo"]))
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



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

def test_indexing_prism_fixtures_succeeds
  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.instance_variable_get(:@entries))
end

#test_prefix_search_for_methodsObject



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

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

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

  entry = T.must(entries.first).first
  assert_equal("baz", entry.name)
end

#test_resolve_method_with_known_receiverObject



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ruby_indexer/test/index_test.rb', line 220

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

  entry = T.must(@index.resolve_method("baz", "Foo::Bar"))
  assert_equal("baz", entry.name)
  assert_equal("Foo::Bar", T.must(entry.owner).name)
end

#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"), <<~RUBY)
    class Bar; end

    module Foo
      class Bar; end
    end
  RUBY

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

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

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

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

#test_resolving_aliases_to_non_existing_constants_with_conflicting_namesObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# 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"), <<~RUBY)
    module Foo
      class Float < self
        INFINITY = ::Float::INFINITY
      end
    end
  RUBY

  entry = @index.resolve("INFINITY", ["Foo", "Float"]).first
  refute_nil(entry)

  assert_instance_of(Entry::UnresolvedAlias, entry)
end

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

    class Foo::Baz
    end
  RUBY

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

  results = @index.prefix_search("Ba", ["Foo"]).map { |entries| entries.map(&:name) }
  assert_equal([["Foo::Bar", "Foo::Bar"], ["Foo::Baz"]], results)
end

#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"), <<~RUBY)
    class Foo
    end
  RUBY
  @index.index_single(IndexablePath.new("/fake", "/fake/path/other_foo.rb"), <<~RUBY)
    class Foo
    end
  RUBY

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

#test_visitor_does_not_visit_unnecessary_nodesObject



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

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