~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/rdoc/test_rdoc_parser_ruby.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
    util_top_level
23
23
    @options = RDoc::Options.new
24
24
    @options.quiet = true
 
25
    @options.option_parser = OptionParser.new
 
26
 
25
27
    @stats = RDoc::Stats.new 0
26
28
  end
27
29
 
30
32
    @tempfile2.close
31
33
  end
32
34
 
 
35
  def test_collect_first_comment
 
36
    p = util_parser <<-CONTENT
 
37
# first
 
38
 
 
39
# second
 
40
class C; end
 
41
    CONTENT
 
42
 
 
43
    comment = p.collect_first_comment
 
44
 
 
45
    assert_equal "# first\n", comment
 
46
  end
 
47
 
 
48
  def test_collect_first_comment_encoding
 
49
    skip "Encoding not implemented" unless Object.const_defined? :Encoding
 
50
 
 
51
    @options.encoding = Encoding::CP852
 
52
 
 
53
    p = util_parser <<-CONTENT
 
54
# first
 
55
 
 
56
# second
 
57
class C; end
 
58
    CONTENT
 
59
 
 
60
    comment = p.collect_first_comment
 
61
 
 
62
    assert_equal Encoding::CP852, comment.encoding
 
63
  end
 
64
 
 
65
  def test_extract_call_seq
 
66
    m = RDoc::AnyMethod.new nil, 'm'
 
67
    p = util_parser ''
 
68
 
 
69
    comment = <<-COMMENT
 
70
  # call-seq:
 
71
  #   bla => true or false
 
72
  #
 
73
  # moar comment
 
74
    COMMENT
 
75
 
 
76
    p.extract_call_seq comment, m
 
77
 
 
78
    assert_equal "bla => true or false\n", m.call_seq
 
79
  end
 
80
 
 
81
  def test_extract_call_seq_blank
 
82
    m = RDoc::AnyMethod.new nil, 'm'
 
83
    p = util_parser ''
 
84
 
 
85
    comment = <<-COMMENT
 
86
  # call-seq:
 
87
  #   bla => true or false
 
88
  #
 
89
    COMMENT
 
90
 
 
91
    p.extract_call_seq comment, m
 
92
 
 
93
    assert_equal "bla => true or false\n", m.call_seq
 
94
  end
 
95
 
 
96
  def test_extract_call_seq_no_blank
 
97
    m = RDoc::AnyMethod.new nil, 'm'
 
98
    p = util_parser ''
 
99
 
 
100
    comment = <<-COMMENT
 
101
  # call-seq:
 
102
  #   bla => true or false
 
103
    COMMENT
 
104
 
 
105
    p.extract_call_seq comment, m
 
106
 
 
107
    assert_equal "bla => true or false\n", m.call_seq
 
108
  end
 
109
 
 
110
  def test_extract_call_seq_undent
 
111
    m = RDoc::AnyMethod.new nil, 'm'
 
112
    p = util_parser ''
 
113
 
 
114
    comment = <<-COMMENT
 
115
  # call-seq:
 
116
  #   bla => true or false
 
117
  # moar comment
 
118
    COMMENT
 
119
 
 
120
    p.extract_call_seq comment, m
 
121
 
 
122
    assert_equal "bla => true or false\nmoar comment\n", m.call_seq
 
123
  end
 
124
 
 
125
  def test_get_symbol_or_name
 
126
    util_parser "* & | + 5 / 4"
 
127
 
 
128
    assert_equal '*', @parser.get_symbol_or_name
 
129
 
 
130
    @parser.skip_tkspace
 
131
 
 
132
    assert_equal '&', @parser.get_symbol_or_name
 
133
 
 
134
    @parser.skip_tkspace
 
135
 
 
136
    assert_equal '|', @parser.get_symbol_or_name
 
137
 
 
138
    @parser.skip_tkspace
 
139
 
 
140
    assert_equal '+', @parser.get_symbol_or_name
 
141
 
 
142
    @parser.skip_tkspace
 
143
    @parser.get_tk
 
144
    @parser.skip_tkspace
 
145
 
 
146
    assert_equal '/', @parser.get_symbol_or_name
 
147
  end
 
148
 
33
149
  def test_look_for_directives_in_attr
34
150
    util_parser ""
35
151
 
52
168
    assert_equal "# :attr_writer: my_method\n", comment
53
169
  end
54
170
 
 
171
  def test_remove_private_comments
 
172
    util_parser ''
 
173
 
 
174
    comment = <<-EOS
 
175
# This is text
 
176
#--
 
177
# this is private
 
178
    EOS
 
179
 
 
180
    expected = <<-EOS
 
181
# This is text
 
182
    EOS
 
183
 
 
184
    @parser.remove_private_comments(comment)
 
185
 
 
186
    assert_equal expected, comment
 
187
  end
 
188
 
 
189
  def test_remove_private_comments_encoding
 
190
    skip "Encoding not implemented" unless Object.const_defined? :Encoding
 
191
 
 
192
    util_parser ''
 
193
 
 
194
    comment = <<-EOS
 
195
# This is text
 
196
#--
 
197
# this is private
 
198
    EOS
 
199
    comment.force_encoding Encoding::IBM437
 
200
 
 
201
    @parser.remove_private_comments comment
 
202
 
 
203
    assert_equal Encoding::IBM437, comment.encoding
 
204
  end
 
205
 
 
206
  def test_remove_private_comments_long
 
207
    util_parser ''
 
208
 
 
209
    comment = <<-EOS
 
210
#-----
 
211
#++
 
212
# this is text
 
213
#-----
 
214
    EOS
 
215
 
 
216
    expected = <<-EOS
 
217
# this is text
 
218
    EOS
 
219
 
 
220
    @parser.remove_private_comments(comment)
 
221
 
 
222
    assert_equal expected, comment
 
223
  end
 
224
 
 
225
  def test_remove_private_comments_rule
 
226
    util_parser ''
 
227
 
 
228
    comment = <<-EOS
 
229
# This is text with a rule:
 
230
# ---
 
231
# this is also text
 
232
    EOS
 
233
 
 
234
    expected = comment.dup
 
235
 
 
236
    @parser.remove_private_comments(comment)
 
237
 
 
238
    assert_equal expected, comment
 
239
  end
 
240
 
 
241
  def test_remove_private_comments_toggle
 
242
    util_parser ''
 
243
 
 
244
    comment = <<-EOS
 
245
# This is text
 
246
#--
 
247
# this is private
 
248
#++
 
249
# This is text again.
 
250
    EOS
 
251
 
 
252
    expected = <<-EOS
 
253
# This is text
 
254
# This is text again.
 
255
    EOS
 
256
 
 
257
    @parser.remove_private_comments(comment)
 
258
 
 
259
    assert_equal expected, comment
 
260
  end
 
261
 
 
262
  def test_remove_private_comments_toggle_encoding
 
263
    skip "Encoding not implemented" unless Object.const_defined? :Encoding
 
264
 
 
265
    util_parser ''
 
266
 
 
267
    comment = <<-EOS
 
268
# This is text
 
269
#--
 
270
# this is private
 
271
#++
 
272
# This is text again.
 
273
    EOS
 
274
 
 
275
    comment.force_encoding Encoding::IBM437
 
276
 
 
277
    @parser.remove_private_comments comment
 
278
 
 
279
    assert_equal Encoding::IBM437, comment.encoding
 
280
  end
 
281
 
 
282
  def test_remove_private_comments_toggle_encoding_ruby_bug?
 
283
    skip "Encoding not implemented" unless Object.const_defined? :Encoding
 
284
 
 
285
    util_parser ''
 
286
 
 
287
    comment = <<-EOS
 
288
#--
 
289
# this is private
 
290
#++
 
291
# This is text again.
 
292
    EOS
 
293
 
 
294
    comment.force_encoding Encoding::IBM437
 
295
 
 
296
    @parser.remove_private_comments comment
 
297
 
 
298
    assert_equal Encoding::IBM437, comment.encoding
 
299
  end
 
300
 
55
301
  def test_look_for_directives_in_commented
56
302
    util_parser ""
57
303
 
67
313
                 comment
68
314
  end
69
315
 
70
 
  def test_look_for_directives_in_enddoc
71
 
    util_parser ""
72
 
 
73
 
    assert_throws :enddoc do
74
 
      @parser.look_for_directives_in @top_level, "# :enddoc:\n"
75
 
    end
76
 
  end
77
 
 
78
 
  def test_look_for_directives_in_main
79
 
    util_parser ""
80
 
 
81
 
    @parser.look_for_directives_in @top_level, "# :main: new main page\n"
82
 
 
83
 
    assert_equal 'new main page', @options.main_page
84
 
  end
85
 
 
86
316
  def test_look_for_directives_in_method
87
317
    util_parser ""
88
318
 
99
329
    assert_equal "# :singleton-method: my_method\n", comment
100
330
  end
101
331
 
102
 
  def test_look_for_directives_in_startdoc
103
 
    util_parser ""
104
 
 
105
 
    @top_level.stop_doc
106
 
    assert !@top_level.document_self
107
 
    assert !@top_level.document_children
108
 
    assert !@top_level.force_documentation
109
 
 
110
 
    @parser.look_for_directives_in @top_level, "# :startdoc:\n"
111
 
 
112
 
    assert @top_level.document_self
113
 
    assert @top_level.document_children
114
 
    assert @top_level.force_documentation
115
 
  end
116
 
 
117
 
  def test_look_for_directives_in_stopdoc
118
 
    util_parser ""
119
 
 
120
 
    assert @top_level.document_self
121
 
    assert @top_level.document_children
122
 
 
123
 
    @parser.look_for_directives_in @top_level, "# :stopdoc:\n"
124
 
 
125
 
    assert !@top_level.document_self
126
 
    assert !@top_level.document_children
127
 
  end
128
 
 
129
332
  def test_look_for_directives_in_section
130
333
    util_parser ""
131
334
 
140
343
    assert_equal '', comment
141
344
  end
142
345
 
143
 
  def test_look_for_directives_in_title
144
 
    util_parser ""
145
 
 
146
 
    @parser.look_for_directives_in @top_level, "# :title: new title\n"
147
 
 
148
 
    assert_equal 'new title', @options.title
149
 
  end
150
 
 
151
346
  def test_look_for_directives_in_unhandled
152
347
    util_parser ""
153
348
 
166
361
 
167
362
    alas = @parser.parse_alias klass, RDoc::Parser::Ruby::NORMAL, tk, 'comment'
168
363
 
169
 
    assert_equal 'bar',     alas.old_name
170
 
    assert_equal 'next=',   alas.new_name
171
 
    assert_equal klass,     alas.parent
172
 
    assert_equal 'comment', alas.comment
 
364
    assert_equal 'bar',      alas.old_name
 
365
    assert_equal 'next=',    alas.new_name
 
366
    assert_equal klass,      alas.parent
 
367
    assert_equal 'comment',  alas.comment
 
368
    assert_equal @top_level, alas.file
 
369
    assert_equal 0,          alas.offset
 
370
    assert_equal 1,          alas.line
 
371
  end
 
372
 
 
373
  def test_parse_alias_singleton
 
374
    klass = RDoc::NormalClass.new 'Foo'
 
375
    klass.parent = @top_level
 
376
 
 
377
    util_parser "alias :next= :bar"
 
378
 
 
379
    tk = @parser.get_tk
 
380
 
 
381
    alas = @parser.parse_alias klass, RDoc::Parser::Ruby::SINGLE, tk, 'comment'
 
382
 
 
383
    assert_equal 'bar',      alas.old_name
 
384
    assert_equal 'next=',    alas.new_name
 
385
    assert_equal klass,      alas.parent
 
386
    assert_equal 'comment',  alas.comment
 
387
    assert_equal @top_level, alas.file
 
388
    assert                   alas.singleton
 
389
  end
 
390
 
 
391
  def test_parse_alias_stopdoc
 
392
    klass = RDoc::NormalClass.new 'Foo'
 
393
    klass.parent = @top_level
 
394
    klass.stop_doc
 
395
 
 
396
    util_parser "alias :next= :bar"
 
397
 
 
398
    tk = @parser.get_tk
 
399
 
 
400
    @parser.parse_alias klass, RDoc::Parser::Ruby::NORMAL, tk, 'comment'
 
401
 
 
402
    assert_empty klass.aliases
 
403
    assert_empty klass.unmatched_alias_lists
173
404
  end
174
405
 
175
406
  def test_parse_alias_meta
202
433
    foo = klass.attributes.first
203
434
    assert_equal 'foo', foo.name
204
435
    assert_equal 'my attr', foo.comment
 
436
    assert_equal @top_level, foo.file
 
437
    assert_equal 0, foo.offset
 
438
    assert_equal 1, foo.line
 
439
  end
 
440
 
 
441
  def test_parse_attr_stopdoc
 
442
    klass = RDoc::NormalClass.new 'Foo'
 
443
    klass.parent = @top_level
 
444
    klass.stop_doc
 
445
 
 
446
    comment = "##\n# my attr\n"
 
447
 
 
448
    util_parser "attr :foo, :bar"
 
449
 
 
450
    tk = @parser.get_tk
 
451
 
 
452
    @parser.parse_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
453
 
 
454
    assert_empty klass.attributes
205
455
  end
206
456
 
207
457
  def test_parse_attr_accessor
222
472
    assert_equal 'foo', foo.name
223
473
    assert_equal 'RW', foo.rw
224
474
    assert_equal 'my attr', foo.comment
 
475
    assert_equal @top_level, foo.file
 
476
    assert_equal 0, foo.offset
 
477
    assert_equal 1, foo.line
225
478
 
226
479
    bar = klass.attributes.last
227
480
    assert_equal 'bar', bar.name
229
482
    assert_equal 'my attr', bar.comment
230
483
  end
231
484
 
 
485
  def test_parse_attr_accessor_nodoc
 
486
    klass = RDoc::NormalClass.new 'Foo'
 
487
    klass.parent = @top_level
 
488
 
 
489
    comment = "##\n# my attr\n"
 
490
 
 
491
    util_parser "attr_accessor :foo, :bar # :nodoc:"
 
492
 
 
493
    tk = @parser.get_tk
 
494
 
 
495
    @parser.parse_attr_accessor klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
496
 
 
497
    assert_equal 0, klass.attributes.length
 
498
  end
 
499
 
 
500
  def test_parse_attr_accessor_stopdoc
 
501
    klass = RDoc::NormalClass.new 'Foo'
 
502
    klass.parent = @top_level
 
503
    klass.stop_doc
 
504
 
 
505
    comment = "##\n# my attr\n"
 
506
 
 
507
    util_parser "attr_accessor :foo, :bar"
 
508
 
 
509
    tk = @parser.get_tk
 
510
 
 
511
    @parser.parse_attr_accessor klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
512
 
 
513
    assert_empty klass.attributes
 
514
  end
 
515
 
232
516
  def test_parse_attr_accessor_writer
233
517
    klass = RDoc::NormalClass.new 'Foo'
234
518
    klass.parent = @top_level
247
531
    assert_equal 'foo', foo.name
248
532
    assert_equal 'W', foo.rw
249
533
    assert_equal "my attr", foo.comment
 
534
    assert_equal @top_level, foo.file
250
535
 
251
536
    bar = klass.attributes.last
252
537
    assert_equal 'bar', bar.name
271
556
    assert_equal 'foo', foo.name
272
557
    assert_equal 'RW', foo.rw
273
558
    assert_equal "my method", foo.comment
 
559
    assert_equal @top_level, foo.file
274
560
  end
275
561
 
276
562
  def test_parse_meta_attr_accessor
290
576
    assert_equal 'foo', foo.name
291
577
    assert_equal 'RW', foo.rw
292
578
    assert_equal 'my method', foo.comment
 
579
    assert_equal @top_level, foo.file
293
580
  end
294
581
 
295
582
  def test_parse_meta_attr_named
309
596
    assert_equal 'foo', foo.name
310
597
    assert_equal 'RW', foo.rw
311
598
    assert_equal 'my method', foo.comment
 
599
    assert_equal @top_level, foo.file
312
600
  end
313
601
 
314
602
  def test_parse_meta_attr_reader
327
615
    assert_equal 'foo', foo.name
328
616
    assert_equal 'R', foo.rw
329
617
    assert_equal 'my method', foo.comment
 
618
    assert_equal @top_level, foo.file
 
619
  end
 
620
 
 
621
  def test_parse_meta_attr_stopdoc
 
622
    klass = RDoc::NormalClass.new 'Foo'
 
623
    klass.parent = @top_level
 
624
    klass.stop_doc
 
625
 
 
626
    comment = "##\n# :attr: \n# my method\n"
 
627
 
 
628
    util_parser "add_my_method :foo, :bar"
 
629
 
 
630
    tk = @parser.get_tk
 
631
 
 
632
    @parser.parse_meta_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
633
 
 
634
    assert_empty klass.attributes
330
635
  end
331
636
 
332
637
  def test_parse_meta_attr_writer
345
650
    assert_equal 'foo', foo.name
346
651
    assert_equal 'W', foo.rw
347
652
    assert_equal "my method", foo.comment
 
653
    assert_equal @top_level, foo.file
348
654
  end
349
655
 
350
656
  def test_parse_class
351
 
    comment = "##\n# my method\n"
 
657
    comment = "##\n# my class\n"
352
658
 
353
 
    util_parser 'class Foo; end'
 
659
    util_parser "class Foo\nend"
354
660
 
355
661
    tk = @parser.get_tk
356
662
 
358
664
 
359
665
    foo = @top_level.classes.first
360
666
    assert_equal 'Foo', foo.full_name
361
 
    assert_equal 'my method', foo.comment
 
667
    assert_equal 'my class', foo.comment
 
668
    assert_equal [@top_level], foo.in_files
 
669
    assert_equal 0, foo.offset
 
670
    assert_equal 1, foo.line
362
671
  end
363
672
 
364
673
  def test_parse_class_ghost_method
379
688
 
380
689
    blah = foo.method_list.first
381
690
    assert_equal 'Foo#blah', blah.full_name
 
691
    assert_equal @top_level, blah.file
 
692
  end
 
693
 
 
694
  def test_parse_class_multi_ghost_methods
 
695
    util_parser <<-'CLASS'
 
696
class Foo
 
697
  ##
 
698
  # :method: one
 
699
  #
 
700
  # my method
 
701
 
 
702
  ##
 
703
  # :method: two
 
704
  #
 
705
  # my method
 
706
 
 
707
  [:one, :two].each do |t|
 
708
    eval("def #{t}; \"#{t}\"; end")
 
709
  end
 
710
end
 
711
    CLASS
 
712
 
 
713
    tk = @parser.get_tk
 
714
 
 
715
    @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
 
716
 
 
717
    foo = @top_level.classes.first
 
718
    assert_equal 'Foo', foo.full_name
 
719
 
 
720
    assert_equal 2, foo.method_list.length
 
721
  end
 
722
 
 
723
  def test_parse_class_nodoc
 
724
    comment = "##\n# my class\n"
 
725
 
 
726
    util_parser "class Foo # :nodoc:\nend"
 
727
 
 
728
    tk = @parser.get_tk
 
729
 
 
730
    @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
 
731
 
 
732
    foo = @top_level.classes.first
 
733
    assert_equal 'Foo', foo.full_name
 
734
    assert_empty foo.comment
 
735
    assert_equal [@top_level], foo.in_files
 
736
    assert_equal 0, foo.offset
 
737
    assert_equal 1, foo.line
 
738
  end
 
739
 
 
740
  def test_parse_class_stopdoc
 
741
    @top_level.stop_doc
 
742
 
 
743
    comment = "##\n# my class\n"
 
744
 
 
745
    util_parser "class Foo\nend"
 
746
 
 
747
    tk = @parser.get_tk
 
748
 
 
749
    @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
 
750
 
 
751
    assert_empty @top_level.classes.first.comment
 
752
  end
 
753
 
 
754
  def test_parse_multi_ghost_methods
 
755
    util_parser <<-'CLASS'
 
756
class Foo
 
757
  ##
 
758
  # :method: one
 
759
  #
 
760
  # my method
 
761
 
 
762
  ##
 
763
  # :method: two
 
764
  #
 
765
  # my method
 
766
 
 
767
  [:one, :two].each do |t|
 
768
    eval("def #{t}; \"#{t}\"; end")
 
769
  end
 
770
end
 
771
    CLASS
 
772
 
 
773
    tk = @parser.get_tk
 
774
 
 
775
    @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
 
776
 
 
777
    foo = @top_level.classes.first
 
778
    assert_equal 'Foo', foo.full_name
 
779
 
 
780
    assert_equal 2, foo.method_list.length
 
781
  end
 
782
 
 
783
  def test_parse_const_fail_w_meta
 
784
    util_parser <<-CLASS
 
785
class ConstFailMeta
 
786
  ##
 
787
  # :attr: one
 
788
  #
 
789
  # an attribute
 
790
 
 
791
  OtherModule.define_attr(self, :one)
 
792
end
 
793
    CLASS
 
794
 
 
795
    tk = @parser.get_tk
 
796
 
 
797
    @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
 
798
 
 
799
    const_fail_meta = @top_level.classes.first
 
800
    assert_equal 'ConstFailMeta', const_fail_meta.full_name
 
801
 
 
802
    assert_equal 1, const_fail_meta.attributes.length
382
803
  end
383
804
 
384
805
  def test_parse_class_nested_superclass
385
 
    foo = RDoc::NormalModule.new 'Foo'
386
 
    foo.parent = @top_level
 
806
    util_top_level
 
807
    foo = @top_level.add_module RDoc::NormalModule, 'Foo'
387
808
 
388
809
    util_parser "class Bar < Super\nend"
389
810
 
398
819
  def test_parse_module
399
820
    comment = "##\n# my module\n"
400
821
 
401
 
    util_parser 'module Foo; end'
 
822
    util_parser "module Foo\nend"
 
823
 
 
824
    tk = @parser.get_tk
 
825
 
 
826
    @parser.parse_module @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
 
827
 
 
828
    foo = @top_level.modules.first
 
829
    assert_equal 'Foo', foo.full_name
 
830
    assert_equal 'my module', foo.comment
 
831
  end
 
832
 
 
833
  def test_parse_module_nodoc
 
834
    @top_level.stop_doc
 
835
 
 
836
    comment = "##\n# my module\n"
 
837
 
 
838
    util_parser "module Foo # :nodoc:\nend"
 
839
 
 
840
    tk = @parser.get_tk
 
841
 
 
842
    @parser.parse_module @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
 
843
 
 
844
    foo = @top_level.modules.first
 
845
    assert_equal 'Foo', foo.full_name
 
846
    assert_empty foo.comment
 
847
  end
 
848
 
 
849
  def test_parse_module_stopdoc
 
850
    @top_level.stop_doc
 
851
 
 
852
    comment = "##\n# my module\n"
 
853
 
 
854
    util_parser "module Foo\nend"
402
855
 
403
856
    tk = @parser.get_tk
404
857
 
430
883
  class << B
431
884
  end
432
885
  class << d = Object.new
 
886
    def foo; end
 
887
    alias bar foo
433
888
  end
434
889
end
435
890
    CODE
439
894
    @parser.parse_class @top_level, false, @parser.get_tk, ''
440
895
 
441
896
    assert_equal %w[A],    RDoc::TopLevel.classes.map { |c| c.full_name }
442
 
    assert_equal %w[A::B], RDoc::TopLevel.modules.map { |c| c.full_name }
 
897
    assert_equal %w[A::B A::d], RDoc::TopLevel.modules.map { |c| c.full_name }
 
898
 
 
899
    b = RDoc::TopLevel.modules.first
 
900
    assert_equal 10, b.offset
 
901
    assert_equal 2,  b.line
 
902
 
 
903
    # make sure method/alias was not added to enclosing class/module
 
904
    a = RDoc::TopLevel.all_classes_hash['A']
 
905
    assert_empty a.method_list
 
906
 
 
907
    # make sure non-constant-named module will be removed from documentation
 
908
    d = RDoc::TopLevel.all_modules_hash['A::d']
 
909
    assert d.remove_from_documentation?
 
910
 
 
911
  end
 
912
 
 
913
  # TODO this is really a Context#add_class test
 
914
  def test_parse_class_object
 
915
    code = <<-CODE
 
916
module A
 
917
  class B
 
918
  end
 
919
  class Object
 
920
  end
 
921
  class C < Object
 
922
  end
 
923
end
 
924
    CODE
 
925
 
 
926
    util_parser code
 
927
 
 
928
    @parser.parse_module @top_level, false, @parser.get_tk, ''
 
929
 
 
930
    assert_equal %w[A],    RDoc::TopLevel.modules.map { |c| c.full_name }
 
931
    assert_equal %w[A::B A::C A::Object], RDoc::TopLevel.classes.map { |c| c.full_name }.sort
 
932
    assert_equal 'Object', RDoc::TopLevel.classes_hash['A::B'].superclass
 
933
    assert_equal 'Object', RDoc::TopLevel.classes_hash['A::Object'].superclass
 
934
    assert_equal 'A::Object', RDoc::TopLevel.classes_hash['A::C'].superclass.full_name
443
935
  end
444
936
 
445
937
  def test_parse_class_mistaken_for_module
447
939
    # before Foo::Bar is encountered), but RDoc might encounter Foo::Bar
448
940
    # before Foo if they live in different files.
449
941
 
450
 
    code = <<-EOF
 
942
    code = <<-RUBY
451
943
class Foo::Bar
452
944
end
453
945
 
456
948
 
457
949
class Foo
458
950
end
459
 
    EOF
 
951
    RUBY
460
952
 
461
953
    util_parser code
462
954
 
542
1034
    @parser.parse_comment klass, tk, comment
543
1035
 
544
1036
    foo = klass.attributes.first
545
 
    assert_equal 'foo',     foo.name
546
 
    assert_equal 'RW',      foo.rw
547
 
    assert_equal 'my attr',   foo.comment
548
 
 
549
 
    assert_equal nil,       foo.viewer
550
 
    assert_equal true,      foo.document_children
551
 
    assert_equal true,      foo.document_self
552
 
    assert_equal false,     foo.done_documenting
553
 
    assert_equal false,     foo.force_documentation
554
 
    assert_equal klass,     foo.parent
555
 
    assert_equal :public,   foo.visibility
556
 
    assert_equal "\n",      foo.text
 
1037
    assert_equal 'foo',      foo.name
 
1038
    assert_equal 'RW',       foo.rw
 
1039
    assert_equal 'my attr',  foo.comment
 
1040
    assert_equal @top_level, foo.file
 
1041
    assert_equal 0,          foo.offset
 
1042
    assert_equal 1,          foo.line
 
1043
 
 
1044
    assert_equal nil,        foo.viewer
 
1045
    assert_equal true,       foo.document_children
 
1046
    assert_equal true,       foo.document_self
 
1047
    assert_equal false,      foo.done_documenting
 
1048
    assert_equal false,      foo.force_documentation
 
1049
    assert_equal klass,      foo.parent
 
1050
    assert_equal :public,    foo.visibility
 
1051
    assert_equal "\n",       foo.text
 
1052
 
557
1053
    assert_equal klass.current_section, foo.section
558
1054
  end
559
1055
 
 
1056
  def test_parse_comment_attr_stopdoc
 
1057
    klass = RDoc::NormalClass.new 'Foo'
 
1058
    klass.parent = @top_level
 
1059
    klass.stop_doc
 
1060
 
 
1061
    comment = "##\n# :attr: foo\n# my attr\n"
 
1062
 
 
1063
    util_parser "\n"
 
1064
 
 
1065
    tk = @parser.get_tk
 
1066
 
 
1067
    @parser.parse_comment klass, tk, comment
 
1068
 
 
1069
    assert_empty klass.attributes
 
1070
  end
 
1071
 
560
1072
  def test_parse_comment_method
561
1073
    klass = RDoc::NormalClass.new 'Foo'
562
1074
    klass.parent = @top_level
570
1082
    @parser.parse_comment klass, tk, comment
571
1083
 
572
1084
    foo = klass.method_list.first
573
 
    assert_equal 'foo',     foo.name
574
 
    assert_equal 'my method',   foo.comment
 
1085
    assert_equal 'foo',       foo.name
 
1086
    assert_equal 'my method', foo.comment
 
1087
    assert_equal @top_level,  foo.file
 
1088
    assert_equal 0,           foo.offset
 
1089
    assert_equal 1,           foo.line
575
1090
 
576
1091
    assert_equal [],        foo.aliases
577
1092
    assert_equal nil,       foo.block_params
599
1114
    assert_equal stream, foo.token_stream
600
1115
  end
601
1116
 
 
1117
  def test_parse_comment_method_stopdoc
 
1118
    klass = RDoc::NormalClass.new 'Foo'
 
1119
    klass.parent = @top_level
 
1120
    klass.stop_doc
 
1121
 
 
1122
    comment = "##\n# :method: foo\n# my method\n"
 
1123
 
 
1124
    util_parser "\n"
 
1125
 
 
1126
    tk = @parser.get_tk
 
1127
 
 
1128
    @parser.parse_comment klass, tk, comment
 
1129
 
 
1130
    assert_empty klass.method_list
 
1131
  end
 
1132
 
 
1133
  def test_parse_constant
 
1134
    util_top_level
 
1135
 
 
1136
    klass = @top_level.add_class RDoc::NormalClass, 'Foo'
 
1137
 
 
1138
    util_parser "A = v"
 
1139
 
 
1140
    tk = @parser.get_tk
 
1141
 
 
1142
    @parser.parse_constant klass, tk, ''
 
1143
 
 
1144
    foo = klass.constants.first
 
1145
 
 
1146
    assert_equal 'A', foo.name
 
1147
    assert_equal @top_level, foo.file
 
1148
    assert_equal 0, foo.offset
 
1149
    assert_equal 1, foo.line
 
1150
  end
 
1151
 
 
1152
  def test_parse_constant_attrasgn
 
1153
    util_top_level
 
1154
 
 
1155
    klass = @top_level.add_class RDoc::NormalClass, 'Foo'
 
1156
 
 
1157
    util_parser "A[k] = v"
 
1158
 
 
1159
    tk = @parser.get_tk
 
1160
 
 
1161
    @parser.parse_constant klass, tk, ''
 
1162
 
 
1163
    assert klass.constants.empty?
 
1164
  end
 
1165
 
602
1166
  def test_parse_constant_alias
603
 
    klass = RDoc::NormalClass.new 'Foo'
604
 
    klass.parent = @top_level
 
1167
    util_top_level
 
1168
    klass = @top_level.add_class RDoc::NormalClass, 'Foo'
605
1169
    cB = klass.add_class RDoc::NormalClass, 'B'
606
1170
 
607
1171
    util_parser "A = B"
629
1193
    assert_equal top_bar, bar.find_module_named('A')
630
1194
  end
631
1195
 
 
1196
  def test_parse_constant_stopdoc
 
1197
    util_top_level
 
1198
 
 
1199
    klass = @top_level.add_class RDoc::NormalClass, 'Foo'
 
1200
    klass.stop_doc
 
1201
 
 
1202
    util_parser "A = v"
 
1203
 
 
1204
    tk = @parser.get_tk
 
1205
 
 
1206
    @parser.parse_constant klass, tk, ''
 
1207
 
 
1208
    assert_empty klass.constants
 
1209
  end
 
1210
 
 
1211
  def test_parse_include
 
1212
    klass = RDoc::NormalClass.new 'C'
 
1213
    klass.parent = @top_level
 
1214
 
 
1215
    comment = "# my include\n"
 
1216
 
 
1217
    util_parser "include I"
 
1218
 
 
1219
    @parser.get_tk # include
 
1220
 
 
1221
    @parser.parse_include klass, comment
 
1222
 
 
1223
    assert_equal 1, klass.includes.length
 
1224
 
 
1225
    incl = klass.includes.first
 
1226
    assert_equal 'I', incl.name
 
1227
    assert_equal 'my include', incl.comment
 
1228
    assert_equal @top_level, incl.file
 
1229
  end
 
1230
 
632
1231
  def test_parse_meta_method
633
1232
    klass = RDoc::NormalClass.new 'Foo'
634
1233
    klass.parent = @top_level
642
1241
    @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
643
1242
 
644
1243
    foo = klass.method_list.first
645
 
    assert_equal 'foo', foo.name
 
1244
    assert_equal 'foo',       foo.name
646
1245
    assert_equal 'my method', foo.comment
 
1246
    assert_equal @top_level,  foo.file
 
1247
    assert_equal 0,           foo.offset
 
1248
    assert_equal 1,           foo.line
647
1249
 
648
1250
    assert_equal [],      foo.aliases
649
1251
    assert_equal nil,     foo.block_params
678
1280
    assert_equal stream, foo.token_stream
679
1281
  end
680
1282
 
 
1283
  def test_parse_meta_method_block
 
1284
    klass = RDoc::NormalClass.new 'Foo'
 
1285
    klass.parent = @top_level
 
1286
 
 
1287
    comment = "##\n# my method\n"
 
1288
 
 
1289
    content = <<-CONTENT
 
1290
inline(:my_method) do |*args|
 
1291
  "this method causes z to disappear"
 
1292
end
 
1293
    CONTENT
 
1294
 
 
1295
    util_parser content
 
1296
 
 
1297
    tk = @parser.get_tk
 
1298
 
 
1299
    @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
1300
 
 
1301
    assert_nil @parser.get_tk
 
1302
  end
 
1303
 
681
1304
  def test_parse_meta_method_name
682
1305
    klass = RDoc::NormalClass.new 'Foo'
683
1306
    klass.parent = @top_level
691
1314
    @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
692
1315
 
693
1316
    foo = klass.method_list.first
694
 
    assert_equal 'woo_hoo!', foo.name
 
1317
    assert_equal 'woo_hoo!',  foo.name
695
1318
    assert_equal 'my method', foo.comment
 
1319
    assert_equal @top_level,  foo.file
696
1320
  end
697
1321
 
698
1322
  def test_parse_meta_method_singleton
711
1335
    assert_equal 'foo', foo.name
712
1336
    assert_equal true, foo.singleton, 'singleton method'
713
1337
    assert_equal 'my method', foo.comment
 
1338
    assert_equal @top_level,  foo.file
714
1339
  end
715
1340
 
716
1341
  def test_parse_meta_method_singleton_name
729
1354
    assert_equal 'woo_hoo!', foo.name
730
1355
    assert_equal true, foo.singleton, 'singleton method'
731
1356
    assert_equal 'my method', foo.comment
 
1357
    assert_equal @top_level,  foo.file
732
1358
  end
733
1359
 
734
1360
  def test_parse_meta_method_string_name
744
1370
    foo = klass.method_list.first
745
1371
    assert_equal 'foo', foo.name
746
1372
    assert_equal 'my method', foo.comment
 
1373
    assert_equal @top_level,  foo.file
 
1374
  end
 
1375
 
 
1376
  def test_parse_meta_method_stopdoc
 
1377
    klass = RDoc::NormalClass.new 'Foo'
 
1378
    klass.parent = @top_level
 
1379
    klass.stop_doc
 
1380
 
 
1381
    comment = "##\n# my method\n"
 
1382
 
 
1383
    util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
 
1384
 
 
1385
    tk = @parser.get_tk
 
1386
 
 
1387
    @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
1388
 
 
1389
    assert_empty klass.method_list
747
1390
  end
748
1391
 
749
1392
  def test_parse_meta_method_unknown
759
1402
    foo = klass.method_list.first
760
1403
    assert_equal 'unknown', foo.name
761
1404
    assert_equal 'my method', foo.comment
 
1405
    assert_equal @top_level,  foo.file
762
1406
  end
763
1407
 
764
1408
  def test_parse_method
774
1418
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
775
1419
 
776
1420
    foo = klass.method_list.first
777
 
    assert_equal 'foo',     foo.name
778
 
    assert_equal 'my method',   foo.comment
 
1421
    assert_equal 'foo',       foo.name
 
1422
    assert_equal 'my method', foo.comment
 
1423
    assert_equal @top_level,  foo.file
 
1424
    assert_equal 0,           foo.offset
 
1425
    assert_equal 1,           foo.line
779
1426
 
780
1427
    assert_equal [],        foo.aliases
781
1428
    assert_equal nil,       foo.block_params
826
1473
    assert klass.aliases.empty?
827
1474
  end
828
1475
 
829
 
  def test_parse_method_utf8
830
 
    klass = RDoc::NormalClass.new 'Foo'
831
 
    klass.parent = @top_level
832
 
 
833
 
    comment = "##\n# my method\n"
834
 
 
835
 
    method = "def ω() end"
836
 
 
837
 
    assert_equal Encoding::UTF_8, method.encoding if defined? ::Encoding
838
 
 
839
 
    util_parser method
 
1476
  def test_parse_method_false
 
1477
    util_parser "def false.foo() :bar end"
840
1478
 
841
1479
    tk = @parser.get_tk
842
1480
 
843
 
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
844
 
 
845
 
    omega = klass.method_list.first
846
 
    assert_equal "def \317\211", omega.text
 
1481
    @parser.parse_method @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
 
1482
 
 
1483
    klass = RDoc::TopLevel.find_class_named 'FalseClass'
 
1484
 
 
1485
    foo = klass.method_list.first
 
1486
    assert_equal 'foo', foo.name
847
1487
  end
848
1488
 
849
1489
  def test_parse_method_funky
850
1490
    klass = RDoc::NormalClass.new 'Foo'
851
1491
    klass.parent = @top_level
852
1492
 
853
 
    comment = "##\n# my method\n"
854
 
 
855
1493
    util_parser "def (blah).foo() :bar end"
856
1494
 
857
1495
    tk = @parser.get_tk
858
1496
 
859
 
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
1497
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
860
1498
 
861
1499
    assert klass.method_list.empty?
862
1500
  end
863
1501
 
 
1502
  def test_parse_method_gvar
 
1503
    util_parser "def $stdout.foo() :bar end"
 
1504
 
 
1505
    tk = @parser.get_tk
 
1506
 
 
1507
    @parser.parse_method @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
 
1508
 
 
1509
    assert @top_level.method_list.empty?
 
1510
  end
 
1511
 
 
1512
  def test_parse_method_internal_gvar
 
1513
    klass = RDoc::NormalClass.new 'Foo'
 
1514
    klass.parent = @top_level
 
1515
 
 
1516
    util_parser "def foo() def $blah.bar() end end"
 
1517
 
 
1518
    tk = @parser.get_tk
 
1519
 
 
1520
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
 
1521
 
 
1522
    assert_equal 1, klass.method_list.length
 
1523
  end
 
1524
 
864
1525
  def test_parse_method_internal_ivar
865
1526
    klass = RDoc::NormalClass.new 'Foo'
866
1527
    klass.parent = @top_level
887
1548
    assert_equal 1, klass.method_list.length
888
1549
  end
889
1550
 
 
1551
  def test_parse_method_nil
 
1552
    util_parser "def nil.foo() :bar end"
 
1553
 
 
1554
    tk = @parser.get_tk
 
1555
 
 
1556
    @parser.parse_method @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
 
1557
 
 
1558
    klass = RDoc::TopLevel.find_class_named 'NilClass'
 
1559
 
 
1560
    foo = klass.method_list.first
 
1561
    assert_equal 'foo', foo.name
 
1562
  end
 
1563
 
890
1564
  def test_parse_method_no_parens
891
1565
    klass = RDoc::NormalClass.new 'Foo'
892
1566
    klass.parent = @top_level
893
1567
 
894
 
    comment = "##\n# my method\n"
895
 
 
896
 
    util_parser "def foo arg1, arg2\nend"
 
1568
    util_parser "def foo arg1, arg2 = {}\nend"
897
1569
 
898
1570
    tk = @parser.get_tk
899
1571
 
900
 
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
1572
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
901
1573
 
902
1574
    foo = klass.method_list.first
903
 
    assert_equal '(arg1, arg2)', foo.params
 
1575
    assert_equal '(arg1, arg2 = {})', foo.params
 
1576
    assert_equal @top_level, foo.file
904
1577
  end
905
1578
 
906
1579
  def test_parse_method_parameters_comment
907
1580
    klass = RDoc::NormalClass.new 'Foo'
908
1581
    klass.parent = @top_level
909
1582
 
910
 
    comment = "##\n# my method\n"
911
 
 
912
1583
    util_parser "def foo arg1, arg2 # some useful comment\nend"
913
1584
 
914
1585
    tk = @parser.get_tk
915
1586
 
916
 
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
1587
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
917
1588
 
918
1589
    foo = klass.method_list.first
919
1590
    assert_equal '(arg1, arg2)', foo.params
923
1594
    klass = RDoc::NormalClass.new 'Foo'
924
1595
    klass.parent = @top_level
925
1596
 
926
 
    comment = "##\n# my method\n"
927
 
 
928
1597
    util_parser "def foo arg1, arg2, # some useful comment\narg3\nend"
929
1598
 
930
1599
    tk = @parser.get_tk
931
1600
 
932
 
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
1601
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
933
1602
 
934
1603
    foo = klass.method_list.first
935
1604
    assert_equal '(arg1, arg2, arg3)', foo.params
936
1605
  end
937
1606
 
 
1607
  def test_parse_method_stopdoc
 
1608
    klass = RDoc::NormalClass.new 'Foo'
 
1609
    klass.parent = @top_level
 
1610
    klass.stop_doc
 
1611
 
 
1612
    comment = "##\n# my method\n"
 
1613
 
 
1614
    util_parser "def foo() :bar end"
 
1615
 
 
1616
    tk = @parser.get_tk
 
1617
 
 
1618
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
1619
 
 
1620
    assert_empty klass.method_list
 
1621
  end
 
1622
 
938
1623
  def test_parse_method_toplevel
939
1624
    klass = @top_level
940
1625
 
941
 
    comment = "##\n# my method\n"
942
 
 
943
1626
    util_parser "def foo arg1, arg2\nend"
944
1627
 
945
1628
    tk = @parser.get_tk
946
1629
 
947
 
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
 
1630
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
948
1631
 
949
1632
    object = RDoc::TopLevel.find_class_named 'Object'
950
1633
 
951
1634
    foo = object.method_list.first
952
1635
    assert_equal 'Object#foo', foo.full_name
 
1636
    assert_equal @top_level, foo.file
953
1637
  end
954
1638
 
955
1639
  def test_parse_method_toplevel_class
967
1651
    assert_equal 'Object::foo', foo.full_name
968
1652
  end
969
1653
 
 
1654
  def test_parse_method_true
 
1655
    util_parser "def true.foo() :bar end"
 
1656
 
 
1657
    tk = @parser.get_tk
 
1658
 
 
1659
    @parser.parse_method @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
 
1660
 
 
1661
    klass = RDoc::TopLevel.find_class_named 'TrueClass'
 
1662
 
 
1663
    foo = klass.method_list.first
 
1664
    assert_equal 'foo', foo.name
 
1665
  end
 
1666
 
 
1667
  def test_parse_method_utf8
 
1668
    klass = RDoc::NormalClass.new 'Foo'
 
1669
    klass.parent = @top_level
 
1670
 
 
1671
    method = "def ω() end"
 
1672
 
 
1673
    assert_equal Encoding::UTF_8, method.encoding if
 
1674
      Object.const_defined? :Encoding
 
1675
 
 
1676
    util_parser method
 
1677
 
 
1678
    tk = @parser.get_tk
 
1679
 
 
1680
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
 
1681
 
 
1682
    omega = klass.method_list.first
 
1683
    assert_equal "def \317\211", omega.text
 
1684
  end
 
1685
 
970
1686
  def test_parse_statements_class_if
971
 
    comment = "##\n# my method\n"
972
 
 
973
1687
    util_parser <<-CODE
974
1688
module Foo
975
1689
  X = if TRUE then
1006
1720
    assert_equal 'my method', bar.comment
1007
1721
  end
1008
1722
 
 
1723
  def test_parse_statements_encoding
 
1724
    skip "Encoding not implemented" unless Object.const_defined? :Encoding
 
1725
    @options.encoding = Encoding::CP852
 
1726
 
 
1727
    content = <<-EOF
 
1728
class Foo
 
1729
  ##
 
1730
  # this is my method
 
1731
  add_my_method :foo
 
1732
end
 
1733
    EOF
 
1734
 
 
1735
    util_parser content
 
1736
 
 
1737
    @parser.parse_statements @top_level
 
1738
 
 
1739
    foo = @top_level.classes.first.method_list.first
 
1740
    assert_equal 'foo', foo.name
 
1741
    assert_equal 'this is my method', foo.comment
 
1742
    assert_equal Encoding::CP852, foo.comment.encoding
 
1743
  end
 
1744
 
1009
1745
  def test_parse_statements_identifier_meta_method
1010
1746
    content = <<-EOF
1011
1747
class Foo
1017
1753
 
1018
1754
    util_parser content
1019
1755
 
1020
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
1756
    @parser.parse_statements @top_level
1021
1757
 
1022
1758
    foo = @top_level.classes.first.method_list.first
1023
1759
    assert_equal 'foo', foo.name
1024
1760
  end
1025
1761
 
1026
1762
  def test_parse_statements_identifier_alias_method
1027
 
    content = "class Foo def foo() end; alias_method :foo2, :foo end"
 
1763
    content = <<-RUBY
 
1764
class Foo
 
1765
  def foo() end
 
1766
  alias_method :foo2, :foo
 
1767
end
 
1768
    RUBY
1028
1769
 
1029
1770
    util_parser content
1030
1771
 
1031
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
1772
    @parser.parse_statements @top_level
1032
1773
 
1033
1774
    foo = @top_level.classes.first.method_list[0]
1034
1775
    assert_equal 'foo', foo.name
1061
1802
 
1062
1803
    util_parser content
1063
1804
 
1064
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
1805
    @parser.parse_statements @top_level
1065
1806
 
1066
1807
    foo = @top_level.classes.first.method_list[0]
1067
1808
    assert_equal 'foo', foo.name
1078
1819
    assert_equal 'foo4', foo4.name
1079
1820
    assert_equal 'foo', foo4.is_alias_for.name
1080
1821
 
1081
 
    assert_equal 'unknown', @top_level.classes.first.aliases[0].old_name
 
1822
    assert_equal 'unknown', @top_level.classes.first.external_aliases[0].old_name
1082
1823
  end
1083
1824
 
1084
1825
  def test_parse_statements_identifier_constant
 
1826
 
 
1827
    sixth_constant = <<-EOF
 
1828
Class.new do
 
1829
  rule :file do
 
1830
    all(x, y, z) {
 
1831
      def value
 
1832
        find(:require).each {|r| require r.value }
 
1833
        find(:grammar).map {|g| g.value }
 
1834
      end
 
1835
      def min; end
 
1836
    }
 
1837
  end
 
1838
end
 
1839
    EOF
 
1840
 
1085
1841
    content = <<-EOF
1086
1842
class Foo
1087
1843
  FIRST_CONSTANT = 5
1103
1859
  end
1104
1860
 
1105
1861
  FIFTH_CONSTANT = SECOND_CONSTANT.map { |element| element + 1 }
 
1862
 
 
1863
  SIXTH_CONSTANT = #{sixth_constant}
 
1864
 
 
1865
  SEVENTH_CONSTANT = proc { |i| begin i end }
1106
1866
end
1107
1867
EOF
1108
1868
 
1109
1869
    util_parser content
1110
1870
 
1111
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
1871
    @parser.parse_statements @top_level
1112
1872
 
1113
1873
    constants = @top_level.classes.first.constants
1114
1874
 
1115
1875
    constant = constants[0]
1116
1876
    assert_equal 'FIRST_CONSTANT', constant.name
1117
1877
    assert_equal '5', constant.value
 
1878
    assert_equal @top_level, constant.file
1118
1879
 
1119
1880
    constant = constants[1]
1120
1881
    assert_equal 'SECOND_CONSTANT', constant.name
1121
 
    assert_equal '[      1,      2,      3   ]', constant.value
 
1882
    assert_equal "[\n1,\n2,\n3\n]", constant.value
 
1883
    assert_equal @top_level, constant.file
1122
1884
 
1123
1885
    constant = constants[2]
1124
1886
    assert_equal 'THIRD_CONSTANT', constant.name
1125
 
    assert_equal "{      :foo => 'bar',      :x => 'y'   }", constant.value
 
1887
    assert_equal "{\n:foo => 'bar',\n:x => 'y'\n}", constant.value
 
1888
    assert_equal @top_level, constant.file
1126
1889
 
1127
1890
    constant = constants[3]
1128
1891
    assert_equal 'FOURTH_CONSTANT', constant.name
1129
 
    assert_equal 'SECOND_CONSTANT.map do |element|     element + 1     element + 2   end', constant.value
 
1892
    assert_equal "SECOND_CONSTANT.map do |element|\nelement + 1\nelement + 2\nend", constant.value
 
1893
    assert_equal @top_level, constant.file
1130
1894
 
1131
 
    constant = constants.last
 
1895
    constant = constants[4]
1132
1896
    assert_equal 'FIFTH_CONSTANT', constant.name
1133
1897
    assert_equal 'SECOND_CONSTANT.map { |element| element + 1 }', constant.value
 
1898
    assert_equal @top_level, constant.file
 
1899
 
 
1900
    # TODO: parse as class
 
1901
    constant = constants[5]
 
1902
    assert_equal 'SIXTH_CONSTANT', constant.name
 
1903
    assert_equal sixth_constant.lines.map(&:strip).join("\n"), constant.value
 
1904
    assert_equal @top_level, constant.file
 
1905
 
 
1906
    # TODO: parse as method
 
1907
    constant = constants[6]
 
1908
    assert_equal 'SEVENTH_CONSTANT', constant.name
 
1909
    assert_equal "proc { |i| begin i end }", constant.value
 
1910
    assert_equal @top_level, constant.file
1134
1911
  end
1135
1912
 
1136
1913
  def test_parse_statements_identifier_attr
1137
 
    content = "class Foo; attr :foo; end"
 
1914
    content = "class Foo\nattr :foo\nend"
1138
1915
 
1139
1916
    util_parser content
1140
1917
 
1141
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
1918
    @parser.parse_statements @top_level
1142
1919
 
1143
1920
    foo = @top_level.classes.first.attributes.first
1144
1921
    assert_equal 'foo', foo.name
1146
1923
  end
1147
1924
 
1148
1925
  def test_parse_statements_identifier_attr_accessor
1149
 
    content = "class Foo; attr_accessor :foo; end"
 
1926
    content = "class Foo\nattr_accessor :foo\nend"
1150
1927
 
1151
1928
    util_parser content
1152
1929
 
1153
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
1930
    @parser.parse_statements @top_level
1154
1931
 
1155
1932
    foo = @top_level.classes.first.attributes.first
1156
1933
    assert_equal 'foo', foo.name
1158
1935
  end
1159
1936
 
1160
1937
  def test_parse_statements_identifier_include
1161
 
    content = "class Foo; include Bar; end"
 
1938
    content = "class Foo\ninclude Bar\nend"
1162
1939
 
1163
1940
    util_parser content
1164
1941
 
1165
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
1942
    @parser.parse_statements @top_level
1166
1943
 
1167
1944
    foo = @top_level.classes.first
1168
1945
    assert_equal 'Foo', foo.name
1170
1947
  end
1171
1948
 
1172
1949
  def test_parse_statements_identifier_module_function
1173
 
    content = "module Foo def foo() end; module_function :foo; end"
 
1950
    content = "module Foo\ndef foo() end\nmodule_function :foo\nend"
1174
1951
 
1175
1952
    util_parser content
1176
1953
 
1177
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
1954
    @parser.parse_statements @top_level
1178
1955
 
1179
1956
    foo, s_foo = @top_level.modules.first.method_list
1180
 
    assert_equal 'foo', foo.name, 'instance method name'
 
1957
    assert_equal 'foo',    foo.name,       'instance method name'
1181
1958
    assert_equal :private, foo.visibility, 'instance method visibility'
1182
 
    assert_equal false, foo.singleton, 'instance method singleton'
 
1959
    assert_equal false,    foo.singleton,  'instance method singleton'
1183
1960
 
1184
 
    assert_equal 'foo', s_foo.name, 'module function name'
 
1961
    assert_equal 'foo',   s_foo.name,       'module function name'
1185
1962
    assert_equal :public, s_foo.visibility, 'module function visibility'
1186
 
    assert_equal true, s_foo.singleton, 'module function singleton'
 
1963
    assert_equal true,    s_foo.singleton,  'module function singleton'
1187
1964
  end
1188
1965
 
1189
1966
  def test_parse_statements_identifier_private
1190
 
    content = "class Foo private; def foo() end end"
 
1967
    content = "class Foo\nprivate\ndef foo() end\nend"
1191
1968
 
1192
1969
    util_parser content
1193
1970
 
1194
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
1971
    @parser.parse_statements @top_level
1195
1972
 
1196
1973
    foo = @top_level.classes.first.method_list.first
1197
1974
    assert_equal 'foo', foo.name
1198
1975
    assert_equal :private, foo.visibility
1199
1976
  end
1200
1977
 
 
1978
  def test_parse_statements_identifier_public_class_method
 
1979
    content = <<-CONTENT
 
1980
class Date
 
1981
  def self.now; end
 
1982
  private_class_method :now
 
1983
end
 
1984
 
 
1985
class DateTime < Date
 
1986
  public_class_method :now
 
1987
end
 
1988
    CONTENT
 
1989
 
 
1990
    util_parser content
 
1991
 
 
1992
    @parser.parse_statements @top_level
 
1993
 
 
1994
    date, date_time = @top_level.classes
 
1995
 
 
1996
    date_now      = date.method_list.first
 
1997
    date_time_now = date_time.method_list.first
 
1998
 
 
1999
    assert_equal :private, date_now.visibility
 
2000
    assert_equal :public,  date_time_now.visibility
 
2001
  end
 
2002
 
 
2003
  def test_parse_statements_identifier_private_class_method
 
2004
    content = <<-CONTENT
 
2005
class Date
 
2006
  def self.now; end
 
2007
  public_class_method :now
 
2008
end
 
2009
 
 
2010
class DateTime < Date
 
2011
  private_class_method :now
 
2012
end
 
2013
    CONTENT
 
2014
 
 
2015
    util_parser content
 
2016
 
 
2017
    @parser.parse_statements @top_level
 
2018
 
 
2019
    date, date_time = @top_level.classes
 
2020
 
 
2021
    date_now      = date.method_list.first
 
2022
    date_time_now = date_time.method_list.first
 
2023
 
 
2024
    assert_equal :public,  date_now.visibility,      date_now.full_name
 
2025
    assert_equal :private, date_time_now.visibility, date_time_now.full_name
 
2026
  end
 
2027
 
1201
2028
  def test_parse_statements_identifier_require
1202
2029
    content = "require 'bar'"
1203
2030
 
1204
2031
    util_parser content
1205
2032
 
1206
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
2033
    @parser.parse_statements @top_level
1207
2034
 
1208
2035
    assert_equal 1, @top_level.requires.length
1209
2036
  end
1210
2037
 
 
2038
  def test_parse_statements_stopdoc_TkALIAS
 
2039
    util_top_level
 
2040
 
 
2041
    klass = @top_level.add_class RDoc::NormalClass, 'Foo'
 
2042
 
 
2043
    util_parser "\n# :stopdoc:\nalias old new"
 
2044
 
 
2045
    @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
 
2046
 
 
2047
    assert_empty klass.aliases
 
2048
    assert_empty klass.unmatched_alias_lists
 
2049
  end
 
2050
 
 
2051
  def test_parse_statements_stopdoc_TkIDENTIFIER_alias_method
 
2052
    util_top_level
 
2053
 
 
2054
    klass = @top_level.add_class RDoc::NormalClass, 'Foo'
 
2055
 
 
2056
    util_parser "\n# :stopdoc:\nalias_method :old :new"
 
2057
 
 
2058
    @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
 
2059
 
 
2060
    assert_empty klass.aliases
 
2061
    assert_empty klass.unmatched_alias_lists
 
2062
  end
 
2063
 
 
2064
  def test_parse_statements_stopdoc_TkIDENTIFIER_metaprogrammed
 
2065
    util_top_level
 
2066
 
 
2067
    klass = @top_level.add_class RDoc::NormalClass, 'Foo'
 
2068
 
 
2069
    util_parser "\n# :stopdoc:\n# attr :meta"
 
2070
 
 
2071
    @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
 
2072
 
 
2073
    assert_empty klass.method_list
 
2074
    assert_empty klass.attributes
 
2075
  end
 
2076
 
 
2077
  def test_parse_statements_stopdoc_TkCONSTANT
 
2078
    util_top_level
 
2079
 
 
2080
    klass = @top_level.add_class RDoc::NormalClass, 'Foo'
 
2081
 
 
2082
    util_parser "\n# :stopdoc:\nA = v"
 
2083
 
 
2084
    @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
 
2085
 
 
2086
    assert_empty klass.constants
 
2087
  end
 
2088
 
 
2089
  def test_parse_statements_stopdoc_TkDEF
 
2090
    util_top_level
 
2091
 
 
2092
    klass = @top_level.add_class RDoc::NormalClass, 'Foo'
 
2093
 
 
2094
    util_parser "\n# :stopdoc:\ndef m\n end"
 
2095
 
 
2096
    @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
 
2097
 
 
2098
    assert_empty klass.method_list
 
2099
  end
 
2100
 
1211
2101
  def test_parse_statements_while_begin
1212
2102
    util_parser <<-RUBY
1213
2103
class A
1221
2111
end
1222
2112
    RUBY
1223
2113
 
1224
 
    @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
 
2114
    @parser.parse_statements @top_level
1225
2115
 
1226
2116
    c_a = @top_level.classes.first
1227
2117
    assert_equal 'A', c_a.full_name
1235
2125
    assert_equal 'A#b', m_b.full_name
1236
2126
  end
1237
2127
 
1238
 
  def test_parse_top_level_statements_alias_method
 
2128
  def test_parse_symbol_in_arg
 
2129
    util_parser ':blah "blah" "#{blah}" blah'
 
2130
 
 
2131
    assert_equal 'blah', @parser.parse_symbol_in_arg
 
2132
 
 
2133
    @parser.skip_tkspace
 
2134
 
 
2135
    assert_equal 'blah', @parser.parse_symbol_in_arg
 
2136
 
 
2137
    @parser.skip_tkspace
 
2138
 
 
2139
    assert_equal nil, @parser.parse_symbol_in_arg
 
2140
 
 
2141
    @parser.skip_tkspace
 
2142
 
 
2143
    assert_equal nil, @parser.parse_symbol_in_arg
 
2144
  end
 
2145
 
 
2146
  def test_parse_statements_alias_method
1239
2147
    content = <<-CONTENT
1240
2148
class A
1241
2149
  alias_method :a, :[] unless c
1250
2158
    util_parser content
1251
2159
 
1252
2160
    @parser.parse_statements @top_level
 
2161
 
 
2162
    # HACK where are the assertions?
 
2163
  end
 
2164
 
 
2165
  def test_parse_top_level_statements_stopdoc
 
2166
    @top_level.stop_doc
 
2167
    content = "# this is the top-level comment"
 
2168
 
 
2169
    util_parser content
 
2170
 
 
2171
    @parser.parse_top_level_statements @top_level
 
2172
 
 
2173
    assert_empty @top_level.comment
 
2174
  end
 
2175
 
 
2176
  def test_parse_top_level_statements_stopdoc_integration
 
2177
    content = <<-CONTENT
 
2178
# :stopdoc:
 
2179
 
 
2180
class Example
 
2181
  def method_name
 
2182
  end
 
2183
end
 
2184
    CONTENT
 
2185
 
 
2186
    util_parser content
 
2187
 
 
2188
    @parser.parse_top_level_statements @top_level
 
2189
 
 
2190
    assert_equal 1, @top_level.classes.length
 
2191
    assert_empty @top_level.modules
 
2192
 
 
2193
    assert @top_level.find_module_named('Example').ignored?
 
2194
  end
 
2195
 
 
2196
  # This tests parse_comment
 
2197
  def test_parse_top_level_statements_constant_nodoc_integration
 
2198
    content = <<-CONTENT
 
2199
class A
 
2200
  C = A # :nodoc:
 
2201
end
 
2202
    CONTENT
 
2203
 
 
2204
    util_parser content
 
2205
 
 
2206
    @parser.parse_top_level_statements @top_level
 
2207
 
 
2208
    klass = @top_level.find_module_named('A')
 
2209
 
 
2210
    c = klass.constants.first
 
2211
 
 
2212
    assert_nil c.document_self, 'C should not be documented'
 
2213
  end
 
2214
 
 
2215
  def test_parse_yield_in_braces_with_parens
 
2216
    klass = RDoc::NormalClass.new 'Foo'
 
2217
    klass.parent = @top_level
 
2218
 
 
2219
    util_parser "def foo\nn.times { |i| yield nth(i) }\nend"
 
2220
 
 
2221
    tk = @parser.get_tk
 
2222
 
 
2223
    @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
 
2224
 
 
2225
    foo = klass.method_list.first
 
2226
    assert_equal 'nth(i)', foo.block_params
1253
2227
  end
1254
2228
 
1255
2229
  def test_sanity_integer
1272
2246
  # If you're writing code like this you're doing it wrong
1273
2247
 
1274
2248
  def test_sanity_interpolation_crazy
1275
 
    last_tk = nil
1276
2249
    util_parser '"#{"#{"a")}" if b}"'
1277
2250
 
1278
2251
    assert_equal '"#{"#{"a")}" if b}"', @parser.get_tk.text
1280
2253
  end
1281
2254
 
1282
2255
  def test_sanity_interpolation_curly
1283
 
    last_tk = nil
1284
2256
    util_parser '%{ #{} }'
1285
2257
 
1286
2258
    assert_equal '%{ #{} }', @parser.get_tk.text
1290
2262
  def test_sanity_interpolation_format
1291
2263
    util_parser '"#{stftime("%m-%d")}"'
1292
2264
 
1293
 
    while tk = @parser.get_tk do end
 
2265
    while @parser.get_tk do end
1294
2266
  end
1295
2267
 
1296
2268
  def test_sanity_symbol_interpolation
1297
2269
    util_parser ':"#{bar}="'
1298
2270
 
1299
 
    while tk = @parser.get_tk do end
 
2271
    while @parser.get_tk do end
 
2272
  end
 
2273
 
 
2274
  def test_scan_cr
 
2275
    content = <<-CONTENT
 
2276
class C\r
 
2277
  def m\r
 
2278
    a=\\\r
 
2279
      123\r
 
2280
  end\r
 
2281
end\r
 
2282
    CONTENT
 
2283
 
 
2284
    util_parser content
 
2285
 
 
2286
    @parser.scan
 
2287
 
 
2288
    c = @top_level.classes.first
 
2289
 
 
2290
    assert_equal 1, c.method_list.length
 
2291
  end
 
2292
 
 
2293
  def test_scan_block_comment
 
2294
    content = <<-CONTENT
 
2295
=begin rdoc
 
2296
Foo comment
 
2297
=end
 
2298
 
 
2299
class Foo
 
2300
 
 
2301
=begin
 
2302
m comment
 
2303
=end
 
2304
 
 
2305
  def m() end
 
2306
end
 
2307
    CONTENT
 
2308
 
 
2309
    util_parser content
 
2310
 
 
2311
    @parser.scan
 
2312
 
 
2313
    foo = @top_level.classes.first
 
2314
 
 
2315
    assert_equal 'Foo comment', foo.comment
 
2316
 
 
2317
    m = foo.method_list.first
 
2318
 
 
2319
    assert_equal 'm comment', m.comment
 
2320
  end
 
2321
 
 
2322
  def test_scan_block_comment_nested # Issue #41
 
2323
    content = <<-CONTENT
 
2324
require 'something'
 
2325
=begin rdoc
 
2326
findmeindoc
 
2327
=end
 
2328
module Foo
 
2329
    class Bar
 
2330
    end
 
2331
end
 
2332
    CONTENT
 
2333
 
 
2334
    util_parser content
 
2335
 
 
2336
    @parser.scan
 
2337
 
 
2338
    foo = @top_level.modules.first
 
2339
 
 
2340
    assert_equal 'Foo', foo.full_name
 
2341
    assert_equal 'findmeindoc', foo.comment
 
2342
 
 
2343
    bar = foo.classes.first
 
2344
 
 
2345
    assert_equal 'Foo::Bar', bar.full_name
 
2346
    assert_equal '', bar.comment
 
2347
  end
 
2348
 
 
2349
  def test_scan_block_comment_notflush
 
2350
  ##
 
2351
  #
 
2352
  # The previous test assumes that between the =begin/=end blocs that there is
 
2353
  # only one line, or minima formatting directives. This test tests for those
 
2354
  # who use the =begin bloc with longer / more advanced formatting within.
 
2355
  #
 
2356
  ##
 
2357
    content = <<-CONTENT
 
2358
=begin rdoc
 
2359
 
 
2360
= DESCRIPTION
 
2361
 
 
2362
This is a simple test class
 
2363
 
 
2364
= RUMPUS
 
2365
 
 
2366
Is a silly word
 
2367
 
 
2368
=end
 
2369
class StevenSimpleClass
 
2370
  # A band on my iPhone as I wrote this test
 
2371
  FRUIT_BATS="Make nice music"
 
2372
 
 
2373
=begin rdoc
 
2374
A nice girl
 
2375
=end
 
2376
 
 
2377
  def lauren
 
2378
    puts "Summoning Lauren!"
 
2379
  end
 
2380
end
 
2381
    CONTENT
 
2382
    util_parser content
 
2383
 
 
2384
    @parser.scan
 
2385
 
 
2386
    foo = @top_level.classes.first
 
2387
 
 
2388
    assert_equal "= DESCRIPTION\n\nThis is a simple test class\n\n= RUMPUS\n\nIs a silly word",
 
2389
      foo.comment
 
2390
 
 
2391
    m = foo.method_list.first
 
2392
 
 
2393
    assert_equal 'A nice girl', m.comment
 
2394
  end
 
2395
 
 
2396
  def test_scan_meta_method_block
 
2397
    content = <<-CONTENT
 
2398
class C
 
2399
 
 
2400
  ##
 
2401
  #  my method
 
2402
 
 
2403
  inline(:my_method) do |*args|
 
2404
    "this method used to cause z to disappear"
 
2405
  end
 
2406
 
 
2407
  def z
 
2408
  end
 
2409
    CONTENT
 
2410
 
 
2411
    util_parser content
 
2412
 
 
2413
    @parser.scan
 
2414
 
 
2415
    assert_equal 2, @top_level.classes.first.method_list.length
 
2416
  end
 
2417
 
 
2418
  def test_stopdoc_after_comment
 
2419
    util_parser <<-EOS
 
2420
      module Bar
 
2421
        # hello
 
2422
        module Foo
 
2423
          # :stopdoc:
 
2424
        end
 
2425
        # there
 
2426
        class Baz
 
2427
          # :stopdoc:
 
2428
        end
 
2429
      end
 
2430
    EOS
 
2431
 
 
2432
    @parser.parse_statements @top_level
 
2433
 
 
2434
    foo = @top_level.modules.first.modules.first
 
2435
    assert_equal 'Foo', foo.name
 
2436
    assert_equal 'hello', foo.comment
 
2437
 
 
2438
    baz = @top_level.modules.first.classes.first
 
2439
    assert_equal 'Baz', baz.name
 
2440
    assert_equal 'there', baz.comment
1300
2441
  end
1301
2442
 
1302
2443
  def tk(klass, line, char, name, text)