-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathrdoc_parser_prism_ruby_test.rb
More file actions
2165 lines (1923 loc) · 54 KB
/
rdoc_parser_prism_ruby_test.rb
File metadata and controls
2165 lines (1923 loc) · 54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
357
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
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
838
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require_relative 'helper'
require 'rdoc/parser'
require 'rdoc/parser/prism_ruby'
module RDocParserPrismTestCases
def setup
super
@tempfile = Tempfile.new self.class.name
@filename = @tempfile.path
@top_level = @store.add_file @filename
@options = RDoc::Options.new
@options.quiet = true
@options.option_parser = OptionParser.new
@comment = RDoc::Comment.new '', @top_level
@stats = RDoc::Stats.new @store, 0
end
def teardown
super
@tempfile.close!
end
def test_look_for_directives_in_section
util_parser <<~RUBY
# :section: new section
RUBY
section = @top_level.current_section
assert_equal 'new section', section.title
end
def test_look_for_directives_in_commented
util_parser <<~RUBY
# how to make a section:
# # :section: new section
RUBY
section = @top_level.current_section
assert_nil section.title
end
def test_look_for_directives_in_unhandled
util_parser <<~RUBY
# :unhandled: blah
RUBY
assert_equal 'blah', @top_level.metadata['unhandled']
end
def test_block_comment
util_parser <<~RUBY
=begin rdoc
foo
=end
class A
=begin
bar
baz
=end
def f; end
end
RUBY
klass = @top_level.classes.first
meth = klass.method_list.first
assert_equal 'foo', klass.comment.text.strip
assert_equal "bar\nbaz", meth.comment.text.strip
end
def test_module
util_parser <<~RUBY
# my module
module Foo
end
RUBY
mod = @top_level.modules.first
assert_equal 'Foo', mod.full_name
assert_equal 'my module', mod.comment.text
assert_equal [@top_level], mod.in_files
end
def test_nested_module_with_colon
util_parser <<~RUBY
module Foo
module Bar; end
module Bar::Baz1; end
module ::Foo
module Bar2; end
end
end
module ::Baz; end
module Foo::Bar::Baz2
module ::Foo2
module Bar; end
end
module Blah; end
end
RUBY
module_names = @store.all_modules.map(&:full_name)
expected = %w[
Foo Foo::Bar Foo::Bar::Baz1 Foo::Bar2 Baz Foo::Bar::Baz2 Foo2 Foo2::Bar Foo::Bar::Baz2::Blah
]
assert_equal expected.sort, module_names.sort
end
def test_class
util_parser <<~RUBY
# my class
class Foo
end
RUBY
klass = @top_level.classes.first
assert_equal 'Foo', klass.full_name
assert_equal 'my class', klass.comment.text
assert_equal [@top_level], klass.in_files
assert_equal 2, klass.line
end
def test_nested_class_with_colon
util_parser <<~RUBY
class Foo
class Bar; end
class Bar::Baz1; end
class ::Foo
class Bar2; end
end
end
class ::Baz; end
class Foo::Bar::Baz2
class ::Foo2
class Bar; end
end
class Blah; end
end
RUBY
class_names = @store.all_classes.map(&:full_name)
expected = %w[
Foo Foo::Bar Foo::Bar::Baz1 Foo::Bar2 Baz Foo::Bar::Baz2 Foo2 Foo2::Bar Foo::Bar::Baz2::Blah
]
assert_equal expected.sort, class_names.sort
end
def test_open_class_with_superclass
util_parser <<~RUBY
class A; end
class B < A
def m1; end
end
class B < A
def m2; end
end
class C < String
def m1; end
end
class C < String
def m2; end
end
RUBY
classes = @top_level.classes
assert_equal 3, classes.size
_a, b, c = classes
assert_equal 'A', b.superclass.full_name
assert_equal 'String', c.superclass
assert_equal ['m1', 'm2'], b.method_list.map(&:name)
assert_equal ['m1', 'm2'], c.method_list.map(&:name)
end
def test_open_class_with_superclass_specified_later
util_parser <<~RUBY
# file_2
require 'file_1'
class A; end
class B; end
class C; end
RUBY
_a, b, c = @top_level.classes
assert_equal 'Object', b.superclass
assert_equal 'Object', c.superclass
util_parser <<~RUBY
# file_1
class B < A; end
class C < Unknown; end
RUBY
assert_equal 'A', b.superclass.full_name
assert_equal 'Unknown', c.superclass
end
def test_open_class_with_superclass_specified_later_with_object_defined
util_parser <<~RUBY
# file_2
require 'file_1'
class Object; end
class A; end
class B; end
class C; end
RUBY
_object, _a, b, c = @top_level.classes
# If Object exists, superclass will be a NormalClass(Object) instead of string "Object"
assert_equal 'Object', b.superclass.full_name
assert_equal 'Object', c.superclass.full_name
util_parser <<~RUBY
# file_1
class B < A; end
class C < Unknown; end
RUBY
assert_equal 'A', b.superclass.full_name
assert_equal 'Unknown', c.superclass
end
def test_confusing_superclass
util_parser <<~RUBY
module A
class B; end
end
module A
class C1 < A::B; end
end
class A::C2 < A::B; end
module A::A
class B; end
end
module A
class C3 < A::B; end
end
class A::C4 < A::B; end
RUBY
mod = @top_level.modules.first
classes = mod.classes
assert_equal ['A::B', 'A::C1', 'A::C2', 'A::C3', 'A::C4'], classes.map(&:full_name)
assert_equal ['A::B', 'A::B', 'A::A::B', 'A::B'], classes.drop(1).map(&:superclass).map(&:full_name)
end
def test_pseudo_recursive_superclass
util_parser <<~RUBY
module Foo
class Bar
class Foo < Bar; end
# This class definition is used in OpenSSL::Cipher::Cipher
class Bar < Bar; end
class Baz < Bar; end
end
end
RUBY
foo_klass = @store.find_class_named 'Foo::Bar::Foo'
bar_klass = @store.find_class_named 'Foo::Bar::Bar'
baz_klass = @store.find_class_named 'Foo::Bar::Baz'
assert_equal 'Foo::Bar', foo_klass.superclass.full_name
assert_equal 'Foo::Bar', bar_klass.superclass.full_name
assert_equal 'Foo::Bar::Bar', baz_klass.superclass.full_name
end
def test_class_module_nodoc
util_parser <<~RUBY
class Foo # :nodoc:
end
class Bar
end # :nodoc:
class Baz; end
class Baz::A; end # :nodoc:
module MFoo # :nodoc:
end
module MBar
end # :nodoc:
module MBaz; end
module MBaz::M; end; # :nodoc:
RUBY
documentables = @store.all_classes_and_modules.select(&:document_self)
assert_equal ['Baz', 'MBaz'], documentables.map(&:full_name) unless accept_legacy_bug?
end
def test_class_module_stopdoc
util_parser <<~RUBY
# comment
class Foo
class A; end
# :stopdoc:
class B; end
end
# comment
module Bar
module A; end
# :stopdoc:
module B; end
end
RUBY
klass = @top_level.classes.first
mod = @top_level.modules.first
assert_equal 'comment', klass.comment.text.strip
assert_equal 'comment', mod.comment.text.strip
assert_equal ['Foo::A'], klass.classes.select(&:document_self).map(&:full_name)
assert_equal ['Bar::A'], mod.modules.select(&:document_self).map(&:full_name)
end
def test_class_superclass
util_parser <<~RUBY
class Foo; end
class Bar < Foo
end
class Baz < (any expression)
end
RUBY
assert_equal ['Foo', 'Bar', 'Baz'], @top_level.classes.map(&:full_name)
foo, bar, baz = @top_level.classes
assert_equal foo, bar.superclass
assert_equal '(any expression)', baz.superclass
end
def test_class_new_notnew
util_parser <<~RUBY
class A
def initialize(*args); end
end
class B
##
# :args: a, b, c
def initialize(*args); end
end
class C
def self.initialize(*args); end
end
class D
##
# :args: a, b, c
def initialize(*args); end # :notnew:
end
class E
def initialize(*args); end # :not-new:
end
class F
def initialize(*args); end # :not_new:
end
class G
def initialize(*args)
end # :notnew:
end
RUBY
expected = [
'new(*args)', 'new(a, b, c)',
'initialize(*args)', 'initialize(a, b, c)',
'initialize(*args)', 'initialize(*args)',
'initialize(*args)'
]
arglists = @top_level.classes.map { |c| c.method_list.first.arglists }
assert_equal expected, arglists
end
def test_class_mistaken_for_module
util_parser <<~RUBY
class A::Foo; end
class B::Foo; end
module C::Bar; end
module D::Baz; end
class A; end
class X < C; end
RUBY
assert_equal ['A', 'C', 'X'], @top_level.classes.map(&:full_name)
assert_equal ['B', 'D'], @top_level.modules.map(&:full_name)
end
def test_parenthesized_cdecl
util_parser <<~RUBY
module DidYouMean
# Not a module, but creates a dummy module for document
class << (NameErrorCheckers = Object.new)
def new; end
end
end
RUBY
mod = @store.find_class_or_module('DidYouMean').modules.first
assert_equal 'DidYouMean::NameErrorCheckers', mod.full_name
assert_equal ['DidYouMean::NameErrorCheckers::new'], mod.method_list.map(&:full_name)
end
def test_ghost_method
util_parser <<~RUBY
class Foo
##
# :method: one
#
# my method one
##
# :method:
# :call-seq:
# two(name)
#
# my method two
##
# :method: three
# :args: a, b
#
# my method three
# :stopdoc:
##
# :method: hidden1
#
# comment
##
# :method:
# :call-seq:
# hidden2(name)
#
# comment
end
RUBY
klass = @store.find_class_named 'Foo'
assert_equal 3, klass.method_list.size
one, two, three = klass.method_list
assert_equal 'Foo#one', one.full_name
assert_equal 'Foo#two', two.full_name
assert_equal 'Foo#three', three.full_name
assert_equal 'two(name)', two.call_seq.chomp
assert_equal 'three(a, b)', three.arglists
assert_equal 'my method one', one.comment.text.strip
assert_equal 'my method two', two.comment.text.strip
assert_equal 'my method three', three.comment.text.strip
assert_equal 3, one.line
assert_equal 8, two.line
assert_equal 15, three.line
assert_equal @top_level, one.file
assert_equal @top_level, two.file
assert_equal @top_level, three.file
end
def test_invalid_meta_method
util_parser <<~RUBY
class Foo
# These are invalid meta method comments
# because meta method comment should start with `##`
# but rdoc accepts them as meta method comments for now.
# :method: m1
# :singleton-method: sm1
# :attr: a1
# :attr_reader: ar1
# :attr_writer: aw1
# :attr_accessor: arw1
# If there is a node following meta-like normal comment, it is not a meta method comment
# :method: m2
add_my_method(name)
# :singleton-method: sm2
add_my_singleton_method(name)
# :method:
add_my_method(:m3)
# :singleton-method:
add_my_singleton_method(:sm3)
# :attr:
add_my_attribute(:a2)
# :attr-reader:
add_my_attribute(:ar2)
# :attr-writer:
add_my_attribute(:aw2)
# :attr-accessor:
add_my_attribute(:arw2)
# :attr: a3
add_my_attribute_a3
# :attr-reader: ar3
add_my_attribute_ar3
# :attr-writer: aw3
add_my_attribute_aw2
# :attr-accessor: arw3
add_my_attribute_arw3
end
RUBY
klass = @store.find_class_named 'Foo'
assert_equal ['m1', 'sm1'], klass.method_list.map(&:name)
assert_equal ['a1', 'ar1', 'aw1', 'arw1'], klass.attributes.map(&:name)
end
def test_unknown_meta_method
util_parser <<~RUBY
class Foo
##
# :call-seq:
# two(name)
#
# method or singleton-method directive is missing
end
class Bar
##
# unknown meta method
add_my_method("foo" + "bar")
end
RUBY
foo = @store.find_class_named 'Foo'
bar = @store.find_class_named 'Bar'
assert_equal [], foo.method_list.map(&:name)
assert_equal ['unknown'], bar.method_list.map(&:name)
end
def test_method
util_parser <<~RUBY
class Foo
# my method one
def one; end
# my method two
def two(x); end
# my method three
def three x; end
end
RUBY
klass = @store.find_class_named 'Foo'
assert_equal 3, klass.method_list.size
one, two, three = klass.method_list
assert_equal 'Foo#one', one.full_name
assert_equal 'Foo#two', two.full_name
assert_equal 'Foo#three', three.full_name
assert_equal 'one()', one.arglists
assert_equal 'two(x)', two.arglists
assert_equal 'three(x)', three.arglists unless accept_legacy_bug?
assert_equal 'my method one', one.comment.text.strip
assert_equal 'my method two', two.comment.text.strip
assert_equal 'my method three', three.comment.text.strip
assert_equal 3, one.line
assert_equal 5, two.line
assert_equal 7, three.line
assert_equal @top_level, one.file
assert_equal @top_level, two.file
assert_equal @top_level, three.file
end
def test_method_with_modifier_if_unless
util_parser <<~RUBY
class Foo
# my method one
def one
end if foo
# my method two
def two
end unless foo
end
RUBY
klass = @store.find_class_named 'Foo'
one, two = klass.method_list
assert_equal 'Foo#one', one.full_name
assert_equal 'my method one', one.comment.text.strip
assert_equal 'Foo#two', two.full_name
assert_equal 'my method two', two.comment.text.strip
end
def test_method_toplevel
util_parser <<~RUBY
# comment
def foo; end
RUBY
object = @store.find_class_named 'Object'
foo = object.method_list.first
assert_equal 'Object#foo', foo.full_name
assert_equal 'comment', foo.comment.text.strip
assert_equal @top_level, foo.file
end
def test_meta_method
util_parser <<~RUBY
class Foo
##
# my method
add_my_method :method_foo, :arg
end
RUBY
klass = @store.find_class_named 'Foo'
assert_equal 1, klass.method_list.size
method = klass.method_list.first
assert_equal 'Foo#method_foo', method.full_name
assert_equal 'my method', method.comment.text.strip
assert_equal 4, method.line
assert_equal @top_level, method.file
end
def test_first_comment_is_not_a_meta_method
util_parser <<~RUBY
##
# first comment is not a meta method
add_my_method :foo
##
# this is a meta method
add_my_method :bar
RUBY
object = @store.find_class_named 'Object'
assert_equal ['bar'], object.method_list.map(&:name)
end
def test_meta_method_unknown
util_parser <<~RUBY
class Foo
##
# my method
add_my_method (:foo), :bar
end
RUBY
klass = @store.find_class_named 'Foo'
assert_equal 1, klass.method_list.size
method = klass.method_list.first
assert_equal 'Foo#unknown', method.full_name
assert_equal 'my method', method.comment.text.strip
assert_equal 4, method.line
assert_equal @top_level, method.file
end
def test_meta_define_method
util_parser <<~RUBY
class Foo
##
# comment 1
define_method :foo do end
##
# comment 2
define_method :bar, ->{}
# not a meta comment, not a meta method
define_method :ignored do end
class << self
##
# comment 3
define_method :baz do end
end
end
RUBY
klass = @store.find_class_named 'Foo'
klass.method_list.last.singleton = true if accept_legacy_bug?
assert_equal 3, klass.method_list.size
assert_equal ['Foo#foo', 'Foo#bar', 'Foo::baz'], klass.method_list.map(&:full_name)
assert_equal [false, false, true], klass.method_list.map(&:singleton)
assert_equal ['comment 1', 'comment 2', 'comment 3'], klass.method_list.map { |m| m.comment.text.strip }
assert_equal [4, 7, 13], klass.method_list.map(&:line)
assert_equal [@top_level] * 3, klass.method_list.map(&:file)
end
def test_method_definition_nested_inside_block
util_parser <<~RUBY
module A
extend ActiveSupport::Concern
included do
##
# :singleton-method:
# comment foo
mattr_accessor :foo
##
# :method: bar
# comment bar
add_my_method :bar
end
metaprogramming do
# class that defines this method is unknown
def baz1; end
end
my_decorator def self.baz2; end
self.my_decorator def baz3; end
end
RUBY
mod = @store.find_module_named 'A'
methods = mod.method_list
unless accept_legacy_bug?
assert_equal ['A::foo', 'A#bar', 'A::baz2', 'A#baz3'], methods.map(&:full_name)
end
assert_equal ['comment foo', 'comment bar'], methods.take(2).map { |m| m.comment.text.strip }
end
def test_method_yields_directive
util_parser <<~RUBY
class Foo
def f1(a, &b); end
def f2
def o.foo
yield :dummy
end
yield
end
def f3(&b)
yield a, *b, c: 1
yield 1, 2, 3
end
def f4(a, &b) # :yields: d, e
yields 1, 2
end
def f5 # :yield: f
yields 1, 2
end
def f6; end # :yields:
##
# :yields: g, h
add_my_method :f7
end
RUBY
klass = @top_level.classes.first
methods = klass.method_list
expected = [
'f1(a, &b)',
'f2() { || ... }',
'f3() { |a, *b, c: 1| ... }',
'f4(a) { |d, e| ... }',
'f5() { |f| ... }',
'f6() { || ... }',
'f7() { |g, h| ... }'
]
assert_equal expected, methods.map(&:arglists)
end
def test_calls_super
util_parser <<~RUBY
class A
def m1; foo; bar; end
def m2; if cond; super(a); end; end # SuperNode
def m3; tap do; super; end; end # ForwardingSuperNode
def m4; def a.b; super; end; end # super inside another method
end
RUBY
klass = @store.find_class_named 'A'
methods = klass.method_list
assert_equal ['m1', 'm2', 'm3', 'm4'], methods.map(&:name)
assert_equal [false, true, true, false], methods.map(&:calls_super)
end
def test_method_args_directive
util_parser <<~RUBY
class Foo
def method1 # :args: a, b, c
end
##
# :args: d, e, f
def method2(*args); end
##
# :args: g, h
add_my_method :method3
end
RUBY
klass = @top_level.classes.first
methods = klass.method_list
assert_equal ['method1(a, b, c)', 'method2(d, e, f)', 'method3(g, h)'], methods.map(&:arglists)
end
def test_class_repeatedly
util_parser <<~RUBY
class Foo
def foo; end
end
class Foo
def bar; end
end
RUBY
util_parser <<~RUBY
class Foo
def baz; end
end
RUBY
klass = @store.find_class_named 'Foo'
assert_equal ['Foo#foo', 'Foo#bar', 'Foo#baz'], klass.method_list.map(&:full_name)
end
def test_undefined_singleton_class_defines_module
util_parser <<~RUBY
module A
class << Foo
end
class << ::Bar
end
Baz1 = ''
class << Baz1
end
class << Baz2 # :nodoc:
end
end
RUBY
modules = @store.all_modules
modules = modules.take(3) if accept_legacy_bug?
assert_equal ['A', 'A::Foo', 'Bar'], modules.map(&:full_name)
end
def test_singleton_class
util_parser <<~RUBY
class A; end
class Foo
def self.m1; end
def (any expression).dummy1; end
class << self
def m2; end
def self.dummy2; end
end
class << A
def dummy3; end
end
class << Foo
def m3; end
def self.dummy4; end
end
class << ::Foo
def m4; end
end
class << (any expression)
def dummy5; end
end
end
class << Foo
def m5; end
end
class << ::Foo
def m6; end
end
RUBY
klass = @store.find_class_named 'Foo'
methods = klass.method_list
methods = methods.reject {|m| m.name =~ /dummy2|dummy4/ } if accept_legacy_bug?
assert_equal ['m1', 'm2', 'm3', 'm4', 'm5', 'm6'], methods.map(&:name)
assert_equal [true] * 6, methods.map(&:singleton)
end
def test_singleton_class_meta_method
util_parser <<~RUBY
class Foo
##
# :singleton-method: m1
##
# :singleton-method:
add_my_smethod :m2, :arg
##
# :singleton-method:
add_my_smethod 'm3', :arg
# comment
class << self
##
# method of a singleton class is a singleton method
# :method: m4
##
# :singleton-method: m5
end
end
RUBY
klass = @store.find_class_named 'Foo'
assert_equal ['m1', 'm2', 'm3', 'm4', 'm5'], klass.method_list.map(&:name)
klass.method_list[3].singleton = true if accept_legacy_bug?
assert_equal [true] * 5, klass.method_list.map(&:singleton)
end
def test_method_nested_visibility
util_parser <<~RUBY
class A
def pub1; end
private
def pri1; end
class B
def pub_b1; end
private
def pri_b1; end
public
def pub_b2; end
end
def pri2; end
end
class A
def pub2; end
private
def pri2; end
end
RUBY
klass_a = @store.find_class_named 'A'
klass_b = klass_a.find_class_named 'B'
public_a = klass_a.method_list.select { |m| m.visibility == :public }.map(&:name)
public_b = klass_b.method_list.select { |m| m.visibility == :public }.map(&:name)
assert_equal ['pub1', 'pub2'], public_a
assert_equal ['pub_b1', 'pub_b2'], public_b
end
def test_attributes_visibility
util_parser <<~RUBY
class A
attr :pub_a
attr_reader :pub_r
attr_writer :pub_w
attr_accessor :pub_rw
private
attr :pri_a
attr_reader :pri_r
attr_writer :pri_w
attr_accessor :pri_rw
end
RUBY
klass = @store.find_class_named 'A'
assert_equal ['pub_a', 'pub_r', 'pub_w', 'pub_rw', 'pri_a', 'pri_r', 'pri_w', 'pri_rw'], klass.attributes.map(&:name)
assert_equal [:public] * 4 + [:private] * 4, klass.attributes.map(&:visibility)
end
def test_method_singleton_class_visibility
util_parser <<~RUBY
class A
def self.pub1; end
private
def self.pub2; end
class << self
def pub3; end
private
def pri1; end
public
def pub4; end
private
end
end
RUBY
klass = @store.find_class_named 'A'
public_singleton_methods = klass.method_list.select { |m| m.singleton && m.visibility == :public }
assert_equal ['pub1', 'pub2', 'pub3', 'pub4'], public_singleton_methods.map(&:name)
end
def test_private_def_public_def
util_parser <<~RUBY
class A
private def m1; end
public def m2; end
private
public def m3; end
end
RUBY
klass = @store.find_class_named 'A'