From 6f867144e1a73d6a697e7b362ec3a1677ce1b1b5 Mon Sep 17 00:00:00 2001 From: hsbt Date: Tue, 12 Sep 2017 03:42:54 +0000 Subject: Merge rdoc-6.0.0.beta2 from upstream. * This version changed lexer used Ripper from lexer based IRB. see details: https://github.com/ruby/rdoc/pull/512 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/rdoc/test_rdoc_parser_ruby.rb | 274 +++++++++++++++++++++++++++---------- 1 file changed, 204 insertions(+), 70 deletions(-) (limited to 'test/rdoc/test_rdoc_parser_ruby.rb') diff --git a/test/rdoc/test_rdoc_parser_ruby.rb b/test/rdoc/test_rdoc_parser_ruby.rb index 55c03d4628..cda407ed51 100644 --- a/test/rdoc/test_rdoc_parser_ruby.rb +++ b/test/rdoc/test_rdoc_parser_ruby.rb @@ -74,7 +74,7 @@ class C; end comment = parser.collect_first_comment - assert_equal RDoc::Comment.new("=begin\nfirst\n=end\n\n", @top_level), comment + assert_equal RDoc::Comment.new("=begin\nfirst\n=end\n", @top_level), comment end def test_get_class_or_module @@ -84,7 +84,7 @@ class C; end cont, name_t, given_name = util_parser('A') .get_class_or_module ctxt assert_equal ctxt, cont - assert_equal 'A', name_t.text + assert_equal 'A', name_t[:text] assert_equal 'A', given_name cont, name_t, given_name = util_parser('B::C') .get_class_or_module ctxt @@ -92,16 +92,16 @@ class C; end b = @store.find_module_named('B') assert_equal b, cont assert_equal [@top_level], b.in_files - assert_equal 'C', name_t.text + assert_equal 'C', name_t[:text] assert_equal 'B::C', given_name cont, name_t, given_name = util_parser('D:: E').get_class_or_module ctxt assert_equal @store.find_module_named('D'), cont - assert_equal 'E', name_t.text + assert_equal 'E', name_t[:text] assert_equal 'D::E', given_name - assert_raises NoMethodError do + assert_raises RDoc::Error do util_parser("A::\nB").get_class_or_module ctxt end end @@ -1194,10 +1194,12 @@ EOF assert_equal klass.current_section, foo.section stream = [ - tk(:COMMENT, 0, 1, 1, nil, - "# File #{@top_level.relative_name}, line 1"), - RDoc::Parser::Ruby::NEWLINE_TOKEN, - tk(:SPACE, 0, 1, 1, nil, ''), + { + :line_no => 1, :char_no => 1, :kind => :on_comment, + :text => "# File #{@top_level.relative_name}, line 1" + }, + { :line_no => 0, :char_no => 0, :kind => :on_nl, :text => "\n" }, + { :line_no => 1, :char_no => 1, :kind => :on_sp, :text => '' } ] assert_equal stream, foo.token_stream @@ -1358,6 +1360,33 @@ A::B::C = 1 assert_equal 'comment', c.comment end + def test_parse_constant_with_bracket + util_parser <<-RUBY +class Klass +end + +class Klass2 + CONSTANT = Klass +end + +class Klass3 + CONSTANT_2 = {} + CONSTANT_2[1] = Klass +end + RUBY + + @parser.scan + + klass = @store.find_class_named 'Klass' + klass2 = @store.find_class_named 'Klass2' + klass3 = @store.find_class_named 'Klass3' + constant = klass2.find_module_named 'CONSTANT' + constant2 = klass3.find_module_named 'CONSTANT_2' + assert_equal klass, klass2.constants.first.is_alias_for + refute_equal klass, klass3.constants.first.is_alias_for + assert_nil klass3.find_module_named 'CONSTANT_2' + end + def test_parse_extend_or_include_extend klass = RDoc::NormalClass.new 'C' klass.parent = @top_level @@ -1434,20 +1463,30 @@ A::B::C = 1 assert_equal klass.current_section, foo.section stream = [ - tk(:COMMENT, 0, 1, 1, nil, - "# File #{@top_level.relative_name}, line 1"), - RDoc::Parser::Ruby::NEWLINE_TOKEN, - tk(:SPACE, 0, 1, 1, nil, ''), - tk(:IDENTIFIER, 0, 1, 0, 'add_my_method', 'add_my_method'), - tk(:SPACE, 0, 1, 13, nil, ' '), - tk(:SYMBOL, 0, 1, 14, nil, ':foo'), - tk(:COMMA, 0, 1, 18, nil, ','), - tk(:SPACE, 0, 1, 19, nil, ' '), - tk(:SYMBOL, 0, 1, 20, nil, ':bar'), - tk(:NL, 0, 1, 24, nil, "\n"), + { + :line_no => 1, :char_no => 1, :kind => :on_comment, + :text => "# File #{@top_level.relative_name}, line 1" + }, + { :line_no => 0, :char_no => 0, :kind => :on_nl, :text => "\n" }, + { :line_no => 1, :char_no => 1, :kind => :on_sp, :text => '' }, + { :line_no => 1, :char_no => 0, :kind => :on_ident, :text => 'add_my_method' }, + { :line_no => 1, :char_no => 13, :kind => :on_sp, :text => ' ' }, + { :line_no => 1, :char_no => 14, :kind => :on_symbol, :text => ':foo' }, + { :line_no => 1, :char_no => 18, :kind => :on_comma, :text => ',' }, + { :line_no => 1, :char_no => 19, :kind => :on_sp, :text => ' ' }, + { :line_no => 1, :char_no => 20, :kind => :on_symbol, :text => ':bar' }, + { :line_no => 1, :char_no => 24, :kind => :on_nl, :text => "\n" } ] + parsed_stream = foo.token_stream.map { |t| + { + :line_no => t[:line_no], + :char_no => t[:char_no], + :kind => t[:kind], + :text => t[:text] + } + } - assert_equal stream, foo.token_stream + assert_equal stream, parsed_stream end def test_parse_meta_method_block @@ -1468,7 +1507,10 @@ end @parser.parse_meta_method klass, RDoc::Parser::Ruby::NORMAL, tk, comment - assert_equal tk(:NL, 0, 3, 3, 3, "\n"), @parser.get_tk + rest = { :line_no => 3, :char_no => 3, :kind => :on_nl, :text => "\n" } + tk = @parser.get_tk + tk = { :line_no => tk[:line_no], :char_no => tk[:char_no], :kind => tk[:kind], :text => tk[:text] } + assert_equal rest, tk end def test_parse_meta_method_define_method @@ -1631,23 +1673,30 @@ end assert_equal klass.current_section, foo.section stream = [ - tk(:COMMENT, 0, 1, 1, nil, - "# File #{@top_level.relative_name}, line 1"), - RDoc::Parser::Ruby::NEWLINE_TOKEN, - tk(:SPACE, 0, 1, 1, nil, ''), - tk(:DEF, 0, 1, 0, 'def', 'def'), - tk(:SPACE, 3, 1, 3, nil, ' '), - tk(:IDENTIFIER, 4, 1, 4, 'foo', 'foo'), - tk(:LPAREN, 7, 1, 7, nil, '('), - tk(:RPAREN, 8, 1, 8, nil, ')'), - tk(:SPACE, 9, 1, 9, nil, ' '), - tk(:COLON, 10, 1, 10, nil, ':'), - tk(:IDENTIFIER, 11, 1, 11, 'bar', 'bar'), - tk(:SPACE, 14, 1, 14, nil, ' '), - tk(:END, 15, 1, 15, 'end', 'end'), + { + :line_no => 1, :char_no => 1, :kind => :on_comment, + :text => "# File #{@top_level.relative_name}, line 1" }, + { :line_no => 0, :char_no => 0, :kind => :on_nl, :text => "\n" }, + { :line_no => 1, :char_no => 1, :kind => :on_sp, :text => '' }, + { :line_no => 1, :char_no => 0, :kind => :on_kw, :text => 'def' }, + { :line_no => 1, :char_no => 3, :kind => :on_sp, :text => ' ' }, + { :line_no => 1, :char_no => 4, :kind => :on_ident, :text => 'foo' }, + { :line_no => 1, :char_no => 7, :kind => :on_lparen, :text => '(' }, + { :line_no => 1, :char_no => 8, :kind => :on_rparen, :text => ')' }, + { :line_no => 1, :char_no => 9, :kind => :on_sp, :text => ' ' }, + { :line_no => 1, :char_no => 10, :kind => :on_symbol, :text => ':bar' }, + { :line_no => 1, :char_no => 14, :kind => :on_sp, :text => ' ' }, + { :line_no => 1, :char_no => 15, :kind => :on_kw, :text => 'end' } ] - - assert_equal stream, foo.token_stream + parsed_stream = foo.token_stream.map { |t| + { + :line_no => t[:line_no], + :char_no => t[:char_no], + :kind => t[:kind], + :text => t[:text] + } + } + assert_equal stream, parsed_stream end def test_parse_redefinable_methods @@ -1664,8 +1713,8 @@ end end klass.method_list.each do |method| - assert_kind_of RDoc::RubyToken::TkId, method.token_stream[5] - assert_includes redefinable_ops, method.token_stream[5].text + assert_equal :on_ident, method.token_stream[5][:kind] + assert_includes redefinable_ops, method.token_stream[5][:text] end end @@ -1909,6 +1958,20 @@ end assert_equal '(arg1, arg2, arg3)', foo.params end + def test_parse_method_parameters_with_paren_comment_continue + klass = RDoc::NormalClass.new 'Foo' + klass.parent = @top_level + + util_parser "def foo(arg1, arg2, # some useful comment\narg3)\nend" + + tk = @parser.get_tk + + @parser.parse_method klass, RDoc::Parser::Ruby::NORMAL, tk, @comment + + foo = klass.method_list.first + assert_equal '(arg1, arg2, arg3)', foo.params + end + def test_parse_method_star klass = RDoc::NormalClass.new 'Foo' klass.parent = @top_level @@ -2091,22 +2154,34 @@ end assert_equal 2, x.method_list.length a = x.method_list.first + expected = [ - tk(:COMMENT, 0, 2, 1, nil, "# File #{@filename}, line 2"), - tk(:NL, 0, 0, 0, nil, "\n"), - tk(:SPACE, 0, 1, 1, nil, ''), - tk(:DEF, 8, 2, 0, 'def', 'def'), - tk(:SPACE, 11, 2, 3, nil, ' '), - tk(:IDENTIFIER, 12, 2, 4, 'a', 'a'), - tk(:NL, 13, 2, 5, nil, "\n"), - tk(:REGEXP, 14, 3, 0, nil, '%r{#}'), - tk(:NL, 19, 3, 5, nil, "\n"), - tk(:DREGEXP, 20, 4, 0, nil, '%r{#{}}'), - tk(:NL, 27, 4, 7, nil, "\n"), - tk(:END, 28, 5, 0, 'end', 'end'), + { + :line_no => 2, :char_no => 1, :kind => :on_comment, + :text => "# File #{@filename}, line 2" + }, + { :line_no => 0, :char_no => 0, :kind => :on_nl, :text => "\n" }, + { :line_no => 1, :char_no => 1, :kind => :on_sp, :text => '' }, + { :line_no => 2, :char_no => 0, :kind => :on_kw, :text => 'def' }, + { :line_no => 2, :char_no => 3, :kind => :on_sp, :text => ' ' }, + { :line_no => 2, :char_no => 4, :kind => :on_ident, :text => 'a' }, + { :line_no => 2, :char_no => 5, :kind => :on_nl, :text => "\n" }, + { :line_no => 3, :char_no => 0, :kind => :on_regexp, :text => '%r{#}' }, + { :line_no => 3, :char_no => 5, :kind => :on_nl, :text => "\n" }, + { :line_no => 4, :char_no => 0, :kind => :on_regexp, :text => '%r{#{}}' }, + { :line_no => 4, :char_no => 7, :kind => :on_nl, :text => "\n" }, + { :line_no => 5, :char_no => 0, :kind => :on_kw, :text => 'end' } ] + parsed_stream = a.token_stream.map { |tk| + { + :line_no => tk[:line_no], + :char_no => tk[:char_no], + :kind => tk[:kind], + :text => tk[:text] + } + } - assert_equal expected, a.token_stream + assert_equal expected, parsed_stream end def test_parse_statements_encoding @@ -2286,6 +2361,9 @@ class Foo SIXTH_CONSTANT = #{sixth_constant} SEVENTH_CONSTANT = proc { |i| begin i end } + + EIGHTH_CONSTANT = "a" \\ + "b" end EOF @@ -2331,6 +2409,11 @@ EOF assert_equal 'SEVENTH_CONSTANT', constant.name assert_equal "proc { |i| begin i end }", constant.value assert_equal @top_level, constant.file + + constant = constants[7] + assert_equal 'EIGHTH_CONSTANT', constant.name + assert_equal "\"a\" \\\n\"b\"", constant.value + assert_equal @top_level, constant.file end def test_parse_statements_identifier_attr @@ -2504,7 +2587,7 @@ EXPTECTED util_parser <def blah() - <<~EOM if true + <<-EOM if true EOM end EXPTECTED @@ -2525,7 +2608,7 @@ EXPTECTED blah = foo.method_list.first markup_code = blah.markup_code.sub(/^.*\n/, '') - assert_equal markup_code, expected + assert_equal expected, markup_code end def test_parse_statements_method_oneliner_with_regexp @@ -2770,17 +2853,21 @@ RUBY end def test_parse_symbol_in_arg - util_parser ':blah "blah" "#{blah}" blah' + util_parser '[:blah, "blah", "#{blah}", blah]' + @parser.get_tk # skip '[' assert_equal 'blah', @parser.parse_symbol_in_arg + @parser.get_tk # skip ',' @parser.skip_tkspace assert_equal 'blah', @parser.parse_symbol_in_arg + @parser.get_tk # skip ',' @parser.skip_tkspace assert_equal nil, @parser.parse_symbol_in_arg + @parser.get_tk # skip ',' @parser.skip_tkspace @@ -2888,7 +2975,7 @@ end assert_equal 'category', directive assert_equal 'test', value - assert_kind_of RDoc::RubyToken::TkNL, parser.get_tk + assert_equal nil, parser.get_tk end def test_read_directive_allow @@ -2898,7 +2985,7 @@ end assert_nil directive - assert_kind_of RDoc::RubyToken::TkNL, parser.get_tk + assert_equal nil, parser.get_tk end def test_read_directive_empty @@ -2908,7 +2995,7 @@ end assert_nil directive - assert_kind_of RDoc::RubyToken::TkNL, parser.get_tk + assert_equal nil, parser.get_tk end def test_read_directive_no_comment @@ -2918,18 +3005,18 @@ end assert_nil directive - assert_kind_of RDoc::RubyToken::TkNL, parser.get_tk + assert_equal nil, parser.get_tk end def test_read_directive_one_liner - parser = util_parser '; end # :category: test' + parser = util_parser 'AAA = 1 # :category: test' directive, value = parser.read_directive %w[category] assert_equal 'category', directive assert_equal 'test', value - assert_kind_of RDoc::RubyToken::TkSEMICOLON, parser.get_tk + assert_equal :on_const, parser.get_tk[:kind] end def test_read_documentation_modifiers @@ -2974,10 +3061,10 @@ end def test_sanity_integer util_parser '1' - assert_equal '1', @parser.get_tk.text + assert_equal '1', @parser.get_tk[:text] util_parser '1.0' - assert_equal '1.0', @parser.get_tk.text + assert_equal '1.0', @parser.get_tk[:text] end def test_sanity_interpolation @@ -2986,7 +3073,7 @@ end while tk = @parser.get_tk do last_tk = tk end - assert_equal "\n", last_tk.text + assert_equal 'end', last_tk[:text] end # If you're writing code like this you're doing it wrong @@ -2994,15 +3081,15 @@ end def test_sanity_interpolation_crazy util_parser '"#{"#{"a")}" if b}"' - assert_equal '"#{"#{"a")}" if b}"', @parser.get_tk.text - assert_equal RDoc::RubyToken::TkNL, @parser.get_tk.class + assert_equal '"#{"#{"a")}" if b}"', @parser.get_tk[:text] + assert_equal nil, @parser.get_tk end def test_sanity_interpolation_curly util_parser '%{ #{} }' - assert_equal '%{ #{} }', @parser.get_tk.text - assert_equal RDoc::RubyToken::TkNL, @parser.get_tk.class + assert_equal '%{ #{} }', @parser.get_tk[:text] + assert_equal nil, @parser.get_tk end def test_sanity_interpolation_format @@ -3537,6 +3624,53 @@ end assert_equal 2, public_method_count end + def test_scan_constant_visibility + util_parser <<-RUBY +class C + CONST_A = 123 + + CONST_B = 234 + private_constant :CONST_B + + CONST_C = 345 + public_constant :CONST_C +end + RUBY + + @parser.scan + + c = @store.find_class_named 'C' + const_a, const_b, const_c, const_d = c.constants.sort_by(&:name) + + assert_equal 'CONST_A', const_a.name + assert_equal :public, const_a.visibility + + assert_equal 'CONST_B', const_b.name + assert_equal :private, const_b.visibility + + assert_equal 'CONST_C', const_c.name + assert_equal :public, const_c.visibility + end + + def test_document_after_rescue_inside_paren + util_parser <<-RUBY +class C + attr_accessor :sample if (1.inexistent_method rescue false) + # first + # second + def a + end +end + RUBY + + @parser.scan + + c = @store.find_class_named 'C' + + c_a = c.find_method_named 'a' + assert_equal "first\nsecond", c_a.comment.text + end + def test_singleton_method_via_eigenclass util_parser <<-RUBY class C -- cgit v1.2.3