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 Hash.new
18 @stats = RDoc::Stats.new
20 @progress = StringIO.new
27 def test_look_for_directives_in_commented
30 comment = "# how to make a section:\n# # :section: new section\n"
32 @parser.look_for_directives_in @top_level, comment
34 section = @top_level.current_section
35 assert_equal nil, section.title
36 assert_equal nil, section.comment
38 assert_equal "# how to make a section:\n# # :section: new section\n",
42 def test_look_for_directives_in_enddoc
45 assert_throws :enddoc do
46 @parser.look_for_directives_in @top_level, "# :enddoc:\n"
50 def test_look_for_directives_in_main
53 @parser.look_for_directives_in @top_level, "# :main: new main page\n"
55 assert_equal 'new main page', @options.main_page
58 def test_look_for_directives_in_method
61 comment = "# :method: my_method\n"
63 @parser.look_for_directives_in @top_level, comment
65 assert_equal "# :method: my_method\n", comment
67 comment = "# :singleton-method: my_method\n"
69 @parser.look_for_directives_in @top_level, comment
71 assert_equal "# :singleton-method: my_method\n", comment
74 def test_look_for_directives_in_startdoc
78 assert !@top_level.document_self
79 assert !@top_level.document_children
80 assert !@top_level.force_documentation
82 @parser.look_for_directives_in @top_level, "# :startdoc:\n"
84 assert @top_level.document_self
85 assert @top_level.document_children
86 assert @top_level.force_documentation
89 def test_look_for_directives_in_stopdoc
92 assert @top_level.document_self
93 assert @top_level.document_children
95 @parser.look_for_directives_in @top_level, "# :stopdoc:\n"
97 assert !@top_level.document_self
98 assert !@top_level.document_children
101 def test_look_for_directives_in_section
104 comment = "# :section: new section\n# woo stuff\n"
106 @parser.look_for_directives_in @top_level, comment
108 section = @top_level.current_section
109 assert_equal 'new section', section.title
110 assert_equal "# woo stuff\n", section.comment
112 assert_equal '', comment
115 def test_look_for_directives_in_title
118 @parser.look_for_directives_in @top_level, "# :title: new title\n"
120 assert_equal 'new title', @options.title
123 def test_look_for_directives_in_unhandled
126 comment = "# :unhandled: \n# :title: hi\n"
128 @parser.look_for_directives_in @top_level, comment
130 assert_equal "# :unhandled: \n", comment
132 assert_equal 'hi', @options.title
135 def test_parse_meta_method
136 klass = RDoc::NormalClass.new 'Foo'
137 klass.parent = @top_level
139 comment = "##\n# my method\n"
141 util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
145 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
147 foo = klass.method_list.first
148 assert_equal 'foo', foo.name
149 assert_equal comment, foo.comment
151 assert_equal [], foo.aliases
152 assert_equal nil, foo.block_params
153 assert_equal nil, foo.call_seq
154 assert_equal true, foo.document_children
155 assert_equal true, foo.document_self
156 assert_equal false, foo.done_documenting
157 assert_equal false, foo.dont_rename_initialize
158 assert_equal false, foo.force_documentation
159 assert_equal nil, foo.is_alias_for
160 assert_equal '', foo.params
161 assert_equal klass, foo.parent
162 assert_equal false, foo.singleton
163 assert_equal 'add_my_method :foo', foo.text
164 assert_equal nil, foo.viewer
165 assert_equal :public, foo.visibility
166 assert_equal klass.current_section, foo.section
169 tk(:COMMENT, 1, 1, nil, "# File #{@top_level.file_absolute_name}, line 1"),
170 RDoc::Parser::Ruby::NEWLINE_TOKEN,
171 tk(:SPACE, 1, 1, nil, ''),
172 tk(:IDENTIFIER, 1, 0, 'add_my_method', 'add_my_method'),
173 tk(:SPACE, 1, 13, nil, ' '),
174 tk(:SYMBOL, 1, 14, nil, ':foo'),
175 tk(:COMMA, 1, 18, nil, ','),
176 tk(:SPACE, 1, 19, nil, ' '),
177 tk(:SYMBOL, 1, 20, nil, ':bar'),
178 tk(:NL, 1, 24, nil, "\n"),
181 assert_equal stream, foo.token_stream
184 def test_parse_meta_method_name
185 klass = RDoc::NormalClass.new 'Foo'
186 klass.parent = @top_level
188 comment = "##\n# :method: woo_hoo!\n# my method\n"
190 util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
194 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
196 foo = klass.method_list.first
197 assert_equal 'woo_hoo!', foo.name
198 assert_equal "##\n# my method\n", foo.comment
201 def test_parse_meta_method_singleton
202 klass = RDoc::NormalClass.new 'Foo'
203 klass.parent = @top_level
205 comment = "##\n# :singleton-method:\n# my method\n"
207 util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
211 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
213 foo = klass.method_list.first
214 assert_equal 'foo', foo.name
215 assert_equal true, foo.singleton, 'singleton method'
216 assert_equal "##\n# my method\n", foo.comment
219 def test_parse_meta_method_singleton_name
220 klass = RDoc::NormalClass.new 'Foo'
221 klass.parent = @top_level
223 comment = "##\n# :singleton-method: woo_hoo!\n# my method\n"
225 util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
229 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
231 foo = klass.method_list.first
232 assert_equal 'woo_hoo!', foo.name
233 assert_equal true, foo.singleton, 'singleton method'
234 assert_equal "##\n# my method\n", foo.comment
237 def test_parse_meta_method_string_name
238 klass = RDoc::NormalClass.new 'Foo'
239 comment = "##\n# my method\n"
241 util_parser "add_my_method 'foo'"
245 @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
247 foo = klass.method_list.first
248 assert_equal 'foo', foo.name
249 assert_equal comment, foo.comment
252 def test_parse_method
253 klass = RDoc::NormalClass.new 'Foo'
254 klass.parent = @top_level
256 comment = "##\n# my method\n"
258 util_parser "def foo() :bar end"
262 @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
264 foo = klass.method_list.first
265 assert_equal 'foo', foo.name
266 assert_equal comment, foo.comment
268 assert_equal [], foo.aliases
269 assert_equal nil, foo.block_params
270 assert_equal nil, foo.call_seq
271 assert_equal nil, foo.is_alias_for
272 assert_equal nil, foo.viewer
273 assert_equal true, foo.document_children
274 assert_equal true, foo.document_self
275 assert_equal '()', foo.params
276 assert_equal false, foo.done_documenting
277 assert_equal false, foo.dont_rename_initialize
278 assert_equal false, foo.force_documentation
279 assert_equal klass, foo.parent
280 assert_equal false, foo.singleton
281 assert_equal :public, foo.visibility
282 assert_equal 'def foo', foo.text
283 assert_equal klass.current_section, foo.section
286 tk(:COMMENT, 1, 1, nil, "# File #{@top_level.file_absolute_name}, line 1"),
287 RDoc::Parser::Ruby::NEWLINE_TOKEN,
288 tk(:SPACE, 1, 1, nil, ''),
289 tk(:DEF, 1, 0, 'def', 'def'),
290 tk(:SPACE, 1, 3, nil, ' '),
291 tk(:IDENTIFIER, 1, 4, 'foo', 'foo'),
292 tk(:LPAREN, 1, 7, nil, '('),
293 tk(:RPAREN, 1, 8, nil, ')'),
294 tk(:SPACE, 1, 9, nil, ' '),
295 tk(:COLON, 1, 10, nil, ':'),
296 tk(:IDENTIFIER, 1, 11, 'bar', 'bar'),
297 tk(:SPACE, 1, 14, nil, ' '),
298 tk(:END, 1, 15, 'end', 'end'),
301 assert_equal stream, foo.token_stream
304 def test_parse_statements_comment
313 klass = RDoc::NormalClass.new 'Foo'
314 klass.parent = @top_level
316 comment = "##\n# :method: foo\n# my method\n"
322 @parser.parse_comment klass, tk, comment
324 foo = klass.method_list.first
325 assert_equal 'foo', foo.name
326 assert_equal comment, foo.comment
328 assert_equal [], foo.aliases
329 assert_equal nil, foo.block_params
330 assert_equal nil, foo.call_seq
331 assert_equal nil, foo.is_alias_for
332 assert_equal nil, foo.viewer
333 assert_equal true, foo.document_children
334 assert_equal true, foo.document_self
335 assert_equal '', foo.params
336 assert_equal false, foo.done_documenting
337 assert_equal false, foo.dont_rename_initialize
338 assert_equal false, foo.force_documentation
339 assert_equal klass, foo.parent
340 assert_equal false, foo.singleton
341 assert_equal :public, foo.visibility
342 assert_equal "\n", foo.text
343 assert_equal klass.current_section, foo.section
346 tk(:COMMENT, 1, 1, nil, "# File #{@top_level.file_absolute_name}, line 1"),
347 RDoc::Parser::Ruby::NEWLINE_TOKEN,
348 tk(:SPACE, 1, 1, nil, ''),
351 assert_equal stream, foo.token_stream
354 def test_parse_statements_identifier_meta_method
365 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
367 foo = @top_level.classes.first.method_list.first
368 assert_equal 'foo', foo.name
371 def test_parse_statements_identifier_alias_method
372 content = "class Foo def foo() end; alias_method :foo2, :foo end"
376 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
378 foo2 = @top_level.classes.first.method_list.last
379 assert_equal 'foo2', foo2.name
380 assert_equal 'foo', foo2.is_alias_for.name
383 def test_parse_statements_identifier_attr
384 content = "class Foo; attr :foo; end"
388 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
390 foo = @top_level.classes.first.attributes.first
391 assert_equal 'foo', foo.name
392 assert_equal 'R', foo.rw
395 def test_parse_statements_identifier_attr_accessor
396 content = "class Foo; attr_accessor :foo; end"
400 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
402 foo = @top_level.classes.first.attributes.first
403 assert_equal 'foo', foo.name
404 assert_equal 'RW', foo.rw
407 def test_parse_statements_identifier_extra_accessors
408 @options.extra_accessors = /^my_accessor$/
410 content = "class Foo; my_accessor :foo; end"
414 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
416 foo = @top_level.classes.first.attributes.first
417 assert_equal 'foo', foo.name
418 assert_equal '?', foo.rw
421 def test_parse_statements_identifier_include
422 content = "class Foo; include Bar; end"
426 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
428 foo = @top_level.classes.first
429 assert_equal 'Foo', foo.name
430 assert_equal 1, foo.includes.length
433 def test_parse_statements_identifier_module_function
434 content = "module Foo def foo() end; module_function :foo; end"
438 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
440 foo, s_foo = @top_level.modules.first.method_list
441 assert_equal 'foo', foo.name, 'instance method name'
442 assert_equal :private, foo.visibility, 'instance method visibility'
443 assert_equal false, foo.singleton, 'instance method singleton'
445 assert_equal 'foo', s_foo.name, 'module function name'
446 assert_equal :public, s_foo.visibility, 'module function visibility'
447 assert_equal true, s_foo.singleton, 'module function singleton'
450 def test_parse_statements_identifier_private
451 content = "class Foo private; def foo() end end"
455 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
457 foo = @top_level.classes.first.method_list.first
458 assert_equal 'foo', foo.name
459 assert_equal :private, foo.visibility
462 def test_parse_statements_identifier_require
463 content = "require 'bar'"
467 @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
469 assert_equal 1, @top_level.requires.length
472 def tk(klass, line, char, name, text)
473 klass = RDoc::RubyToken.const_get "Tk#{klass.to_s.upcase}"
475 token = if klass.instance_method(:initialize).arity == 2 then
476 raise ArgumentError, "name not used for #{klass}" unless name.nil?
479 klass.new line, char, name
487 def util_parser(content)
488 @parser = RDoc::Parser::Ruby.new @top_level, @filename, content, @options,
490 @parser.progress = @progress
496 @top_level = RDoc::TopLevel.new @filename