diff options
author | aamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-09-20 06:14:09 +0000 |
---|---|---|
committer | aamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-09-20 06:14:09 +0000 |
commit | 44a0f819078a4520ef733d85f97cf163332005be (patch) | |
tree | d14a4b0fc2946a251b7de6de46ad85d6f8aa1aa5 /test/ripper/test_scanner_events.rb | |
parent | 6766808733c13dfbc07fbd81ddff3acc3a117c86 (diff) | |
download | ruby-44a0f819078a4520ef733d85f97cf163332005be.tar.gz ruby-44a0f819078a4520ef733d85f97cf163332005be.tar.xz ruby-44a0f819078a4520ef733d85f97cf163332005be.zip |
* test/ripper/test_scanner_events.rb: test #lineno and #column.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@6933 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ripper/test_scanner_events.rb')
-rw-r--r-- | test/ripper/test_scanner_events.rb | 78 |
1 files changed, 59 insertions, 19 deletions
diff --git a/test/ripper/test_scanner_events.rb b/test/ripper/test_scanner_events.rb index 6b95c4f5d..8a8efbb16 100644 --- a/test/ripper/test_scanner_events.rb +++ b/test/ripper/test_scanner_events.rb @@ -9,20 +9,12 @@ class TestRipper_ScannerEvents < Test::Unit::TestCase class R < Ripper def R.scan(target, src) - lex(target, src).map {|id, tok| tok } - end - - def R.lex(target, src) new(src, target).parse end def initialize(src, target) super src - if target - @target = ('on__' + target).intern - else - @target = nil - end + @target = target ? ('on__' + target).intern : nil end def parse @@ -32,18 +24,19 @@ class TestRipper_ScannerEvents < Test::Unit::TestCase end def on__scan(type, tok) - @tokens.push [type,tok] if !@target or type == @target - end - - def warn(fmt, *args) - #p [fmt, args] + @tokens.push tok if !@target or type == @target end + end - def warning(fmt, *args) - #p [fmt, args] + class PosInfo < Ripper + def parse + @q = [] + super + @q end - def compile_error(msg) + def on__scan(type, tok) + @q.push [tok, type, lineno(), column()] end end @@ -56,12 +49,47 @@ class TestRipper_ScannerEvents < Test::Unit::TestCase R.scan(nil, '1') assert_equal ['1', ';', 'def', ' ', 'm', '(', 'arg', ')', 'end'], R.scan(nil, "1;def m(arg)end") - assert_equal ['print', '(', '<<EOS', "heredoc\n", "EOS\n", ')', "\n"], + assert_equal ['print', '(', '<<EOS', ')', "\n", "heredoc\n", "EOS\n"], R.scan(nil, "print(<<EOS)\nheredoc\nEOS\n") - assert_equal ['print', '(', ' ', '<<EOS', "heredoc\n", "EOS\n", ')', "\n"], + assert_equal ['print', '(', ' ', '<<EOS', ')', "\n", "heredoc\n", "EOS\n"], R.scan(nil, "print( <<EOS)\nheredoc\nEOS\n") end + def test_location + validate_location "" + validate_location " " + validate_location "@" + validate_location "\n" + validate_location "\r\n" + validate_location "\n\n\n\n\n\r\n\n\n" + validate_location "\n;\n;\n;\n;\n" + validate_location "nil" + validate_location "@ivar" + validate_location "1;2;3" + validate_location "1\n2\n3" + validate_location "1\n2\n3\n" + validate_location "def m(a) nil end" + validate_location "if true then false else nil end" + validate_location "BEGIN{print nil}" + validate_location "%w(a b\nc\r\nd \ne )" + validate_location %Q["a\nb\r\nc"] + validate_location "print(<<EOS)\nheredoc\nEOS\n" + validate_location %Q[print(<<-"EOS")\nheredoc\n EOS\n] + end + + def validate_location(src) + data = PosInfo.new(src).parse + buf = '' + data.each do |tok, type, line, col| + assert_equal buf.count("\n") + 1, line, + "wrong lineno: #{tok.inspect} (#{type}) [#{line}:#{col}]" + assert_equal buf.sub(/\A.*\n/m, '').size, col, + "wrong column: #{tok.inspect} (#{type}) [#{line}:#{col}]" + buf << tok + end + assert_equal src, buf + end + def test_backref assert_equal ["$`", "$&", "$'", '$1', '$2', '$3'], R.scan('backref', %q[m($~, $`, $&, $', $1, $2, $3)]) @@ -727,6 +755,18 @@ class TestRipper_ScannerEvents < Test::Unit::TestCase end def test___end__ + assert_equal [], + R.scan('__end__', "") + assert_equal ["__END__"], + R.scan('__end__', "__END__") + assert_equal ["__END__\n"], + R.scan('__end__', "__END__\n") + assert_equal ["__END__\n"], + R.scan(nil, "__END__\njunk junk junk") + assert_equal ["__END__"], + R.scan('__end__', "1\n__END__") + assert_equal [], + R.scan('__end__', "print('__END__')") end def test_CHAR |