Ejemplo n.º 1
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)))
Ejemplo n.º 2
0
    def testMixfix(self):
        self.assertQueryParses("'foo'[0  ]",
                               ast.Select(ast.Literal("foo"), ast.Literal(0)))

        self.assertQueryParses(
            # I refer you to my previous statement about making sense.
            " (5 +5) [ 'foo']",
            ast.Select(ast.Sum(ast.Literal(5), ast.Literal(5)),
                       ast.Literal("foo")))

        self.assertQueryParses(
            "5 + 5['foo' + 10]",
            ast.Sum(
                ast.Literal(5),
                ast.Select(ast.Literal(5),
                           ast.Sum(ast.Literal("foo"), ast.Literal(10)))))
Ejemplo n.º 3
0
 def testSelectWhat(self):
     self.assertQueryMatches(
         "SELECT proc.parent.pid AS ppid,"
         "proc.pid,"
         "'foo',"
         "asdate(proc.starttime),"
         "proc.fd[5]"
         "FROM pslist()",
         ast.Map(
             ast.Apply(ast.Var("pslist")),
             ast.Bind(
                 ast.Pair(
                     ast.Literal("ppid"),
                     ast.Resolve(
                         ast.Resolve(ast.Var("proc"),
                                     ast.Literal("parent")),
                         ast.Literal("pid"))),
                 ast.Pair(ast.Literal("pid"),
                          ast.Resolve(ast.Var("proc"), ast.Literal("pid"))),
                 ast.Pair(ast.Literal("column_2"), ast.Literal("foo")),
                 ast.Pair(
                     ast.Literal("asdate"),
                     ast.Apply(
                         ast.Var("asdate"),
                         ast.Resolve(ast.Var("proc"),
                                     ast.Literal("starttime")))),
                 ast.Pair(
                     ast.Literal("fd_5"),
                     ast.Select(
                         ast.Resolve(ast.Var("proc"), ast.Literal("fd")),
                         ast.Literal(5))))))
Ejemplo n.º 4
0
    def testCircumfix(self):
        self.assertQueryParses(
            "[1, 2, 3]",
            ast.Tuple(ast.Literal(1), ast.Literal(2), ast.Literal(3)))

        self.assertQueryParses(
            # Lists and selection are non-ambiguous.
            "10 + ['foo', 'bar'][1]",
            ast.Sum(
                ast.Literal(10),
                ast.Select(ast.Tuple(ast.Literal("foo"), ast.Literal("bar")),
                           ast.Literal(1))))
Ejemplo n.º 5
0
    def testBuiltins(self):
        self.assertQueryMatches(
            "filter(pslist(), proc.pid == 1)",
            ast.Filter(
                ast.Apply(ast.Var("pslist")),
                ast.Equivalence(
                    ast.Resolve(ast.Var("proc"), ast.Literal("pid")),
                    ast.Literal(1))))

        self.assertQueryMatches(
            "map(pslist(), [proc.pid, proc['command']])",
            ast.Map(
                ast.Apply(ast.Var("pslist")),
                ast.Tuple(ast.Resolve(ast.Var("proc"), ast.Literal("pid")),
                          ast.Select(ast.Var("proc"),
                                     ast.Literal("command")))))

        self.assertQueryMatches(
            "bind(x: 1, y: 2)",
            ast.Bind(ast.Pair(ast.Var("x"), ast.Literal(1)),
                     ast.Pair(ast.Var("y"), ast.Literal(2))))

        self.assertQueryRaises("bind (x: 1, y: 2)")