# # test_scanner_events.rb # begin require 'ripper' require 'test/unit' ripper_test = true module TestRipper; end rescue LoadError end class TestRipper::ScannerEvents < Test::Unit::TestCase def test_event_coverage dispatched = Ripper::SCANNER_EVENTS.map {|event,_| event } dispatched.each do |e| assert_equal true, respond_to?("test_#{e}", true), "event not tested: #{e}" end end def scan(target, str) sym = "on_#{target}".intern Ripper.lex(str).select {|_1,type,_2| type == sym }.map {|_1,_2,tok| tok } end def test_tokenize assert_equal [], Ripper.tokenize('') assert_equal ['a'], Ripper.tokenize('a') assert_equal ['1'], Ripper.tokenize('1') assert_equal ['1', ';', 'def', ' ', 'm', '(', 'arg', ')', 'end'], Ripper.tokenize("1;def m(arg)end") assert_equal ['print', '(', '< $"), scan('gvar', 'm($_, $~, $*, $$, $?, $!, $@, $/, $\\, $;, $,, $., $=, $:, $<, $>, $")') end def test_ident assert_equal [], scan('ident', '') assert_equal ['lvar'], scan('ident', 'lvar') assert_equal ['m', 'lvar'], scan('ident', 'm(lvar, @ivar, @@cvar, $gvar)') end def test_int assert_equal [], scan('int', '') assert_equal ['1', '10', '100000000000000'], scan('int', 'm(1,10,100000000000000)') end def test_ivar assert_equal [], scan('ivar', '') assert_equal ['@ivar'], scan('ivar', '@ivar') assert_equal ['@__ivar__'], scan('ivar', '@__ivar__') assert_equal ['@IVAR'], scan('ivar', '@IVAR') assert_equal ['@ivar'], scan('ivar', 'm(lvar, @ivar, @@cvar, $gvar)') end def test_kw assert_equal [], scan('kw', '') assert_equal %w(not), scan('kw', 'not 1') assert_equal %w(and), scan('kw', '1 and 2') assert_equal %w(or), scan('kw', '1 or 2') assert_equal %w(if then else end), scan('kw', 'if 1 then 2 else 3 end') assert_equal %w(if then elsif else end), scan('kw', 'if 1 then 2 elsif 3 else 4 end') assert_equal %w(unless then end), scan('kw', 'unless 1 then end') assert_equal %w(if true), scan('kw', '1 if true') assert_equal %w(unless false), scan('kw', '2 unless false') assert_equal %w(case when when else end), scan('kw', 'case n; when 1; when 2; else 3 end') assert_equal %w(while do nil end), scan('kw', 'while 1 do nil end') assert_equal %w(until do nil end), scan('kw', 'until 1 do nil end') assert_equal %w(while), scan('kw', '1 while 2') assert_equal %w(until), scan('kw', '1 until 2') assert_equal %w(while break next retry end), scan('kw', 'while 1; break; next; retry end') assert_equal %w(for in next break end), scan('kw', 'for x in obj; next 1; break 2 end') assert_equal %w(begin rescue retry end), scan('kw', 'begin 1; rescue; retry; end') assert_equal %w(rescue), scan('kw', '1 rescue 2') assert_equal %w(def redo return end), scan('kw', 'def m() redo; return end') assert_equal %w(def yield yield end), scan('kw', 'def m() yield; yield 1 end') assert_equal %w(def super super super end), scan('kw', 'def m() super; super(); super(1) end') assert_equal %w(alias), scan('kw', 'alias a b') assert_equal %w(undef), scan('kw', 'undef public') assert_equal %w(class end), scan('kw', 'class A < Object; end') assert_equal %w(module end), scan('kw', 'module M; end') assert_equal %w(class end), scan('kw', 'class << obj; end') assert_equal %w(BEGIN), scan('kw', 'BEGIN { }') assert_equal %w(END), scan('kw', 'END { }') assert_equal %w(self), scan('kw', 'self.class') assert_equal %w(nil true false), scan('kw', 'p(nil, true, false)') assert_equal %w(__FILE__ __LINE__), scan('kw', 'p __FILE__, __LINE__') assert_equal %w(defined?), scan('kw', 'defined?(Object)') end def test_lbrace assert_equal [], scan('lbrace', '') assert_equal ['{'], scan('lbrace', '3.times{ }') assert_equal ['{'], scan('lbrace', '3.times { }') assert_equal ['{'], scan('lbrace', '3.times{}') assert_equal [], scan('lbrace', '"{}"') assert_equal ['{'], scan('lbrace', '{1=>2}') end def test_rbrace assert_equal [], scan('rbrace', '') assert_equal ['}'], scan('rbrace', '3.times{ }') assert_equal ['}'], scan('rbrace', '3.times { }') assert_equal ['}'], scan('rbrace', '3.times{}') assert_equal [], scan('rbrace', '"{}"') assert_equal ['}'], scan('rbrace', '{1=>2}') end def test_lbracket assert_equal [], scan('lbracket', '') assert_equal ['['], scan('lbracket', '[]') assert_equal ['['], scan('lbracket', 'a[1]') assert_equal [], scan('lbracket', 'm(%q[])') end def test_rbracket assert_equal [], scan('rbracket', '') assert_equal [']'], scan('rbracket', '[]') assert_equal [']'], scan('rbracket', 'a[1]') assert_equal [], scan('rbracket', 'm(%q[])') end def test_lparen assert_equal [], scan('lparen', '') assert_equal ['('], scan('lparen', '()') assert_equal ['('], scan('lparen', 'm()') assert_equal ['('], scan('lparen', 'm (a)') assert_equal [], scan('lparen', '"()"') assert_equal [], scan('lparen', '"%w()"') end def test_rparen assert_equal [], scan('rparen', '') assert_equal [')'], scan('rparen', '()') assert_equal [')'], scan('rparen', 'm()') assert_equal [')'], scan('rparen', 'm (a)') assert_equal [], scan('rparen', '"()"') assert_equal [], scan('rparen', '"%w()"') end def test_op assert_equal [], scan('op', '') assert_equal ['|'], scan('op', '1 | 1') assert_equal ['^'], scan('op', '1 ^ 1') assert_equal ['&'], scan('op', '1 & 1') assert_equal ['<=>'], scan('op', '1 <=> 1') assert_equal ['=='], scan('op', '1 == 1') assert_equal ['==='], scan('op', '1 === 1') assert_equal ['=~'], scan('op', '1 =~ 1') assert_equal ['>'], scan('op', '1 > 1') assert_equal ['>='], scan('op', '1 >= 1') assert_equal ['<'], scan('op', '1 < 1') assert_equal ['<='], scan('op', '1 <= 1') assert_equal ['<<'], scan('op', '1 << 1') assert_equal ['>>'], scan('op', '1 >> 1') assert_equal ['+'], scan('op', '1 + 1') assert_equal ['-'], scan('op', '1 - 1') assert_equal ['*'], scan('op', '1 * 1') assert_equal ['/'], scan('op', '1 / 1') assert_equal ['%'], scan('op', '1 % 1') assert_equal ['**'], scan('op', '1 ** 1') assert_equal ['~'], scan('op', '~1') assert_equal ['-'], scan('op', '-a') assert_equal ['+'], scan('op', '+a') assert_equal ['[]'], scan('op', ':[]') assert_equal ['[]='], scan('op', ':[]=') assert_equal [], scan('op', %q[`make all`]) end def test_symbeg assert_equal [], scan('symbeg', '') assert_equal [':'], scan('symbeg', ':sym') assert_equal [':'], scan('symbeg', '[1,2,3,:sym]') assert_equal [], scan('symbeg', '":sym"') assert_equal [], scan('symbeg', 'a ? b : c') end def test_tstring_beg assert_equal [], scan('tstring_beg', '') assert_equal ['"'], scan('tstring_beg', '"abcdef"') assert_equal ['%q['], scan('tstring_beg', '%q[abcdef]') assert_equal ['%Q['], scan('tstring_beg', '%Q[abcdef]') end def test_tstring_content assert_equal [], scan('tstring_content', '') assert_equal ['abcdef'], scan('tstring_content', '"abcdef"') assert_equal ['abcdef'], scan('tstring_content', '%q[abcdef]') assert_equal ['abcdef'], scan('tstring_content', '%Q[abcdef]') assert_equal ['abc', 'def'], scan('tstring_content', '"abc#{1}def"') assert_equal ['sym'], scan('tstring_content', ':"sym"') end def test_tstring_end assert_equal [], scan('tstring_end', '') assert_equal ['"'], scan('tstring_end', '"abcdef"') assert_equal [']'], scan('tstring_end', '%q[abcdef]') assert_equal [']'], scan('tstring_end', '%Q[abcdef]') end def test_regexp_beg assert_equal [], scan('regexp_beg', '') assert_equal ['/'], scan('regexp_beg', '/re/') assert_equal ['%r<'], scan('regexp_beg', '%r') assert_equal [], scan('regexp_beg', '5 / 5') end def test_regexp_end assert_equal [], scan('regexp_end', '') assert_equal ['/'], scan('regexp_end', '/re/') assert_equal ['>'], scan('regexp_end', '%r') end def test_words_beg assert_equal [], scan('words_beg', '') assert_equal ['%W('], scan('words_beg', '%W()') assert_equal ['%W('], scan('words_beg', '%W(w w w)') assert_equal ['%W( '], scan('words_beg', '%W( w w w )') end def test_qwords_beg assert_equal [], scan('qwords_beg', '') assert_equal ['%w('], scan('qwords_beg', '%w()') assert_equal ['%w('], scan('qwords_beg', '%w(w w w)') assert_equal ['%w( '], scan('qwords_beg', '%w( w w w )') end # FIXME: Close paren must not present (`words_end' scanner event?). def test_words_sep assert_equal [], scan('words_sep', '') assert_equal [')'], scan('words_sep', '%w()') assert_equal [' ', ' ', ')'], scan('words_sep', '%w(w w w)') assert_equal [' ', ' ', ' )'], scan('words_sep', '%w( w w w )') assert_equal ["\n", ' ', ' )'], scan('words_sep', "%w( w\nw w )") end def test_heredoc_beg assert_equal [], scan('heredoc_beg', '') assert_equal ['<