Esempio n. 1
0
 def testPrefix(self):
     self.assertQueryParses(
         "-5 + 5 eq - (10)",
         ast.Equivalence(
             ast.Sum(ast.Product(ast.Literal(-1), ast.Literal(5)),
                     ast.Literal(5)),
             ast.Product(ast.Literal(-1), ast.Literal(10))))
Esempio n. 2
0
    def testParens(self):
        # Base case.
        self.assertQueryMatches(
            "x + y * z",
            ast.Sum(ast.Var("x"), ast.Product(ast.Var("y"), ast.Var("z"))))

        # With parens.
        self.assertQueryMatches(
            "(x + y) * z",
            ast.Product(ast.Sum(ast.Var("x"), ast.Var("y")), ast.Var("z")))

        # Missing rparen.
        self.assertQueryRaises("(x + y")

        # Empty expressions make no sense.
        self.assertQueryRaises("()")
Esempio n. 3
0
    def testSubscript(self):
        self.assertQueryMatches("d['foo']",
                                ast.Select(ast.Var("d"), ast.Literal("foo")))

        self.assertQueryMatches(
            "d['foo'] + 10",
            ast.Sum(ast.Select(ast.Var("d"), ast.Literal("foo")),
                    ast.Literal(10)))

        self.assertQueryMatches(
            "obj.props[0]",
            ast.Select(ast.Resolve(ast.Var("obj"), ast.Literal("props")),
                       ast.Literal(0)))

        self.assertQueryMatches(
            "obj.props[0].foo",
            ast.Resolve(
                ast.Select(ast.Resolve(ast.Var("obj"), ast.Literal("props")),
                           ast.Literal(0)), ast.Literal("foo")))

        self.assertQueryMatches(
            "obj.props[10 + 10].foo",
            ast.Resolve(
                ast.Select(ast.Resolve(ast.Var("obj"), ast.Literal("props")),
                           ast.Sum(ast.Literal(10), ast.Literal(10))),
                ast.Literal("foo")))

        self.assertQueryMatches(
            "w['x'][y[5] + 5] * 10",
            ast.Product(
                ast.Select(
                    ast.Select(ast.Var("w"), ast.Literal("x")),
                    ast.Sum(ast.Select(ast.Var("y"), ast.Literal(5)),
                            ast.Literal(5))), ast.Literal(10)))
Esempio n. 4
0
    def testInfix(self):
        self.assertQueryMatches("x + y", ast.Sum(ast.Var("x"), ast.Var("y")))

        self.assertQueryMatches(
            "w.x.y.z",
            ast.Resolve(
                ast.Resolve(ast.Resolve(ast.Var("w"), ast.Literal("x")),
                            ast.Literal("y")), ast.Literal("z")))

        # Operator precedence should work correctly.
        self.assertQueryMatches(
            "x + y * z",
            ast.Sum(ast.Var("x"), ast.Product(ast.Var("y"), ast.Var("z"))))

        self.assertQueryMatches(
            "x * y + z",
            ast.Sum(ast.Product(ast.Var("x"), ast.Var("y")), ast.Var("z")))
Esempio n. 5
0
    def testOperatorPrecedence(self):
        # Prefix operator, like the unary minus sign, should respect operator
        # precedence order.
        self.assertQueryMatches(
            "-x + y",
            ast.Sum(ast.Product(ast.Literal(-1), ast.Var("x")), ast.Var("y")))

        self.assertQueryMatches(
            "not x and y",
            ast.Intersection(ast.Complement(ast.Var("x")), ast.Var("y")))

        self.assertQueryMatches(
            "x / -f(y) or not z(a, b)",
            ast.Union(
                ast.Quotient(
                    ast.Var("x"),
                    ast.Product(ast.Literal(-1),
                                ast.Apply(ast.Var("f"), ast.Var("y")))),
                ast.Complement(
                    ast.Apply(ast.Var("z"), ast.Var("a"), ast.Var("b")))))
Esempio n. 6
0
 def testPrefix(self):
     self.assertQueryMatches("-x", ast.Product(ast.Literal(-1),
                                               ast.Var("x")))
Esempio n. 7
0
def NegateValue(*args, **kwargs):
    """Change -x to (-1 * x)."""
    return ast.Product(
        ast.Literal(-1),
        *args,
        **kwargs)