PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jruby-1.7.3/test/externals/ruby1.9/rdoc/test_rdoc_parser_ruby.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 2135 lines | 1390 code | 649 blank | 96 comment | 2 complexity | 4450b41ba18052a6e5699dba6aed1743 MD5 | raw file
  1. # coding: utf-8
  2. require 'stringio'
  3. require 'tempfile'
  4. require 'rubygems'
  5. require 'minitest/autorun'
  6. require 'rdoc/options'
  7. require 'rdoc/parser/ruby'
  8. require 'rdoc/stats'
  9. class TestRDocParserRuby < MiniTest::Unit::TestCase
  10. def setup
  11. @tempfile = Tempfile.new self.class.name
  12. @filename = @tempfile.path
  13. # Some tests need two paths.
  14. @tempfile2 = Tempfile.new self.class.name
  15. @filename2 = @tempfile2.path
  16. util_top_level
  17. @options = RDoc::Options.new
  18. @options.quiet = true
  19. @options.option_parser = OptionParser.new
  20. @stats = RDoc::Stats.new 0
  21. end
  22. def teardown
  23. @tempfile.close
  24. @tempfile2.close
  25. end
  26. def test_collect_first_comment
  27. p = util_parser <<-CONTENT
  28. # first
  29. # second
  30. class C; end
  31. CONTENT
  32. comment = p.collect_first_comment
  33. assert_equal "# first\n", comment
  34. end
  35. def test_collect_first_comment_encoding
  36. skip "Encoding not implemented" unless Object.const_defined? :Encoding
  37. @options.encoding = Encoding::CP852
  38. p = util_parser <<-CONTENT
  39. # first
  40. # second
  41. class C; end
  42. CONTENT
  43. comment = p.collect_first_comment
  44. assert_equal Encoding::CP852, comment.encoding
  45. end
  46. def test_extract_call_seq
  47. m = RDoc::AnyMethod.new nil, 'm'
  48. p = util_parser ''
  49. comment = <<-COMMENT
  50. # call-seq:
  51. # bla => true or false
  52. #
  53. # moar comment
  54. COMMENT
  55. p.extract_call_seq comment, m
  56. assert_equal "bla => true or false\n", m.call_seq
  57. end
  58. def test_extract_call_seq_blank
  59. m = RDoc::AnyMethod.new nil, 'm'
  60. p = util_parser ''
  61. comment = <<-COMMENT
  62. # call-seq:
  63. # bla => true or false
  64. #
  65. COMMENT
  66. p.extract_call_seq comment, m
  67. assert_equal "bla => true or false\n", m.call_seq
  68. end
  69. def test_extract_call_seq_no_blank
  70. m = RDoc::AnyMethod.new nil, 'm'
  71. p = util_parser ''
  72. comment = <<-COMMENT
  73. # call-seq:
  74. # bla => true or false
  75. COMMENT
  76. p.extract_call_seq comment, m
  77. assert_equal "bla => true or false\n", m.call_seq
  78. end
  79. def test_extract_call_seq_undent
  80. m = RDoc::AnyMethod.new nil, 'm'
  81. p = util_parser ''
  82. comment = <<-COMMENT
  83. # call-seq:
  84. # bla => true or false
  85. # moar comment
  86. COMMENT
  87. p.extract_call_seq comment, m
  88. assert_equal "bla => true or false\nmoar comment\n", m.call_seq
  89. end
  90. def test_get_symbol_or_name
  91. util_parser "* & | + 5 / 4"
  92. assert_equal '*', @parser.get_symbol_or_name
  93. @parser.skip_tkspace
  94. assert_equal '&', @parser.get_symbol_or_name
  95. @parser.skip_tkspace
  96. assert_equal '|', @parser.get_symbol_or_name
  97. @parser.skip_tkspace
  98. assert_equal '+', @parser.get_symbol_or_name
  99. @parser.skip_tkspace
  100. @parser.get_tk
  101. @parser.skip_tkspace
  102. assert_equal '/', @parser.get_symbol_or_name
  103. end
  104. def test_look_for_directives_in_attr
  105. util_parser ""
  106. comment = "# :attr: my_attr\n"
  107. @parser.look_for_directives_in @top_level, comment
  108. assert_equal "# :attr: my_attr\n", comment
  109. comment = "# :attr_reader: my_method\n"
  110. @parser.look_for_directives_in @top_level, comment
  111. assert_equal "# :attr_reader: my_method\n", comment
  112. comment = "# :attr_writer: my_method\n"
  113. @parser.look_for_directives_in @top_level, comment
  114. assert_equal "# :attr_writer: my_method\n", comment
  115. end
  116. def test_remove_private_comments
  117. util_parser ''
  118. comment = <<-EOS
  119. # This is text
  120. #--
  121. # this is private
  122. EOS
  123. expected = <<-EOS
  124. # This is text
  125. EOS
  126. @parser.remove_private_comments(comment)
  127. assert_equal expected, comment
  128. end
  129. def test_remove_private_comments_encoding
  130. skip "Encoding not implemented" unless Object.const_defined? :Encoding
  131. util_parser ''
  132. comment = <<-EOS
  133. # This is text
  134. #--
  135. # this is private
  136. EOS
  137. comment.force_encoding Encoding::IBM437
  138. @parser.remove_private_comments comment
  139. assert_equal Encoding::IBM437, comment.encoding
  140. end
  141. def test_remove_private_comments_long
  142. util_parser ''
  143. comment = <<-EOS
  144. #-----
  145. #++
  146. # this is text
  147. #-----
  148. EOS
  149. expected = <<-EOS
  150. # this is text
  151. EOS
  152. @parser.remove_private_comments(comment)
  153. assert_equal expected, comment
  154. end
  155. def test_remove_private_comments_rule
  156. util_parser ''
  157. comment = <<-EOS
  158. # This is text with a rule:
  159. # ---
  160. # this is also text
  161. EOS
  162. expected = comment.dup
  163. @parser.remove_private_comments(comment)
  164. assert_equal expected, comment
  165. end
  166. def test_remove_private_comments_toggle
  167. util_parser ''
  168. comment = <<-EOS
  169. # This is text
  170. #--
  171. # this is private
  172. #++
  173. # This is text again.
  174. EOS
  175. expected = <<-EOS
  176. # This is text
  177. # This is text again.
  178. EOS
  179. @parser.remove_private_comments(comment)
  180. assert_equal expected, comment
  181. end
  182. def test_remove_private_comments_toggle_encoding
  183. skip "Encoding not implemented" unless Object.const_defined? :Encoding
  184. util_parser ''
  185. comment = <<-EOS
  186. # This is text
  187. #--
  188. # this is private
  189. #++
  190. # This is text again.
  191. EOS
  192. comment.force_encoding Encoding::IBM437
  193. @parser.remove_private_comments comment
  194. assert_equal Encoding::IBM437, comment.encoding
  195. end
  196. def test_remove_private_comments_toggle_encoding_ruby_bug?
  197. skip "Encoding not implemented" unless Object.const_defined? :Encoding
  198. util_parser ''
  199. comment = <<-EOS
  200. #--
  201. # this is private
  202. #++
  203. # This is text again.
  204. EOS
  205. comment.force_encoding Encoding::IBM437
  206. @parser.remove_private_comments comment
  207. assert_equal Encoding::IBM437, comment.encoding
  208. end
  209. def test_look_for_directives_in_commented
  210. util_parser ""
  211. comment = "# how to make a section:\n# # :section: new section\n"
  212. @parser.look_for_directives_in @top_level, comment
  213. section = @top_level.current_section
  214. assert_equal nil, section.title
  215. assert_equal nil, section.comment
  216. assert_equal "# how to make a section:\n# # :section: new section\n",
  217. comment
  218. end
  219. def test_look_for_directives_in_method
  220. util_parser ""
  221. comment = "# :method: my_method\n"
  222. @parser.look_for_directives_in @top_level, comment
  223. assert_equal "# :method: my_method\n", comment
  224. comment = "# :singleton-method: my_method\n"
  225. @parser.look_for_directives_in @top_level, comment
  226. assert_equal "# :singleton-method: my_method\n", comment
  227. end
  228. def test_look_for_directives_in_section
  229. util_parser ""
  230. comment = "# :section: new section\n# woo stuff\n"
  231. @parser.look_for_directives_in @top_level, comment
  232. section = @top_level.current_section
  233. assert_equal 'new section', section.title
  234. assert_equal "# woo stuff\n", section.comment
  235. assert_equal '', comment
  236. end
  237. def test_look_for_directives_in_unhandled
  238. util_parser ""
  239. @parser.look_for_directives_in @top_level, "# :unhandled: blah\n"
  240. assert_equal 'blah', @top_level.metadata['unhandled']
  241. end
  242. def test_parse_alias
  243. klass = RDoc::NormalClass.new 'Foo'
  244. klass.parent = @top_level
  245. util_parser "alias :next= :bar"
  246. tk = @parser.get_tk
  247. alas = @parser.parse_alias klass, RDoc::Parser::Ruby::NORMAL, tk, 'comment'
  248. assert_equal 'bar', alas.old_name
  249. assert_equal 'next=', alas.new_name
  250. assert_equal klass, alas.parent
  251. assert_equal 'comment', alas.comment
  252. assert_equal @top_level, alas.file
  253. assert_equal 0, alas.offset
  254. assert_equal 1, alas.line
  255. end
  256. def test_parse_alias_singleton
  257. klass = RDoc::NormalClass.new 'Foo'
  258. klass.parent = @top_level
  259. util_parser "alias :next= :bar"
  260. tk = @parser.get_tk
  261. alas = @parser.parse_alias klass, RDoc::Parser::Ruby::SINGLE, tk, 'comment'
  262. assert_equal 'bar', alas.old_name
  263. assert_equal 'next=', alas.new_name
  264. assert_equal klass, alas.parent
  265. assert_equal 'comment', alas.comment
  266. assert_equal @top_level, alas.file
  267. assert alas.singleton
  268. end
  269. def test_parse_alias_stopdoc
  270. klass = RDoc::NormalClass.new 'Foo'
  271. klass.parent = @top_level
  272. klass.stop_doc
  273. util_parser "alias :next= :bar"
  274. tk = @parser.get_tk
  275. @parser.parse_alias klass, RDoc::Parser::Ruby::NORMAL, tk, 'comment'
  276. assert_empty klass.aliases
  277. assert_empty klass.unmatched_alias_lists
  278. end
  279. def test_parse_alias_meta
  280. klass = RDoc::NormalClass.new 'Foo'
  281. klass.parent = @top_level
  282. util_parser "alias m.chop m"
  283. tk = @parser.get_tk
  284. alas = @parser.parse_alias klass, RDoc::Parser::Ruby::NORMAL, tk, 'comment'
  285. assert_nil alas
  286. end
  287. def test_parse_attr
  288. klass = RDoc::NormalClass.new 'Foo'
  289. klass.parent = @top_level
  290. comment = "##\n# my attr\n"
  291. util_parser "attr :foo, :bar"
  292. tk = @parser.get_tk
  293. @parser.parse_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  294. assert_equal 1, klass.attributes.length
  295. foo = klass.attributes.first
  296. assert_equal 'foo', foo.name
  297. assert_equal 'my attr', foo.comment
  298. assert_equal @top_level, foo.file
  299. assert_equal 0, foo.offset
  300. assert_equal 1, foo.line
  301. end
  302. def test_parse_attr_stopdoc
  303. klass = RDoc::NormalClass.new 'Foo'
  304. klass.parent = @top_level
  305. klass.stop_doc
  306. comment = "##\n# my attr\n"
  307. util_parser "attr :foo, :bar"
  308. tk = @parser.get_tk
  309. @parser.parse_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  310. assert_empty klass.attributes
  311. end
  312. def test_parse_attr_accessor
  313. klass = RDoc::NormalClass.new 'Foo'
  314. klass.parent = @top_level
  315. comment = "##\n# my attr\n"
  316. util_parser "attr_accessor :foo, :bar"
  317. tk = @parser.get_tk
  318. @parser.parse_attr_accessor klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  319. assert_equal 2, klass.attributes.length
  320. foo = klass.attributes.first
  321. assert_equal 'foo', foo.name
  322. assert_equal 'RW', foo.rw
  323. assert_equal 'my attr', foo.comment
  324. assert_equal @top_level, foo.file
  325. assert_equal 0, foo.offset
  326. assert_equal 1, foo.line
  327. bar = klass.attributes.last
  328. assert_equal 'bar', bar.name
  329. assert_equal 'RW', bar.rw
  330. assert_equal 'my attr', bar.comment
  331. end
  332. def test_parse_attr_accessor_nodoc
  333. klass = RDoc::NormalClass.new 'Foo'
  334. klass.parent = @top_level
  335. comment = "##\n# my attr\n"
  336. util_parser "attr_accessor :foo, :bar # :nodoc:"
  337. tk = @parser.get_tk
  338. @parser.parse_attr_accessor klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  339. assert_equal 0, klass.attributes.length
  340. end
  341. def test_parse_attr_accessor_stopdoc
  342. klass = RDoc::NormalClass.new 'Foo'
  343. klass.parent = @top_level
  344. klass.stop_doc
  345. comment = "##\n# my attr\n"
  346. util_parser "attr_accessor :foo, :bar"
  347. tk = @parser.get_tk
  348. @parser.parse_attr_accessor klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  349. assert_empty klass.attributes
  350. end
  351. def test_parse_attr_accessor_writer
  352. klass = RDoc::NormalClass.new 'Foo'
  353. klass.parent = @top_level
  354. comment = "##\n# my attr\n"
  355. util_parser "attr_writer :foo, :bar"
  356. tk = @parser.get_tk
  357. @parser.parse_attr_accessor klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  358. assert_equal 2, klass.attributes.length
  359. foo = klass.attributes.first
  360. assert_equal 'foo', foo.name
  361. assert_equal 'W', foo.rw
  362. assert_equal "my attr", foo.comment
  363. assert_equal @top_level, foo.file
  364. bar = klass.attributes.last
  365. assert_equal 'bar', bar.name
  366. assert_equal 'W', bar.rw
  367. assert_equal "my attr", bar.comment
  368. end
  369. def test_parse_meta_attr
  370. klass = RDoc::NormalClass.new 'Foo'
  371. klass.parent = @top_level
  372. comment = "##\n# :attr: \n# my method\n"
  373. util_parser "add_my_method :foo, :bar"
  374. tk = @parser.get_tk
  375. @parser.parse_meta_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  376. assert_equal 2, klass.attributes.length
  377. foo = klass.attributes.first
  378. assert_equal 'foo', foo.name
  379. assert_equal 'RW', foo.rw
  380. assert_equal "my method", foo.comment
  381. assert_equal @top_level, foo.file
  382. end
  383. def test_parse_meta_attr_accessor
  384. klass = RDoc::NormalClass.new 'Foo'
  385. klass.parent = @top_level
  386. comment = "##\n# :attr_accessor: \n# my method\n"
  387. util_parser "add_my_method :foo, :bar"
  388. tk = @parser.get_tk
  389. @parser.parse_meta_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  390. assert_equal 2, klass.attributes.length
  391. foo = klass.attributes.first
  392. assert_equal 'foo', foo.name
  393. assert_equal 'RW', foo.rw
  394. assert_equal 'my method', foo.comment
  395. assert_equal @top_level, foo.file
  396. end
  397. def test_parse_meta_attr_named
  398. klass = RDoc::NormalClass.new 'Foo'
  399. klass.parent = @top_level
  400. comment = "##\n# :attr: foo\n# my method\n"
  401. util_parser "add_my_method :foo, :bar"
  402. tk = @parser.get_tk
  403. @parser.parse_meta_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  404. assert_equal 1, klass.attributes.length
  405. foo = klass.attributes.first
  406. assert_equal 'foo', foo.name
  407. assert_equal 'RW', foo.rw
  408. assert_equal 'my method', foo.comment
  409. assert_equal @top_level, foo.file
  410. end
  411. def test_parse_meta_attr_reader
  412. klass = RDoc::NormalClass.new 'Foo'
  413. klass.parent = @top_level
  414. comment = "##\n# :attr_reader: \n# my method\n"
  415. util_parser "add_my_method :foo, :bar"
  416. tk = @parser.get_tk
  417. @parser.parse_meta_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  418. foo = klass.attributes.first
  419. assert_equal 'foo', foo.name
  420. assert_equal 'R', foo.rw
  421. assert_equal 'my method', foo.comment
  422. assert_equal @top_level, foo.file
  423. end
  424. def test_parse_meta_attr_stopdoc
  425. klass = RDoc::NormalClass.new 'Foo'
  426. klass.parent = @top_level
  427. klass.stop_doc
  428. comment = "##\n# :attr: \n# my method\n"
  429. util_parser "add_my_method :foo, :bar"
  430. tk = @parser.get_tk
  431. @parser.parse_meta_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  432. assert_empty klass.attributes
  433. end
  434. def test_parse_meta_attr_writer
  435. klass = RDoc::NormalClass.new 'Foo'
  436. klass.parent = @top_level
  437. comment = "##\n# :attr_writer: \n# my method\n"
  438. util_parser "add_my_method :foo, :bar"
  439. tk = @parser.get_tk
  440. @parser.parse_meta_attr klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  441. foo = klass.attributes.first
  442. assert_equal 'foo', foo.name
  443. assert_equal 'W', foo.rw
  444. assert_equal "my method", foo.comment
  445. assert_equal @top_level, foo.file
  446. end
  447. def test_parse_class
  448. comment = "##\n# my class\n"
  449. util_parser "class Foo\nend"
  450. tk = @parser.get_tk
  451. @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
  452. foo = @top_level.classes.first
  453. assert_equal 'Foo', foo.full_name
  454. assert_equal 'my class', foo.comment
  455. assert_equal [@top_level], foo.in_files
  456. assert_equal 0, foo.offset
  457. assert_equal 1, foo.line
  458. end
  459. def test_parse_class_ghost_method
  460. util_parser <<-CLASS
  461. class Foo
  462. ##
  463. # :method: blah
  464. # my method
  465. end
  466. CLASS
  467. tk = @parser.get_tk
  468. @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
  469. foo = @top_level.classes.first
  470. assert_equal 'Foo', foo.full_name
  471. blah = foo.method_list.first
  472. assert_equal 'Foo#blah', blah.full_name
  473. assert_equal @top_level, blah.file
  474. end
  475. def test_parse_class_multi_ghost_methods
  476. util_parser <<-'CLASS'
  477. class Foo
  478. ##
  479. # :method: one
  480. #
  481. # my method
  482. ##
  483. # :method: two
  484. #
  485. # my method
  486. [:one, :two].each do |t|
  487. eval("def #{t}; \"#{t}\"; end")
  488. end
  489. end
  490. CLASS
  491. tk = @parser.get_tk
  492. @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
  493. foo = @top_level.classes.first
  494. assert_equal 'Foo', foo.full_name
  495. assert_equal 2, foo.method_list.length
  496. end
  497. def test_parse_class_nodoc
  498. comment = "##\n# my class\n"
  499. util_parser "class Foo # :nodoc:\nend"
  500. tk = @parser.get_tk
  501. @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
  502. foo = @top_level.classes.first
  503. assert_equal 'Foo', foo.full_name
  504. assert_empty foo.comment
  505. assert_equal [@top_level], foo.in_files
  506. assert_equal 0, foo.offset
  507. assert_equal 1, foo.line
  508. end
  509. def test_parse_class_stopdoc
  510. @top_level.stop_doc
  511. comment = "##\n# my class\n"
  512. util_parser "class Foo\nend"
  513. tk = @parser.get_tk
  514. @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
  515. assert_empty @top_level.classes.first.comment
  516. end
  517. def test_parse_multi_ghost_methods
  518. util_parser <<-'CLASS'
  519. class Foo
  520. ##
  521. # :method: one
  522. #
  523. # my method
  524. ##
  525. # :method: two
  526. #
  527. # my method
  528. [:one, :two].each do |t|
  529. eval("def #{t}; \"#{t}\"; end")
  530. end
  531. end
  532. CLASS
  533. tk = @parser.get_tk
  534. @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
  535. foo = @top_level.classes.first
  536. assert_equal 'Foo', foo.full_name
  537. assert_equal 2, foo.method_list.length
  538. end
  539. def test_parse_const_fail_w_meta
  540. util_parser <<-CLASS
  541. class ConstFailMeta
  542. ##
  543. # :attr: one
  544. #
  545. # an attribute
  546. OtherModule.define_attr(self, :one)
  547. end
  548. CLASS
  549. tk = @parser.get_tk
  550. @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
  551. const_fail_meta = @top_level.classes.first
  552. assert_equal 'ConstFailMeta', const_fail_meta.full_name
  553. assert_equal 1, const_fail_meta.attributes.length
  554. end
  555. def test_parse_class_nested_superclass
  556. util_top_level
  557. foo = @top_level.add_module RDoc::NormalModule, 'Foo'
  558. util_parser "class Bar < Super\nend"
  559. tk = @parser.get_tk
  560. @parser.parse_class foo, RDoc::Parser::Ruby::NORMAL, tk, ''
  561. bar = foo.classes.first
  562. assert_equal 'Super', bar.superclass
  563. end
  564. def test_parse_module
  565. comment = "##\n# my module\n"
  566. util_parser "module Foo\nend"
  567. tk = @parser.get_tk
  568. @parser.parse_module @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
  569. foo = @top_level.modules.first
  570. assert_equal 'Foo', foo.full_name
  571. assert_equal 'my module', foo.comment
  572. end
  573. def test_parse_module_nodoc
  574. @top_level.stop_doc
  575. comment = "##\n# my module\n"
  576. util_parser "module Foo # :nodoc:\nend"
  577. tk = @parser.get_tk
  578. @parser.parse_module @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
  579. foo = @top_level.modules.first
  580. assert_equal 'Foo', foo.full_name
  581. assert_empty foo.comment
  582. end
  583. def test_parse_module_stopdoc
  584. @top_level.stop_doc
  585. comment = "##\n# my module\n"
  586. util_parser "module Foo\nend"
  587. tk = @parser.get_tk
  588. @parser.parse_module @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
  589. foo = @top_level.modules.first
  590. assert_equal 'Foo', foo.full_name
  591. assert_equal 'my module', foo.comment
  592. end
  593. def test_parse_class_colon3
  594. code = <<-CODE
  595. class A
  596. class ::B
  597. end
  598. end
  599. CODE
  600. util_parser code
  601. @parser.parse_class @top_level, false, @parser.get_tk, ''
  602. assert_equal %w[A B], RDoc::TopLevel.classes.map { |c| c.full_name }
  603. end
  604. def test_parse_class_single
  605. code = <<-CODE
  606. class A
  607. class << B
  608. end
  609. class << d = Object.new
  610. def foo; end
  611. alias bar foo
  612. end
  613. end
  614. CODE
  615. util_parser code
  616. @parser.parse_class @top_level, false, @parser.get_tk, ''
  617. assert_equal %w[A], RDoc::TopLevel.classes.map { |c| c.full_name }
  618. assert_equal %w[A::B A::d], RDoc::TopLevel.modules.map { |c| c.full_name }
  619. b = RDoc::TopLevel.modules.first
  620. assert_equal 10, b.offset
  621. assert_equal 2, b.line
  622. # make sure method/alias was not added to enclosing class/module
  623. a = RDoc::TopLevel.all_classes_hash['A']
  624. assert_empty a.method_list
  625. # make sure non-constant-named module will be removed from documentation
  626. d = RDoc::TopLevel.all_modules_hash['A::d']
  627. assert d.remove_from_documentation?
  628. end
  629. # TODO this is really a Context#add_class test
  630. def test_parse_class_object
  631. code = <<-CODE
  632. module A
  633. class B
  634. end
  635. class Object
  636. end
  637. class C < Object
  638. end
  639. end
  640. CODE
  641. util_parser code
  642. @parser.parse_module @top_level, false, @parser.get_tk, ''
  643. assert_equal %w[A], RDoc::TopLevel.modules.map { |c| c.full_name }
  644. assert_equal %w[A::B A::C A::Object], RDoc::TopLevel.classes.map { |c| c.full_name }.sort
  645. assert_equal 'Object', RDoc::TopLevel.classes_hash['A::B'].superclass
  646. assert_equal 'Object', RDoc::TopLevel.classes_hash['A::Object'].superclass
  647. assert_equal 'A::Object', RDoc::TopLevel.classes_hash['A::C'].superclass.full_name
  648. end
  649. def test_parse_class_mistaken_for_module
  650. # The code below is not strictly legal Ruby (Foo must have been defined
  651. # before Foo::Bar is encountered), but RDoc might encounter Foo::Bar
  652. # before Foo if they live in different files.
  653. code = <<-RUBY
  654. class Foo::Bar
  655. end
  656. module Foo::Baz
  657. end
  658. class Foo
  659. end
  660. RUBY
  661. util_parser code
  662. @parser.scan
  663. assert_equal %w[Foo::Baz], RDoc::TopLevel.modules_hash.keys
  664. assert_empty @top_level.modules
  665. foo = @top_level.classes.first
  666. assert_equal 'Foo', foo.full_name
  667. bar = foo.classes.first
  668. assert_equal 'Foo::Bar', bar.full_name
  669. baz = foo.modules.first
  670. assert_equal 'Foo::Baz', baz.full_name
  671. end
  672. def test_parse_class_definition_encountered_after_class_reference
  673. # The code below is not strictly legal Ruby (Foo must have been defined
  674. # before Foo.bar is encountered), but RDoc might encounter Foo.bar before
  675. # Foo if they live in different files.
  676. code = <<-EOF
  677. def Foo.bar
  678. end
  679. class Foo < IO
  680. end
  681. EOF
  682. util_parser code
  683. @parser.scan
  684. assert_empty RDoc::TopLevel.modules_hash
  685. # HACK why does it fail?
  686. #assert_empty @top_level.modules
  687. foo = @top_level.classes.first
  688. assert_equal 'Foo', foo.full_name
  689. assert_equal 'IO', foo.superclass
  690. bar = foo.method_list.first
  691. assert_equal 'bar', bar.name
  692. end
  693. def test_parse_module_relative_to_top_level_namespace
  694. comment = <<-EOF
  695. #
  696. # Weirdly named module
  697. #
  698. EOF
  699. code = comment + <<-EOF
  700. module ::Foo
  701. class Helper
  702. end
  703. end
  704. EOF
  705. util_parser code
  706. @parser.scan()
  707. foo = @top_level.modules.first
  708. assert_equal 'Foo', foo.full_name
  709. assert_equal 'Weirdly named module', foo.comment
  710. helper = foo.classes.first
  711. assert_equal 'Foo::Helper', helper.full_name
  712. end
  713. def test_parse_comment_attr
  714. klass = RDoc::NormalClass.new 'Foo'
  715. klass.parent = @top_level
  716. comment = "##\n# :attr: foo\n# my attr\n"
  717. util_parser "\n"
  718. tk = @parser.get_tk
  719. @parser.parse_comment klass, tk, comment
  720. foo = klass.attributes.first
  721. assert_equal 'foo', foo.name
  722. assert_equal 'RW', foo.rw
  723. assert_equal 'my attr', foo.comment
  724. assert_equal @top_level, foo.file
  725. assert_equal 0, foo.offset
  726. assert_equal 1, foo.line
  727. assert_equal nil, foo.viewer
  728. assert_equal true, foo.document_children
  729. assert_equal true, foo.document_self
  730. assert_equal false, foo.done_documenting
  731. assert_equal false, foo.force_documentation
  732. assert_equal klass, foo.parent
  733. assert_equal :public, foo.visibility
  734. assert_equal "\n", foo.text
  735. assert_equal klass.current_section, foo.section
  736. end
  737. def test_parse_comment_attr_stopdoc
  738. klass = RDoc::NormalClass.new 'Foo'
  739. klass.parent = @top_level
  740. klass.stop_doc
  741. comment = "##\n# :attr: foo\n# my attr\n"
  742. util_parser "\n"
  743. tk = @parser.get_tk
  744. @parser.parse_comment klass, tk, comment
  745. assert_empty klass.attributes
  746. end
  747. def test_parse_comment_method
  748. klass = RDoc::NormalClass.new 'Foo'
  749. klass.parent = @top_level
  750. comment = "##\n# :method: foo\n# my method\n"
  751. util_parser "\n"
  752. tk = @parser.get_tk
  753. @parser.parse_comment klass, tk, comment
  754. foo = klass.method_list.first
  755. assert_equal 'foo', foo.name
  756. assert_equal 'my method', foo.comment
  757. assert_equal @top_level, foo.file
  758. assert_equal 0, foo.offset
  759. assert_equal 1, foo.line
  760. assert_equal [], foo.aliases
  761. assert_equal nil, foo.block_params
  762. assert_equal nil, foo.call_seq
  763. assert_equal nil, foo.is_alias_for
  764. assert_equal nil, foo.viewer
  765. assert_equal true, foo.document_children
  766. assert_equal true, foo.document_self
  767. assert_equal '', foo.params
  768. assert_equal false, foo.done_documenting
  769. assert_equal false, foo.dont_rename_initialize
  770. assert_equal false, foo.force_documentation
  771. assert_equal klass, foo.parent
  772. assert_equal false, foo.singleton
  773. assert_equal :public, foo.visibility
  774. assert_equal "\n", foo.text
  775. assert_equal klass.current_section, foo.section
  776. stream = [
  777. tk(:COMMENT, 1, 1, nil, "# File #{@top_level.absolute_name}, line 1"),
  778. RDoc::Parser::Ruby::NEWLINE_TOKEN,
  779. tk(:SPACE, 1, 1, nil, ''),
  780. ]
  781. assert_equal stream, foo.token_stream
  782. end
  783. def test_parse_comment_method_stopdoc
  784. klass = RDoc::NormalClass.new 'Foo'
  785. klass.parent = @top_level
  786. klass.stop_doc
  787. comment = "##\n# :method: foo\n# my method\n"
  788. util_parser "\n"
  789. tk = @parser.get_tk
  790. @parser.parse_comment klass, tk, comment
  791. assert_empty klass.method_list
  792. end
  793. def test_parse_constant
  794. util_top_level
  795. klass = @top_level.add_class RDoc::NormalClass, 'Foo'
  796. util_parser "A = v"
  797. tk = @parser.get_tk
  798. @parser.parse_constant klass, tk, ''
  799. foo = klass.constants.first
  800. assert_equal 'A', foo.name
  801. assert_equal @top_level, foo.file
  802. assert_equal 0, foo.offset
  803. assert_equal 1, foo.line
  804. end
  805. def test_parse_constant_attrasgn
  806. util_top_level
  807. klass = @top_level.add_class RDoc::NormalClass, 'Foo'
  808. util_parser "A[k] = v"
  809. tk = @parser.get_tk
  810. @parser.parse_constant klass, tk, ''
  811. assert klass.constants.empty?
  812. end
  813. def test_parse_constant_alias
  814. util_top_level
  815. klass = @top_level.add_class RDoc::NormalClass, 'Foo'
  816. cB = klass.add_class RDoc::NormalClass, 'B'
  817. util_parser "A = B"
  818. tk = @parser.get_tk
  819. @parser.parse_constant klass, tk, ''
  820. assert_equal cB, klass.find_module_named('A')
  821. end
  822. def test_parse_constant_alias_same_name
  823. foo = @top_level.add_class RDoc::NormalClass, 'Foo'
  824. top_bar = @top_level.add_class RDoc::NormalClass, 'Bar'
  825. bar = foo.add_class RDoc::NormalClass, 'Bar'
  826. assert RDoc::TopLevel.find_class_or_module('::Bar')
  827. util_parser "A = ::Bar"
  828. tk = @parser.get_tk
  829. @parser.parse_constant foo, tk, ''
  830. assert_equal top_bar, bar.find_module_named('A')
  831. end
  832. def test_parse_constant_stopdoc
  833. util_top_level
  834. klass = @top_level.add_class RDoc::NormalClass, 'Foo'
  835. klass.stop_doc
  836. util_parser "A = v"
  837. tk = @parser.get_tk
  838. @parser.parse_constant klass, tk, ''
  839. assert_empty klass.constants
  840. end
  841. def test_parse_include
  842. klass = RDoc::NormalClass.new 'C'
  843. klass.parent = @top_level
  844. comment = "# my include\n"
  845. util_parser "include I"
  846. @parser.get_tk # include
  847. @parser.parse_include klass, comment
  848. assert_equal 1, klass.includes.length
  849. incl = klass.includes.first
  850. assert_equal 'I', incl.name
  851. assert_equal 'my include', incl.comment
  852. assert_equal @top_level, incl.file
  853. end
  854. def test_parse_meta_method
  855. klass = RDoc::NormalClass.new 'Foo'
  856. klass.parent = @top_level
  857. comment = "##\n# my method\n"
  858. util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
  859. tk = @parser.get_tk
  860. @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  861. foo = klass.method_list.first
  862. assert_equal 'foo', foo.name
  863. assert_equal 'my method', foo.comment
  864. assert_equal @top_level, foo.file
  865. assert_equal 0, foo.offset
  866. assert_equal 1, foo.line
  867. assert_equal [], foo.aliases
  868. assert_equal nil, foo.block_params
  869. assert_equal nil, foo.call_seq
  870. assert_equal true, foo.document_children
  871. assert_equal true, foo.document_self
  872. assert_equal false, foo.done_documenting
  873. assert_equal false, foo.dont_rename_initialize
  874. assert_equal false, foo.force_documentation
  875. assert_equal nil, foo.is_alias_for
  876. assert_equal '', foo.params
  877. assert_equal klass, foo.parent
  878. assert_equal false, foo.singleton
  879. assert_equal 'add_my_method :foo', foo.text
  880. assert_equal nil, foo.viewer
  881. assert_equal :public, foo.visibility
  882. assert_equal klass.current_section, foo.section
  883. stream = [
  884. tk(:COMMENT, 1, 1, nil, "# File #{@top_level.absolute_name}, line 1"),
  885. RDoc::Parser::Ruby::NEWLINE_TOKEN,
  886. tk(:SPACE, 1, 1, nil, ''),
  887. tk(:IDENTIFIER, 1, 0, 'add_my_method', 'add_my_method'),
  888. tk(:SPACE, 1, 13, nil, ' '),
  889. tk(:SYMBOL, 1, 14, nil, ':foo'),
  890. tk(:COMMA, 1, 18, nil, ','),
  891. tk(:SPACE, 1, 19, nil, ' '),
  892. tk(:SYMBOL, 1, 20, nil, ':bar'),
  893. tk(:NL, 1, 24, nil, "\n"),
  894. ]
  895. assert_equal stream, foo.token_stream
  896. end
  897. def test_parse_meta_method_block
  898. klass = RDoc::NormalClass.new 'Foo'
  899. klass.parent = @top_level
  900. comment = "##\n# my method\n"
  901. content = <<-CONTENT
  902. inline(:my_method) do |*args|
  903. "this method causes z to disappear"
  904. end
  905. CONTENT
  906. util_parser content
  907. tk = @parser.get_tk
  908. @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  909. assert_nil @parser.get_tk
  910. end
  911. def test_parse_meta_method_name
  912. klass = RDoc::NormalClass.new 'Foo'
  913. klass.parent = @top_level
  914. comment = "##\n# :method: woo_hoo!\n# my method\n"
  915. util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
  916. tk = @parser.get_tk
  917. @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  918. foo = klass.method_list.first
  919. assert_equal 'woo_hoo!', foo.name
  920. assert_equal 'my method', foo.comment
  921. assert_equal @top_level, foo.file
  922. end
  923. def test_parse_meta_method_singleton
  924. klass = RDoc::NormalClass.new 'Foo'
  925. klass.parent = @top_level
  926. comment = "##\n# :singleton-method:\n# my method\n"
  927. util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
  928. tk = @parser.get_tk
  929. @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  930. foo = klass.method_list.first
  931. assert_equal 'foo', foo.name
  932. assert_equal true, foo.singleton, 'singleton method'
  933. assert_equal 'my method', foo.comment
  934. assert_equal @top_level, foo.file
  935. end
  936. def test_parse_meta_method_singleton_name
  937. klass = RDoc::NormalClass.new 'Foo'
  938. klass.parent = @top_level
  939. comment = "##\n# :singleton-method: woo_hoo!\n# my method\n"
  940. util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
  941. tk = @parser.get_tk
  942. @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  943. foo = klass.method_list.first
  944. assert_equal 'woo_hoo!', foo.name
  945. assert_equal true, foo.singleton, 'singleton method'
  946. assert_equal 'my method', foo.comment
  947. assert_equal @top_level, foo.file
  948. end
  949. def test_parse_meta_method_string_name
  950. klass = RDoc::NormalClass.new 'Foo'
  951. comment = "##\n# my method\n"
  952. util_parser "add_my_method 'foo'"
  953. tk = @parser.get_tk
  954. @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  955. foo = klass.method_list.first
  956. assert_equal 'foo', foo.name
  957. assert_equal 'my method', foo.comment
  958. assert_equal @top_level, foo.file
  959. end
  960. def test_parse_meta_method_stopdoc
  961. klass = RDoc::NormalClass.new 'Foo'
  962. klass.parent = @top_level
  963. klass.stop_doc
  964. comment = "##\n# my method\n"
  965. util_parser "add_my_method :foo, :bar\nadd_my_method :baz"
  966. tk = @parser.get_tk
  967. @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  968. assert_empty klass.method_list
  969. end
  970. def test_parse_meta_method_unknown
  971. klass = RDoc::NormalClass.new 'Foo'
  972. comment = "##\n# my method\n"
  973. util_parser "add_my_method ('foo')"
  974. tk = @parser.get_tk
  975. @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  976. foo = klass.method_list.first
  977. assert_equal 'unknown', foo.name
  978. assert_equal 'my method', foo.comment
  979. assert_equal @top_level, foo.file
  980. end
  981. def test_parse_method
  982. klass = RDoc::NormalClass.new 'Foo'
  983. klass.parent = @top_level
  984. comment = "##\n# my method\n"
  985. util_parser "def foo() :bar end"
  986. tk = @parser.get_tk
  987. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  988. foo = klass.method_list.first
  989. assert_equal 'foo', foo.name
  990. assert_equal 'my method', foo.comment
  991. assert_equal @top_level, foo.file
  992. assert_equal 0, foo.offset
  993. assert_equal 1, foo.line
  994. assert_equal [], foo.aliases
  995. assert_equal nil, foo.block_params
  996. assert_equal nil, foo.call_seq
  997. assert_equal nil, foo.is_alias_for
  998. assert_equal nil, foo.viewer
  999. assert_equal true, foo.document_children
  1000. assert_equal true, foo.document_self
  1001. assert_equal '()', foo.params
  1002. assert_equal false, foo.done_documenting
  1003. assert_equal false, foo.dont_rename_initialize
  1004. assert_equal false, foo.force_documentation
  1005. assert_equal klass, foo.parent
  1006. assert_equal false, foo.singleton
  1007. assert_equal :public, foo.visibility
  1008. assert_equal 'def foo', foo.text
  1009. assert_equal klass.current_section, foo.section
  1010. stream = [
  1011. tk(:COMMENT, 1, 1, nil, "# File #{@top_level.absolute_name}, line 1"),
  1012. RDoc::Parser::Ruby::NEWLINE_TOKEN,
  1013. tk(:SPACE, 1, 1, nil, ''),
  1014. tk(:DEF, 1, 0, 'def', 'def'),
  1015. tk(:SPACE, 1, 3, nil, ' '),
  1016. tk(:IDENTIFIER, 1, 4, 'foo', 'foo'),
  1017. tk(:LPAREN, 1, 7, nil, '('),
  1018. tk(:RPAREN, 1, 8, nil, ')'),
  1019. tk(:SPACE, 1, 9, nil, ' '),
  1020. tk(:COLON, 1, 10, nil, ':'),
  1021. tk(:IDENTIFIER, 1, 11, 'bar', 'bar'),
  1022. tk(:SPACE, 1, 14, nil, ' '),
  1023. tk(:END, 1, 15, 'end', 'end'),
  1024. ]
  1025. assert_equal stream, foo.token_stream
  1026. end
  1027. def test_parse_method_alias
  1028. klass = RDoc::NormalClass.new 'Foo'
  1029. klass.parent = @top_level
  1030. util_parser "def m() alias a b; end"
  1031. tk = @parser.get_tk
  1032. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1033. assert klass.aliases.empty?
  1034. end
  1035. def test_parse_method_false
  1036. util_parser "def false.foo() :bar end"
  1037. tk = @parser.get_tk
  1038. @parser.parse_method @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
  1039. klass = RDoc::TopLevel.find_class_named 'FalseClass'
  1040. foo = klass.method_list.first
  1041. assert_equal 'foo', foo.name
  1042. end
  1043. def test_parse_method_funky
  1044. klass = RDoc::NormalClass.new 'Foo'
  1045. klass.parent = @top_level
  1046. util_parser "def (blah).foo() :bar end"
  1047. tk = @parser.get_tk
  1048. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1049. assert klass.method_list.empty?
  1050. end
  1051. def test_parse_method_gvar
  1052. util_parser "def $stdout.foo() :bar end"
  1053. tk = @parser.get_tk
  1054. @parser.parse_method @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
  1055. assert @top_level.method_list.empty?
  1056. end
  1057. def test_parse_method_internal_gvar
  1058. klass = RDoc::NormalClass.new 'Foo'
  1059. klass.parent = @top_level
  1060. util_parser "def foo() def $blah.bar() end end"
  1061. tk = @parser.get_tk
  1062. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1063. assert_equal 1, klass.method_list.length
  1064. end
  1065. def test_parse_method_internal_ivar
  1066. klass = RDoc::NormalClass.new 'Foo'
  1067. klass.parent = @top_level
  1068. util_parser "def foo() def @blah.bar() end end"
  1069. tk = @parser.get_tk
  1070. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1071. assert_equal 1, klass.method_list.length
  1072. end
  1073. def test_parse_method_internal_lvar
  1074. klass = RDoc::NormalClass.new 'Foo'
  1075. klass.parent = @top_level
  1076. util_parser "def foo() def blah.bar() end end"
  1077. tk = @parser.get_tk
  1078. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1079. assert_equal 1, klass.method_list.length
  1080. end
  1081. def test_parse_method_nil
  1082. util_parser "def nil.foo() :bar end"
  1083. tk = @parser.get_tk
  1084. @parser.parse_method @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
  1085. klass = RDoc::TopLevel.find_class_named 'NilClass'
  1086. foo = klass.method_list.first
  1087. assert_equal 'foo', foo.name
  1088. end
  1089. def test_parse_method_no_parens
  1090. klass = RDoc::NormalClass.new 'Foo'
  1091. klass.parent = @top_level
  1092. util_parser "def foo arg1, arg2 = {}\nend"
  1093. tk = @parser.get_tk
  1094. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1095. foo = klass.method_list.first
  1096. assert_equal '(arg1, arg2 = {})', foo.params
  1097. assert_equal @top_level, foo.file
  1098. end
  1099. def test_parse_method_parameters_comment
  1100. klass = RDoc::NormalClass.new 'Foo'
  1101. klass.parent = @top_level
  1102. util_parser "def foo arg1, arg2 # some useful comment\nend"
  1103. tk = @parser.get_tk
  1104. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1105. foo = klass.method_list.first
  1106. assert_equal '(arg1, arg2)', foo.params
  1107. end
  1108. def test_parse_method_parameters_comment_continue
  1109. klass = RDoc::NormalClass.new 'Foo'
  1110. klass.parent = @top_level
  1111. util_parser "def foo arg1, arg2, # some useful comment\narg3\nend"
  1112. tk = @parser.get_tk
  1113. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1114. foo = klass.method_list.first
  1115. assert_equal '(arg1, arg2, arg3)', foo.params
  1116. end
  1117. def test_parse_method_stopdoc
  1118. klass = RDoc::NormalClass.new 'Foo'
  1119. klass.parent = @top_level
  1120. klass.stop_doc
  1121. comment = "##\n# my method\n"
  1122. util_parser "def foo() :bar end"
  1123. tk = @parser.get_tk
  1124. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment
  1125. assert_empty klass.method_list
  1126. end
  1127. def test_parse_method_toplevel
  1128. klass = @top_level
  1129. util_parser "def foo arg1, arg2\nend"
  1130. tk = @parser.get_tk
  1131. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1132. object = RDoc::TopLevel.find_class_named 'Object'
  1133. foo = object.method_list.first
  1134. assert_equal 'Object#foo', foo.full_name
  1135. assert_equal @top_level, foo.file
  1136. end
  1137. def test_parse_method_toplevel_class
  1138. klass = @top_level
  1139. util_parser "def Object.foo arg1, arg2\nend"
  1140. tk = @parser.get_tk
  1141. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1142. object = RDoc::TopLevel.find_class_named 'Object'
  1143. foo = object.method_list.first
  1144. assert_equal 'Object::foo', foo.full_name
  1145. end
  1146. def test_parse_method_true
  1147. util_parser "def true.foo() :bar end"
  1148. tk = @parser.get_tk
  1149. @parser.parse_method @top_level, RDoc::Parser::Ruby::NORMAL, tk, ''
  1150. klass = RDoc::TopLevel.find_class_named 'TrueClass'
  1151. foo = klass.method_list.first
  1152. assert_equal 'foo', foo.name
  1153. end
  1154. def test_parse_method_utf8
  1155. klass = RDoc::NormalClass.new 'Foo'
  1156. klass.parent = @top_level
  1157. method = "def ω() end"
  1158. assert_equal Encoding::UTF_8, method.encoding if
  1159. Object.const_defined? :Encoding
  1160. util_parser method
  1161. tk = @parser.get_tk
  1162. @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, ''
  1163. omega = klass.method_list.first
  1164. assert_equal "def \317\211", omega.text
  1165. end
  1166. def test_parse_statements_class_if
  1167. util_parser <<-CODE
  1168. module Foo
  1169. X = if TRUE then
  1170. ''
  1171. end
  1172. def blah
  1173. end
  1174. end
  1175. CODE
  1176. @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
  1177. foo = @top_level.modules.first
  1178. assert_equal 'Foo', foo.full_name, 'module Foo'
  1179. methods = foo.method_list
  1180. assert_equal 1, methods.length
  1181. assert_equal 'Foo#blah', methods.first.full_name
  1182. end
  1183. def test_parse_statements_class_nested
  1184. comment = "##\n# my method\n"
  1185. util_parser "module Foo\n#{comment}class Bar\nend\nend"
  1186. @parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
  1187. foo = @top_level.modules.first
  1188. assert_equal 'Foo', foo.full_name, 'module Foo'
  1189. bar = foo.classes.first
  1190. assert_equal 'Foo::Bar', bar.full_name, 'class Foo::Bar'
  1191. assert_equal 'my method', bar.comment
  1192. end
  1193. def test_parse_statements_encoding
  1194. skip "Encoding not implemented" unless Object.const_defined? :Encoding
  1195. @options.encoding = Encoding::CP852
  1196. content = <<-EOF
  1197. class Foo
  1198. ##
  1199. # this is my method
  1200. add_my_method :foo
  1201. end
  1202. EOF
  1203. util_parser content
  1204. @parser.parse_statements @top_level
  1205. foo = @top_level.classes.first.method_list.first
  1206. assert_equal 'foo', foo.name
  1207. assert_equal 'this is my method', foo.comment
  1208. assert_equal Encoding::CP852, foo.comment.encoding
  1209. end
  1210. def test_parse_statements_identifier_meta_method
  1211. content = <<-EOF
  1212. class Foo
  1213. ##
  1214. # this is my method
  1215. add_my_method :foo
  1216. end
  1217. EOF
  1218. util_parser content
  1219. @parser.parse_statements @top_level
  1220. foo = @top_level.classes.first.method_list.first
  1221. assert_equal 'foo', foo.name
  1222. end
  1223. def test_parse_statements_identifier_alias_method
  1224. content = <<-RUBY
  1225. class Foo
  1226. def foo() end
  1227. alias_method :foo2, :foo
  1228. end
  1229. RUBY
  1230. util_parser content
  1231. @parser.parse_statements @top_level
  1232. foo = @top_level.classes.first.method_list[0]
  1233. assert_equal 'foo', foo.name
  1234. foo2 = @top_level.classes.first.method_list.last
  1235. assert_equal 'foo2', foo2.name
  1236. assert_equal 'foo', foo2.is_alias_for.name
  1237. assert @top_level.classes.first.aliases.empty?
  1238. end
  1239. def test_parse_statements_identifier_alias_method_before_original_method
  1240. # This is not strictly legal Ruby code, but it simulates finding an alias
  1241. # for a method before finding the original method, which might happen
  1242. # to rdoc if the alias is in a different file than the original method
  1243. # and rdoc processes the alias' file first.
  1244. content = <<-EOF
  1245. class Foo
  1246. alias_method :foo2, :foo
  1247. alias_method :foo3, :foo
  1248. def foo()
  1249. end
  1250. alias_method :foo4, :foo
  1251. alias_method :foo5, :unknown
  1252. end
  1253. EOF
  1254. util_parser content
  1255. @parser.parse_statements @top_level
  1256. foo = @top_level.classes.first.method_list[0]
  1257. assert_equal 'foo', foo.name
  1258. foo2 = @top_level.classes.first.method_list[1]
  1259. assert_equal 'foo2', foo2.name
  1260. assert_equal 'foo', foo2.is_alias_for.name
  1261. foo3 = @top_level.classes.first.method_list[2]
  1262. assert_equal 'foo3', foo3.name
  1263. assert_equal 'foo', foo3.is_alias_for.name
  1264. foo4 = @top_level.classes.first.method_list.last
  1265. assert_equal 'foo4', foo4.name
  1266. assert_equal 'foo', foo4.is_alias_for.name
  1267. assert_equal 'unknown', @top_level.classes.first.external_aliases[0].old_name
  1268. end
  1269. def test_parse_statements_identifier_constant
  1270. sixth_constant = <<-EOF
  1271. Class.new do
  1272. rule :file do
  1273. all(x, y, z) {
  1274. def value
  1275. find(:require).each {|r| require r.value }
  1276. find(:grammar).map {|g| g.value }
  1277. end
  1278. def min; end
  1279. }
  1280. end
  1281. end
  1282. EOF
  1283. content = <<-EOF
  1284. class Foo
  1285. FIRST_CONSTANT = 5
  1286. SECOND_CONSTANT = [
  1287. 1,
  1288. 2,
  1289. 3
  1290. ]
  1291. THIRD_CONSTANT = {
  1292. :foo => 'bar',
  1293. :x => 'y'
  1294. }
  1295. FOURTH_CONSTANT = SECOND_CONSTANT.map do |element|
  1296. element + 1
  1297. element + 2
  1298. end
  1299. FIFTH_CONSTANT = SECOND_CONSTANT.map { |element| element + 1 }
  1300. SIXTH_CONSTANT = #{sixth_constant}
  1301. SEVENTH_CONSTANT = proc { |i| begin i end }
  1302. end
  1303. EOF
  1304. util_parser content
  1305. @parser.parse_statements @top_level
  1306. constants = @top_level.classes.first.constants
  1307. constant = constants[0]
  1308. assert_equal 'FIRST_CONSTANT', constant.name
  1309. assert_equal '5', constant.value
  1310. assert_equal @top_level, constant.file
  1311. constant = constants[1]
  1312. assert_equal 'SECOND_CONSTANT', constant.name
  1313. assert_equal "[\n1,\n2,\n3\n]", constant.value
  1314. assert_equal @top_level, constant.file
  1315. constant = constants[2]
  1316. assert_equal 'THIRD_CONSTANT', constant.name
  1317. assert_equal "{\n:foo => 'bar',\n:x => 'y'\n}", constant.value
  1318. assert_equal @top_level, constant.file
  1319. constant = constants[3]
  1320. assert_equal 'FOURTH_CONSTANT', constant.name
  1321. assert_equal "SECOND_CONSTANT.map do |element|\nelement + 1\nelement + 2\nend", constant.value
  1322. assert_equal @top_level, constant.file
  1323. constant = constants[4]
  1324. assert_equal 'FIFTH_CONSTANT', constant.name
  1325. assert_equal 'SECOND_CONSTANT.map { |element| element + 1 }', constant.value
  1326. assert_equal @top_level, constant.file
  1327. # TODO: parse as class
  1328. constant = constants[5]
  1329. assert_equal 'SIXTH_CONSTANT', constant.name
  1330. assert_equal sixth_constant.lines.map(&:strip).join("\n"), constant.value
  1331. assert_equal @top_level, constant.file
  1332. # TODO: parse as method
  1333. constant = constants[6]
  1334. assert_equal 'SEVENTH_CONSTANT', constant.name
  1335. assert_equal "proc { |i| begin i end }", constant.value
  1336. assert_equal @top_level, constant.file
  1337. end
  1338. def test_parse_statements_identifier_attr
  1339. content = "class Foo\nattr :foo\nend"
  1340. util_parser content
  1341. @parser.parse_statements @top_level
  1342. foo = @top_level.classes.first.attributes.first
  1343. assert_equal 'foo', foo.name
  1344. assert_equal 'R', foo.rw
  1345. end
  1346. def test_parse_statements_identifier_attr_accessor
  1347. content = "class Foo\nattr_accessor :foo\nend"
  1348. util_parser content
  1349. @parser.parse_statements @top_level
  1350. foo = @top_level.classes.first.attributes.first
  1351. assert_equal 'foo', foo.name
  1352. assert_equal 'RW', foo.rw
  1353. end
  1354. def test_parse_statements_identifier_include
  1355. content = "class Foo\ninclude Bar\nend"
  1356. util_parser content
  1357. @parser.parse_statements @top_level
  1358. foo = @top_level.classes.first
  1359. assert_equal 'Foo', foo.name
  1360. assert_equal 1, foo.includes.length
  1361. end
  1362. def test_parse_statements_identifier_module_function
  1363. content = "module Foo\ndef foo() end\nmodule_function :foo\nend"
  1364. util_parser content
  1365. @parser.parse_statements @top_level
  1366. foo, s_foo = @top_level.modules.first.method_list
  1367. assert_equal 'foo', foo.name, 'instance method name'
  1368. assert_equal :private, foo.visibility, 'instance method visibility'
  1369. assert_equal false, foo.singleton, 'instance method singleton'
  1370. assert_equal 'foo', s_foo.name, 'module function name'
  1371. assert_equal :public, s_foo.visibility, 'module function visibility'
  1372. assert_equal true, s_foo.singleton, 'module function singleton'
  1373. end
  1374. def test_parse_statements_identifier_private
  1375. content = "class Foo\nprivate\ndef foo() end\nend"
  1376. util_parser content
  1377. @parser.parse_statements @top_level
  1378. foo = @top_level.classes.first.method_list.first
  1379. assert_equal 'foo', foo.name
  1380. assert_equal :private, foo.visibility
  1381. end
  1382. def test_parse_statements_identifier_public_class_method
  1383. content = <<-CONTENT
  1384. class Date
  1385. def self.now; end
  1386. private_class_method :now
  1387. end
  1388. class DateTime < Date
  1389. public_class_method :now
  1390. end
  1391. CONTENT
  1392. util_parser content
  1393. @parser.parse_statements @top_level
  1394. date, date_time = @top_level.classes
  1395. date_now = date.method_list.first
  1396. date_time_now = date_time.method_list.first
  1397. assert_equal :private, date_now.visibility
  1398. assert_equal :public, date_time_now.visibility
  1399. end
  1400. def test_parse_statements_identifier_private_class_method
  1401. content = <<-CONTENT
  1402. class Date
  1403. def self.now; end
  1404. public_class_method :now
  1405. end
  1406. class DateTime < Date
  1407. private_class_method :now
  1408. end
  1409. CONTENT
  1410. util_parser content
  1411. @parser.parse_statements @top_level
  1412. date, date_time = @top_level.classes
  1413. date_now = date.method_list.first
  1414. date_time_now = date_time.method_list.first
  1415. assert_equal :public, date_now.visibility, date_now.full_name
  1416. assert_equal :private, date_time_now.visibility, date_time_now.full_name
  1417. end
  1418. def test_parse_statements_identifier_require
  1419. content = "require 'bar'"
  1420. util_parser content
  1421. @parser.parse_statements @top_level
  1422. assert_equal 1, @top_level.requires.length
  1423. end
  1424. def test_parse_statements_stopdoc_TkALIAS
  1425. util_top_level
  1426. klass = @top_level.add_class RDoc::NormalClass, 'Foo'
  1427. util_parser "\n# :stopdoc:\nalias old new"
  1428. @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
  1429. assert_empty klass.aliases
  1430. assert_empty klass.unmatched_alias_lists
  1431. end
  1432. def test_parse_statements_stopdoc_TkIDENTIFIER_alias_method
  1433. util_top_level
  1434. klass = @top_level.add_class RDoc::NormalClass, 'Foo'
  1435. util_parser "\n# :stopdoc:\nalias_method :old :new"
  1436. @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
  1437. assert_empty klass.aliases
  1438. assert_empty klass.unmatched_alias_lists
  1439. end
  1440. def test_parse_statements_stopdoc_TkIDENTIFIER_metaprogrammed
  1441. util_top_level
  1442. klass = @top_level.add_class RDoc::NormalClass, 'Foo'
  1443. util_parser "\n# :stopdoc:\n# attr :meta"
  1444. @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
  1445. assert_empty klass.method_list
  1446. assert_empty klass.attributes
  1447. end
  1448. def test_parse_statements_stopdoc_TkCONSTANT
  1449. util_top_level
  1450. klass = @top_level.add_class RDoc::NormalClass, 'Foo'
  1451. util_parser "\n# :stopdoc:\nA = v"
  1452. @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
  1453. assert_empty klass.constants
  1454. end
  1455. def test_parse_statements_stopdoc_TkDEF
  1456. util_top_level
  1457. klass = @top_level.add_class RDoc::NormalClass, 'Foo'
  1458. util_parser "\n# :stopdoc:\ndef m\n end"
  1459. @parser.parse_statements klass, RDoc::Parser::Ruby::NORMAL, nil
  1460. assert_empty klass.method_list
  1461. end
  1462. def test_parse_statements_while_begin
  1463. util_parser <<-RUBY
  1464. class A
  1465. def a
  1466. while begin a; b end
  1467. end
  1468. end
  1469. def b
  1470. end
  1471. end
  1472. RUBY
  1473. @parser.parse_statements @top_level
  1474. c_a = @top_level.classes.first
  1475. assert_equal 'A', c_a.full_name
  1476. assert_equal 1, @top_level.classes.length
  1477. m_a = c_a.method_list.first
  1478. m_b = c_a.method_list.last
  1479. assert_equal 'A#a', m_a.full_name
  1480. assert_equal 'A#b', m_b.full_name
  1481. end
  1482. def test_parse_symbol_in_arg
  1483. util_parser ':blah "blah" "#{blah}" blah'
  1484. assert_equal 'blah', @parser.parse_symbol_in_arg
  1485. @parser.skip_tkspace
  1486. assert