def test_assignment(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0), BuiltinTypes.I32), ast.Assignment(ast.Identifier('foo'), ast.IntegerConstant(1)) ]) tp = TypePass() tp.visit(root)
def test_var_with_promotion(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0.0), BuiltinTypes.F32) ]) tp = TypePass() tp.visit(root)
def test_assignment_incompatible_types(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0), BuiltinTypes.I32), ast.Assignment(ast.Identifier('foo'), ast.FloatConstant(1.0)) ]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_var_unknown_type(): root = ast.Block([ ast.Var('foo', ast.FloatConstant(0.0), ast.Type('mytype')) ]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_augmented_assignment_bad(): root = ast.Block([ ast.Var('foo', ast.IntegerConstant(0), BuiltinTypes.I32), ast.Assignment(ast.Identifier('foo'), ast.IntegerConstant(1), Operator.LOGICAL_OR) ]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def parse_var(self): """ var_stmt : VAR IDENT = expr | VAR IDENT : ty = expr """ loc = self.curr_token.loc self.advance('VAR') name = self.curr_token.value self.advance('IDENT') if self.curr_token.kind == 'COLON': self.advance() ty = self.parse_type() else: ty = None self.advance('ASSIGN') initial_value = self.parse_expr() return ast.Var(name, initial_value, ty, loc=loc)
def test_var_incompatible_types(value, ty): root = ast.Block([ast.Var('foo', value, ty)]) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
def test_var(): root = ast.Block([ast.Var('foo', ast.IntegerConstant(0))]) tp = TypePass() tp.visit(root)
def test_expr_visitor(node): visitor = StubExprVisitor() method_name = 'visit_' + type(node).__name__ with mock.patch.object(visitor, method_name): visitor.visit(node) getattr(visitor, method_name).assert_called_once_with(node) @pytest.mark.parametrize('node', [ ast.Block(None), ast.If(None, None), ast.While(None, None), ast.Break(), ast.Continue(), ast.Return(), ast.Var(None, None), ast.Expression(None) ]) def test_stmt_visitor(node): visitor = StubStmtVisitor() method_name = 'visit_' + type(node).__name__ with mock.patch.object(visitor, method_name): visitor.visit(node) getattr(visitor, method_name).assert_called_once_with(node) @pytest.mark.parametrize('node', [ast.Function(None, None), ast.TranslationUnit(None)]) def test_decl_visitor(node): visitor = StubDeclVisitor()