diff options
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | lib/rdoc/parsers/parse_c.rb | 38 | ||||
-rw-r--r-- | test/rdoc/parsers/test_parse_c.rb | 117 |
3 files changed, 165 insertions, 3 deletions
@@ -1,3 +1,16 @@ +Sun Jan 7 12:13:26 2007 Eric Hodel <drbrain@segment7.net> + + * lib/rdoc/parsers/parse_c.rb (RDoc::C_Parser#find_class_comment): + Look for class and module comments above rb_define_class and + rb_define_module. Patch by Daniel Berger <djberg96 at gmail.com> + +Sun Jan 7 10:32:12 2007 Eric Hodel <drbrain@segment7.net> + + * lib/rdoc/parsers/parse_c.rb (RDoc::C_Parser#handle_constants): + Properly handle escaping of : in comments. + * test/rdoc/parsers/test_parse_c.rb: + Test RDoc::C_Parser#do_classes and Rdoc::C_Parser#find_class_comment. + Sun Jan 7 09:31:18 2007 Tadayoshi Funaba <tadf@dotrb.org> * lib/date/format.rb: updated based on date2 4.0.1. diff --git a/lib/rdoc/parsers/parse_c.rb b/lib/rdoc/parsers/parse_c.rb index 7053fac7d5..ec9ae1877d 100644 --- a/lib/rdoc/parsers/parse_c.rb +++ b/lib/rdoc/parsers/parse_c.rb @@ -266,7 +266,31 @@ module RDoc @known_classes[var_name] = cm.full_name end - ############################################################ + ## + # Look for class or module documentation above Init_+class_name+(void), + # in a Document-class +class_name+ (or module) comment or above an + # rb_define_class (or module). If a comment is supplied above a matching + # Init_ and a rb_define_class the Init_ comment is used. + # + # /* + # * This is a comment for Foo + # */ + # Init_Foo(void) { + # VALUE cFoo = rb_define_class("Foo", rb_cObject); + # } + # + # /* + # * Document-class: Foo + # * This is a comment for Foo + # */ + # Init_foo(void) { + # VALUE cFoo = rb_define_class("Foo", rb_cObject); + # } + # + # /* + # * This is a comment for Foo + # */ + # VALUE cFoo = rb_define_class("Foo", rb_cObject); def find_class_comment(class_name, class_meth) comment = nil @@ -275,6 +299,18 @@ module RDoc comment = $1 elsif @body =~ %r{Document-(class|module):\s#{class_name}\s*?\n((?>.*?\*/))}m comment = $2 + else + if @body =~ /rb_define_(class|module)/m then + class_name = class_name.split("::").last + comments = [] + @body.split(/(\/\*.*?\*\/)\s*?\n/m).each_with_index do |chunk, index| + comments[index] = chunk + if chunk =~ /rb_define_(class|module).*?"(#{class_name})"/m then + comment = comments[index-1] + break + end + end + end end class_meth.comment = mangle_comment(comment) if comment end diff --git a/test/rdoc/parsers/test_parse_c.rb b/test/rdoc/parsers/test_parse_c.rb index c7815a95c0..6157a9e1d4 100644 --- a/test/rdoc/parsers/test_parse_c.rb +++ b/test/rdoc/parsers/test_parse_c.rb @@ -1,4 +1,3 @@ -require 'pp' require 'stringio' require 'tempfile' require 'test/unit' @@ -28,6 +27,66 @@ class TestRdocC_Parser < Test::Unit::TestCase @tempfile.unlink end + def test_do_classes_boot_class + content = <<-EOF +/* Document-class: Foo + * this is the Foo boot class + */ +VALUE cFoo = boot_defclass("Foo", 0); + EOF + + klass = util_get_class content, 'cFoo' + assert_equal " this is the Foo boot class\n ", klass.comment + end + + def test_do_classes_class + content = <<-EOF +/* Document-class: Foo + * this is the Foo class + */ +VALUE cFoo = rb_define_class("Foo", rb_cObject); + EOF + + klass = util_get_class content, 'cFoo' + assert_equal " this is the Foo class\n ", klass.comment + end + + def test_do_classes_class_under + content = <<-EOF +/* Document-class: Kernel::Foo + * this is the Foo class under Kernel + */ +VALUE cFoo = rb_define_class_under(rb_mKernel, "Foo", rb_cObject); + EOF + + klass = util_get_class content, 'cFoo' + assert_equal " this is the Foo class under Kernel\n ", klass.comment + end + + def test_do_classes_module + content = <<-EOF +/* Document-module: Foo + * this is the Foo module + */ +VALUE mFoo = rb_define_module("Foo"); + EOF + + klass = util_get_class content, 'mFoo' + assert_equal " this is the Foo module\n ", klass.comment + end + + def test_do_classes_module_under + content = <<-EOF +/* Document-module: Kernel::Foo + * this is the Foo module under Kernel + */ +VALUE mFoo = rb_define_module_under(rb_mKernel, "Foo"); + EOF + + klass = util_get_class content, 'mFoo' + assert_equal " this is the Foo module under Kernel\n ", klass.comment + end + def test_do_constants content = <<-EOF #include <ruby.h> @@ -83,7 +142,7 @@ void Init_foo(){ parser.do_classes parser.do_constants - klass = parser.classes['cFoo'] + klass = parser.classes['cFoo'] assert klass constants = klass.constants @@ -138,6 +197,60 @@ void Init_foo(){ assert constants.empty?, constants.inspect end + def test_find_class_comment_init + content = <<-EOF +/* + * a comment for class Foo + */ +void +Init_Foo(void) { + VALUE foo = rb_define_class("Foo", rb_cObject); +} + EOF + + klass = util_get_class content, 'foo' + + assert_equal " \n a comment for class Foo\n \n", klass.comment + end + + def test_find_class_comment_define_class + content = <<-EOF +/* + * a comment for class Foo + */ +VALUE foo = rb_define_class("Foo", rb_cObject); + EOF + + klass = util_get_class content, 'foo' + + assert_equal " \n a comment for class Foo\n ", klass.comment + end + + def test_find_class_comment_define_class + content = <<-EOF +/* + * a comment for class Foo on Init + */ +void +Init_Foo(void) { + /* + * a comment for class Foo on rb_define_class + */ + VALUE foo = rb_define_class("Foo", rb_cObject); +} + EOF + + klass = util_get_class content, 'foo' + + assert_equal " \n a comment for class Foo on Init\n \n", klass.comment + end + + def util_get_class(content, name) + parser = util_parser content + parser.do_classes + parser.classes[name] + end + def util_parser(content) parser = RDoc::C_Parser.new @top_level, @fn, content, @options, @stats parser.progress = @progress |