# # test_scanner_events.rb # require 'ripper.so' raise 'ripper version differ' unless Ripper::Version == '0.1.0' require 'test/unit' class TestRipper_ScannerEvents < Test::Unit::TestCase class R < Ripper def R.scan(target, src) r = new(src, target) r.parse r.tokens.map {|id, tok| tok } end def R.lex(src) r = new(src, 'scan') r.parse r.tokens end def initialize(src, target) super src @target = ('on__' + target).intern @tokens = [] end attr_reader :tokens def method_missing(mid, *args) case mid.to_s when /\Aon__scan/ if @target == :on__scan @tokens.push args else @tokens.push args if @target == args[0] end when /\Aon__/ ; else raise NoMethodError, "no such method: #{mid}" end end def warn(fmt, *args) #p [fmt, args] end def warning(fmt, *args) #p [fmt, args] end def compile_error(msg) end end def test_scan assert_equal [], R.scan('scan', '') assert_equal ['a'], R.scan('scan', 'a') assert_equal ['1'], R.scan('scan', '1') assert_equal ['1', ';', 'def', ' ', 'm', '(', 'arg', ')', 'end'], R.scan('scan', "1;def m(arg)end") end def test_backref assert_equal ["$`", "$&", "$'", '$1', '$2', '$3'], R.scan('backref', %q[m($~, $`, $&, $', $1, $2, $3)]) end def test_backtick assert_equal ["`"], R.scan('backtick', %q[p `make all`]) end def test_comma assert_equal [','] * 6, R.scan('comma', %q[ m(0,1,2,3,4,5,6) ]) assert_equal [], R.scan('comma', %q[".,.,.,.,.,.,.."]) assert_equal [], R.scan('comma', %Q[< $"), R.scan('gvar', 'm($_, $~, $*, $$, $?, $!, $@, $/, $\\, $;, $,, $., $=, $:, $<, $>, $")') end def test_ident assert_equal [], R.scan('ident', '') assert_equal ['lvar'], R.scan('ident', 'lvar') assert_equal ['m', 'lvar'], R.scan('ident', 'm(lvar, @ivar, @@cvar, $gvar)') end def test_int assert_equal [], R.scan('int', '') assert_equal ['1', '10', '100000000000000'], R.scan('int', 'm(1,10,100000000000000)') end def test_ivar assert_equal [], R.scan('ivar', '') assert_equal ['@ivar'], R.scan('ivar', '@ivar') assert_equal ['@__ivar__'], R.scan('ivar', '@__ivar__') assert_equal ['@IVAR'], R.scan('ivar', '@IVAR') assert_equal ['@ivar'], R.scan('ivar', 'm(lvar, @ivar, @@cvar, $gvar)') end def test_kw assert_equal [], R.scan('kw', '') assert_equal %w(not), R.scan('kw', 'not 1') assert_equal %w(and), R.scan('kw', '1 and 2') assert_equal %w(or), R.scan('kw', '1 or 2') assert_equal %w(if then else end), R.scan('kw', 'if 1 then 2 else 3 end') assert_equal %w(if then elsif else end), R.scan('kw', 'if 1 then 2 elsif 3 else 4 end') assert_equal %w(unless then end), R.scan('kw', 'unless 1 then end') assert_equal %w(if true), R.scan('kw', '1 if true') assert_equal %w(unless false), R.scan('kw', '2 unless false') assert_equal %w(case when when else end), R.scan('kw', 'case n; when 1; when 2; else 3 end') assert_equal %w(while do nil end), R.scan('kw', 'while 1 do nil end') assert_equal %w(until do nil end), R.scan('kw', 'until 1 do nil end') assert_equal %w(while), R.scan('kw', '1 while 2') assert_equal %w(until), R.scan('kw', '1 until 2') assert_equal %w(while break next retry end), R.scan('kw', 'while 1; break; next; retry end') assert_equal %w(for in next break end), R.scan('kw', 'for x in obj; next 1; break 2 end') assert_equal %w(begin rescue retry end), R.scan('kw', 'begin 1; rescue; retry; end') assert_equal %w(rescue), R.scan('kw', '1 rescue 2') assert_equal %w(def redo return end), R.scan('kw', 'def m() redo; return end') assert_equal %w(def yield yield end), R.scan('kw', 'def m() yield; yield 1 end') assert_equal %w(def super super super end), R.scan('kw', 'def m() super; super(); super(1) end') assert_equal %w(alias), R.scan('kw', 'alias a b') assert_equal %w(undef), R.scan('kw', 'undef public') assert_equal %w(class end), R.scan('kw', 'class A < Object; end') assert_equal %w(module end), R.scan('kw', 'module M; end') assert_equal %w(class end), R.scan('kw', 'class << obj; end') assert_equal %w(BEGIN), R.scan('kw', 'BEGIN { }') assert_equal %w(END), R.scan('kw', 'END { }') assert_equal %w(self), R.scan('kw', 'self.class') assert_equal %w(nil true false), R.scan('kw', 'p(nil, true, false)') assert_equal %w(__FILE__ __LINE__), R.scan('kw', 'p __FILE__, __LINE__') assert_equal %w(defined?), R.scan('kw', 'defined?(Object)') end def test_lbrace assert_equal [], R.scan('lbrace', '') assert_equal ['{'], R.scan('lbrace', '3.times{ }') assert_equal ['{'], R.scan('lbrace', '3.times { }') assert_equal ['{'], R.scan('lbrace', '3.times{}') assert_equal [], R.scan('lbrace', '"{}"') assert_equal ['{'], R.scan('lbrace', '{1=>2}') end def test_rbrace assert_equal [], R.scan('rbrace', '') assert_equal ['}'], R.scan('rbrace', '3.times{ }') assert_equal ['}'], R.scan('rbrace', '3.times { }') assert_equal ['}'], R.scan('rbrace', '3.times{}') assert_equal [], R.scan('rbrace', '"{}"') assert_equal ['}'], R.scan('rbrace', '{1=>2}') end def test_lbracket assert_equal [], R.scan('lbracket', '') assert_equal ['['], R.scan('lbracket', '[]') assert_equal ['['], R.scan('lbracket', 'a[1]') assert_equal [], R.scan('lbracket', 'm(%q[])') end def test_rbracket assert_equal [], R.scan('rbracket', '') assert_equal [']'], R.scan('rbracket', '[]') assert_equal [']'], R.scan('rbracket', 'a[1]') assert_equal [], R.scan('rbracket', 'm(%q[])') end def test_lparen assert_equal [], R.scan('lparen', '') assert_equal ['('], R.scan('lparen', '()') assert_equal ['('], R.scan('lparen', 'm()') assert_equal ['('], R.scan('lparen', 'm (a)') assert_equal [], R.scan('lparen', '"()"') assert_equal [], R.scan('lparen', '"%w()"') end def test_rparen assert_equal [], R.scan('rparen', '') assert_equal [')'], R.scan('rparen', '()') assert_equal [')'], R.scan('rparen', 'm()') assert_equal [')'], R.scan('rparen', 'm (a)') assert_equal [], R.scan('rparen', '"()"') assert_equal [], R.scan('rparen', '"%w()"') end def test_op assert_equal [], R.scan('op', '') assert_equal ['|'], R.scan('op', '1 | 1') assert_equal ['^'], R.scan('op', '1 ^ 1') assert_equal ['&'], R.scan('op', '1 & 1') assert_equal ['<=>'], R.scan('op', '1 <=> 1') assert_equal ['=='], R.scan('op', '1 == 1') assert_equal ['==='], R.scan('op', '1 === 1') assert_equal ['=~'], R.scan('op', '1 =~ 1') assert_equal ['>'], R.scan('op', '1 > 1') assert_equal ['>='], R.scan('op', '1 >= 1') assert_equal ['<'], R.scan('op', '1 < 1') assert_equal ['<='], R.scan('op', '1 <= 1') assert_equal ['<<'], R.scan('op', '1 << 1') assert_equal ['>>'], R.scan('op', '1 >> 1') assert_equal ['+'], R.scan('op', '1 + 1') assert_equal ['-'], R.scan('op', '1 - 1') assert_equal ['*'], R.scan('op', '1 * 1') assert_equal ['/'], R.scan('op', '1 / 1') assert_equal ['%'], R.scan('op', '1 % 1') assert_equal ['**'], R.scan('op', '1 ** 1') assert_equal ['~'], R.scan('op', '~1') assert_equal ['-'], R.scan('op', '-a') assert_equal ['+'], R.scan('op', '+a') assert_equal ['[]'], R.scan('op', ':[]') assert_equal ['[]='], R.scan('op', ':[]=') assert_equal [], R.scan('op', %q[`make all`]) end def test_symbeg assert_equal [], R.scan('symbeg', '') assert_equal [':'], R.scan('symbeg', ':sym') assert_equal [':'], R.scan('symbeg', '[1,2,3,:sym]') assert_equal [], R.scan('symbeg', '":sym"') assert_equal [], R.scan('symbeg', 'a ? b : c') end def test_tstring_beg assert_equal [], R.scan('tstring_beg', '') assert_equal ['"'], R.scan('tstring_beg', '"abcdef"') assert_equal ['%q['], R.scan('tstring_beg', '%q[abcdef]') assert_equal ['%Q['], R.scan('tstring_beg', '%Q[abcdef]') end def test_tstring_content assert_equal [], R.scan('tstring_content', '') assert_equal ['abcdef'], R.scan('tstring_content', '"abcdef"') assert_equal ['abcdef'], R.scan('tstring_content', '%q[abcdef]') assert_equal ['abcdef'], R.scan('tstring_content', '%Q[abcdef]') assert_equal ['abc', 'def'], R.scan('tstring_content', '"abc#{1}def"') assert_equal ['sym'], R.scan('tstring_content', ':"sym"') end def test_tstring_end assert_equal [], R.scan('tstring_end', '') assert_equal ['"'], R.scan('tstring_end', '"abcdef"') assert_equal [']'], R.scan('tstring_end', '%q[abcdef]') assert_equal [']'], R.scan('tstring_end', '%Q[abcdef]') end def test_regexp_beg assert_equal [], R.scan('regexp_beg', '') assert_equal ['/'], R.scan('regexp_beg', '/re/') assert_equal ['%r<'], R.scan('regexp_beg', '%r') assert_equal [], R.scan('regexp_beg', '5 / 5') end def test_regexp_end assert_equal [], R.scan('regexp_end', '') assert_equal ['/'], R.scan('regexp_end', '/re/') assert_equal ['>'], R.scan('regexp_end', '%r') end def test_words_beg assert_equal [], R.scan('words_beg', '') assert_equal ['%W('], R.scan('words_beg', '%W()') assert_equal ['%W('], R.scan('words_beg', '%W(w w w)') assert_equal ['%W( '], R.scan('words_beg', '%W( w w w )') end def test_qwords_beg assert_equal [], R.scan('qwords_beg', '') assert_equal ['%w('], R.scan('qwords_beg', '%w()') assert_equal ['%w('], R.scan('qwords_beg', '%w(w w w)') assert_equal ['%w( '], R.scan('qwords_beg', '%w( w w w )') end # FIXME: Close paren must not present (`words_end' scanner event?). def test_words_sep assert_equal [], R.scan('words_sep', '') assert_equal [')'], R.scan('words_sep', '%w()') assert_equal [' ', ' ', ')'], R.scan('words_sep', '%w(w w w)') assert_equal [' ', ' ', ' )'], R.scan('words_sep', '%w( w w w )') assert_equal ["\n", ' ', ' )'], R.scan('words_sep', "%w( w\nw w )") end def test_heredoc_beg assert_equal [], R.scan('heredoc_beg', '') assert_equal ['<