# coding: UTF-8 # frozen_string_literal: false require 'rdoc/test_case' class TestRDocRubyLex < RDoc::TestCase def setup @TK = RDoc::RubyToken end def test_token_position tokens = RDoc::RubyLex.tokenize '[ 1, :a, nil ]', nil assert_equal '[', tokens[0].text assert_equal 0, tokens[0].seek assert_equal 1, tokens[0].line_no assert_equal 0, tokens[0].char_no assert_equal '1', tokens[2].text assert_equal 2, tokens[2].seek assert_equal 1, tokens[2].line_no assert_equal 2, tokens[2].char_no assert_equal ':a', tokens[5].text assert_equal 5, tokens[5].seek assert_equal 1, tokens[5].line_no assert_equal 5, tokens[5].char_no assert_equal 'nil', tokens[8].text assert_equal 9, tokens[8].seek assert_equal 1, tokens[8].line_no assert_equal 9, tokens[8].char_no assert_equal ']', tokens[10].text assert_equal 13, tokens[10].seek assert_equal 1, tokens[10].line_no assert_equal 13, tokens[10].char_no end def test_class_tokenize tokens = RDoc::RubyLex.tokenize "def x() end", nil expected = [ @TK::TkDEF .new( 0, 1, 0, "def"), @TK::TkSPACE .new( 3, 1, 3, " "), @TK::TkIDENTIFIER.new( 4, 1, 4, "x"), @TK::TkLPAREN .new( 5, 1, 5, "("), @TK::TkRPAREN .new( 6, 1, 6, ")"), @TK::TkSPACE .new( 7, 1, 7, " "), @TK::TkEND .new( 8, 1, 8, "end"), @TK::TkNL .new(11, 1, 11, "\n"), ] assert_equal expected, tokens end def test_class_tokenize___END__ tokens = RDoc::RubyLex.tokenize '__END__', nil expected = [ @TK::TkEND_OF_SCRIPT.new(0, 1, 0, '__END__'), @TK::TkNL .new(7, 1, 7, "\n"), ] assert_equal expected, tokens end def test_class_tokenize___ENCODING__ tokens = RDoc::RubyLex.tokenize '__ENCODING__', nil expected = [ @TK::Tk__ENCODING__.new( 0, 1, 0, '__ENCODING__'), @TK::TkNL .new(12, 1, 12, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_character_literal tokens = RDoc::RubyLex.tokenize "?c", nil expected = [ @TK::TkCHAR.new( 0, 1, 0, "?c"), @TK::TkNL .new( 2, 1, 2, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_character_literal_with_escape tokens = RDoc::RubyLex.tokenize "?\\s", nil expected = [ @TK::TkCHAR.new( 0, 1, 0, "?\\s"), @TK::TkNL .new( 3, 1, 3, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_def_heredoc tokens = RDoc::RubyLex.tokenize <<-'RUBY', nil def x < "foo" }', nil expected = [ @TK::TkLBRACE .new( 0, 1, 0, '{'), @TK::TkSPACE .new( 1, 1, 1, ' '), @TK::TkSYMBOL .new( 2, 1, 2, ':class'), @TK::TkSPACE .new( 8, 1, 8, ' '), @TK::TkHASHROCKET.new( 9, 1, 9, '=>'), @TK::TkSPACE .new(11, 1, 11, ' '), @TK::TkSTRING .new(12, 1, 12, '"foo"'), @TK::TkSPACE .new(17, 1, 17, ' '), @TK::TkRBRACE .new(18, 1, 18, '}'), @TK::TkNL .new(19, 1, 19, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_heredoc_CR_NL tokens = RDoc::RubyLex.tokenize <<-RUBY, nil string = <<-STRING\r Line 1\r Line 2\r STRING\r RUBY expected = [ @TK::TkIDENTIFIER.new( 0, 1, 0, 'string'), @TK::TkSPACE .new( 6, 1, 6, ' '), @TK::TkASSIGN .new( 7, 1, 7, '='), @TK::TkSPACE .new( 8, 1, 8, ' '), @TK::TkHEREDOCBEG.new( 9, 1, 9, '<<-STRING'), @TK::TkSPACE .new(18, 1, 18, "\r"), @TK::TkNL .new(19, 1, 19, "\n"), @TK::TkHEREDOC .new(19, 1, 19, %Q{Line 1\nLine 2\n}), @TK::TkHEREDOCEND.new(45, 4, 36, " STRING\n"), ] assert_equal expected, tokens end def test_class_tokenize_opassign tokens = RDoc::RubyLex.tokenize <<'RUBY', nil a %= b a /= b a -= b a += b a |= b a &= b a >>= b a <<= b a *= b a &&= b a ||= b a **= b RUBY expected = [ @TK::TkIDENTIFIER.new( 0, 1, 0, "a"), @TK::TkSPACE .new( 1, 1, 1, " "), @TK::TkOPASGN .new( 2, 1, 2, "%"), @TK::TkSPACE .new( 4, 1, 4, " "), @TK::TkIDENTIFIER.new( 5, 1, 5, "b"), @TK::TkNL .new( 6, 1, 6, "\n"), @TK::TkIDENTIFIER.new( 7, 2, 0, "a"), @TK::TkSPACE .new( 8, 2, 1, " "), @TK::TkOPASGN .new( 9, 2, 2, "/"), @TK::TkSPACE .new( 11, 2, 4, " "), @TK::TkIDENTIFIER.new( 12, 2, 5, "b"), @TK::TkNL .new( 13, 2, 7, "\n"), @TK::TkIDENTIFIER.new( 14, 3, 0, "a"), @TK::TkSPACE .new( 15, 3, 1, " "), @TK::TkOPASGN .new( 16, 3, 2, "-"), @TK::TkSPACE .new( 18, 3, 4, " "), @TK::TkIDENTIFIER.new( 19, 3, 5, "b"), @TK::TkNL .new( 20, 3, 14, "\n"), @TK::TkIDENTIFIER.new( 21, 4, 0, "a"), @TK::TkSPACE .new( 22, 4, 1, " "), @TK::TkOPASGN .new( 23, 4, 2, "+"), @TK::TkSPACE .new( 25, 4, 4, " "), @TK::TkIDENTIFIER.new( 26, 4, 5, "b"), @TK::TkNL .new( 27, 4, 21, "\n"), @TK::TkIDENTIFIER.new( 28, 5, 0, "a"), @TK::TkSPACE .new( 29, 5, 1, " "), @TK::TkOPASGN .new( 30, 5, 2, "|"), @TK::TkSPACE .new( 32, 5, 4, " "), @TK::TkIDENTIFIER.new( 33, 5, 5, "b"), @TK::TkNL .new( 34, 5, 28, "\n"), @TK::TkIDENTIFIER.new( 35, 6, 0, "a"), @TK::TkSPACE .new( 36, 6, 1, " "), @TK::TkOPASGN .new( 37, 6, 2, "&"), @TK::TkSPACE .new( 39, 6, 4, " "), @TK::TkIDENTIFIER.new( 40, 6, 5, "b"), @TK::TkNL .new( 41, 6, 35, "\n"), @TK::TkIDENTIFIER.new( 42, 7, 0, "a"), @TK::TkSPACE .new( 43, 7, 1, " "), @TK::TkOPASGN .new( 44, 7, 2, ">>"), @TK::TkSPACE .new( 47, 7, 5, " "), @TK::TkIDENTIFIER.new( 48, 7, 6, "b"), @TK::TkNL .new( 49, 7, 42, "\n"), @TK::TkIDENTIFIER.new( 50, 8, 0, "a"), @TK::TkSPACE .new( 51, 8, 1, " "), @TK::TkOPASGN .new( 52, 8, 2, "<<"), @TK::TkSPACE .new( 55, 8, 5, " "), @TK::TkIDENTIFIER.new( 56, 8, 6, "b"), @TK::TkNL .new( 57, 8, 50, "\n"), @TK::TkIDENTIFIER.new( 58, 9, 0, "a"), @TK::TkSPACE .new( 59, 9, 1, " "), @TK::TkOPASGN .new( 60, 9, 2, "*"), @TK::TkSPACE .new( 62, 9, 4, " "), @TK::TkIDENTIFIER.new( 63, 9, 5, "b"), @TK::TkNL .new( 64, 9, 58, "\n"), @TK::TkIDENTIFIER.new( 65, 10, 0, "a"), @TK::TkSPACE .new( 66, 10, 1, " "), @TK::TkOPASGN .new( 67, 10, 2, "&&"), @TK::TkSPACE .new( 70, 10, 5, " "), @TK::TkIDENTIFIER.new( 71, 10, 6, "b"), @TK::TkNL .new( 72, 10, 65, "\n"), @TK::TkIDENTIFIER.new( 73, 11, 0, "a"), @TK::TkSPACE .new( 74, 11, 1, " "), @TK::TkOPASGN .new( 75, 11, 2, "||"), @TK::TkSPACE .new( 78, 11, 5, " "), @TK::TkIDENTIFIER.new( 79, 11, 6, "b"), @TK::TkNL .new( 80, 11, 73, "\n"), @TK::TkIDENTIFIER.new( 81, 12, 0, "a"), @TK::TkSPACE .new( 82, 12, 1, " "), @TK::TkOPASGN .new( 83, 12, 2, "**"), @TK::TkSPACE .new( 86, 12, 5, " "), @TK::TkIDENTIFIER.new( 87, 12, 6, "b"), @TK::TkNL .new( 88, 12, 81, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_heredoc_call tokens = RDoc::RubyLex.tokenize <<-'RUBY', nil string = <<-STRING.chomp Line 1 Line 2 STRING RUBY expected = [ @TK::TkIDENTIFIER.new( 0, 1, 0, 'string'), @TK::TkSPACE .new( 6, 1, 6, ' '), @TK::TkASSIGN .new( 7, 1, 7, '='), @TK::TkSPACE .new( 8, 1, 8, ' '), @TK::TkHEREDOCBEG.new( 9, 1, 9, '<<-STRING'), @TK::TkDOT .new(18, 1, 18, '.'), @TK::TkIDENTIFIER.new(19, 1, 19, 'chomp'), @TK::TkNL .new(24, 1, 24, "\n"), @TK::TkHEREDOC .new(24, 1, 24, "Line 1\nLine 2\n"), @TK::TkHEREDOCEND.new(47, 4, 39, " STRING\n"), ] assert_equal expected, tokens end def test_class_tokenize_heredoc_indent tokens = RDoc::RubyLex.tokenize <<-'RUBY', nil string = <<-STRING Line 1 Line 2 STRING RUBY expected = [ @TK::TkIDENTIFIER.new( 0, 1, 0, 'string'), @TK::TkSPACE .new( 6, 1, 6, ' '), @TK::TkASSIGN .new( 7, 1, 7, '='), @TK::TkSPACE .new( 8, 1, 8, ' '), @TK::TkHEREDOCBEG.new( 9, 1, 9, '<<-STRING'), @TK::TkNL .new(18, 1, 18, "\n"), @TK::TkHEREDOC .new(18, 1, 18, "Line 1\nLine 2\n"), @TK::TkHEREDOCEND.new(41, 4, 33, " STRING\n") ] assert_equal expected, tokens end def test_class_tokenize_heredoc_missing_end e = assert_raises RDoc::RubyLex::Error do RDoc::RubyLex.tokenize <<-'RUBY', nil >> string1 = <<-TXT >" That's swell >" TXT RUBY end assert_equal 'Missing terminating TXT for string', e.message end def test_class_tokenize_heredoc_percent_N tokens = RDoc::RubyLex.tokenize <<-'RUBY', nil a b <<-U %N U RUBY expected = [ @TK::TkIDENTIFIER.new( 0, 1, 0, 'a'), @TK::TkSPACE .new( 1, 1, 1, ' '), @TK::TkIDENTIFIER.new( 2, 1, 2, 'b'), @TK::TkSPACE .new( 3, 1, 3, ' '), @TK::TkHEREDOCBEG.new( 4, 1, 4, '<<-U'), @TK::TkNL .new( 8, 1, 8, "\n"), @TK::TkHEREDOC .new( 8, 1, 8, "%N\n"), @TK::TkHEREDOCEND.new(13, 3, 12, "U\n") ] assert_equal expected, tokens end def test_class_tokenize_identifier_high_unicode tokens = RDoc::RubyLex.tokenize '𝖒', nil expected = @TK::TkIDENTIFIER.new(0, 1, 0, '𝖒') assert_equal expected, tokens.first end def test_class_tokenize_lambda tokens = RDoc::RubyLex.tokenize 'a = -> x, y { x + y }', nil expected = [ @TK::TkIDENTIFIER.new( 0, 1, 0, 'a'), @TK::TkSPACE .new( 1, 1, 1, ' '), @TK::TkASSIGN .new( 2, 1, 2, '='), @TK::TkSPACE .new( 3, 1, 3, ' '), @TK::TkLAMBDA .new( 4, 1, 4, '->'), @TK::TkSPACE .new( 6, 1, 6, ' '), @TK::TkIDENTIFIER.new( 7, 1, 7, 'x'), @TK::TkCOMMA .new( 8, 1, 8, ','), @TK::TkSPACE .new( 9, 1, 9, ' '), @TK::TkIDENTIFIER.new(10, 1, 10, 'y'), @TK::TkSPACE .new(11, 1, 11, ' '), @TK::TkfLBRACE .new(12, 1, 12, '{'), @TK::TkSPACE .new(13, 1, 13, ' '), @TK::TkIDENTIFIER.new(14, 1, 14, 'x'), @TK::TkSPACE .new(15, 1, 15, ' '), @TK::TkPLUS .new(16, 1, 16, '+'), @TK::TkSPACE .new(17, 1, 17, ' '), @TK::TkIDENTIFIER.new(18, 1, 18, 'y'), @TK::TkSPACE .new(19, 1, 19, ' '), @TK::TkRBRACE .new(20, 1, 20, '}'), @TK::TkNL .new(21, 1, 21, "\n") ] assert_equal expected, tokens end def test_class_tokenize_percent_1 tokens = RDoc::RubyLex.tokenize 'v%10==10', nil expected = [ @TK::TkIDENTIFIER.new(0, 1, 0, 'v'), @TK::TkMOD.new( 1, 1, 1, '%'), @TK::TkINTEGER.new( 2, 1, 2, '10'), @TK::TkEQ.new( 4, 1, 4, '=='), @TK::TkINTEGER.new( 6, 1, 6, '10'), @TK::TkNL.new( 8, 1, 8, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_percent_r tokens = RDoc::RubyLex.tokenize '%r[hi]', nil expected = [ @TK::TkREGEXP.new( 0, 1, 0, '%r[hi]'), @TK::TkNL .new( 6, 1, 6, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_percent_r_with_slash tokens = RDoc::RubyLex.tokenize '%r/hi/', nil expected = [ @TK::TkREGEXP.new( 0, 1, 0, '%r/hi/'), @TK::TkNL .new( 6, 1, 6, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_percent_large_q tokens = RDoc::RubyLex.tokenize '%Q/hi/', nil expected = [ @TK::TkSTRING.new( 0, 1, 0, '%Q/hi/'), @TK::TkNL .new( 6, 1, 6, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_percent_large_q_with_double_quote tokens = RDoc::RubyLex.tokenize '%Q"hi"', nil expected = [ @TK::TkSTRING.new( 0, 1, 0, '%Q"hi"'), @TK::TkNL .new( 6, 1, 6, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_percent_w tokens = RDoc::RubyLex.tokenize '%w[hi]', nil expected = [ @TK::TkDSTRING.new( 0, 1, 0, '%w[hi]'), @TK::TkNL .new( 6, 1, 6, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_percent_w_quote tokens = RDoc::RubyLex.tokenize '%w"hi"', nil expected = [ @TK::TkDSTRING.new( 0, 1, 0, '%w"hi"'), @TK::TkNL .new( 6, 1, 6, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_hash_rocket tokens = RDoc::RubyLex.tokenize "{ :foo=> 1 }", nil expected = [ @TK::TkLBRACE .new( 0, 1, 0, '{'), @TK::TkSPACE .new( 1, 1, 1, ' '), @TK::TkSYMBOL .new( 2, 1, 2, ':foo'), @TK::TkHASHROCKET.new( 6, 1, 6, '=>'), @TK::TkSPACE .new( 8, 1, 8, ' '), @TK::TkINTEGER .new( 9, 1, 9, '1'), @TK::TkSPACE .new(10, 1, 10, ' '), @TK::TkRBRACE .new(11, 1, 11, '}'), @TK::TkNL .new(12, 1, 12, "\n") ] assert_equal expected, tokens end def test_class_tokenize_percent_sign_quote tokens = RDoc::RubyLex.tokenize '%%hi%', nil expected = [ @TK::TkSTRING.new( 0, 1, 0, '%%hi%'), @TK::TkNL .new( 5, 1, 5, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_regexp tokens = RDoc::RubyLex.tokenize "/hay/", nil expected = [ @TK::TkREGEXP.new( 0, 1, 0, "/hay/"), @TK::TkNL .new( 5, 1, 5, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_regexp_options tokens = RDoc::RubyLex.tokenize "/hAY/i", nil expected = [ @TK::TkREGEXP.new( 0, 1, 0, "/hAY/i"), @TK::TkNL .new( 6, 1, 6, "\n"), ] assert_equal expected, tokens tokens = RDoc::RubyLex.tokenize "/hAY/ix", nil expected = [ @TK::TkREGEXP.new( 0, 1, 0, "/hAY/ix"), @TK::TkNL .new( 7, 1, 7, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_regexp_backref tokens = RDoc::RubyLex.tokenize "/[csh](..) [csh]\\1 in/", nil expected = [ @TK::TkREGEXP.new( 0, 1, 0, "/[csh](..) [csh]\\1 in/"), @TK::TkNL .new(22, 1, 22, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_regexp_escape tokens = RDoc::RubyLex.tokenize "/\\//", nil expected = [ @TK::TkREGEXP.new( 0, 1, 0, "/\\//"), @TK::TkNL .new( 4, 1, 4, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_number_with_sign_character tokens = RDoc::RubyLex.tokenize "+3--3r", nil expected = [ @TK::TkINTEGER .new(0, 1, 0, "+3"), @TK::TkMINUS .new(2, 1, 2, "-"), @TK::TkRATIONAL.new(3, 1, 3, "-3r"), @TK::TkNL .new(6, 1, 6, "\n"), ] assert_equal expected, tokens end def test_class_tokenize_regexp_continuing_backslash tokens = RDoc::RubyLex.tokenize "/(?