Class: RubyIndexer::ClassesAndModulesTest

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

Instance Method Summary collapse

Methods inherited from TestCase

#setup, #teardown

Instance Method Details

#test_class_with_statementsObject



26
27
28
29
30
31
32
33
34
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 26

def test_class_with_statements
  index("    class Foo\n      def something; end\n    end\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Class, \"/fake/path/foo.rb:0-0:2-3\")\nend\n")

#test_colon_colon_classObject



36
37
38
39
40
41
42
43
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 36

def test_colon_colon_class
  index("    class ::Foo\n    end\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Class, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_colon_colon_class_inside_classObject



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

def test_colon_colon_class_inside_class
  index("    class Bar\n      class ::Foo\n      end\n    end\n  RUBY\n\n  assert_entry(\"Bar\", Entry::Class, \"/fake/path/foo.rb:0-0:3-3\")\n  assert_entry(\"Foo\", Entry::Class, \"/fake/path/foo.rb:1-2:2-5\")\nend\n")

#test_colon_colon_moduleObject



119
120
121
122
123
124
125
126
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 119

def test_colon_colon_module
  index("    module ::Foo\n    end\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Module, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_comments_can_be_attached_to_a_classObject



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

def test_comments_can_be_attached_to_a_class
  index("    # This is method comment\n    def foo; end\n    # This is a Foo comment\n    # This is another Foo comment\n    class Foo\n      # This should not be attached\n    end\n\n    # Ignore me\n\n    # This Bar comment has 1 line padding\n\n    class Bar; end\n  RUBY\n\n  foo_entry = @index[\"Foo\"] #: as !nil\n    .first #: as !nil\n  assert_equal(\"This is a Foo comment\\nThis is another Foo comment\", foo_entry.comments)\n\n  bar_entry = @index[\"Bar\"] #: as !nil\n    .first #: as !nil\n  assert_equal(\"This Bar comment has 1 line padding\", bar_entry.comments)\nend\n")

#test_comments_can_be_attached_to_a_namespaced_classObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 244

def test_comments_can_be_attached_to_a_namespaced_class
  index("    # This is a Foo comment\n    # This is another Foo comment\n    class Foo\n      # This is a Bar comment\n      class Bar; end\n    end\n  RUBY\n\n  foo_entry = @index[\"Foo\"] #: as !nil\n    .first #: as !nil\n  assert_equal(\"This is a Foo comment\\nThis is another Foo comment\", foo_entry.comments)\n\n  bar_entry = @index[\"Foo::Bar\"] #: as !nil\n    .first #: as !nil\n  assert_equal(\"This is a Bar comment\", bar_entry.comments)\nend\n")

#test_comments_can_be_attached_to_a_reopened_classObject



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 263

def test_comments_can_be_attached_to_a_reopened_class
  index("    # This is a Foo comment\n    class Foo; end\n\n    # This is another Foo comment\n    class Foo; end\n  RUBY\n\n  first_foo_entry, second_foo_entry = @index[\"Foo\"] #: as !nil\n  assert_equal(\"This is a Foo comment\", first_foo_entry&.comments)\n  assert_equal(\"This is another Foo comment\", second_foo_entry&.comments)\nend\n")

#test_comments_removes_the_leading_pound_and_spaceObject



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 277

def test_comments_removes_the_leading_pound_and_space
  index("    # This is a Foo comment\n    class Foo; end\n\n    #This is a Bar comment\n    class Bar; end\n  RUBY\n\n  first_foo_entry = @index[\"Foo\"] #: as !nil\n    .first #: as !nil\n  assert_equal(\"This is a Foo comment\", first_foo_entry.comments)\n\n  second_foo_entry = @index[\"Bar\"] #: as !nil\n    .first #: as !nil\n  assert_equal(\"This is a Bar comment\", second_foo_entry.comments)\nend\n")

#test_conditional_classObject



17
18
19
20
21
22
23
24
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 17

def test_conditional_class
  index("    class Foo\n    end if condition\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Class, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_conditional_moduleObject



100
101
102
103
104
105
106
107
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 100

def test_conditional_module
  index("    module Foo\n    end if condition\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Module, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_deleting_from_index_based_on_file_pathObject



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 195

def test_deleting_from_index_based_on_file_path
  index("    class Foo\n    end\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Class, \"/fake/path/foo.rb:0-0:1-3\")\n\n  @index.delete(URI::Generic.from_path(path: \"/fake/path/foo.rb\"))\n  refute_entry(\"Foo\")\n\n  assert_no_indexed_entries\nend\n")

#test_dynamic_singleton_class_blocksObject



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 521

def test_dynamic_singleton_class_blocks
  index("    class Foo\n      # Some extra comments\n      class << bar\n      end\n    end\n  RUBY\n\n  singleton = @index[\"Foo::<Class:bar>\"] #: as !nil\n    .first #: as Entry::SingletonClass\n\n  # Even though this is not correct, we consider any dynamic singleton class block as a regular singleton class.\n  # That pattern cannot be properly analyzed statically and assuming that it's always a regular singleton simplifies\n  # the implementation considerably.\n  assert_equal(3, singleton.location.start_line)\n  assert_equal(\"Some extra comments\", singleton.comments)\nend\n")

#test_dynamically_namespaced_classObject



66
67
68
69
70
71
72
73
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 66

def test_dynamically_namespaced_class
  index("    class self::Bar\n    end\n  RUBY\n\n  assert_entry(\"self::Bar\", Entry::Class, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_dynamically_namespaced_class_does_not_affect_other_classesObject



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

def test_dynamically_namespaced_class_does_not_affect_other_classes
  index("    class Foo\n      class self::Bar\n      end\n\n      class Bar\n      end\n    end\n  RUBY\n\n  refute_entry(\"self::Bar\")\n  assert_entry(\"Foo\", Entry::Class, \"/fake/path/foo.rb:0-0:6-3\")\n  assert_entry(\"Foo::Bar\", Entry::Class, \"/fake/path/foo.rb:4-2:5-5\")\nend\n")

#test_dynamically_namespaced_moduleObject



137
138
139
140
141
142
143
144
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 137

def test_dynamically_namespaced_module
  index("    module self::Bar\n    end\n  RUBY\n\n  assert_entry(\"self::Bar\", Entry::Module, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_dynamically_namespaced_module_does_not_affect_other_modulesObject



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

def test_dynamically_namespaced_module_does_not_affect_other_modules
  index("    module Foo\n      class self::Bar\n      end\n\n      module Bar\n      end\n    end\n  RUBY\n\n  assert_entry(\"Foo::self::Bar\", Entry::Class, \"/fake/path/foo.rb:1-2:2-5\")\n  assert_entry(\"Foo\", Entry::Module, \"/fake/path/foo.rb:0-0:6-3\")\n  assert_entry(\"Foo::Bar\", Entry::Module, \"/fake/path/foo.rb:4-2:5-5\")\nend\n")

#test_empty_statements_classObject



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

def test_empty_statements_class
  index("    class Foo\n    end\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Class, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_empty_statements_moduleObject



91
92
93
94
95
96
97
98
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 91

def test_empty_statements_module
  index("    module Foo\n    end\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Module, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_indexing_namespaces_inside_nested_top_level_referencesObject



623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 623

def test_indexing_namespaces_inside_nested_top_level_references
  index("    class Baz\n      module ::Foo\n        class Bar\n        end\n\n        class ::Qux\n        end\n      end\n    end\n  RUBY\n\n  refute_entry(\"Baz::Foo\")\n  refute_entry(\"Baz::Foo::Bar\")\n  assert_entry(\"Baz\", Entry::Class, \"/fake/path/foo.rb:0-0:8-3\")\n  assert_entry(\"Foo\", Entry::Module, \"/fake/path/foo.rb:1-2:7-5\")\n  assert_entry(\"Foo::Bar\", Entry::Class, \"/fake/path/foo.rb:2-4:3-7\")\n  assert_entry(\"Qux\", Entry::Class, \"/fake/path/foo.rb:5-4:6-7\")\nend\n")

#test_indexing_namespaces_inside_top_level_referencesObject



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 585

def test_indexing_namespaces_inside_top_level_references
  index("    module ::Foo\n      class Bar\n      end\n    end\n  RUBY\n\n  # We want to explicitly verify that we didn't introduce the leading `::` by accident, but `Index#[]` deletes the\n  # prefix when we use `refute_entry`\n  entries = @index.instance_variable_get(:@entries)\n  refute(entries.key?(\"::Foo\"))\n  refute(entries.key?(\"::Foo::Bar\"))\n  assert_entry(\"Foo\", Entry::Module, \"/fake/path/foo.rb:0-0:3-3\")\n  assert_entry(\"Foo::Bar\", Entry::Class, \"/fake/path/foo.rb:1-2:2-5\")\nend\n")

#test_indexing_singletons_inside_top_level_referencesObject



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 602

def test_indexing_singletons_inside_top_level_references
  index("    module ::Foo\n      class Bar\n        class << self\n        end\n      end\n    end\n  RUBY\n\n  # We want to explicitly verify that we didn't introduce the leading `::` by accident, but `Index#[]` deletes the\n  # prefix when we use `refute_entry`\n  entries = @index.instance_variable_get(:@entries)\n  refute(entries.key?(\"::Foo\"))\n  refute(entries.key?(\"::Foo::Bar\"))\n  refute(entries.key?(\"::Foo::Bar::<Class:Bar>\"))\n  assert_entry(\"Foo\", Entry::Module, \"/fake/path/foo.rb:0-0:5-3\")\n  assert_entry(\"Foo::Bar\", Entry::Class, \"/fake/path/foo.rb:1-2:4-5\")\n  assert_entry(\"Foo::Bar::<Class:Bar>\", Entry::SingletonClass, \"/fake/path/foo.rb:2-4:3-7\")\nend\n")

#test_keeping_track_of_extended_modulesObject



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 456

def test_keeping_track_of_extended_modules
  index("    class Foo\n      # valid syntaxes that we can index\n      extend A1\n      self.extend A2\n      extend A3, A4\n      self.extend A5, A6\n\n      # valid syntaxes that we cannot index because of their dynamic nature\n      extend some_variable_or_method_call\n      self.extend some_variable_or_method_call\n\n      def something\n        extend A7 # We should not index this because of this dynamic nature\n      end\n\n      # Valid inner class syntax definition with its own modules prepended\n      class Qux\n        extend Corge\n        self.extend Corge\n        extend Baz\n\n        extend some_variable_or_method_call\n      end\n    end\n\n    class ConstantPathReferences\n      extend Foo::Bar\n      self.extend Foo::Bar2\n\n      extend dynamic::Bar\n      extend Foo::\n    end\n  RUBY\n\n  foo = @index[\"Foo::<Class:Foo>\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal([\"A1\", \"A2\", \"A3\", \"A4\", \"A5\", \"A6\"], foo.mixin_operation_module_names)\n\n  qux = @index[\"Foo::Qux::<Class:Qux>\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal([\"Corge\", \"Corge\", \"Baz\"], qux.mixin_operation_module_names)\n\n  constant_path_references = @index[\"ConstantPathReferences::<Class:ConstantPathReferences>\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal([\"Foo::Bar\", \"Foo::Bar2\"], constant_path_references.mixin_operation_module_names)\nend\n")

#test_keeping_track_of_included_modulesObject



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 358

def test_keeping_track_of_included_modules
  index("    class Foo\n      # valid syntaxes that we can index\n      include A1\n      self.include A2\n      include A3, A4\n      self.include A5, A6\n\n      # valid syntaxes that we cannot index because of their dynamic nature\n      include some_variable_or_method_call\n      self.include some_variable_or_method_call\n\n      def something\n        include A7 # We should not index this because of this dynamic nature\n      end\n\n      # Valid inner class syntax definition with its own modules included\n      class Qux\n        include Corge\n        self.include Corge\n        include Baz\n\n        include some_variable_or_method_call\n      end\n    end\n\n    class ConstantPathReferences\n      include Foo::Bar\n      self.include Foo::Bar2\n\n      include dynamic::Bar\n      include Foo::\n    end\n  RUBY\n\n  foo = @index[\"Foo\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal([\"A1\", \"A2\", \"A3\", \"A4\", \"A5\", \"A6\"], foo.mixin_operation_module_names)\n\n  qux = @index[\"Foo::Qux\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal([\"Corge\", \"Corge\", \"Baz\"], qux.mixin_operation_module_names)\n\n  constant_path_references = @index[\"ConstantPathReferences\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal([\"Foo::Bar\", \"Foo::Bar2\"], constant_path_references.mixin_operation_module_names)\nend\n")

#test_keeping_track_of_prepended_modulesObject



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 407

def test_keeping_track_of_prepended_modules
  index("    class Foo\n      # valid syntaxes that we can index\n      prepend A1\n      self.prepend A2\n      prepend A3, A4\n      self.prepend A5, A6\n\n      # valid syntaxes that we cannot index because of their dynamic nature\n      prepend some_variable_or_method_call\n      self.prepend some_variable_or_method_call\n\n      def something\n        prepend A7 # We should not index this because of this dynamic nature\n      end\n\n      # Valid inner class syntax definition with its own modules prepended\n      class Qux\n        prepend Corge\n        self.prepend Corge\n        prepend Baz\n\n        prepend some_variable_or_method_call\n      end\n    end\n\n    class ConstantPathReferences\n      prepend Foo::Bar\n      self.prepend Foo::Bar2\n\n      prepend dynamic::Bar\n      prepend Foo::\n    end\n  RUBY\n\n  foo = @index[\"Foo\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal([\"A1\", \"A2\", \"A3\", \"A4\", \"A5\", \"A6\"], foo.mixin_operation_module_names)\n\n  qux = @index[\"Foo::Qux\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal([\"Corge\", \"Corge\", \"Baz\"], qux.mixin_operation_module_names)\n\n  constant_path_references = @index[\"ConstantPathReferences\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal([\"Foo::Bar\", \"Foo::Bar2\"], constant_path_references.mixin_operation_module_names)\nend\n")

#test_keeping_track_of_super_classesObject



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 321

def test_keeping_track_of_super_classes
  index("    class Foo < Bar\n    end\n\n    class Baz\n    end\n\n    module Something\n      class Baz\n      end\n\n      class Qux < ::Baz\n      end\n    end\n\n    class FinalThing < Something::Baz\n    end\n  RUBY\n\n  foo = @index[\"Foo\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal(\"Bar\", foo.parent_class)\n\n  baz = @index[\"Baz\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal(\"::Object\", baz.parent_class)\n\n  qux = @index[\"Something::Qux\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal(\"::Baz\", qux.parent_class)\n\n  final_thing = @index[\"FinalThing\"] #: as !nil\n    .first #: as Entry::Class\n  assert_equal(\"Something::Baz\", final_thing.parent_class)\nend\n")

#test_lazy_comment_fetching_does_not_fail_if_file_gets_deletedObject



661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 661

def test_lazy_comment_fetching_does_not_fail_if_file_gets_deleted
  uri = URI::Generic.from_path(
    load_path_entry: "#{Dir.pwd}/lib",
    path: "#{Dir.pwd}/lib/ruby_lsp/does_not_exist.rb",
  )

  @index.index_single(uri, "    class Foo\n    end\n  RUBY\n\n  entry = @index[\"Foo\"]&.first #: as !nil\n  assert_empty(entry.comments)\nend\n", collect_comments: false)

#test_lazy_comment_fetching_uses_correct_line_breaks_for_renderingObject



644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 644

def test_lazy_comment_fetching_uses_correct_line_breaks_for_rendering
  uri = URI::Generic.from_path(
    load_path_entry: "#{Dir.pwd}/lib",
    path: "#{Dir.pwd}/lib/ruby_lsp/node_context.rb",
  )

  @index.index_file(uri, collect_comments: false)

  entry = @index["RubyLsp::NodeContext"] #: as !nil
    .first #: as !nil

  assert_equal("    This class allows listeners to access contextual information about a node in the AST, such as its parent,\n    its namespace nesting, and the surrounding CallNode (e.g. a method call).\n  COMMENTS\nend\n".chomp, entry.comments)

#test_lazy_comments_with_no_spaces_are_properly_attributedObject



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

def test_lazy_comments_with_no_spaces_are_properly_attributed
  path = File.join(Dir.pwd, "lib", "foo.rb")
  source = "    require \"whatever\"\n\n    # These comments belong to the declaration below\n    # They have to be associated with it\n    class Foo\n    end\n  RUBY\n  File.write(path, source)\n  @index.index_single(URI::Generic.from_path(path: path), source, collect_comments: false)\n\n  entry = @index[\"Foo\"]&.first #: as !nil\n\n  begin\n    assert_equal(<<~COMMENTS.chomp, entry.comments)\n      These comments belong to the declaration below\n      They have to be associated with it\n    COMMENTS\n  ensure\n    FileUtils.rm(path)\n  end\nend\n"

#test_lazy_comments_with_spaces_are_properly_attributedObject



695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 695

def test_lazy_comments_with_spaces_are_properly_attributed
  path = File.join(Dir.pwd, "lib", "foo.rb")
  source =  "    require \"whatever\"\n\n    # These comments belong to the declaration below\n    # They have to be associated with it\n\n    class Foo\n    end\n  RUBY\n  File.write(path, source)\n  @index.index_single(URI::Generic.from_path(path: path), source, collect_comments: false)\n\n  entry = @index[\"Foo\"]&.first #: as !nil\n\n  begin\n    assert_equal(<<~COMMENTS.chomp, entry.comments)\n      These comments belong to the declaration below\n      They have to be associated with it\n    COMMENTS\n  ensure\n    FileUtils.rm(path)\n  end\nend\n"

#test_lazy_comments_with_two_extra_spaces_are_properly_ignoredObject



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

def test_lazy_comments_with_two_extra_spaces_are_properly_ignored
  path = File.join(Dir.pwd, "lib", "foo.rb")
  source = "    require \"whatever\"\n\n    # These comments don't belong to the declaration below\n    # They will not be associated with it\n\n\n    class Foo\n    end\n  RUBY\n  File.write(path, source)\n  @index.index_single(URI::Generic.from_path(path: path), source, collect_comments: false)\n\n  entry = @index[\"Foo\"]&.first #: as !nil\n\n  begin\n    assert_empty(entry.comments)\n  ensure\n    FileUtils.rm(path)\n  end\nend\n"

#test_module_with_statementsObject



109
110
111
112
113
114
115
116
117
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 109

def test_module_with_statements
  index("    module Foo\n      def something; end\n    end\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Module, \"/fake/path/foo.rb:0-0:2-3\")\nend\n")

#test_name_location_points_to_constant_path_locationObject



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 553

def test_name_location_points_to_constant_path_location
  index("    class Foo\n      def foo; end\n    end\n\n    module Bar\n      def bar; end\n    end\n  RUBY\n\n  foo = @index[\"Foo\"] #: as !nil\n    .first #: as Entry::Class\n  refute_equal(foo.location, foo.name_location)\n\n  name_location = foo.name_location\n  assert_equal(1, name_location.start_line)\n  assert_equal(1, name_location.end_line)\n  assert_equal(6, name_location.start_column)\n  assert_equal(9, name_location.end_column)\n\n  bar = @index[\"Bar\"] #: as !nil\n    .first #: as Entry::Module\n  refute_equal(bar.location, bar.name_location)\n\n  name_location = bar.name_location\n  assert_equal(5, name_location.start_line)\n  assert_equal(5, name_location.end_line)\n  assert_equal(7, name_location.start_column)\n  assert_equal(10, name_location.end_column)\nend\n")

#test_namespaced_classObject



57
58
59
60
61
62
63
64
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 57

def test_namespaced_class
  index("    class Foo::Bar\n    end\n  RUBY\n\n  assert_entry(\"Foo::Bar\", Entry::Class, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_namespaced_moduleObject



128
129
130
131
132
133
134
135
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 128

def test_namespaced_module
  index("    module Foo::Bar\n    end\n  RUBY\n\n  assert_entry(\"Foo::Bar\", Entry::Module, \"/fake/path/foo.rb:0-0:1-3\")\nend\n")

#test_namespaces_inside_singleton_blocksObject



540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 540

def test_namespaces_inside_singleton_blocks
  index("    class Foo\n      class << self\n        class Bar\n        end\n      end\n    end\n  RUBY\n\n  assert_entry(\"Foo::<Class:Foo>::Bar\", Entry::Class, \"/fake/path/foo.rb:2-4:3-7\")\nend\n")

#test_nested_modules_and_classesObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 173

def test_nested_modules_and_classes
  index("    module Foo\n      class Bar\n      end\n\n      module Baz\n        class Qux\n          class Something\n          end\n        end\n      end\n    end\n  RUBY\n\n  assert_entry(\"Foo\", Entry::Module, \"/fake/path/foo.rb:0-0:10-3\")\n  assert_entry(\"Foo::Bar\", Entry::Class, \"/fake/path/foo.rb:1-2:2-5\")\n  assert_entry(\"Foo::Baz\", Entry::Module, \"/fake/path/foo.rb:4-2:9-5\")\n  assert_entry(\"Foo::Baz::Qux\", Entry::Class, \"/fake/path/foo.rb:5-4:8-7\")\n  assert_entry(\"Foo::Baz::Qux::Something\", Entry::Class, \"/fake/path/foo.rb:6-6:7-9\")\nend\n")

#test_nested_modules_and_classes_with_multibyte_charactersObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 162

def test_nested_modules_and_classes_with_multibyte_characters
  index("    module A\u52D5\u7269\n      class B\u306D\u3053; end\n    end\n  RUBY\n\n  assert_entry(\"A\u52D5\u7269\", Entry::Module, \"/fake/path/foo.rb:0-0:2-3\")\n  assert_entry(\"A\u52D5\u7269::B\u306D\u3053\", Entry::Class, \"/fake/path/foo.rb:1-2:1-16\")\nend\n")

#test_private_class_and_module_indexingObject



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

def test_private_class_and_module_indexing
  index("    class A\n      class B; end\n      private_constant(:B)\n\n      module C; end\n      private_constant(\"C\")\n\n      class D; end\n    end\n  RUBY\n\n  b_const = @index[\"A::B\"] #: as !nil\n    .first\n  assert_predicate(b_const, :private?)\n\n  c_const = @index[\"A::C\"] #: as !nil\n    .first\n  assert_predicate(c_const, :private?)\n\n  d_const = @index[\"A::D\"] #: as !nil\n    .first\n  assert_predicate(d_const, :public?)\nend\n")

#test_singleton_inside_compact_namespaceObject



676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 676

def test_singleton_inside_compact_namespace
  index("    module Foo::Bar\n      class << self\n        def baz; end\n      end\n    end\n  RUBY\n\n  # Verify we didn't index the incorrect name\n  assert_nil(@index[\"Foo::Bar::<Class:Foo::Bar>\"])\n\n  # Verify we indexed the correct name\n  assert_entry(\"Foo::Bar::<Class:Bar>\", Entry::SingletonClass, \"/fake/path/foo.rb:1-2:3-5\")\n\n  method = @index[\"baz\"]&.first #: as Entry::Method\n  assert_equal(\"Foo::Bar::<Class:Bar>\", method.owner&.name)\nend\n")

#test_skips_comments_containing_invalid_encodingsObject



235
236
237
238
239
240
241
242
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 235

def test_skips_comments_containing_invalid_encodings
  index("    # comment \\xBA\n    class Foo\n    end\n  RUBY\n  assert(@index[\"Foo\"]&.first)\nend\n")

#test_tracking_singleton_classesObject



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/ruby_indexer/test/classes_and_modules_test.rb', line 505

def test_tracking_singleton_classes
  index("    class Foo; end\n    class Foo\n      # Some extra comments\n      class << self\n      end\n    end\n  RUBY\n\n  foo = @index[\"Foo::<Class:Foo>\"] #: as !nil\n    .first #: as Entry::SingletonClass\n  assert_equal(4, foo.location.start_line)\n  assert_equal(\"Some extra comments\", foo.comments)\nend\n")