File: test_lex.py

package info (click to toggle)
python-jsonpath 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,024 kB
  • sloc: python: 9,462; makefile: 6
file content (30 lines) | stat: -rw-r--r-- 823 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import json
import operator
from typing import Any
from typing import Dict

import pytest

from jsonpath import DEFAULT_ENV
from jsonpath.exceptions import JSONPathSyntaxError
from jsonpath.token import Token

with open("tests/test_lex.json", encoding="UTF-8") as fd:
    """Loads the test case data. Each test case is:
    description: str
    path: str
    want: List[Token]
    """
    CASES = json.load(fd)["tests"]


@pytest.mark.parametrize("case", CASES, ids=operator.itemgetter("description"))
def test_default_lexer(case: Dict[str, Any]) -> None:
    tokens = list(DEFAULT_ENV.lexer.tokenize(case["path"]))
    want = [Token(**token) for token in case["want"]]
    assert tokens == want


def test_illegal_token() -> None:
    with pytest.raises(JSONPathSyntaxError):
        list(DEFAULT_ENV.lexer.tokenize("%"))