1# frozen_string_literal: true
2require File.expand_path '../xref_test_case', __FILE__
3
4class TestRDocContext < XrefTestCase
5
6  def setup
7    super
8
9    @context = RDoc::Context.new
10    @context.store = @store
11  end
12
13  def test_initialize
14    assert_empty @context.in_files
15    assert_equal 'unknown', @context.name
16    assert_equal '', @context.comment
17    assert_nil   @context.parent
18    assert_equal :public, @context.visibility
19    assert_equal 1, @context.sections.length
20    assert_nil   @context.temporary_section
21
22    assert_empty @context.classes_hash
23    assert_empty @context.modules_hash
24
25    assert_empty @context.method_list
26    assert_empty @context.attributes
27    assert_empty @context.aliases
28    assert_empty @context.requires
29    assert_empty @context.includes
30    assert_empty @context.constants
31  end
32
33  def test_add_alias
34    as = RDoc::Alias.new nil, 'old_name', 'new_name', 'comment'
35
36    @context.add_alias as
37
38    assert_equal [as], @context.external_aliases
39    assert_equal [as], @context.unmatched_alias_lists['#old_name']
40  end
41
42  def test_add
43    @context.add RDoc::Extend,  'Ext', 'comment'
44    @context.add RDoc::Include, 'Incl', 'comment'
45
46    refute_empty @context.extends
47    refute_empty @context.includes
48  end
49
50  def test_add_alias_method_attr
51    top_level = @store.add_file 'file.rb'
52
53    attr = RDoc::Attr.new nil, 'old_name', 'R', ''
54
55    as = RDoc::Alias.new nil, 'old_name', 'new_name', 'comment'
56    as.record_location top_level
57    as.parent = @context
58
59    @context.add_attribute attr
60    @context.add_alias as
61
62    assert_empty @context.aliases
63    assert_empty @context.unmatched_alias_lists
64    assert_equal %w[old_name new_name], @context.attributes.map { |m| m.name }
65
66    new = @context.attributes.last
67    assert_equal top_level, new.file
68  end
69
70  def test_add_alias_method
71    top_level = @store.add_file 'file.rb'
72
73    meth = RDoc::AnyMethod.new nil, 'old_name'
74    meth.singleton = false
75
76    as = RDoc::Alias.new nil, 'old_name', 'new_name', 'comment'
77    as.record_location top_level
78    as.parent = @context
79
80    @context.add_method meth
81    @context.add_alias as
82
83    assert_empty @context.aliases
84    assert_empty @context.unmatched_alias_lists
85    assert_equal %w[old_name new_name], @context.method_list.map { |m| m.name }
86
87    new = @context.method_list.last
88    assert_equal top_level, new.file
89  end
90
91  def test_add_alias_method_singleton
92    meth = RDoc::AnyMethod.new nil, 'old_name'
93    meth.singleton = true
94
95    as = RDoc::Alias.new nil, 'old_name', 'new_name', 'comment'
96    as.singleton = true
97
98    as.parent = @context
99
100    @context.add_method meth
101    @context.add_alias as
102
103    assert_empty @context.aliases
104    assert_empty @context.unmatched_alias_lists
105    assert_equal %w[old_name new_name], @context.method_list.map { |m| m.name }
106
107    assert @context.method_list.last.singleton
108  end
109
110  def test_add_class
111    @c1.add_class RDoc::NormalClass, 'Klass', 'Object'
112
113    assert_includes @c1.classes.map { |k| k.full_name }, 'C1::Klass'
114    assert_includes @store.all_classes.map { |k| k.full_name }, 'C1::Klass'
115  end
116
117  def test_add_class_basic_object
118    @xref_data.add_class RDoc::NormalClass, 'BasicObject'
119
120    basic = @xref_data.find_module_named 'BasicObject'
121
122    assert_nil basic.superclass
123
124    @c1.add_class RDoc::NormalClass, 'BasicObject'
125
126    basic = @c1.find_module_named 'BasicObject'
127
128    assert_equal 'Object', basic.superclass
129  end
130
131  def test_add_class_object
132    @xref_data.add_class RDoc::NormalClass, 'Object'
133
134    object = @xref_data.find_module_named 'Object'
135
136    assert_equal 'BasicObject', object.superclass
137
138    @c1.add_class RDoc::NormalClass, 'Object'
139
140    object = @c1.find_module_named 'Object'
141
142    assert_equal 'Object', object.superclass.full_name
143  end
144
145  def test_add_class_singleton
146    @c1.add_class RDoc::NormalClass, 'Klass', 'Object'
147
148    assert_includes @c1.classes.map { |k| k.full_name }, 'C1::Klass'
149    assert_includes @store.all_classes.map { |k| k.full_name }, 'C1::Klass'
150  end
151
152  def test_add_class_superclass
153    @c1.add_class RDoc::NormalClass, 'Klass', 'Object'
154    @c1.add_class RDoc::NormalClass, 'Klass', 'Other'
155    @c1.add_class RDoc::NormalClass, 'Klass', 'Object'
156
157    klass = @c1.find_module_named 'Klass'
158    assert_equal 'Other', klass.superclass
159  end
160
161  def test_add_class_upgrade
162    @c1.add_module RDoc::NormalModule, 'Klass'
163    @c1.add_class RDoc::NormalClass, 'Klass', nil
164
165    assert_includes @c1.classes.map { |k| k.full_name }, 'C1::Klass',
166                    'c1 classes'
167    refute_includes @c1.modules.map { |k| k.full_name }, 'C1::Klass',
168                    'c1 modules'
169
170    assert_includes @store.all_classes.map { |k| k.full_name }, 'C1::Klass',
171                    'TopLevel classes'
172    refute_includes @store.all_modules.map { |k| k.full_name }, 'C1::Klass',
173                    'TopLevel modules'
174  end
175
176  def test_add_constant
177    const = RDoc::Constant.new 'NAME', 'value', 'comment'
178    @context.add_constant const
179
180    assert_equal [const], @context.constants
181  end
182
183  def test_add_extend
184    ext = RDoc::Extend.new 'Name', 'comment'
185    @context.add_extend ext
186
187    assert_equal [ext], @context.extends
188  end
189
190  def test_add_include
191    incl = RDoc::Include.new 'Name', 'comment'
192    @context.add_include incl
193
194    assert_equal [incl], @context.includes
195  end
196
197  def test_add_method
198    meth = RDoc::AnyMethod.new nil, 'old_name'
199    meth.visibility = nil
200
201    @context.add_method meth
202
203    assert_equal [meth], @context.method_list
204    assert_equal :public, meth.visibility
205  end
206
207  def test_add_method_alias
208    as = RDoc::Alias.new nil, 'old_name', 'new_name', 'comment'
209    meth = RDoc::AnyMethod.new nil, 'old_name'
210
211    @context.add_alias as
212    refute_empty @context.external_aliases
213
214    @context.add_method meth
215
216    assert_empty @context.external_aliases
217    assert_empty @context.unmatched_alias_lists
218    assert_equal %w[old_name new_name], @context.method_list.map { |m| m.name }
219  end
220
221  def test_add_method_duplicate
222    @store.rdoc.options.verbosity = 2
223
224    meth1 = RDoc::AnyMethod.new nil, 'name'
225    meth1.record_location @store.add_file 'first.rb'
226    meth1.visibility = nil
227    meth1.comment = comment 'first'
228
229    @context.add_method meth1
230
231    meth2 = RDoc::AnyMethod.new nil, 'name'
232    meth2.record_location @store.add_file 'second.rb'
233    meth2.comment = comment 'second'
234
235    _, err = verbose_capture_io do
236      @context.add_method meth2
237    end
238
239    expected = 'Duplicate method (unknown)#name in file second.rb, ' \
240               'previously in file first.rb'
241
242    assert_equal expected, err.chomp
243
244    method = @context.method_list.first
245
246    assert_equal 'first', method.comment.text
247  end
248
249  def test_add_method_duplicate_loading
250    @context.store = nil
251
252    meth1 = RDoc::AnyMethod.new nil, 'name'
253    meth1.record_location @store.add_file 'first.rb'
254    meth1.visibility = nil
255    meth1.comment = comment 'first'
256
257    @context.add_method meth1
258
259    meth2 = RDoc::AnyMethod.new nil, 'name'
260    meth2.record_location @store.add_file 'second.rb'
261    meth2.comment = comment 'second'
262
263    _, err = verbose_capture_io do
264      @context.add_method meth2
265    end
266
267    assert_empty err
268
269    method = @context.method_list.first
270
271    assert_equal 'first', method.comment.text
272  end
273
274  def test_add_module
275    @c1.add_module RDoc::NormalModule, 'Mod'
276
277    assert_includes @c1.modules.map { |m| m.full_name }, 'C1::Mod'
278  end
279
280  def test_add_module_alias
281    tl = @store.add_file 'file.rb'
282
283    c4 = RDoc::Constant.new 'C4', '', ''
284    c3_c4 = @c2.add_module_alias @c2_c3, @c2_c3.name, c4, tl
285
286    alias_constant = @c2.constants.first
287
288    assert_equal 'C2::C4', c3_c4.full_name
289    assert_equal tl, alias_constant.file
290  end
291
292  def test_add_module_alias_top_level
293    store = RDoc::Store.new
294
295    top_level = store.add_file 'file.rb'
296
297    klass  = top_level.add_class RDoc::NormalClass, 'Klass'
298    klass.comment = 'klass comment'
299
300    object = top_level.add_class RDoc::NormalClass, 'Object'
301
302    a = RDoc::Constant.new 'A', '', ''
303    top_level.add_module_alias klass, klass.name, a, top_level
304
305    refute_empty object.constants
306
307    constant = object.constants.first
308
309    assert_equal 'klass comment', constant.comment
310  end
311
312  def test_add_module_class
313    k = @c1.add_class RDoc::NormalClass, 'Klass', nil
314    m = @c1.add_module RDoc::NormalModule, 'Klass'
315
316    assert_equal k, m, 'returns class'
317    assert_empty @c1.modules
318  end
319
320  def test_add_require
321    req = RDoc::Require.new 'require', 'comment'
322    @c1.add_require req
323
324    assert_empty @c1.requires
325    assert_includes @c1.top_level.requires, req
326  end
327
328  def test_add_section
329    default_section = @context.sections.first
330
331    @context.add_section nil, comment('comment', @top_level)
332
333    assert_equal 1, @context.sections.length
334    assert_equal [comment("comment", @top_level)],
335                 @context.sections.first.comments
336
337    @context.add_section nil, comment('new comment', @top_level)
338
339    assert_equal 1, @context.sections.length
340    assert_equal [comment('comment', @top_level),
341                  comment('new comment', @top_level)],
342                 @context.sections.first.comments
343
344    @context.add_section 'other', comment('', @top_level)
345
346    assert_equal 2, @context.sections.length
347
348    new_section = @context.sections.find { |section| section.title == 'other' }
349    assert new_section
350    assert_equal default_section, @context.current_section
351  end
352
353  def test_add_section_no_comment
354    default_section = @context.sections.first
355
356    @context.add_section nil
357
358    assert_equal 1, @context.sections.length
359
360    @context.add_section 'other'
361
362    assert_equal 2, @context.sections.length
363
364    new_section = @context.sections.find { |section| section.title == 'other' }
365
366    assert new_section
367    assert_equal default_section, @context.current_section
368  end
369
370  def test_add_to
371    incl = RDoc::Include.new 'Name', 'comment'
372    arr = []
373    @context.add_to arr, incl
374
375    assert_includes arr, incl
376    assert_equal @context, incl.parent
377    assert_equal @context.current_section, incl.section
378  end
379
380  def test_add_to_temporary_section
381    incl = RDoc::Include.new 'Name', 'comment'
382    arr = []
383    section =
384      @context.add_section 'temporary', RDoc::Comment.new('', @top_level)
385    @context.temporary_section = section
386
387    @context.add_to arr, incl
388
389    assert_includes arr, incl
390    assert_equal @context, incl.parent
391    assert_equal section, incl.section
392  end
393
394  def test_add_to_no_document_self
395    incl = RDoc::Include.new 'Name', 'comment'
396    arr = []
397    @context.document_self = false
398    @context.add_to arr, incl
399
400    refute_includes arr, incl
401  end
402
403  def test_add_to_done_documenting
404    incl = RDoc::Include.new 'Name', 'comment'
405    arr = []
406    @context.done_documenting = true
407    @context.add_to arr, incl
408
409    refute_includes arr, incl
410  end
411
412  def bench_add_include
413    cm = RDoc::ClassModule.new 'Klass'
414
415    assert_performance_linear 0.5 do |count|
416      count.times do |i|
417        cm.add_include RDoc::Include.new("N::M#{i}", nil)
418      end
419    end
420  end
421
422  def test_child_name
423    assert_equal 'C1::C1', @c1.child_name('C1')
424  end
425
426  def test_classes
427    assert_equal %w[C2::C3], @c2.classes.map { |k| k.full_name }
428    assert_equal %w[C3::H1 C3::H2], @c3.classes.map { |k| k.full_name }.sort
429  end
430
431  def test_current_section
432    default_section = @context.current_section
433
434    new_section =
435      @context.add_section 'other', RDoc::Comment.new('', @top_level)
436    @context.temporary_section = new_section
437
438    assert_equal new_section, @context.current_section
439    assert_equal default_section, @context.current_section
440  end
441
442  def test_defined_in_eh
443    assert @c1.defined_in?(@c1.top_level)
444
445    refute @c1.defined_in?(@store.add_file('name.rb'))
446  end
447
448  def test_equals2
449    assert_equal @c3,    @c3
450    refute_equal @c2,    @c3
451    refute_equal @c2_c3, @c3
452  end
453
454  def test_each_method_enumerator
455    assert_kind_of Enumerator, @c1.each_method
456  end
457
458  def test_each_section
459    sects  = []
460    consts = []
461    attrs  = []
462
463    @c1.each_section do |section, constants, attributes|
464      sects  << section
465      consts << constants
466      attrs  << attributes
467    end
468
469    assert_equal [nil, 'separate'], sects.map { |section| section.title }
470
471    expected_consts = [
472      [@c1.constants.first],
473      [],
474    ]
475
476    assert_equal expected_consts, consts
477
478    expected_attrs = [
479      [@c1.attributes[0], @c1.attributes[3]],
480      [@c1.attributes[1], @c1.attributes[2]],
481    ]
482
483    assert_equal expected_attrs, attrs
484  end
485
486  def test_each_section_only_display
487    sects  = []
488    consts = []
489    attrs  = []
490
491    @c7.each_section do |section, constants, attributes|
492      sects  << section
493      consts << constants
494      attrs  << attributes
495    end
496
497    assert_equal [nil], sects.map { |section| section.title }
498
499    expected_consts = [
500      @c7.constants.select(&:display?).sort
501    ]
502
503    assert_equal expected_consts, consts
504
505    expected_attrs = [
506      @c7.attributes.select(&:display?).sort
507    ]
508
509    assert_equal expected_attrs, attrs
510  end
511
512  def test_each_section_enumerator
513    assert_kind_of Enumerator, @c1.each_section
514  end
515
516  def test_find_attribute_named
517    assert_nil         @c1.find_attribute_named('none')
518    assert_equal 'R',  @c1.find_attribute_named('attr').rw
519    assert_equal 'R',  @c1.find_attribute_named('attr_reader').rw
520    assert_equal 'W',  @c1.find_attribute_named('attr_writer').rw
521    assert_equal 'RW', @c1.find_attribute_named('attr_accessor').rw
522  end
523
524  def test_find_class_method_named
525    assert_nil @c1.find_class_method_named('none')
526
527    m = @c1.find_class_method_named('m')
528    assert_instance_of RDoc::AnyMethod, m
529    assert m.singleton
530  end
531
532  def test_find_constant_named
533    assert_nil             @c1.find_constant_named('NONE')
534    assert_equal ':const', @c1.find_constant_named('CONST').value
535  end
536
537  def test_find_enclosing_module_named
538    assert_nil        @c2_c3.find_enclosing_module_named('NONE')
539    assert_equal @c1, @c2_c3.find_enclosing_module_named('C1')
540    assert_equal @c2, @c2_c3.find_enclosing_module_named('C2')
541  end
542
543  def test_find_file_named
544    assert_nil               @c1.find_file_named('nonexistent.rb')
545    assert_equal @xref_data, @c1.find_file_named(@file_name)
546  end
547
548  def test_find_instance_method_named
549    assert_nil @c1.find_instance_method_named('none')
550
551    m = @c1.find_instance_method_named('m')
552    assert_instance_of RDoc::AnyMethod, m
553    refute m.singleton
554  end
555
556  def test_find_local_symbol
557    assert_equal true,       @c1.find_local_symbol('m').singleton
558    assert_equal ':const',   @c1.find_local_symbol('CONST').value
559    assert_equal 'R',        @c1.find_local_symbol('attr').rw
560    assert_equal @xref_data, @c1.find_local_symbol(@file_name)
561    assert_equal @c2_c3,     @c2.find_local_symbol('C3')
562  end
563
564  def test_find_method
565    loaded_c2 = Marshal.load Marshal.dump @c2
566    assert_equal @c2_a, loaded_c2.find_method('a', false)
567    assert_equal @c2_b, loaded_c2.find_method('b', false)
568    assert_equal @c2_a, loaded_c2.find_method('a', nil)
569    assert_equal @c2_b, loaded_c2.find_method('b', nil)
570  end
571
572  def test_find_method_named
573    assert_equal true, @c1.find_method_named('m').singleton
574  end
575
576  def test_find_module_named
577    assert_equal @c2_c3, @c2.find_module_named('C3')
578    assert_equal @c2,    @c2.find_module_named('C2')
579    assert_equal @c1,    @c2.find_module_named('C1')
580
581    assert_equal 'C2::C3', @c2.find_module_named('C3').full_name
582  end
583
584  def test_find_symbol
585    c3 = @xref_data.find_module_named('C3')
586    assert_equal c3,     @xref_data.find_symbol('C3')
587    assert_equal c3,     @c2.find_symbol('::C3')
588    assert_equal @c2_c3, @c2.find_symbol('C3')
589  end
590
591  def test_find_symbol_method
592    assert_equal @c1__m, @c1.find_symbol('m')
593    assert_equal @c1_m,  @c1.find_symbol('#m')
594    assert_equal @c1__m, @c1.find_symbol('::m')
595  end
596
597  def test_find_symbol_module
598    assert_nil @m1_m2.find_symbol_module 'N'
599    assert_nil @m1_m2.find_symbol_module 'M2::M1'
600
601    @m1_m2.parent = nil # loaded from legacy ri store
602
603    assert_nil @m1_m2.find_symbol_module 'N'
604    assert_nil @m1_m2.find_symbol_module 'M2::M1'
605  end
606
607  def test_fully_documented_eh
608    context = RDoc::Context.new
609
610    refute context.fully_documented?
611
612    context.comment = 'hi'
613
614    assert context.fully_documented?
615
616    m = @c1_m
617
618    context.add_method m
619
620    refute context.fully_documented?
621
622    m.comment = 'hi'
623
624    assert context.fully_documented?
625
626    c = RDoc::Constant.new 'C', '0', nil
627
628    context.add_constant c
629
630    refute context.fully_documented?
631
632    c.comment = 'hi'
633
634    assert context.fully_documented?
635
636    a = RDoc::Attr.new '', 'a', 'RW', nil
637
638    context.add_attribute a
639
640    refute context.fully_documented?
641
642    a.comment = 'hi'
643
644    assert context.fully_documented?
645  end
646
647  def test_spaceship
648    assert_equal(-1, @c2.<=>(@c3))
649    assert_equal 0,  @c2.<=>(@c2)
650    assert_equal 1,  @c3.<=>(@c2)
651
652    assert_equal 1,  @c2_c3.<=>(@c2)
653    assert_equal(-1, @c2_c3.<=>(@c3))
654
655    assert_nil @c2.<=>(Gem.loaded_specs.values.first)
656  end
657
658  def test_methods_by_type
659    expected = {
660      'instance' => {
661        :private   => [],
662        :protected => [],
663        :public    => [@c1_plus, @c1_m],
664      },
665      'class' => {
666        :private   => [],
667        :protected => [],
668        :public    => [@c1__m],
669      },
670    }
671
672    assert_equal expected, @c1.methods_by_type
673  end
674
675  def test_methods_by_type_section
676    separate = @c1.sections_hash['separate']
677    @c1_m.section = separate
678
679    expected = {
680      'instance' => {
681        :private   => [],
682        :protected => [],
683        :public    => [@c1_m],
684      },
685      'class' => {
686        :private   => [],
687        :protected => [],
688        :public    => [],
689      },
690    }
691
692    assert_equal expected, @c1.methods_by_type(separate)
693  end
694
695  def test_methods_matching
696    methods = []
697
698    @parent.methods_matching 'm' do |m|
699      methods << m
700    end
701
702    assert_equal [@parent_m], methods
703  end
704
705  def test_methods_matching_singleton
706    methods = []
707
708    @parent.methods_matching 'm', true do |m|
709      methods << m
710    end
711
712    assert_equal [@parent__m], methods
713  end
714
715  def test_methods_matching_inherit
716    methods = []
717
718    @child.methods_matching 'm' do |m|
719      methods << m
720    end
721
722    assert_equal [@parent_m], methods
723  end
724
725  def test_remove_invisible_private
726    util_visibilities
727
728    @vis.remove_invisible :private
729
730    assert_equal [@pub, @prot, @priv], @vis.method_list
731    assert_equal [@apub, @aprot, @apriv], @vis.attributes
732    assert_equal [@cpub, @cpriv], @vis.constants
733  end
734
735  def test_remove_invisible_nodoc
736    util_visibilities
737
738    @vis.remove_invisible :nodoc
739
740    assert_equal [@pub, @prot, @priv], @vis.method_list
741    assert_equal [@apub, @aprot, @apriv], @vis.attributes
742    assert_equal [@cpub, @cpriv], @vis.constants
743  end
744
745  def test_remove_invisible_protected
746    util_visibilities
747
748    @vis.remove_invisible :protected
749
750    assert_equal [@pub, @prot], @vis.method_list
751    assert_equal [@apub, @aprot], @vis.attributes
752    assert_equal [@cpub], @vis.constants
753  end
754
755  def test_remove_invisible_public
756    util_visibilities
757
758    @vis.remove_invisible :public
759
760    assert_equal [@pub], @vis.method_list
761    assert_equal [@apub], @vis.attributes
762    assert_equal [@cpub], @vis.constants
763  end
764
765  def test_remove_invisible_public_force
766    util_visibilities
767
768    @priv.force_documentation = true
769    @prot.force_documentation = true
770    @apriv.force_documentation = true
771    @aprot.force_documentation = true
772    @cpriv.force_documentation = true
773
774    @vis.remove_invisible :public
775
776    assert_equal [@pub, @prot, @priv], @vis.method_list
777    assert_equal [@apub, @aprot, @apriv], @vis.attributes
778    assert_equal [@cpub, @cpriv], @vis.constants
779  end
780
781  def test_remove_invisible_in_protected
782    util_visibilities
783
784    methods = [@pub, @prot, @priv]
785
786    @c1.remove_invisible_in methods, :protected
787
788    assert_equal [@pub, @prot], methods
789  end
790
791  def test_remove_invisible_in_protected_force
792    util_visibilities
793
794    @priv.force_documentation = true
795
796    methods = [@pub, @prot, @priv]
797
798    @c1.remove_invisible_in methods, :protected
799
800    assert_equal [@pub, @prot, @priv], methods
801  end
802
803  def test_remove_invisible_in_public
804    util_visibilities
805
806    methods = [@pub, @prot, @priv]
807
808    @c1.remove_invisible_in methods, :public
809
810    assert_equal [@pub], methods
811  end
812
813  def test_remove_invisible_in_public_force
814    util_visibilities
815
816    @prot.force_documentation = true
817    @priv.force_documentation = true
818
819    methods = [@pub, @prot, @priv]
820
821    @c1.remove_invisible_in methods, :public
822
823    assert_equal [@pub, @prot, @priv], methods
824  end
825
826  def test_section_contents
827    default = @context.sections.first
828    @context.add_method RDoc::AnyMethod.new(nil, 'm1')
829
830    b = @context.add_section 'B'
831    m = @context.add_method RDoc::AnyMethod.new(nil, 'm2')
832    m.section = b
833
834    assert_equal [default, b], @context.section_contents
835  end
836
837  def test_section_contents_no_default
838    @context = RDoc::Context.new
839    b = @context.add_section 'B'
840    m = @context.add_method RDoc::AnyMethod.new(nil, 'm')
841    m.section = b
842
843    assert_equal [b], @context.section_contents
844  end
845
846  def test_section_contents_only_default
847    @context = RDoc::Context.new
848
849    @context.add_method RDoc::AnyMethod.new(nil, 'm')
850
851    assert_empty @context.section_contents
852  end
853
854  def test_section_contents_unused
855    @context = RDoc::Context.new
856
857    @context.add_method RDoc::AnyMethod.new(nil, 'm')
858    @context.add_section 'B'
859
860    assert_empty @context.section_contents
861  end
862
863  def test_set_current_section
864    default_section = @context.sections.first
865
866    @context.set_current_section nil, RDoc::Comment.new('', @top_level)
867
868    assert_equal default_section, @context.current_section
869
870    @context.set_current_section 'other', RDoc::Comment.new('', @top_level)
871
872    new_section = @context.sections.find { |section|
873      section != default_section
874    }
875
876    assert_equal new_section, @context.current_section
877  end
878
879  def test_sort_sections
880    c = RDoc::Context.new
881    c.add_section 'C'
882    c.add_section 'A'
883    c.add_section 'B'
884
885    titles = c.sort_sections.map { |section| section.title }
886
887    assert_equal [nil, 'A', 'B', 'C'], titles
888  end
889
890  def test_sort_sections_tomdoc
891    c = RDoc::Context.new
892    c.add_section 'Public'
893    c.add_section 'Internal'
894    c.add_section 'Deprecated'
895
896    titles = c.sort_sections.map { |section| section.title }
897
898    assert_equal [nil, 'Public', 'Internal', 'Deprecated'], titles
899  end
900
901  def test_sort_sections_tomdoc_missing
902    c = RDoc::Context.new
903    c.add_section 'Internal'
904    c.add_section 'Public'
905
906    titles = c.sort_sections.map { |section| section.title }
907
908    assert_equal [nil, 'Public', 'Internal'], titles
909  end
910
911  def test_visibility_def
912    assert_equal :private, @c6.find_method_named('priv1').visibility
913    assert_equal :protected, @c6.find_method_named('prot1').visibility
914    assert_equal :public, @c6.find_method_named('pub1').visibility
915    assert_equal :private, @c6.find_method_named('priv2').visibility
916    assert_equal :protected, @c6.find_method_named('prot2').visibility
917    assert_equal :public, @c6.find_method_named('pub2').visibility
918    assert_equal :private, @c6.find_method_named('priv3').visibility
919    assert_equal :protected, @c6.find_method_named('prot3').visibility
920    assert_equal :public, @c6.find_method_named('pub3').visibility
921    assert_equal :private, @c6.find_method_named('priv4').visibility
922    assert_equal :protected, @c6.find_method_named('prot4').visibility
923    assert_equal :public, @c6.find_method_named('pub4').visibility
924    assert_equal :private, @c6.find_method_named('priv5').visibility
925    assert_equal :protected, @c6.find_method_named('prot5').visibility
926    assert_equal :public, @c6.find_method_named('pub5').visibility
927    assert_equal :private, @c6.find_method_named('priv6').visibility
928    assert_equal :protected, @c6.find_method_named('prot6').visibility
929    assert_equal :public, @c6.find_method_named('pub6').visibility
930  end
931
932  def util_visibilities
933    @pub  = RDoc::AnyMethod.new nil, 'pub'
934    @prot = RDoc::AnyMethod.new nil, 'prot'
935    @priv = RDoc::AnyMethod.new nil, 'priv'
936
937    @apub  = RDoc::Attr.new nil, 'pub',  'RW', nil
938    @aprot = RDoc::Attr.new nil, 'prot', 'RW', nil
939    @apriv = RDoc::Attr.new nil, 'priv', 'RW', nil
940
941    @cpub  = RDoc::Constant.new 'CONST_PUBLIC', nil, nil
942    @cpriv = RDoc::Constant.new 'CONST_PRIVATE', nil, nil
943
944    @vis = RDoc::NormalClass.new 'Vis'
945    @vis.add_method @pub
946    @vis.add_method @prot
947    @vis.add_method @priv
948
949    @vis.add_attribute @apub
950    @vis.add_attribute @aprot
951    @vis.add_attribute @apriv
952
953    @vis.add_constant @cpub
954    @vis.add_constant @cpriv
955
956    @prot.visibility = :protected
957    @priv.visibility = :private
958
959    @aprot.visibility = :protected
960    @apriv.visibility = :private
961
962    @cpriv.visibility = :private
963  end
964
965end
966