begin require_relative 'dummyparser' require_relative '../ruby/envutil' require 'test/unit' ripper_test = true rescue LoadError end class TestRipper_ParserEvents < Test::Unit::TestCase # should be enabled =begin def test_event_coverage dispatched = Ripper::PARSER_EVENTS.map {|event,*| event } dispatched.each do |e| assert_equal true, respond_to?("test_#{e}", true), "event not tested: #{e.inspect}" end end =end def parse(str, nm = nil, &bl) dp = DummyParser.new(str) dp.hook(nm, &bl) if nm dp.parse.to_s end def test_program thru_program = false assert_equal '[void()]', parse('', :on_program) {thru_program = true} assert_equal true, thru_program end def test_stmts_new assert_equal '[void()]', parse('') end def test_stmts_add assert_equal '[ref(nil)]', parse('nil') assert_equal '[ref(nil),ref(nil)]', parse('nil;nil') assert_equal '[ref(nil),ref(nil),ref(nil)]', parse('nil;nil;nil') end def test_void_stmt assert_equal '[void()]', parse('') assert_equal '[void()]', parse('; ;') end def test_var_ref assert_equal '[ref(a)]', parse('a') assert_equal '[ref(nil)]', parse('nil') assert_equal '[ref(true)]', parse('true') end def test_BEGIN assert_equal '[BEGIN([void()])]', parse('BEGIN{}') assert_equal '[BEGIN([ref(nil)])]', parse('BEGIN{nil}') end def test_END assert_equal '[END([void()])]', parse('END{}') assert_equal '[END([ref(nil)])]', parse('END{nil}') end def test_alias assert_equal '[alias(symbol_literal(a),symbol_literal(b))]', parse('alias a b') end def test_var_alias assert_equal '[valias($a,$g)]', parse('alias $a $g') end def test_alias_error assert_equal '[aliaserr(valias($a,$1))]', parse('alias $a $1') end def test_arglist assert_equal '[fcall(m,[])]', parse('m()') assert_equal '[fcall(m,[1])]', parse('m(1)') assert_equal '[fcall(m,[1,2])]', parse('m(1,2)') assert_equal '[fcall(m,[*ref(r)])]', parse('m(*r)') assert_equal '[fcall(m,[1,*ref(r)])]', parse('m(1,*r)') assert_equal '[fcall(m,[1,2,*ref(r)])]', parse('m(1,2,*r)') assert_equal '[fcall(m,[&ref(r)])]', parse('m(&r)') assert_equal '[fcall(m,[1,&ref(r)])]', parse('m(1,&r)') assert_equal '[fcall(m,[1,2,&ref(r)])]', parse('m(1,2,&r)') assert_equal '[fcall(m,[*ref(a),&ref(b)])]', parse('m(*a,&b)') assert_equal '[fcall(m,[1,*ref(a),&ref(b)])]', parse('m(1,*a,&b)') assert_equal '[fcall(m,[1,2,*ref(a),&ref(b)])]', parse('m(1,2,*a,&b)') end def test_arg_paren # FIXME end def test_aref assert_equal '[aref(ref(v),[1])]', parse('v[1]') assert_equal '[aref(ref(v),[1,2])]', parse('v[1,2]') end def test_assocs assert_equal '[fcall(m,[assocs(assoc(1,2))])]', parse('m(1=>2)') assert_equal '[fcall(m,[assocs(assoc(1,2),assoc(3,4))])]', parse('m(1=>2,3=>4)') assert_equal '[fcall(m,[3,assocs(assoc(1,2))])]', parse('m(3,1=>2)') end def test_aref_field assert_equal '[assign(aref_field(ref(a),[1]),2)]', parse('a[1]=2') end def test_arg_ambiguous thru_arg_ambiguous = false parse('m //', :on_arg_ambiguous) {thru_arg_ambiguous = true} assert_equal true, thru_arg_ambiguous end def test_array # array literal assert_equal '[array([1,2,3])]', parse('[1,2,3]') end def test_assign # generic assignment assert_equal '[assign(var_field(v),1)]', parse('v=1') end def test_assign_error thru_assign_error = false parse('$` = 1', :on_assign_error) {thru_assign_error = true} assert_equal true, thru_assign_error thru_assign_error = false parse('$`, _ = 1', :on_assign_error) {thru_assign_error = true} assert_equal true, thru_assign_error thru_assign_error = false parse('self::X = 1', :on_assign_error) {thru_assign_error = true} assert_equal false, thru_assign_error parse('def m\n self::X = 1\nend', :on_assign_error) {thru_assign_error = true} assert_equal true, thru_assign_error thru_assign_error = false parse('X = 1', :on_assign_error) {thru_assign_error = true} assert_equal false, thru_assign_error parse('def m\n X = 1\nend', :on_assign_error) {thru_assign_error = true} assert_equal true, thru_assign_error thru_assign_error = false parse('::X = 1', :on_assign_error) {thru_assign_error = true} assert_equal false, thru_assign_error parse('def m\n ::X = 1\nend', :on_assign_error) {thru_assign_error = true} assert_equal true, thru_assign_error end def test_begin thru_begin = false parse('begin end', :on_begin) {thru_begin = true} assert_equal true, thru_begin end def test_binary thru_binary = nil %w"and or + - * / % ** | ^ & <=> > >= < <= == === != =~ !~ << >> && ||".each do |op| thru_binary = false parse("a #{op} b", :on_binary) {thru_binary = true} assert_equal true, thru_binary end end def test_block_var thru_block_var = false parse("proc{||}", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var thru_block_var = false parse("proc{| |}", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var thru_block_var = false parse("proc{|x|}", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var thru_block_var = false parse("proc{|;y|}", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var thru_block_var = false parse("proc{|x;y|}", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var thru_block_var = false parse("proc do || end", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var thru_block_var = false parse("proc do | | end", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var thru_block_var = false parse("proc do |x| end", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var thru_block_var = false parse("proc do |;y| end", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var thru_block_var = false parse("proc do |x;y| end", :on_block_var) {thru_block_var = true} assert_equal true, thru_block_var end def test_bodystmt thru_bodystmt = false parse("class X\nend", :on_bodystmt) {thru_bodystmt = true} assert_equal true, thru_bodystmt end def test_call bug2233 = '[ruby-core:26165]' tree = nil thru_call = false assert_nothing_raised { tree = parse("self.foo", :on_call) {thru_call = true} } assert_equal true, thru_call assert_equal "[call(ref(self),.,foo)]", tree thru_call = false assert_nothing_raised(bug2233) { tree = parse("foo.()", :on_call) {thru_call = true} } assert_equal true, thru_call assert_equal "[call(ref(foo),.,call,[])]", tree end def test_heredoc bug1921 = '[ruby-core:24855]' thru_heredoc_beg = false tree = parse("<