Class: RubyIndexer::MethodTest

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

Instance Method Summary collapse

Methods inherited from TestCase

#setup

Instance Method Details

#test_conditional_methodObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_indexer/test/method_test.rb', line 19

def test_conditional_method
  index(<<~RUBY)
    class Foo
      def bar
      end if condition
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
end

#test_ignores_attributes_invoked_on_constantObject



366
367
368
369
370
371
372
373
374
375
# File 'lib/ruby_indexer/test/method_test.rb', line 366

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

    Foo.attr_reader :bar
  RUBY

  assert_no_entry("bar")
end

#test_keeps_track_of_aliasesObject



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/ruby_indexer/test/method_test.rb', line 400

def test_keeps_track_of_aliases
  index(<<~RUBY)
    class Foo
      alias whatever to_s
      alias_method :foo, :to_a
      alias_method "bar", "to_a"

      # These two are not indexed because they are dynamic or incomplete
      alias_method baz, :to_a
      alias_method :baz
    end
  RUBY

  assert_entry("whatever", Entry::UnresolvedMethodAlias, "/fake/path/foo.rb:1-8:1-16")
  assert_entry("foo", Entry::UnresolvedMethodAlias, "/fake/path/foo.rb:2-15:2-19")
  assert_entry("bar", Entry::UnresolvedMethodAlias, "/fake/path/foo.rb:3-15:3-20")
  # Foo plus 3 valid aliases
  assert_equal(4, @index.length - @default_indexed_entries.length)
end

#test_keeps_track_of_attributesObject



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

def test_keeps_track_of_attributes
  index(<<~RUBY)
    class Foo
      # Hello there
      attr_reader :bar, :other
      attr_writer :baz
      attr_accessor :qux
    end
  RUBY

  assert_entry("bar", Entry::Accessor, "/fake/path/foo.rb:2-15:2-18")
  assert_equal("Hello there", @index["bar"].first.comments.join("\n"))
  assert_entry("other", Entry::Accessor, "/fake/path/foo.rb:2-21:2-26")
  assert_equal("Hello there", @index["other"].first.comments.join("\n"))
  assert_entry("baz=", Entry::Accessor, "/fake/path/foo.rb:3-15:3-18")
  assert_entry("qux", Entry::Accessor, "/fake/path/foo.rb:4-17:4-20")
  assert_entry("qux=", Entry::Accessor, "/fake/path/foo.rb:4-17:4-20")
end

#test_keeps_track_of_method_ownerObject



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

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

  entry = T.must(@index["bar"].first)
  owner_name = T.must(entry.owner).name

  assert_equal("Foo", owner_name)
end

#test_method_under_dynamic_class_or_moduleObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby_indexer/test/method_test.rb', line 57

def test_method_under_dynamic_class_or_module
  index(<<~RUBY)
    module Foo
      class self::Bar
        def bar
        end
      end
    end

    module Bar
      def bar
      end
    end
  RUBY

  assert_equal(2, @index["bar"].length)
  first_entry = T.must(@index["bar"].first)
  assert_equal("Foo::self::Bar", first_entry.owner.name)
  second_entry = T.must(@index["bar"].last)
  assert_equal("Bar", second_entry.owner.name)
end

#test_method_with_anonymous_rest_parametersObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/ruby_indexer/test/method_test.rb', line 298

def test_method_with_anonymous_rest_parameters
  index(<<~RUBY)
    class Foo
      def bar(*, **)
      end
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(2, parameters.length)
  first, second = parameters

  assert_equal(Entry::RestParameter::DEFAULT_NAME, first.name)
  assert_instance_of(Entry::RestParameter, first)

  assert_equal(Entry::KeywordRestParameter::DEFAULT_NAME, second.name)
  assert_instance_of(Entry::KeywordRestParameter, second)
end

#test_method_with_block_parametersObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ruby_indexer/test/method_test.rb', line 272

def test_method_with_block_parameters
  index(<<~RUBY)
    class Foo
      def bar(&block)
      end

      def baz(&)
      end
    end
  RUBY

  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  param = parameters.first
  assert_equal(:block, param.name)
  assert_instance_of(Entry::BlockParameter, param)

  entry = T.must(@index["baz"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(1, parameters.length)

  param = parameters.first
  assert_equal(Entry::BlockParameter::DEFAULT_NAME, param.name)
  assert_instance_of(Entry::BlockParameter, param)
end

#test_method_with_destructed_parametersObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ruby_indexer/test/method_test.rb', line 133

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

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(1, parameters.length)
  parameter = parameters.first
  assert_equal(:"(a, (b, ))", parameter.name)
  assert_instance_of(Entry::RequiredParameter, parameter)
end

#test_method_with_destructured_rest_parametersObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/ruby_indexer/test/method_test.rb', line 254

def test_method_with_destructured_rest_parameters
  index(<<~RUBY)
    class Foo
      def bar((a, *b))
      end
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(1, parameters.length)
  param = parameters.first

  assert_equal(:"(a, *b)", param.name)
  assert_instance_of(Entry::RequiredParameter, param)
end

#test_method_with_forbidden_keyword_splat_parameterObject



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/ruby_indexer/test/method_test.rb', line 319

def test_method_with_forbidden_keyword_splat_parameter
  index(<<~RUBY)
    class Foo
      def bar(**nil)
      end
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  assert_empty(parameters)
end

#test_method_with_keyword_parametersObject



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

def test_method_with_keyword_parameters
  index(<<~RUBY)
    class Foo
      def bar(a:, b: 123)
      end
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(2, parameters.length)
  a, b = parameters

  assert_equal(:a, a.name)
  assert_instance_of(Entry::KeywordParameter, a)

  assert_equal(:b, b.name)
  assert_instance_of(Entry::OptionalKeywordParameter, b)
end

#test_method_with_no_parametersObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/ruby_indexer/test/method_test.rb', line 8

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

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
end

#test_method_with_optional_parametersObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ruby_indexer/test/method_test.rb', line 150

def test_method_with_optional_parameters
  index(<<~RUBY)
    class Foo
      def bar(a = 123)
      end
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(1, parameters.length)
  parameter = parameters.first
  assert_equal(:a, parameter.name)
  assert_instance_of(Entry::OptionalParameter, parameter)
end

#test_method_with_parametersObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ruby_indexer/test/method_test.rb', line 116

def test_method_with_parameters
  index(<<~RUBY)
    class Foo
      def bar(a)
      end
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(1, parameters.length)
  parameter = parameters.first
  assert_equal(:a, parameter.name)
  assert_instance_of(Entry::RequiredParameter, parameter)
end

#test_method_with_post_parametersObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/ruby_indexer/test/method_test.rb', line 209

def test_method_with_post_parameters
  index(<<~RUBY)
    class Foo
      def bar(*a, b)
      end

      def baz(**a, b)
      end

      def qux(*a, (b, c))
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(2, parameters.length)
  a, b = parameters

  assert_equal(:a, a.name)
  assert_instance_of(Entry::RestParameter, a)

  assert_equal(:b, b.name)
  assert_instance_of(Entry::RequiredParameter, b)

  entry = T.must(@index["baz"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(2, parameters.length)
  a, b = parameters

  assert_equal(:a, a.name)
  assert_instance_of(Entry::KeywordRestParameter, a)

  assert_equal(:b, b.name)
  assert_instance_of(Entry::RequiredParameter, b)

  entry = T.must(@index["qux"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(2, parameters.length)
  _a, second = parameters

  assert_equal(:"(b, c)", second.name)
  assert_instance_of(Entry::RequiredParameter, second)
end

#test_method_with_rest_and_keyword_rest_parametersObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ruby_indexer/test/method_test.rb', line 188

def test_method_with_rest_and_keyword_rest_parameters
  index(<<~RUBY)
    class Foo
      def bar(*a, **b)
      end
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")
  entry = T.must(@index["bar"].first)
  parameters = entry.signatures.first.parameters
  assert_equal(2, parameters.length)
  a, b = parameters

  assert_equal(:a, a.name)
  assert_instance_of(Entry::RestParameter, a)

  assert_equal(:b, b.name)
  assert_instance_of(Entry::KeywordRestParameter, b)
end

#test_name_location_points_to_method_identifier_locationObject



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/ruby_indexer/test/method_test.rb', line 445

def test_name_location_points_to_method_identifier_location
  index(<<~RUBY)
    class Foo
      def bar
        a = 123
        a + 456
      end
    end
  RUBY

  entry = T.must(@index["bar"].first)
  refute_equal(entry.location, entry.name_location)

  name_location = entry.name_location
  assert_equal(2, name_location.start_line)
  assert_equal(2, name_location.end_line)
  assert_equal(6, name_location.start_column)
  assert_equal(9, name_location.end_column)
end

#test_properly_tracks_multiple_levels_of_nestingObject



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/ruby_indexer/test/method_test.rb', line 377

def test_properly_tracks_multiple_levels_of_nesting
  index(<<~RUBY)
    module Foo
      def first_method; end

      module Bar
        def second_method; end
      end

      def third_method; end
    end
  RUBY

  entry = T.cast(@index["first_method"]&.first, Entry::Method)
  assert_equal("Foo", T.must(entry.owner).name)

  entry = T.cast(@index["second_method"]&.first, Entry::Method)
  assert_equal("Foo::Bar", T.must(entry.owner).name)

  entry = T.cast(@index["third_method"]&.first, Entry::Method)
  assert_equal("Foo", T.must(entry.owner).name)
end

#test_singleton_method_using_other_receiver_is_not_indexedObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_indexer/test/method_test.rb', line 46

def test_singleton_method_using_other_receiver_is_not_indexed
  index(<<~RUBY)
    class Foo
      def String.bar
      end
    end
  RUBY

  assert_no_entry("bar")
end

#test_singleton_method_using_self_receiverObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_indexer/test/method_test.rb', line 30

def test_singleton_method_using_self_receiver
  index(<<~RUBY)
    class Foo
      def self.bar
      end
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:2-5")

  entry = T.must(@index["bar"].first)
  owner = T.must(entry.owner)
  assert_equal("Foo::<Class:Foo>", owner.name)
  assert_instance_of(Entry::SingletonClass, owner)
end

#test_singleton_methodsObject



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/ruby_indexer/test/method_test.rb', line 420

def test_singleton_methods
  index(<<~RUBY)
    class Foo
      def self.bar; end

      class << self
        def baz; end
      end
    end
  RUBY

  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:1-2:1-19")
  assert_entry("baz", Entry::Method, "/fake/path/foo.rb:4-4:4-16")

  bar_owner = T.must(T.must(@index["bar"].first).owner)
  baz_owner = T.must(T.must(@index["baz"].first).owner)

  assert_instance_of(Entry::SingletonClass, bar_owner)
  assert_instance_of(Entry::SingletonClass, baz_owner)

  # Regardless of whether the method was added through `self.something` or `class << self`, the owner object must be
  # the exact same
  assert_same(bar_owner, baz_owner)
end

#test_visibility_trackingObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_indexer/test/method_test.rb', line 79

def test_visibility_tracking
  index(<<~RUBY)
    private def foo
    end

    def bar; end

    protected

    def baz; end
  RUBY

  assert_entry("foo", Entry::Method, "/fake/path/foo.rb:0-8:1-3", visibility: Entry::Visibility::PRIVATE)
  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:3-0:3-12", visibility: Entry::Visibility::PUBLIC)
  assert_entry("baz", Entry::Method, "/fake/path/foo.rb:7-0:7-12", visibility: Entry::Visibility::PROTECTED)
end

#test_visibility_tracking_with_nested_class_or_modulesObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ruby_indexer/test/method_test.rb', line 96

def test_visibility_tracking_with_nested_class_or_modules
  index(<<~RUBY)
    class Foo
      private

      def foo; end

      class Bar
        def bar; end
      end

      def baz; end
    end
  RUBY

  assert_entry("foo", Entry::Method, "/fake/path/foo.rb:3-2:3-14", visibility: Entry::Visibility::PRIVATE)
  assert_entry("bar", Entry::Method, "/fake/path/foo.rb:6-4:6-16", visibility: Entry::Visibility::PUBLIC)
  assert_entry("baz", Entry::Method, "/fake/path/foo.rb:9-2:9-14", visibility: Entry::Visibility::PRIVATE)
end