forked from kennknowles/python-jsonpath-rw
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d10c6f1
commit 86d3f21
Showing
1 changed file
with
32 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,38 @@ | ||
import unittest | ||
import pytest | ||
|
||
from jsonpath_ng.jsonpath import Child, Descendants, Fields, Index, Slice, Where | ||
from jsonpath_ng.lexer import JsonPathLexer | ||
from jsonpath_ng.parser import JsonPathParser | ||
from jsonpath_ng.jsonpath import * | ||
|
||
class TestParser(unittest.TestCase): | ||
# TODO: This will be much more effective with a few regression tests and `arbitrary` parse . pretty testing | ||
# Format: (string, expected_object) | ||
parser_test_cases = ( | ||
# | ||
# Atomic | ||
# ------ | ||
# | ||
("foo", Fields("foo")), | ||
("*", Fields("*")), | ||
("baz,bizzle", Fields("baz", "bizzle")), | ||
("[1]", Index(1)), | ||
("[1:]", Slice(start=1)), | ||
("[:]", Slice()), | ||
("[*]", Slice()), | ||
("[:2]", Slice(end=2)), | ||
("[1:2]", Slice(start=1, end=2)), | ||
("[5:-2]", Slice(start=5, end=-2)), | ||
# | ||
# Nested | ||
# ------ | ||
# | ||
("foo.baz", Child(Fields("foo"), Fields("baz"))), | ||
("foo.baz,bizzle", Child(Fields("foo"), Fields("baz", "bizzle"))), | ||
("foo where baz", Where(Fields("foo"), Fields("baz"))), | ||
("foo..baz", Descendants(Fields("foo"), Fields("baz"))), | ||
("foo..baz.bing", Descendants(Fields("foo"), Child(Fields("baz"), Fields("bing")))), | ||
) | ||
|
||
@classmethod | ||
def setup_class(cls): | ||
logging.basicConfig() | ||
|
||
def check_parse_cases(self, test_cases): | ||
parser = JsonPathParser(debug=True, lexer_class=lambda:JsonPathLexer(debug=False)) # Note that just manually passing token streams avoids this dep, but that sucks | ||
|
||
for string, parsed in test_cases: | ||
print(string, '=?=', parsed) # pytest captures this and we see it only on a failure, for debugging | ||
assert parser.parse(string) == parsed | ||
|
||
def test_atomic(self): | ||
self.check_parse_cases([('foo', Fields('foo')), | ||
('*', Fields('*')), | ||
('baz,bizzle', Fields('baz','bizzle')), | ||
('[1]', Index(1)), | ||
('[1:]', Slice(start=1)), | ||
('[:]', Slice()), | ||
('[*]', Slice()), | ||
('[:2]', Slice(end=2)), | ||
('[1:2]', Slice(start=1, end=2)), | ||
('[5:-2]', Slice(start=5, end=-2)) | ||
]) | ||
|
||
def test_nested(self): | ||
self.check_parse_cases([('foo.baz', Child(Fields('foo'), Fields('baz'))), | ||
('foo.baz,bizzle', Child(Fields('foo'), Fields('baz', 'bizzle'))), | ||
('foo where baz', Where(Fields('foo'), Fields('baz'))), | ||
('foo..baz', Descendants(Fields('foo'), Fields('baz'))), | ||
('foo..baz.bing', Descendants(Fields('foo'), Child(Fields('baz'), Fields('bing'))))]) | ||
@pytest.mark.parametrize("string, expected_object", parser_test_cases) | ||
def test_parser(string, expected_object): | ||
parser = JsonPathParser(lexer_class=lambda: JsonPathLexer()) | ||
assert parser.parse(string) == expected_object |