6 require 'rdoc/parser/ruby'
9 class TestRdocParserRuby < Test::Unit::TestCase
12 @tempfile = Tempfile.new self.class.name
13 @filename = @tempfile.path
16 @options = RDoc::Options.new
18 @stats = RDoc::Stats.new 0
25 def test_look_for_directives_in_commented
28 comment = "# how to make a section:\n# # :section: new section\n"
30 @parser.look_for_directives_in @top_level, comment
32 section = @top_level.current_section
33 assert_equal nil, section.title
34 assert_equal nil, section.comment
36 assert_equal "# how to make a section:\n# # :section: new section\n",
40 def test_look_for_directives_in_enddoc
43 assert_throws :enddoc do
44 @parser.look_for_directives_in @top_level, "# :enddoc:\n"
48 def test_look_for_directives_in_main
51 @parser.look_for_directives_in @top_level, "# :main: new main page\n"
53 assert_equal 'new main page', @options.main_page
56 def test_look_for_directives_in_method
59 comment = "# :method: my_method\n"
61 @parser.look_for_directives_in @top_level, comment
63 assert_equal "# :method: my_method\n", comment
65 comment = "# :singleton-method: my_method\n"
67 @parser.look_for_directives_in @top_level, comment
69 assert_equal "# :singleton-method: my_method\n", comment
72 def test_look_for_directives_in_startdoc
76 assert !@top_level.document_self
77 assert !@top_level.document_children
78 assert !@top_level.force_documentation
80 @parser.look_for_directives_in @top_level, "# :startdoc:\n"
82 assert @top_level.document_self
83 assert @top_level.document_children
84 assert @top_level.force_documentation
87 def test_look_for_directives_in_stopdoc
90 assert @top_level.document_self
91 assert @top_level.document_children
93 @parser.look_for_directives_in @top_level, "# :stopdoc:\n"
95 assert !@top_level.document_self
96 assert !@top_level.document_children
99 def test_look_for_directives_in_section
102 comment = "# :section: new section\n# woo stuff\n"
104 @parser.look_for_directives_in @top_level, comment
106 section = @top_level.current_section
107 assert_equal 'new section', section.title
108 assert_equal "# woo stuff\n", section.comment
110 assert_equal '', comment
113 def test_look_for_directives_in_title
116 @parser.look_for_directives_in @top_level, "# :title: new title\n"
118 assert_equal 'new title', @options.title
121 def test_look_for_directives_in_unhandled
124 comment = "# :unhandled: \n# :title: hi\n"
126 @parser.look_for_directives_in @top_level, comment
128 assert_equal "# :unhandled: \n", comment
130 assert_equal 'hi', @options.title
134 comment = "##\n# my method\n"
136 util_parser 'class Foo; end'
140 @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
142 foo = @top_level.classes.first
143 assert_equal 'Foo', foo.full_name
144 assert_equal comment, foo.comment
147 def test_parse_class_nested_superclass
148 foo = RDoc::NormalModule.new 'Foo'
149 foo.parent = @top_level
151 util_parser "class Bar < Super\nend"
155 @parser.parse_class foo, RDoc::Parser::Ruby::NORMAL, tk, ''
157 bar = foo.classes.first
158 assert_equal 'Super', bar.superclass
161 def test_parse_comment
170 klass = RDoc::NormalClass.new 'Foo'
171 klass.parent = @top_level
173 comment = "##\n# :method: foo\n# my method\n"
179 @parser.parse_comment klass, tk, comment
181 foo = klass.method_list.first
182 assert_equal 'foo', foo.name
183 assert_equal comment, foo.comment
185 assert_equal [], foo.aliases
186 assert_equal nil, foo.block_params
187 assert_equal nil, foo.call_seq
188 assert_equal nil, foo.is_alias_for
189 assert_equal nil, foo.viewer
190 assert_equal true, foo.document_children
191 assert_equal true, foo.document_self
192 assert_equal '', foo.params
193 assert_equal false, foo.done_documenting
194 assert_equal false, foo.dont_rename_initialize
195 assert_equal false, foo.force_documentation
196 assert_equal klass, foo.parent
197 assert_equal false, foo.singleton
198 assert_equal :public, foo.visibility
199 assert_equal "\n", foo.text
200 assert_equal klass.current_section, foo.section
203 tk(:COMMENT, 1, 1, nil, "# File #{@top_level.file_absolute_name}, line 1"),
204 RDoc::Parser::Ruby::NEWLINE_TOKEN,
205 tk(:SPACE, 1, 1, nil, ''),
208 assert_equal stream, foo.token_stream
211 def test_parse_meta_method
212 klass = RDoc::NormalClass.new 'Foo'
213 klass.parent = @top_level
215 comment = "##\n# my method\n"
217 util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
221 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
223 foo = klass.method_list.first
224 assert_equal 'foo', foo.name
225 assert_equal comment, foo.comment
227 assert_equal [], foo.aliases
228 assert_equal nil, foo.block_params
229 assert_equal nil, foo.call_seq
230 assert_equal true, foo.document_children
231 assert_equal true, foo.document_self
232 assert_equal false, foo.done_documenting
233 assert_equal false, foo.dont_rename_initialize
234 assert_equal false, foo.force_documentation
235 assert_equal nil, foo.is_alias_for
236 assert_equal '', foo.params
237 assert_equal klass, foo.parent
238 assert_equal false, foo.singleton
239 assert_equal 'add_my_method :foo', foo.text
240 assert_equal nil, foo.viewer
241 assert_equal :public, foo.visibility
242 assert_equal klass.current_section, foo.section
245 tk(:COMMENT, 1, 1, nil, "# File #{@top_level.file_absolute_name}, line 1"),
246 RDoc::Parser::Ruby::NEWLINE_TOKEN,
247 tk(:SPACE, 1, 1, nil, ''),
248 tk(:IDENTIFIER, 1, 0, 'add_my_method', 'add_my_method'),
249 tk(:SPACE, 1, 13, nil, ' '),
250 tk(:SYMBOL, 1, 14, nil, ':foo'),
251 tk(:COMMA, 1, 18, nil, ','),
252 tk(:SPACE, 1, 19, nil, ' '),
253 tk(:SYMBOL, 1, 20, nil, ':bar'),
254 tk(:NL, 1, 24, nil, "\n"),
257 assert_equal stream, foo.token_stream
260 def test_parse_meta_method_name
261 klass = RDoc::NormalClass.new 'Foo'
262 klass.parent = @top_level
264 comment = "##\n# :method: woo_hoo!\n# my method\n"
266 util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
270 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
272 foo = klass.method_list.first
273 assert_equal 'woo_hoo!', foo.name
274 assert_equal "##\n# my method\n", foo.comment
277 def test_parse_meta_method_singleton
278 klass = RDoc::NormalClass.new 'Foo'
279 klass.parent = @top_level
281 comment = "##\n# :singleton-method:\n# my method\n"
283 util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
287 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
289 foo = klass.method_list.first
290 assert_equal 'foo', foo.name
291 assert_equal true, foo.singleton, 'singleton method'
292 assert_equal "##\n# my method\n", foo.comment
295 def test_parse_meta_method_singleton_name
296 klass = RDoc::NormalClass.new 'Foo'
297 klass.parent = @top_level
299 comment = "##\n# :singleton-method: woo_hoo!\n# my method\n"
301 util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
305 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
307 foo = klass.method_list.first
308 assert_equal 'woo_hoo!', foo.name
309 assert_equal true, foo.singleton, 'singleton method'
310 assert_equal "##\n# my method\n", foo.comment
313 def test_parse_meta_method_string_name
314 klass = RDoc::NormalClass.new 'Foo'
315 comment = "##\n# my method\n"
317 util_parser "add_my_method 'foo'"
321 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
323 foo = klass.method_list.first
324 assert_equal 'foo', foo.name
325 assert_equal comment, foo.comment
328 def test_parse_method
329 klass = RDoc::NormalClass.new 'Foo'
330 klass.parent = @top_level
332 comment = "##\n# my method\n"
334 util_parser "def foo() :bar end"
338 @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
340 foo = klass.method_list.first
341 assert_equal 'foo', foo.name
342 assert_equal comment, foo.comment
344 assert_equal [], foo.aliases
345 assert_equal nil, foo.block_params
346 assert_equal nil, foo.call_seq
347 assert_equal nil, foo.is_alias_for
348 assert_equal nil, foo.viewer
349 assert_equal true, foo.document_children
350 assert_equal true, foo.document_self
351 assert_equal '()', foo.params
352 assert_equal false, foo.done_documenting
353 assert_equal false, foo.dont_rename_initialize
354 assert_equal false, foo.force_documentation
355 assert_equal klass, foo.parent
356 assert_equal false, foo.singleton
357 assert_equal :public, foo.visibility
358 assert_equal 'def foo', foo.text
359 assert_equal klass.current_section, foo.section
362 tk(:COMMENT, 1, 1, nil, "# File #{@top_level.file_absolute_name}, line 1"),
363 RDoc::Parser::Ruby::NEWLINE_TOKEN,
364 tk(:SPACE, 1, 1, nil, ''),
365 tk(:DEF, 1, 0, 'def', 'def'),
366 tk(:SPACE, 1, 3, nil, ' '),
367 tk(:IDENTIFIER, 1, 4, 'foo', 'foo'),
368 tk(:LPAREN, 1, 7, nil, '('),
369 tk(:RPAREN, 1, 8, nil, ')'),
370 tk(:SPACE, 1, 9, nil, ' '),
371 tk(:COLON, 1, 10, nil, ':'),
372 tk(:IDENTIFIER, 1, 11, 'bar', 'bar'),
373 tk(:SPACE, 1, 14, nil, ' '),
374 tk(:END, 1, 15, 'end', 'end'),
377 assert_equal stream, foo.token_stream
380 def test_parse_statements_class_nested
381 comment = "##\n# my method\n"
383 util_parser "module Foo\n#{comment}class Bar\nend\nend"
385 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
387 foo = @top_level.modules.first
388 assert_equal 'Foo', foo.full_name, 'module Foo'
390 bar = foo.classes.first
391 assert_equal 'Foo::Bar', bar.full_name, 'class Foo::Bar'
392 assert_equal comment, bar.comment
395 def test_parse_statements_identifier_meta_method
406 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
408 foo = @top_level.classes.first.method_list.first
409 assert_equal 'foo', foo.name
412 def test_parse_statements_identifier_alias_method
413 content = "class Foo def foo() end; alias_method :foo2, :foo end"
417 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
419 foo2 = @top_level.classes.first.method_list.last
420 assert_equal 'foo2', foo2.name
421 assert_equal 'foo', foo2.is_alias_for.name
424 def test_parse_statements_identifier_attr
425 content = "class Foo; attr :foo; end"
429 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
431 foo = @top_level.classes.first.attributes.first
432 assert_equal 'foo', foo.name
433 assert_equal 'R', foo.rw
436 def test_parse_statements_identifier_attr_accessor
437 content = "class Foo; attr_accessor :foo; end"
441 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
443 foo = @top_level.classes.first.attributes.first
444 assert_equal 'foo', foo.name
445 assert_equal 'RW', foo.rw
448 def test_parse_statements_identifier_extra_accessors
449 @options.extra_accessors = /^my_accessor$/
451 content = "class Foo; my_accessor :foo; end"
455 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
457 foo = @top_level.classes.first.attributes.first
458 assert_equal 'foo', foo.name
459 assert_equal '?', foo.rw
462 def test_parse_statements_identifier_include
463 content = "class Foo; include Bar; end"
467 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
469 foo = @top_level.classes.first
470 assert_equal 'Foo', foo.name
471 assert_equal 1, foo.includes.length
474 def test_parse_statements_identifier_module_function
475 content = "module Foo def foo() end; module_function :foo; end"
479 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
481 foo, s_foo = @top_level.modules.first.method_list
482 assert_equal 'foo', foo.name, 'instance method name'
483 assert_equal :private, foo.visibility, 'instance method visibility'
484 assert_equal false, foo.singleton, 'instance method singleton'
486 assert_equal 'foo', s_foo.name, 'module function name'
487 assert_equal :public, s_foo.visibility, 'module function visibility'
488 assert_equal true, s_foo.singleton, 'module function singleton'
491 def test_parse_statements_identifier_private
492 content = "class Foo private; def foo() end end"
496 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
498 foo = @top_level.classes.first.method_list.first
499 assert_equal 'foo', foo.name
500 assert_equal :private, foo.visibility
503 def test_parse_statements_identifier_require
504 content = "require 'bar'"
508 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
510 assert_equal 1, @top_level.requires.length
513 def tk(klass, line, char, name, text)
514 klass = RDoc::RubyToken.const_get "Tk#{klass.to_s.upcase}"
516 token = if klass.instance_method(:initialize).arity == 2 then
517 raise ArgumentError, "name not used for #{klass}" unless name.nil?
520 klass.new line, char, name
528 def util_parser(content)
529 @parser = RDoc::Parser::Ruby.new @top_level, @filename, content, @options,
535 @top_level = RDoc::TopLevel.new @filename