|
1 |
| -import unittest |
| 1 | +import pytest |
2 | 2 |
|
| 3 | +from jsonpath_ng.jsonpath import Child, Descendants, Fields, Index, Slice, Where |
3 | 4 | from jsonpath_ng.lexer import JsonPathLexer
|
4 | 5 | from jsonpath_ng.parser import JsonPathParser
|
5 |
| -from jsonpath_ng.jsonpath import * |
6 | 6 |
|
7 |
| -class TestParser(unittest.TestCase): |
8 |
| - # TODO: This will be much more effective with a few regression tests and `arbitrary` parse . pretty testing |
| 7 | +# Format: (string, expected_object) |
| 8 | +parser_test_cases = ( |
| 9 | + # |
| 10 | + # Atomic |
| 11 | + # ------ |
| 12 | + # |
| 13 | + ("foo", Fields("foo")), |
| 14 | + ("*", Fields("*")), |
| 15 | + ("baz,bizzle", Fields("baz", "bizzle")), |
| 16 | + ("[1]", Index(1)), |
| 17 | + ("[1:]", Slice(start=1)), |
| 18 | + ("[:]", Slice()), |
| 19 | + ("[*]", Slice()), |
| 20 | + ("[:2]", Slice(end=2)), |
| 21 | + ("[1:2]", Slice(start=1, end=2)), |
| 22 | + ("[5:-2]", Slice(start=5, end=-2)), |
| 23 | + # |
| 24 | + # Nested |
| 25 | + # ------ |
| 26 | + # |
| 27 | + ("foo.baz", Child(Fields("foo"), Fields("baz"))), |
| 28 | + ("foo.baz,bizzle", Child(Fields("foo"), Fields("baz", "bizzle"))), |
| 29 | + ("foo where baz", Where(Fields("foo"), Fields("baz"))), |
| 30 | + ("foo..baz", Descendants(Fields("foo"), Fields("baz"))), |
| 31 | + ("foo..baz.bing", Descendants(Fields("foo"), Child(Fields("baz"), Fields("bing")))), |
| 32 | +) |
9 | 33 |
|
10 |
| - @classmethod |
11 |
| - def setup_class(cls): |
12 |
| - logging.basicConfig() |
13 | 34 |
|
14 |
| - def check_parse_cases(self, test_cases): |
15 |
| - parser = JsonPathParser(debug=True, lexer_class=lambda:JsonPathLexer(debug=False)) # Note that just manually passing token streams avoids this dep, but that sucks |
16 |
| - |
17 |
| - for string, parsed in test_cases: |
18 |
| - print(string, '=?=', parsed) # pytest captures this and we see it only on a failure, for debugging |
19 |
| - assert parser.parse(string) == parsed |
20 |
| - |
21 |
| - def test_atomic(self): |
22 |
| - self.check_parse_cases([('foo', Fields('foo')), |
23 |
| - ('*', Fields('*')), |
24 |
| - ('baz,bizzle', Fields('baz','bizzle')), |
25 |
| - ('[1]', Index(1)), |
26 |
| - ('[1:]', Slice(start=1)), |
27 |
| - ('[:]', Slice()), |
28 |
| - ('[*]', Slice()), |
29 |
| - ('[:2]', Slice(end=2)), |
30 |
| - ('[1:2]', Slice(start=1, end=2)), |
31 |
| - ('[5:-2]', Slice(start=5, end=-2)) |
32 |
| - ]) |
33 |
| - |
34 |
| - def test_nested(self): |
35 |
| - self.check_parse_cases([('foo.baz', Child(Fields('foo'), Fields('baz'))), |
36 |
| - ('foo.baz,bizzle', Child(Fields('foo'), Fields('baz', 'bizzle'))), |
37 |
| - ('foo where baz', Where(Fields('foo'), Fields('baz'))), |
38 |
| - ('foo..baz', Descendants(Fields('foo'), Fields('baz'))), |
39 |
| - ('foo..baz.bing', Descendants(Fields('foo'), Child(Fields('baz'), Fields('bing'))))]) |
| 35 | +@pytest.mark.parametrize("string, expected_object", parser_test_cases) |
| 36 | +def test_parser(string, expected_object): |
| 37 | + parser = JsonPathParser(lexer_class=lambda: JsonPathLexer()) |
| 38 | + assert parser.parse(string) == expected_object |
0 commit comments