summaryrefslogtreecommitdiff
path: root/test/ripper/test_scanner_events.rb
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-09-20 06:14:09 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-09-20 06:14:09 +0000
commitb1e943f465193fe4d834ba4b8e0e8dc142cfcaef (patch)
treed14a4b0fc2946a251b7de6de46ad85d6f8aa1aa5 /test/ripper/test_scanner_events.rb
parent3ad7a18275b55449fc1165441dc106651add72f4 (diff)
* test/ripper/test_scanner_events.rb: test #lineno and #column.
git-svn-id: svn+ssh://ci.ruby-lang.org/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.rb78
1 files changed, 59 insertions, 19 deletions
diff --git a/test/ripper/test_scanner_events.rb b/test/ripper/test_scanner_events.rb
index 6b95c4f5dd..8a8efbb168 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