begin
require_relative 'dummyparser'
require 'test/unit'
ripper_test = true
module TestRipper; end
rescue LoadError
end
class TestRipper::ParserEvents < Test::Unit::TestCase
def test_event_coverage
dispatched = Ripper::PARSER_EVENTS
tested = self.class.instance_methods(false).grep(/\Atest_(\w+)/) {$1.intern}
assert_empty dispatched-tested
end
def parse(str, nm = nil, &bl)
dp = DummyParser.new(str)
dp.hook(*nm, &bl) if nm
dp.parse.to_s
end
def compile_error(str)
parse(str, :compile_error) {|e, msg| return msg}
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 '[assign(var_field(a),ref(a))]', parse('a=a')
assert_equal '[ref(nil)]', parse('nil')
assert_equal '[ref(true)]', parse('true')
end
def test_vcall
assert_equal '[vcall(a)]', parse('a')
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,[*vcall(r)])]', parse('m(*r)')
assert_equal '[fcall(m,[1,*vcall(r)])]', parse('m(1,*r)')
assert_equal '[fcall(m,[1,2,*vcall(r)])]', parse('m(1,2,*r)')
assert_equal '[fcall(m,[&vcall(r)])]', parse('m(&r)')
assert_equal '[fcall(m,[1,&vcall(r)])]', parse('m(1,&r)')
assert_equal '[fcall(m,[1,2,&vcall(r)])]', parse('m(1,2,&r)')
assert_equal '[fcall(m,[*vcall(a),&vcall(b)])]', parse('m(*a,&b)')
assert_equal '[fcall(m,[1,*vcall(a),&vcall(b)])]', parse('m(1,*a,&b)')
assert_equal '[fcall(m,[1,2,*vcall(a),&vcall(b)])]', parse('m(1,2,*a,&b)')
end
def test_args_add
thru_args_add = false
parse('m(a)', :on_args_add) {thru_args_add = true}
assert_equal true, thru_args_add
end
def test_args_add_block
thru_args_add_block = false
parse('m(&b)', :on_args_add_block) {thru_args_add_block = true}
assert_equal true, thru_args_add_block
end
def test_args_add_star
thru_args_add_star = false
parse('m(*a)', :on_args_add_star) {thru_args_add_star = true}
assert_equal true, thru_args_add_star
thru_args_add_star = false
parse('m(*a, &b)', :on_args_add_star) {thru_args_add_star = true}
assert_equal true, thru_args_add_star
end
def test_args_new
thru_args_new = false
parse('m()', :on_args_new) {thru_args_new = true}
assert_equal true, thru_args_new
end
def test_arg_paren
# FIXME
end
def test_aref
assert_equal '[aref(vcall(v),[1])]', parse('v[1]')
assert_equal '[aref(vcall(v),[1,2])]', parse('v[1,2]')
end
def test_assoclist_from_args
thru_assoclist_from_args = false
parse('{a=>b}', :on_assoclist_from_args) {thru_assoclist_from_args = true}
assert_equal true, thru_assoclist_from_args
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_assoc_new
thru_assoc_new = false
parse('{a=>b}', :on_assoc_new) {thru_assoc_new = true}
assert_equal true, thru_assoc_new
end
def test_assoc_splat
thru_assoc_splat = false
parse('m(**h)', :on_assoc_splat) {thru_assoc_splat = true}
assert_equal true, thru_assoc_splat
end
def test_aref_field
assert_equal '[assign(aref_field(vcall(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_operator_ambiguous
thru_operator_ambiguous = false
parse('a=1; a %[]', :on_operator_ambiguous) {thru_operator_ambiguous = true}
assert_equal true, thru_operator_ambiguous
end
def test_array # array literal
assert_equal '[array([1,2,3])]', parse('[1,2,3]')
assert_equal '[array([abc,def])]', parse('%w[abc def]')
assert_equal '[array([abc,def])]', parse('%W[abc def]')
end
def test_assign # generic assignment
assert_equal '[assign(var_field(v),1)]', parse('v=1')
end
def test_assign_error
# for test_coverage
end
def test_assign_error_backref
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
end
def test_assign_error_const_qualified
thru_assign_error = false
|