Exemplo n.º 1
0
 def worker():
     p = parser.Parser()
     for expression in expressions:
         try:
             p.parse(expression)
         except Exception as e:
             errors.append(e)
Exemplo n.º 2
0
 def test_cache_purge(self):
     p = parser.Parser()
     first = p.parse('foo')
     cached = p.parse('foo')
     p.purge()
     second = p.parse('foo')
     self.assertEqual(first.parsed, second.parsed)
     self.assertEqual(first.parsed, cached.parsed)
Exemplo n.º 3
0
 def test_can_have_ordered_dict(self):
     p = parser.Parser()
     parsed = p.parse('{a: a, b: b, c: c}')
     options = visitor.Options(dict_cls=OrderedDict)
     result = parsed.search({"c": "c", "b": "b", "a": "a"}, options=options)
     # The order should be 'a', 'b' because we're using an
     # OrderedDict
     self.assertEqual(list(result), ['a', 'b', 'c'])
Exemplo n.º 4
0
 def test_cache_purge(self):
     p = parser.Parser()
     first = p.parse('foo')
     cached = p.parse('foo')
     p.purge()
     second = p.parse('foo')
     self.assertEqual(as_s_expression(first.parsed),
                      as_s_expression(second.parsed))
     self.assertEqual(as_s_expression(first.parsed),
                      as_s_expression(cached.parsed))
Exemplo n.º 5
0
 def test_dot_file_subexpr(self):
     p = parser.Parser()
     result = p.parse('foo.bar')
     dot_contents = result._render_dot_file()
     self.assertEqual(
         dot_contents, 'digraph AST {\n'
         'subexpression1 [label="subexpression()"]\n'
         '  subexpression1 -> field2\n'
         'field2 [label="field(foo)"]\n'
         '  subexpression1 -> field3\n'
         'field3 [label="field(bar)"]\n}')
Exemplo n.º 6
0
def test_search_api(expr, data):
    try:
        ast = parser.Parser().parse(expr)
    except exceptions.JMESPathError as e:
        # We want to try to parse things that tokenize
        # properly.
        assume(False)
    try:
        ast.search(data)
    except exceptions.JMESPathError as e:
        return
    except Exception as e:
        raise AssertionError("Non JMESPathError raised: %s" % e)
Exemplo n.º 7
0
def test_parser_api_from_str(expr):
    # Same a lexer above with the assumption that we're parsing
    # a valid sequence of tokens.
    try:
        list(lexer.Lexer().tokenize(expr))
    except exceptions.JMESPathError as e:
        # We want to try to parse things that tokenize
        # properly.
        assume(False)
    try:
        ast = parser.Parser().parse(expr)
    except exceptions.JMESPathError as e:
        return
    except Exception as e:
        raise AssertionError("Non JMESPathError raised: %s" % e)
    assert isinstance(ast.parsed, dict)
Exemplo n.º 8
0
 def test_compile_lots_of_expressions(self):
     # We have to be careful here because this is an implementation detail
     # that should be abstracted from the user, but we need to make sure we
     # exercise the code and that it doesn't blow up.
     p = parser.Parser()
     compiled = []
     compiled2 = []
     for i in range(parser.Parser._MAX_SIZE + 1):
         compiled.append(p.parse('foo%s' % i))
     # Rerun the test and half of these entries should be from the
     # cache but they should still be equal to compiled.
     for i in range(parser.Parser._MAX_SIZE + 1):
         compiled2.append(p.parse('foo%s' % i))
     self.assertEqual(len(compiled), len(compiled2))
     self.assertEqual([expr.parsed for expr in compiled],
                      [expr.parsed for expr in compiled2])
Exemplo n.º 9
0
 def setUp(self):
     self.parser = parser.Parser()
     self.data = {
         'foo': [
             {
                 'bar': [{
                     'baz': 'one'
                 }, {
                     'baz': 'two'
                 }]
             },
             {
                 'bar': [{
                     'baz': 'three'
                 }, {
                     'baz': 'four'
                 }, {
                     'baz': 'five'
                 }]
             },
         ]
     }
Exemplo n.º 10
0
def compile(expression):
    return parser.Parser().parse(expression)
Exemplo n.º 11
0
def search(expression, data, options=None):
    return parser.Parser().parse(expression).search(data, options=options)
Exemplo n.º 12
0
def compile(expression, debug=False):
    return parser.Parser(debug=debug).parse(expression)
Exemplo n.º 13
0
def search(expression, data, debug=False):
    return parser.Parser(debug=debug).parse(expression).search(data)
Exemplo n.º 14
0
 def test_dot_file_rendered(self):
     p = parser.Parser()
     result = p.parse('foo')
     dot_contents = result._render_dot_file()
     self.assertEqual(dot_contents,
                      'digraph AST {\nfield1 [label="field(foo)"]\n}')
Exemplo n.º 15
0
def search(expression, data):
    return parser.Parser().parse(expression).search(data)
Exemplo n.º 16
0
 def test_expression_available_from_parser(self):
     p = parser.Parser()
     parsed = p.parse('foo.bar')
     self.assertEqual(parsed.expression, 'foo.bar')
Exemplo n.º 17
0
 def setUp(self):
     self.parser = parser.Parser()
Exemplo n.º 18
0
 def setUp(self):
     self.parser = parser.Parser()
     self.data = {
         "foo": [[["one", "two"], ["three", "four"]],
                 [["five", "six"], ["seven", "eight"]], [["nine"], ["ten"]]]
     }