2 # test_scanner_events.rb
9 class TestRipper_ScannerEvents < Test::Unit::TestCase
11 def test_event_coverage
12 dispatched = Ripper::SCANNER_EVENTS.map {|event,_| event }
13 dispatched.each do |e|
14 assert_equal true, respond_to?("test_#{e}", true), "event not tested: #{e}"
19 sym = "on_#{target}".intern
20 Ripper.lex(str).select {|_1,type,_2| type == sym }.map {|_1,_2,tok| tok }
30 assert_equal ['1', ';', 'def', ' ', 'm', '(', 'arg', ')', 'end'],
31 Ripper.tokenize("1;def m(arg)end")
32 assert_equal ['print', '(', '<<EOS', ')', "\n", "heredoc\n", "EOS\n"],
33 Ripper.tokenize("print(<<EOS)\nheredoc\nEOS\n")
34 assert_equal ['print', '(', ' ', '<<EOS', ')', "\n", "heredoc\n", "EOS\n"],
35 Ripper.tokenize("print( <<EOS)\nheredoc\nEOS\n")
36 assert_equal ["\#\n", "\n", "\#\n", "\n", "nil", "\n"],
37 Ripper.tokenize("\#\n\n\#\n\nnil\n")
43 assert_equal [[[1,0], :on_ident, "a"]],
45 assert_equal [[[1, 0], :on_kw, "nil"]],
47 assert_equal [[[1, 0], :on_kw, "def"],
48 [[1, 3], :on_sp, " "],
49 [[1, 4], :on_ident, "m"],
50 [[1, 5], :on_lparen, "("],
51 [[1, 6], :on_ident, "a"],
52 [[1, 7], :on_rparen, ")"],
53 [[1, 8], :on_kw, "end"]],
54 Ripper.lex("def m(a)end")
55 assert_equal [[[1, 0], :on_int, "1"],
56 [[1, 1], :on_nl, "\n"],
57 [[2, 0], :on_int, "2"],
58 [[2, 1], :on_nl, "\n"],
59 [[3, 0], :on_int, "3"]],
61 assert_equal [[[1, 0], :on_heredoc_beg, "<<EOS"],
62 [[1, 5], :on_nl, "\n"],
63 [[2, 0], :on_tstring_content, "heredoc\n"],
64 [[3, 0], :on_heredoc_end, "EOS"]],
65 Ripper.lex("<<EOS\nheredoc\nEOS")
72 validate_location "\n"
73 validate_location "\r\n"
74 validate_location "\n\n\n\n\n\r\n\n\n"
75 validate_location "\n;\n;\n;\n;\n"
76 validate_location "nil"
77 validate_location "@ivar"
78 validate_location "1;2;3"
79 validate_location "1\n2\n3"
80 validate_location "1\n2\n3\n"
81 validate_location "def m(a) nil end"
82 validate_location "if true then false else nil end"
83 validate_location "BEGIN{print nil}"
84 validate_location "%w(a b\nc\r\nd \ne )"
85 validate_location %Q["a\nb\r\nc"]
86 validate_location "print(<<EOS)\nheredoc\nEOS\n"
87 validate_location %Q[print(<<-"EOS")\nheredoc\n EOS\n]
90 def validate_location(src)
92 Ripper.lex(src).each do |pos, type, tok|
94 assert_equal buf.count("\n") + 1, line,
95 "wrong lineno: #{tok.inspect} (#{type}) [#{line}:#{col}]"
96 assert_equal buf.sub(/\A.*\n/m, '').size, col,
97 "wrong column: #{tok.inspect} (#{type}) [#{line}:#{col}]"
100 assert_equal src, buf
104 assert_equal ["$`", "$&", "$'", '$1', '$2', '$3'],
105 scan('backref', %q[m($~, $`, $&, $', $1, $2, $3)])
110 scan('backtick', %q[p `make all`])
114 assert_equal [','] * 6,
115 scan('comma', %q[ m(0,1,2,3,4,5,6) ])
117 scan('comma', %q[".,.,.,.,.,.,.."])
119 scan('comma', %Q[<<EOS\n,,,,,,,,,,\nEOS])
126 scan('period', 'a.b')
128 scan('period', 'Object.new')
130 scan('period', '"."')
132 scan('period', '1..2')
134 scan('period', '1...3')
138 assert_equal ['CONST'],
139 scan('const', 'CONST')
142 assert_equal ['CONST_A'],
143 scan('const', 'CONST_A')
144 assert_equal ['Const', 'Const2', 'Const3'],
145 scan('const', 'Const; Const2; Const3')
146 assert_equal ['Const'],
147 scan('const', 'Const(a)')
148 assert_equal ['M', 'A', 'A2'],
149 scan('const', 'M(A,A2)')
153 scan('const', 'm(lvar, @ivar, @@cvar, $gvar)')
159 assert_equal ['@@cvar'],
160 scan('cvar', '@@cvar')
161 assert_equal ['@@__cvar__'],
162 scan('cvar', '@@__cvar__')
163 assert_equal ['@@CVAR'],
164 scan('cvar', '@@CVAR')
165 assert_equal ['@@cvar'],
166 scan('cvar', ' @@cvar#comment')
167 assert_equal ['@@cvar'],
168 scan('cvar', ':@@cvar')
169 assert_equal ['@@cvar'],
170 scan('cvar', 'm(lvar, @ivar, @@cvar, $gvar)')
172 scan('cvar', '"@@cvar"')
177 scan('embexpr_beg', '')
179 scan('embexpr_beg', '"#{expr}"')
181 scan('embexpr_beg', '%q[#{expr}]')
183 scan('embexpr_beg', '%Q[#{expr}]')
185 scan('embexpr_beg', "m(<<EOS)\n\#{expr}\nEOS")
190 # currently detected as "rbrace"
192 scan('embexpr_end', '')
194 scan('embexpr_end', '"#{expr}"')
196 scan('embexpr_end', '%q[#{expr}]')
198 scan('embexpr_end', '%Q[#{expr}]')
200 scan('embexpr_end', "m(<<EOS)\n\#{expr}\nEOS")
208 scan('embvar', '"#$gvar"')
210 scan('embvar', '"#@ivar"')
212 scan('embvar', '"#@@cvar"')
214 scan('embvar', '"#lvar"')
216 scan('embvar', '"#"')
218 scan('embvar', '"\#$gvar"')
220 scan('embvar', '"\#@ivar"')
222 scan('embvar', '%q[#@ivar]')
224 scan('embvar', '%Q[#@ivar]')
230 assert_equal ['1.000'],
231 scan('float', '1.000')
232 assert_equal ['123.456'],
233 scan('float', '123.456')
234 assert_equal ['1.2345678901234567890123456789'],
235 scan('float', '1.2345678901234567890123456789')
236 assert_equal ['1.000'],
237 scan('float', ' 1.000# comment')
238 assert_equal ['1.234e5'],
239 scan('float', '1.234e5')
240 assert_equal ['1.234e1234567890'],
241 scan('float', '1.234e1234567890')
242 assert_equal ['1.0'],
243 scan('float', 'm(a,b,1.0,c,d)')
253 assert_equal ['$gvar'],
254 scan('gvar', 'm(lvar, @ivar, @@cvar, $gvar)')
255 assert_equal %w($_ $~ $* $$ $? $! $@ $/ $\\ $; $, $. $= $: $< $> $"),
256 scan('gvar', 'm($_, $~, $*, $$, $?, $!, $@, $/, $\\, $;, $,, $., $=, $:, $<, $>, $")')
262 assert_equal ['lvar'],
263 scan('ident', 'lvar')
264 assert_equal ['m', 'lvar'],
265 scan('ident', 'm(lvar, @ivar, @@cvar, $gvar)')
271 assert_equal ['1', '10', '100000000000000'],
272 scan('int', 'm(1,10,100000000000000)')
278 assert_equal ['@ivar'],
279 scan('ivar', '@ivar')
280 assert_equal ['@__ivar__'],
281 scan('ivar', '@__ivar__')
282 assert_equal ['@IVAR'],
283 scan('ivar', '@IVAR')
284 assert_equal ['@ivar'],
285 scan('ivar', 'm(lvar, @ivar, @@cvar, $gvar)')
291 assert_equal %w(not),
293 assert_equal %w(and),
294 scan('kw', '1 and 2')
297 assert_equal %w(if then else end),
298 scan('kw', 'if 1 then 2 else 3 end')
299 assert_equal %w(if then elsif else end),
300 scan('kw', 'if 1 then 2 elsif 3 else 4 end')
301 assert_equal %w(unless then end),
302 scan('kw', 'unless 1 then end')
303 assert_equal %w(if true),
304 scan('kw', '1 if true')
305 assert_equal %w(unless false),
306 scan('kw', '2 unless false')
307 assert_equal %w(case when when else end),
308 scan('kw', 'case n; when 1; when 2; else 3 end')
309 assert_equal %w(while do nil end),
310 scan('kw', 'while 1 do nil end')
311 assert_equal %w(until do nil end),
312 scan('kw', 'until 1 do nil end')
313 assert_equal %w(while),
314 scan('kw', '1 while 2')
315 assert_equal %w(until),
316 scan('kw', '1 until 2')
317 assert_equal %w(while break next retry end),
318 scan('kw', 'while 1; break; next; retry end')
319 assert_equal %w(for in next break end),
320 scan('kw', 'for x in obj; next 1; break 2 end')
321 assert_equal %w(begin rescue retry end),
322 scan('kw', 'begin 1; rescue; retry; end')
323 assert_equal %w(rescue),
324 scan('kw', '1 rescue 2')
325 assert_equal %w(def redo return end),
326 scan('kw', 'def m() redo; return end')
327 assert_equal %w(def yield yield end),
328 scan('kw', 'def m() yield; yield 1 end')
329 assert_equal %w(def super super super end),
330 scan('kw', 'def m() super; super(); super(1) end')
331 assert_equal %w(alias),
332 scan('kw', 'alias a b')
333 assert_equal %w(undef),
334 scan('kw', 'undef public')
335 assert_equal %w(class end),
336 scan('kw', 'class A < Object; end')
337 assert_equal %w(module end),
338 scan('kw', 'module M; end')
339 assert_equal %w(class end),
340 scan('kw', 'class << obj; end')
341 assert_equal %w(BEGIN),
342 scan('kw', 'BEGIN { }')
343 assert_equal %w(END),
344 scan('kw', 'END { }')
345 assert_equal %w(self),
346 scan('kw', 'self.class')
347 assert_equal %w(nil true false),
348 scan('kw', 'p(nil, true, false)')
349 assert_equal %w(__FILE__ __LINE__),
350 scan('kw', 'p __FILE__, __LINE__')
351 assert_equal %w(defined?),
352 scan('kw', 'defined?(Object)')
359 scan('lbrace', '3.times{ }')
361 scan('lbrace', '3.times { }')
363 scan('lbrace', '3.times{}')
365 scan('lbrace', '"{}"')
367 scan('lbrace', '{1=>2}')
374 scan('rbrace', '3.times{ }')
376 scan('rbrace', '3.times { }')
378 scan('rbrace', '3.times{}')
380 scan('rbrace', '"{}"')
382 scan('rbrace', '{1=>2}')
389 scan('lbracket', '[]')
391 scan('lbracket', 'a[1]')
393 scan('lbracket', 'm(%q[])')
400 scan('rbracket', '[]')
402 scan('rbracket', 'a[1]')
404 scan('rbracket', 'm(%q[])')
413 scan('lparen', 'm()')
415 scan('lparen', 'm (a)')
417 scan('lparen', '"()"')
419 scan('lparen', '"%w()"')
428 scan('rparen', 'm()')
430 scan('rparen', 'm (a)')
432 scan('rparen', '"()"')
434 scan('rparen', '"%w()"')
446 assert_equal ['<=>'],
447 scan('op', '1 <=> 1')
450 assert_equal ['==='],
451 scan('op', '1 === 1')
486 assert_equal ['[]='],
489 scan('op', %q[`make all`])
496 scan('symbeg', ':sym')
498 scan('symbeg', '[1,2,3,:sym]')
500 scan('symbeg', '":sym"')
502 scan('symbeg', 'a ? b : c')
507 scan('tstring_beg', '')
509 scan('tstring_beg', '"abcdef"')
510 assert_equal ['%q['],
511 scan('tstring_beg', '%q[abcdef]')
512 assert_equal ['%Q['],
513 scan('tstring_beg', '%Q[abcdef]')
516 def test_tstring_content
518 scan('tstring_content', '')
519 assert_equal ['abcdef'],
520 scan('tstring_content', '"abcdef"')
521 assert_equal ['abcdef'],
522 scan('tstring_content', '%q[abcdef]')
523 assert_equal ['abcdef'],
524 scan('tstring_content', '%Q[abcdef]')
525 assert_equal ['abc', 'def'],
526 scan('tstring_content', '"abc#{1}def"')
527 assert_equal ['sym'],
528 scan('tstring_content', ':"sym"')
533 scan('tstring_end', '')
535 scan('tstring_end', '"abcdef"')
537 scan('tstring_end', '%q[abcdef]')
539 scan('tstring_end', '%Q[abcdef]')
544 scan('regexp_beg', '')
546 scan('regexp_beg', '/re/')
547 assert_equal ['%r<'],
548 scan('regexp_beg', '%r<re>')
550 scan('regexp_beg', '5 / 5')
555 scan('regexp_end', '')
557 scan('regexp_end', '/re/')
559 scan('regexp_end', '%r<re>')
564 scan('words_beg', '')
565 assert_equal ['%W('],
566 scan('words_beg', '%W()')
567 assert_equal ['%W('],
568 scan('words_beg', '%W(w w w)')
569 assert_equal ['%W( '],
570 scan('words_beg', '%W( w w w )')
575 scan('qwords_beg', '')
576 assert_equal ['%w('],
577 scan('qwords_beg', '%w()')
578 assert_equal ['%w('],
579 scan('qwords_beg', '%w(w w w)')
580 assert_equal ['%w( '],
581 scan('qwords_beg', '%w( w w w )')
584 # FIXME: Close paren must not present (`words_end' scanner event?).
587 scan('words_sep', '')
589 scan('words_sep', '%w()')
590 assert_equal [' ', ' ', ')'],
591 scan('words_sep', '%w(w w w)')
592 assert_equal [' ', ' ', ' )'],
593 scan('words_sep', '%w( w w w )')
594 assert_equal ["\n", ' ', ' )'],
595 scan('words_sep', "%w( w\nw w )")
600 scan('heredoc_beg', '')
601 assert_equal ['<<EOS'],
602 scan('heredoc_beg', "<<EOS\nheredoc\nEOS")
603 assert_equal ['<<EOS'],
604 scan('heredoc_beg', "<<EOS\nheredoc\nEOS\n")
605 assert_equal ['<<EOS'],
606 scan('heredoc_beg', "<<EOS\nheredoc\nEOS \n")
607 assert_equal ['<<-EOS'],
608 scan('heredoc_beg', "<<-EOS\nheredoc\n\tEOS \n")
609 assert_equal ['<<"EOS"'],
610 scan('heredoc_beg', %Q[<<"EOS"\nheredoc\nEOS])
611 assert_equal [%q(<<'EOS')],
612 scan('heredoc_beg', "<<'EOS'\nheredoc\nEOS")
613 assert_equal [%q(<<`EOS`)],
614 scan('heredoc_beg', "<<`EOS`\nheredoc\nEOS")
615 assert_equal [%q(<<" ")],
616 scan('heredoc_beg', %Q[<<" "\nheredoc\nEOS])
619 def test_tstring_content_HEREDOC
621 scan('tstring_content', '')
622 assert_equal ["heredoc\n"],
623 scan('tstring_content', "<<EOS\nheredoc\nEOS")
624 assert_equal ["heredoc\n"],
625 scan('tstring_content', "<<EOS\nheredoc\nEOS\n")
626 assert_equal ["heredoc \n"],
627 scan('tstring_content', "<<EOS\nheredoc \nEOS \n")
628 assert_equal ["heredoc\n"],
629 scan('tstring_content', "<<-EOS\nheredoc\n\tEOS \n")
634 scan('heredoc_end', '')
635 assert_equal ["EOS"],
636 scan('heredoc_end', "<<EOS\nheredoc\nEOS")
637 assert_equal ["EOS\n"],
638 scan('heredoc_end', "<<EOS\nheredoc\nEOS\n")
639 assert_equal ["EOS \n"],
640 scan('heredoc_end', "<<EOS\nheredoc\nEOS \n")
641 assert_equal ["\tEOS \n"],
642 scan('heredoc_end', "<<-EOS\nheredoc\n\tEOS \n")
647 scan('semicolon', '')
649 scan('semicolon', ';')
650 assert_equal %w(; ;),
651 scan('semicolon', '; ;')
652 assert_equal %w(; ; ;),
653 scan('semicolon', 'nil;nil;nil;')
654 assert_equal %w(; ; ;),
655 scan('semicolon', 'nil;nil;nil;nil')
657 scan('semicolon', '";"')
659 scan('semicolon', '%w(;)')
661 scan('semicolon', '/;/')
667 assert_equal ['# comment'],
668 scan('comment', '# comment')
669 assert_equal ["# comment\n"],
670 scan('comment', "# comment\n")
671 assert_equal ["# comment\n"],
672 scan('comment', "# comment\n1 + 1")
673 assert_equal ["# comment\n"],
674 scan('comment', "1 + 1 + 1# comment\n1 + 1")
679 scan('embdoc_beg', '')
680 assert_equal ["=begin\n"],
681 scan('embdoc_beg', "=begin\ndoc\n=end")
682 assert_equal ["=begin \n"],
683 scan('embdoc_beg', "=begin \ndoc\n=end\n")
684 assert_equal ["=begin comment\n"],
685 scan('embdoc_beg', "=begin comment\ndoc\n=end\n")
691 assert_equal ["doc\n"],
692 scan('embdoc', "=begin\ndoc\n=end")
693 assert_equal ["doc\n"],
694 scan('embdoc', "=begin\ndoc\n=end\n")
699 scan('embdoc_end', '')
700 assert_equal ["=end"],
701 scan('embdoc_end', "=begin\ndoc\n=end")
702 assert_equal ["=end\n"],
703 scan('embdoc_end', "=begin\ndoc\n=end\n")
717 assert_equal [' ', ' '],
724 scan('sp', "%w( w )")
729 # `nl' event always means End-Of-Statement.
736 scan('nl', "1 + 1\n")
737 assert_equal ["\n", "\n"],
738 scan('nl', "1 + 1\n2 + 2\n")
743 assert_equal ["\r\n"],
744 scan('nl', "1 + 1\r\n")
751 scan('ignored_nl', '')
753 scan('ignored_nl', "\n")
755 scan('ignored_nl', "1 + 1\n")
757 scan('ignored_nl', "1 + 1\n2 + 2\n")
759 scan('ignored_nl', "1 +\n1")
761 scan('ignored_nl', "1;\n")
763 scan('ignored_nl', "1 + 1\r\n")
764 assert_equal ["\r\n"],
765 scan('ignored_nl', "1;\r\n")
771 assert_equal ["__END__"],
772 scan('__end__', "__END__")
773 assert_equal ["__END__\n"],
774 scan('__end__', "__END__\n")
775 assert_equal ["__END__\n"],
776 Ripper.tokenize("__END__\njunk junk junk")
777 assert_equal ["__END__"],
778 scan('__end__', "1\n__END__")
780 scan('__end__', "print('__END__')")
789 scan('CHAR', "@ivar")