def oper(x): t = type(x) if t == ast.primary_expr: return ast.operand(x) elif t == ast.expression: return ast.operand(ast.primary_expr(x)) elif t == ast.unary_expr: return ast.operand(x) else: raise Exception(str(t))
def test_operand(): pe1 = ast.primary_expr(True) op1 = ast.operand(pe1) assert(op1.value == pe1) assert(op1.value.value == True) id2 = ast.identifier('ident') pe2 = ast.primary_expr(id2) op2 = ast.operand(pe2) assert(op2.value == pe2) assert(op2.value.value.name == 'ident')
def test_operation(): pe1 = ast.primary_expr(True) op1 = ast.operand(pe1) op2 = ast.operation(ast.optoken.op_plus, op1) assert(op2.operator == ast.optoken.op_plus) assert(op2.operand == op1) id2 = ast.identifier('ident') pe2 = ast.primary_expr(id2) op3 = ast.operand(pe2) op4 = ast.unary_expr(ast.optoken.op_minus, op3) assert(op4.operator == ast.optoken.op_minus) assert(op4.operand == op3)
def test_unary_expr(): pe1 = ast.primary_expr(True) op1 = ast.operand(pe1) ue1 = ast.unary_expr(ast.optoken.op_not, op1) assert(ue1.operator == ast.optoken.op_not) assert(ue1.operand == op1) id2 = ast.identifier('ident') pe2 = ast.primary_expr(id2) op2 = ast.operand(pe2) ue2 = ast.unary_expr(ast.optoken.op_negative, op2) assert(ue2.operator == ast.optoken.op_negative) assert(ue2.operand == op2)
def test_expression(): pe1 = ast.primary_expr(True) op1 = ast.operand(pe1) e1 = ast.expression(op1) op2 = ast.operation(ast.optoken.op_plus, op1) e1.rest.append(op2) test_serialization(e1) e1.rest.append([op2, op2]) test_serialization(e1)