Skip to content

Commit 86d3f21

Browse files
kurtmckeemichaelmior
authored andcommitted
Refactor tests/test_parser.py
1 parent d10c6f1 commit 86d3f21

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

tests/test_parser.py

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
import unittest
1+
import pytest
22

3+
from jsonpath_ng.jsonpath import Child, Descendants, Fields, Index, Slice, Where
34
from jsonpath_ng.lexer import JsonPathLexer
45
from jsonpath_ng.parser import JsonPathParser
5-
from jsonpath_ng.jsonpath import *
66

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+
)
933

10-
@classmethod
11-
def setup_class(cls):
12-
logging.basicConfig()
1334

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

Comments
 (0)