73
def test_class_can_parse
74
c_parser = RDoc::Parser::C
76
assert_equal c_parser, c_parser.can_parse('file.C')
77
assert_equal c_parser, c_parser.can_parse('file.CC')
78
assert_equal c_parser, c_parser.can_parse('file.H')
79
assert_equal c_parser, c_parser.can_parse('file.HH')
80
assert_equal c_parser, c_parser.can_parse('file.c')
81
assert_equal c_parser, c_parser.can_parse('file.cc')
82
assert_equal c_parser, c_parser.can_parse('file.cpp')
83
assert_equal c_parser, c_parser.can_parse('file.cxx')
84
assert_equal c_parser, c_parser.can_parse('file.h')
85
assert_equal c_parser, c_parser.can_parse('file.hh')
86
assert_equal c_parser, c_parser.can_parse('file.y')
89
def test_do_attr_rb_attr
91
void Init_Blah(void) {
92
cBlah = rb_define_class("Blah", rb_cObject);
97
rb_attr(cBlah, rb_intern("accessor"), 1, 1, Qfalse);
102
rb_attr(cBlah, rb_intern("reader"), 1, 0, Qfalse);
107
rb_attr(cBlah, rb_intern("writer"), 0, 1, Qfalse);
111
klass = util_get_class content, 'cBlah'
113
attrs = klass.attributes
114
assert_equal 3, attrs.length, attrs.inspect
116
accessor = attrs.shift
117
assert_equal 'accessor', accessor.name
118
assert_equal 'RW', accessor.rw
119
assert_equal 'This is an accessor', accessor.comment
120
assert_equal @top_level, accessor.file
123
assert_equal 'reader', reader.name
124
assert_equal 'R', reader.rw
125
assert_equal 'This is a reader', reader.comment
128
assert_equal 'writer', writer.name
129
assert_equal 'W', writer.rw
130
assert_equal 'This is a writer', writer.comment
133
def test_do_attr_rb_define_attr
135
void Init_Blah(void) {
136
cBlah = rb_define_class("Blah", rb_cObject);
139
* This is an accessor
141
rb_define_attr(cBlah, "accessor", 1, 1);
145
klass = util_get_class content, 'cBlah'
147
attrs = klass.attributes
148
assert_equal 1, attrs.length, attrs.inspect
150
accessor = attrs.shift
151
assert_equal 'accessor', accessor.name
152
assert_equal 'RW', accessor.rw
153
assert_equal 'This is an accessor', accessor.comment
154
assert_equal @top_level, accessor.file
160
* This should show up as an alias with documentation
162
VALUE blah(VALUE klass, VALUE year) {
165
void Init_Blah(void) {
166
cDate = rb_define_class("Date", rb_cObject);
168
rb_define_method(cDate, "blah", blah, 1);
170
rb_define_alias(cDate, "bleh", "blah");
174
klass = util_get_class content, 'cDate'
176
methods = klass.method_list
177
assert_equal 2, methods.length
178
assert_equal 'bleh', methods.last.name
179
assert_equal 'blah', methods.last.is_alias_for.name
181
assert_equal @top_level, methods.last.is_alias_for.file
182
assert_equal @top_level, methods.last.file
185
def test_do_aliases_singleton
188
* This should show up as a method with documentation
190
VALUE blah(VALUE klass, VALUE year) {
193
void Init_Blah(void) {
194
cDate = rb_define_class("Date", rb_cObject);
195
sDate = rb_singleton_class(cDate);
197
rb_define_method(sDate, "blah", blah, 1);
200
* This should show up as an alias
202
rb_define_alias(sDate, "bleh", "blah");
206
klass = util_get_class content, 'cDate'
208
methods = klass.method_list
210
assert_equal 2, methods.length
211
assert_equal 'bleh', methods.last.name
212
assert methods.last.singleton
213
assert_equal 'blah', methods.last.is_alias_for.name
214
assert_equal 'This should show up as an alias', methods.last.comment
33
217
def test_do_classes_boot_class
35
219
/* Document-class: Foo
201
429
assert constants.empty?, constants.inspect
432
def test_do_constants_curses
435
mCurses = rb_define_module("Curses");
438
* Document-const: Curses::COLOR_BLACK
440
* Value of the color black
442
rb_curses_define_const(COLOR_BLACK);
446
@parser = util_parser content
451
klass = @parser.classes['mCurses']
453
constants = klass.constants
454
refute_empty klass.constants
456
assert_equal 'COLOR_BLACK', constants.first.name
457
assert_equal 'UINT2NUM(COLOR_BLACK)', constants.first.value
458
assert_equal 'Value of the color black', constants.first.comment
464
VALUE cFoo = rb_define_class("Foo", rb_cObject);
465
VALUE mInc = rb_define_module("Inc");
467
rb_include_module(cFoo, mInc);
471
klass = util_get_class content, 'cFoo'
473
incl = klass.includes.first
474
assert_equal 'Inc', incl.name
475
assert_equal '', incl.comment
476
assert_equal @top_level, incl.file
479
# HACK parsing warning instead of setting up in file
480
def test_do_methods_in_c
482
VALUE blah(VALUE klass, VALUE year) {
485
void Init_Blah(void) {
486
cDate = rb_define_class("Date", rb_cObject);
488
rb_define_method(cDate, "blah", blah, 1); /* in blah.c */
494
_, err = capture_io do
495
klass = util_get_class content, 'cDate'
498
assert_match ' blah.c ', err
501
# HACK parsing warning instead of setting up in file
502
def test_do_methods_in_cpp
504
VALUE blah(VALUE klass, VALUE year) {
507
void Init_Blah(void) {
508
cDate = rb_define_class("Date", rb_cObject);
510
rb_define_method(cDate, "blah", blah, 1); /* in blah.cpp */
516
_, err = capture_io do
517
klass = util_get_class content, 'cDate'
520
assert_match ' blah.cpp ', err
523
# HACK parsing warning instead of setting up in file
524
def test_do_methods_in_y
526
VALUE blah(VALUE klass, VALUE year) {
529
void Init_Blah(void) {
530
cDate = rb_define_class("Date", rb_cObject);
532
rb_define_method(cDate, "blah", blah, 1); /* in blah.y */
538
_, err = capture_io do
539
klass = util_get_class content, 'cDate'
542
assert_match ' blah.y ', err
545
def test_do_methods_singleton_class
547
VALUE blah(VALUE klass, VALUE year) {
550
void Init_Blah(void) {
551
cDate = rb_define_class("Date", rb_cObject);
552
sDate = rb_singleton_class(cDate);
554
rb_define_method(sDate, "blah", blah, 1);
558
klass = util_get_class content, 'cDate'
560
methods = klass.method_list
561
assert_equal 1, methods.length
562
assert_equal 'blah', methods.first.name
563
assert methods.first.singleton
566
def test_find_alias_comment
567
parser = util_parser ''
569
comment = parser.find_alias_comment 'C', '[]', 'index'
571
assert_equal '', comment
573
parser = util_parser <<-C
578
rb_define_alias(C, "[]", "index");
581
comment = parser.find_alias_comment 'C', '[]', 'index'
583
assert_equal "/*\n * comment\n */\n\n", comment
204
586
def test_find_class_comment_include
205
587
@options.rdoc_include << File.dirname(__FILE__)
336
765
code = other_function.token_stream.first.text
338
assert_equal "VALUE\nother_function() ", code
767
assert_equal "VALUE\nother_function() {\n}", code
772
/* Copyright (C) 2010 Sven Herzberg
774
* This file is free software; the author(s) gives unlimited
775
* permission to copy and/or distribute it, with or without
776
* modifications, as long as this notice is preserved.
782
wrap_initialize (VALUE self)
789
wrap_shift (VALUE self,
796
init_gi_repository (void)
798
VALUE mTest = rb_define_module ("Test");
799
VALUE cTest = rb_define_class_under (mTest, "Test", rb_cObject);
801
rb_define_method (cTest, "initialize", wrap_initialize, 0);
802
rb_define_method (cTest, "shift", wrap_shift, 0);
806
klass = util_get_class content, 'cTest'
807
assert_equal 2, klass.method_list.length
341
810
def test_find_body_define
812
#define something something_else
814
#define other_function rb_other_function
817
* a comment for rb_other_function
820
rb_other_function() {
825
VALUE foo = rb_define_class("Foo", rb_cObject);
827
rb_define_method(foo, "my_method", other_function, 0);
831
klass = util_get_class content, 'foo'
832
other_function = klass.method_list.first
834
assert_equal 'my_method', other_function.name
835
assert_equal 'a comment for rb_other_function', other_function.comment
836
assert_equal '()', other_function.params
837
assert_equal 118, other_function.offset
838
assert_equal 8, other_function.line
840
code = other_function.token_stream.first.text
842
assert_equal "VALUE\nrb_other_function() {\n}", code
845
def test_find_body_define_comment
344
848
* a comment for other_function
404
909
baz = methods.last
405
910
assert_equal 'Foo#baz', baz.full_name
406
assert_equal "a comment for bar", bar.comment
911
assert_equal "a comment for bar", baz.comment
914
def test_find_body_document_method_equals
917
* Document-method: Zlib::GzipFile#mtime=
922
rb_gzfile_set_mtime(VALUE obj, VALUE mtime)
927
mZlib = rb_define_module("Zlib");
928
cGzipFile = rb_define_class_under(mZlib, "GzipFile", rb_cObject);
929
cGzipWriter = rb_define_class_under(mZlib, "GzipWriter", cGzipFile);
930
rb_define_method(cGzipWriter, "mtime=", rb_gzfile_set_mtime, 1);
934
klass = util_get_class content, 'cGzipWriter'
935
assert_equal 1, klass.method_list.length
937
methods = klass.method_list.sort
940
assert_equal 'Zlib::GzipWriter#mtime=', bar.full_name
941
assert_equal 'A comment', bar.comment
944
def test_find_body_document_method_same
955
* Document-method: Foo::bar
957
* a comment for Foo::bar
961
* Document-method: Foo#bar
963
* a comment for Foo#bar
968
VALUE foo = rb_define_class("Foo", rb_cObject);
970
rb_define_singleton_method(foo, "bar", s_bar, 0);
971
rb_define_method(foo, "bar", bar, 0);
975
klass = util_get_class content, 'foo'
976
assert_equal 2, klass.method_list.length
978
methods = klass.method_list.sort
980
s_bar = methods.first
981
assert_equal 'Foo::bar', s_bar.full_name
982
assert_equal "a comment for Foo::bar", s_bar.comment
985
assert_equal 'Foo#bar', bar.full_name
986
assert_equal "a comment for Foo#bar", bar.comment
989
def test_find_modifiers_call_seq
992
* commercial() -> Date <br />
993
* commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
994
* commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
996
* If no arguments are given:
997
* * ruby 1.8: returns a +Date+ for 1582-10-15 (the Day of Calendar Reform in
999
* * ruby 1.9: returns a +Date+ for julian day 0
1001
* Otherwise, returns a +Date+ for the commercial week year, commercial week,
1002
* and commercial week day given. Ignores the 4th argument.
1007
parser = util_parser ''
1008
method_obj = RDoc::AnyMethod.new nil, 'blah'
1010
parser.find_modifiers comment, method_obj
1012
expected = <<-CALL_SEQ.chomp
1013
commercial() -> Date <br />
1014
commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
1015
commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
1019
assert_equal expected, method_obj.call_seq
1022
def test_find_modifiers_call_seq_no_blank
1023
comment = <<-COMMENT
1025
* commercial() -> Date <br />
1026
* commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
1027
* commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
1032
parser = util_parser ''
1033
method_obj = RDoc::AnyMethod.new nil, 'blah'
1035
parser.find_modifiers comment, method_obj
1037
expected = <<-CALL_SEQ.chomp
1038
commercial() -> Date <br />
1039
commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
1040
commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
1044
assert_equal expected, method_obj.call_seq
1047
def test_find_modifiers_nodoc
1048
comment = <<-COMMENT
1056
parser = util_parser ''
1057
method_obj = RDoc::AnyMethod.new nil, 'blah'
1059
parser.find_modifiers comment, method_obj
1061
assert_equal nil, method_obj.document_self
1064
def test_find_modifiers_yields
1065
comment = <<-COMMENT
1073
parser = util_parser ''
1074
method_obj = RDoc::AnyMethod.new nil, 'blah'
1076
parser.find_modifiers comment, method_obj
1078
assert_equal 'a, b', method_obj.block_params
1080
expected = <<-EXPECTED
1088
assert_equal expected, comment
1091
def test_handle_method_args_minus_1
1092
parser = util_parser "Document-method: Object#m\n blah */"
1094
parser.content = <<-BODY
1096
rb_other(VALUE obj) {
1097
rb_funcall(obj, rb_intern("other"), 0);
1098
return rb_str_new2("blah, blah, blah");
1102
rb_m(int argc, VALUE *argv, VALUE obj) {
1104
rb_scan_args(argc, argv, "1", &o1, &o2);
1108
parser.handle_method 'method', 'rb_cObject', 'm', 'rb_m', -1
1110
m = @top_level.find_module_named('Object').method_list.first
1112
assert_equal 'm', m.name
1113
assert_equal @top_level, m.file
1114
assert_equal 115, m.offset
1115
assert_equal 7, m.line
1117
assert_equal '(p1)', m.params
1120
def test_handle_method_args_0
1121
parser = util_parser "Document-method: BasicObject#==\n blah */"
1123
parser.handle_method 'method', 'rb_cBasicObject', '==', 'rb_obj_equal', 0
1125
bo = @top_level.find_module_named 'BasicObject'
1127
assert_equal 1, bo.method_list.length
1129
equals2 = bo.method_list.first
1131
assert_equal '()', equals2.params
1134
def test_handle_method_args_1
1135
parser = util_parser "Document-method: BasicObject#==\n blah */"
1137
parser.handle_method 'method', 'rb_cBasicObject', '==', 'rb_obj_equal', 1
1139
bo = @top_level.find_module_named 'BasicObject'
1141
assert_equal 1, bo.method_list.length
1143
equals2 = bo.method_list.first
1145
assert_equal '(p1)', equals2.params
1148
def test_handle_method_args_2
1149
parser = util_parser "Document-method: BasicObject#==\n blah */"
1151
parser.handle_method 'method', 'rb_cBasicObject', '==', 'rb_obj_equal', 2
1153
bo = @top_level.find_module_named 'BasicObject'
1155
assert_equal 1, bo.method_list.length
1157
equals2 = bo.method_list.first
1159
assert_equal '(p1, p2)', equals2.params
1162
# test_handle_args_minus_1 handled by test_handle_method
1164
def test_handle_method_args_minus_2
1165
parser = util_parser "Document-method: BasicObject#==\n blah */"
1167
parser.handle_method 'method', 'rb_cBasicObject', '==', 'rb_obj_equal', -2
1169
bo = @top_level.find_module_named 'BasicObject'
1171
assert_equal 1, bo.method_list.length
1173
equals2 = bo.method_list.first
1175
assert_equal '(*args)', equals2.params
1178
def test_handle_method_initialize
1179
parser = util_parser "Document-method: BasicObject::new\n blah */"
1181
parser.handle_method('private_method', 'rb_cBasicObject',
1182
'initialize', 'rb_obj_dummy', -1)
1184
bo = @top_level.find_module_named 'BasicObject'
1186
assert_equal 1, bo.method_list.length
1188
new = bo.method_list.first
1190
assert_equal 'new', new.name
1191
assert_equal :public, new.visibility
1194
def test_handle_singleton
1195
parser = util_parser <<-SINGLE
1196
void Init_Blah(void) {
1197
cDate = rb_define_class("Date", rb_cObject);
1198
sDate = rb_singleton_class(cDate);
1204
assert_equal 'Date', parser.known_classes['sDate']
1205
assert_equal 'Date', parser.singleton_classes['sDate']
409
1208
def test_look_for_directives_in
472
1304
assert_equal "Method Comment! ", read_method.comment
1307
def test_define_method_private_singleton
1309
/*Method Comment! */
1311
rb_io_s_read(argc, argv, io)
1321
* a comment for class Foo on rb_define_class
1323
VALUE rb_cIO = rb_define_class("IO", rb_cObject);
1324
VALUE rb_cIO_s = rb_singleton_class(rb_cIO);
1325
rb_define_private_method(rb_cIO_s, "read", rb_io_s_read, -1);
1329
klass = util_get_class content, 'rb_cIO'
1330
read_method = klass.method_list.first
1331
assert_equal "read", read_method.name
1332
assert_equal "Method Comment! ", read_method.comment
1333
assert_equal :private, read_method.visibility
1334
assert read_method.singleton
1337
def test_define_method_singleton
1339
/*Method Comment! */
1341
rb_io_s_read(argc, argv, io)
1351
* a comment for class Foo on rb_define_class
1353
VALUE rb_cIO = rb_define_class("IO", rb_cObject);
1354
VALUE rb_cIO_s = rb_singleton_class(rb_cIO);
1355
rb_define_method(rb_cIO_s, "read", rb_io_s_read, -1);
1359
klass = util_get_class content, 'rb_cIO'
1360
read_method = klass.method_list.first
1361
assert_equal "read", read_method.name
1362
assert_equal "Method Comment! ", read_method.comment
1363
assert read_method.singleton
1366
def test_rb_scan_args
1367
parser = util_parser ''
1369
assert_equal '(p1)',
1370
parser.rb_scan_args('rb_scan_args(a, b, "1",)')
1371
assert_equal '(p1, p2)',
1372
parser.rb_scan_args('rb_scan_args(a, b, "2",)')
1374
assert_equal '(p1 = v1)',
1375
parser.rb_scan_args('rb_scan_args(a, b, "01",)')
1376
assert_equal '(p1 = v1, p2 = v2)',
1377
parser.rb_scan_args('rb_scan_args(a, b, "02",)')
1379
assert_equal '(p1, p2 = v2)',
1380
parser.rb_scan_args('rb_scan_args(a, b, "11",)')
1382
assert_equal '(p1, *args)',
1383
parser.rb_scan_args('rb_scan_args(a, b, "1*",)')
1384
assert_equal '(p1, p2 = {})',
1385
parser.rb_scan_args('rb_scan_args(a, b, "1:",)')
1386
assert_equal '(p1, &block)',
1387
parser.rb_scan_args('rb_scan_args(a, b, "1&",)')
1389
assert_equal '(p1, p2)',
1390
parser.rb_scan_args('rb_scan_args(a, b, "101",)')
1392
assert_equal '(p1, p2 = v2, p3)',
1393
parser.rb_scan_args('rb_scan_args(a, b, "111",)')
1395
assert_equal '(p1, *args, p3)',
1396
parser.rb_scan_args('rb_scan_args(a, b, "1*1",)')
1398
assert_equal '(p1, p2 = v2, *args)',
1399
parser.rb_scan_args('rb_scan_args(a, b, "11*",)')
1400
assert_equal '(p1, p2 = v2, p3 = {})',
1401
parser.rb_scan_args('rb_scan_args(a, b, "11:",)')
1402
assert_equal '(p1, p2 = v2, &block)',
1403
parser.rb_scan_args('rb_scan_args(a, b, "11&",)')
1405
assert_equal '(p1, p2 = v2, *args, p4, p5 = {}, &block)',
1406
parser.rb_scan_args('rb_scan_args(a, b, "11*1:&",)')
1408
# The following aren't valid according to spec but are according to the
1410
assert_equal '(*args)',
1411
parser.rb_scan_args('rb_scan_args(a, b, "*",)')
1412
assert_equal '(p1 = {})',
1413
parser.rb_scan_args('rb_scan_args(a, b, ":",)')
1414
assert_equal '(&block)',
1415
parser.rb_scan_args('rb_scan_args(a, b, "&",)')
1417
assert_equal '(*args, p2 = {})',
1418
parser.rb_scan_args('rb_scan_args(a, b, "*:",)')
1419
assert_equal '(p1 = {}, &block)',
1420
parser.rb_scan_args('rb_scan_args(a, b, ":&",)')
1421
assert_equal '(*args, p2 = {}, &block)',
1422
parser.rb_scan_args('rb_scan_args(a, b, "*:&",)')
475
1425
def util_get_class(content, name)
476
1426
@parser = util_parser content