Example #1
0
    def test_binops(self):
        binop1 = ["1000", ">", "100.0"]
        binop_node1 = AST.GTNode(AST.IntNode(int(binop1[0])),
                                 AST.DecimalNode(Decimal(binop1[2])))
        self.validate_node(self.exp_parser, "".join(binop1), binop_node1)

        binop2 = binop1 + ["==", "true"]
        binop_node2 = AST.EqNode(binop_node1, AST.BoolNode(True))
        self.validate_node(self.exp_parser, "".join(binop2), binop_node2)

        # (100 + var) * (true) + "String"
        lit_bool = AST.BoolNode(True)
        lit_int = AST.IntNode(int("100"))
        lit_var = AST.VarNode("var")
        lit_string = AST.StringNode("String")

        binop_node3 = AST.AddNode(lit_int, lit_var)
        binop_node3 = AST.MulNode(binop_node3, lit_bool)
        binop_node3 = AST.AddNode(binop_node3, lit_string)
        self.validate_node(self.exp_parser, "(100 + var) * true + \"String\"",
                           binop_node3)

        self.check_parse_exception(self.exp_parser, '20 +', pp.ParseException)
        self.check_parse_exception(self.exp_parser, '20 ** 30',
                                   pp.ParseException)
        self.check_parse_exception(self.exp_parser, '30 // 40',
                                   pp.ParseException)
Example #2
0
    def test_expression_combinations(self):
        # !(5.0 + +10 / var1 / 2 - !var2 && (19. * .12) || false)
        # = invalid eq but is parseable.
        int1 = AST.IntNode(int("10"))
        int2 = AST.IntNode(int("2"))
        dec1 = AST.DecimalNode(Decimal("5.0"))
        dec2 = AST.DecimalNode(Decimal("19."))
        dec3 = AST.DecimalNode(Decimal(".12"))
        var1 = AST.VarNode("var1")
        var2 = AST.VarNode("var2")
        bool1 = AST.BoolNode(False)

        # First prefix +, then division
        expr = AST.DivNode(AST.PlusNode(int1), var1)
        expr = AST.DivNode(expr, int2)
        # Secondly + and -
        expr = AST.AddNode(dec1, expr)
        expr = AST.SubNode(expr, AST.NegNode(var2))
        # Thirdly &&, last || and the very last !
        expr = AST.AndNode(expr, AST.MulNode(dec2, dec3))
        expr = AST.NegNode(AST.OrNode(expr, bool1))

        string_expr = "!(5.0 + +10 / var1 / 2 - !var2 && (19. * .12) || false)"
        self.validate_node(self.exp_parser, string_expr, expr)