def testFormatters(self): """Creating a query with raw AST should generate the source.""" q = query.Query( ast.Complement( ast.Equivalence(ast.Map(ast.Var("Process"), ast.Var("pid")), ast.Literal(10)))) self.assertEqual(q.source, "Process.pid != 10")
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")))))
def testMultiWordOperators(self): self.assertQueryMatches( "x not in y", ast.Complement(ast.Membership(ast.Var("x"), ast.Var("y"))))
def ReverseComplementMembership(x, y, **kwargs): """Change (x doesn't contain y) to not(y in x).""" return ast.Complement( ast.Membership(y, x, **kwargs), **kwargs)
def ComplementMembership(*args, **kwargs): """Change (x not in y) to not(x in y).""" return ast.Complement( ast.Membership(*args, **kwargs), **kwargs)
def ComplementEquivalence(*args, **kwargs): """Change x != y to not(x == y).""" return ast.Complement( ast.Equivalence(*args, **kwargs), **kwargs)