Skip to content

Commit

Permalink
Support rb_singleton_class()
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Aug 11, 2010
1 parent 995ca9a commit 8d6dd97
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
5 changes: 5 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
=== 2.5.10 / 2010-08-??

* Minor Enhancements
* Support rb_singleton_class(). Reported by Jeremy Evans.

=== 2.5.9 / 2010-07-06

* Bug Fixes
Expand Down
29 changes: 24 additions & 5 deletions lib/rdoc/parser/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def initialize(top_level, file_name, content, options, stats)
@known_classes = RDoc::KNOWN_CLASSES.dup
@content = handle_tab_width handle_ifdefs_in(@content)
@classes = Hash.new
@singleton_classes = Hash.new
@file_dir = File.dirname(@file_name)
end

Expand Down Expand Up @@ -165,13 +166,20 @@ def do_classes
end

@content.scan(/([\w\.]+)\s* = \s*rb_define_class_under\s*
\(
\s*(\w+),
\s*"(\w+)",
\s*([\w\*\s\(\)\.\->]+)\s* # for SWIG
\s*\)/mx) do |var_name, in_module, class_name, parent|
\(
\s*(\w+),
\s*"(\w+)",
\s*([\w\*\s\(\)\.\->]+)\s* # for SWIG
\s*\)/mx) do |var_name, in_module, class_name, parent|
handle_class_module(var_name, "class", class_name, parent, in_module)
end

@content.scan(/([\w\.]+)\s* = \s*rb_singleton_class\s*
\(
\s*(\w+)
\s*\)/mx) do |sclass_var, class_var|
handle_singleton sclass_var, class_var
end
end

def do_constants
Expand Down Expand Up @@ -594,6 +602,11 @@ def handle_method(type, var_name, meth_name, meth_body, param_count,
source_file = nil)
class_name = @known_classes[var_name]

unless class_name then
class_name = @singleton_classes[var_name]
type = "singleton_method" if class_name and type == "method"
end

return unless class_name

class_obj = find_class var_name, class_name
Expand Down Expand Up @@ -637,6 +650,12 @@ def handle_method(type, var_name, meth_name, meth_body, param_count,
end
end

def handle_singleton sclass_var, class_var
class_name = @known_classes[class_var]

@singleton_classes[sclass_var] = class_name
end

def handle_tab_width(body)
if /\t/ =~ body
tab_width = @options.tab_width
Expand Down
46 changes: 43 additions & 3 deletions test/test_rdoc_parser_c.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'stringio'
require 'tempfile'
require 'rubygems'
require 'minitest/unit'
require 'minitest/autorun'
require 'rdoc/options'
require 'rdoc/parser/c'

class RDoc::Parser::C
attr_accessor :classes
attr_accessor :classes, :singleton_classes

public :do_classes, :do_constants
end
Expand Down Expand Up @@ -68,6 +68,17 @@ def test_do_classes_class
assert_equal "this is the Foo class", klass.comment
end

def test_do_classes_singleton
content = <<-EOF
VALUE cFoo = rb_define_class("Foo", rb_cObject);
VALUE cFooS = rb_singleton_class(cFoo);
EOF

util_get_class content, 'cFooS'

assert_equal 'Foo', @parser.singleton_classes['cFooS']
end

def test_do_classes_class_under
content = <<-EOF
/* Document-class: Kernel::Foo
Expand Down Expand Up @@ -442,6 +453,7 @@ def test_define_method
read_method = klass.method_list.first
assert_equal "read", read_method.name
assert_equal "Method Comment! ", read_method.comment
assert read_method.singleton
end

def test_define_method_private
Expand Down Expand Up @@ -472,6 +484,35 @@ def test_define_method_private
assert_equal "Method Comment! ", read_method.comment
end

def test_define_method_singleton
content = <<-EOF
/*Method Comment! */
static VALUE
rb_io_s_read(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
}
void
Init_IO(void) {
/*
* a comment for class Foo on rb_define_class
*/
VALUE rb_cIO = rb_define_class("IO", rb_cObject);
VALUE rb_cIO_s = rb_singleton_class(rb_cIO);
rb_define_singleton_method(rb_cIO_s, "read", rb_io_s_read, -1);
}
EOF

klass = util_get_class content, 'rb_cIO'
read_method = klass.method_list.first
assert_equal "read", read_method.name
assert_equal "Method Comment! ", read_method.comment
assert read_method.singleton
end

def util_get_class(content, name)
@parser = util_parser content
@parser.scan
Expand All @@ -484,4 +525,3 @@ def util_parser(content)

end

MiniTest::Unit.autorun

0 comments on commit 8d6dd97

Please sign in to comment.