Skip to content

Commit 499c029

Browse files
committed
Handle all definitions per file
RDoc::Parser::C cann't discern each documents per file for one class, so RDoc::ClassModule adopts the latest comment per file, however RDoc::Parser::Ruby can take each documents per file for one class. This commit changes the behavior only when RDoc::ClassModule is for RDoc::Parser::C. Of course this works around RDoc::Parser::C.
1 parent e629a82 commit 499c029

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

lib/rdoc/class_module.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ def add_comment comment, location
136136
normalize_comment comment
137137
end
138138

139-
@comment_location.delete_if { |(_, l)| l == location }
139+
if location.parser == RDoc::Parser::C
140+
@comment_location.delete_if { |(_, l)| l == location }
141+
end
140142

141143
@comment_location << [comment, location]
142144

test/test_rdoc_class_module.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def test_add_comment_duplicate
4242
cm.add_comment '# comment 1', tl1
4343
cm.add_comment '# comment 2', tl1
4444

45-
assert_equal [['comment 2', tl1]], cm.comment_location
45+
assert_equal [['comment 1', tl1],
46+
['comment 2', tl1]], cm.comment_location
4647
end
4748

4849
def test_add_comment_stopdoc

test/test_rdoc_parser_c.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,25 @@ def test_do_classes_class
357357
assert_equal "this is the Foo class", klass.comment.text
358358
end
359359

360+
def test_do_classes_duplicate_class
361+
content = <<-EOF
362+
/* Document-class: Foo
363+
* first
364+
*/
365+
VALUE cFoo = rb_define_class("Foo", rb_cObject);
366+
/* Document-class: Foo
367+
* second
368+
*/
369+
VALUE cFoo = rb_define_class("Foo", rb_cObject);
370+
EOF
371+
372+
klass = util_get_class content, 'cFoo'
373+
assert_equal 1, klass.comment_location.size
374+
first = klass.comment_location.first
375+
first_comment = first[0]
376+
assert_equal 'first', first_comment.text
377+
end
378+
360379
def test_do_classes_struct
361380
content = <<-EOF
362381
/* Document-class: Foo

test/test_rdoc_parser_ruby.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,32 @@ class Foo
681681
assert_equal @top_level, blah.file
682682
end
683683

684+
def test_parse_class_in_a_file_repeatedly
685+
@filename = 'a.rb'
686+
comment_a = RDoc::Comment.new "# aaa\n", @top_level
687+
util_parser "class Foo\nend"
688+
tk = @parser.get_tk
689+
@parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment_a
690+
comment_b = RDoc::Comment.new "# bbb\n", @top_level
691+
util_parser "class Foo\nend"
692+
tk = @parser.get_tk
693+
@parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment_b
694+
695+
@filename = 'b.rb'
696+
comment_c = RDoc::Comment.new "# ccc\n", @top_level
697+
util_parser "class Foo\nend"
698+
tk = @parser.get_tk
699+
@parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment_c
700+
701+
foo = @top_level.classes.first
702+
assert_equal 'Foo', foo.full_name
703+
assert_equal [[comment_a, @top_level],
704+
[comment_b, @top_level],
705+
[comment_c, @top_level]], foo.comment_location
706+
assert_equal [@top_level], foo.in_files
707+
assert_equal 1, foo.line
708+
end
709+
684710
def test_parse_class_ghost_method_yields
685711
util_parser <<-CLASS
686712
class Foo
@@ -3393,6 +3419,7 @@ module Foo
33933419
foo = @top_level.modules.first
33943420

33953421
expected = [
3422+
RDoc::Comment.new('comment a', @top_level),
33963423
RDoc::Comment.new('comment b', @top_level)
33973424
]
33983425

0 commit comments

Comments
 (0)