|
| 1 | +import pytest |
| 2 | + |
| 3 | +try: |
| 4 | + import iregexp_check # noqa: F401 |
| 5 | + |
| 6 | + IREGEXP_AVAILABLE = True |
| 7 | +except ImportError: |
| 8 | + IREGEXP_AVAILABLE = False |
| 9 | + |
| 10 | +import jsonpath |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.skipif(IREGEXP_AVAILABLE is False, reason="requires iregexp_check") |
| 14 | +def test_iregexp_check() -> None: |
| 15 | + # Character classes are OK. |
| 16 | + query = "$[?match(@, '[0-9]+')]" |
| 17 | + data = ["123", "abc", "abc123"] |
| 18 | + assert jsonpath.findall(query, data) == ["123"] |
| 19 | + |
| 20 | + # Multi character escapes are not. |
| 21 | + query = "$[?match(@, '\\\\d+')]" |
| 22 | + assert jsonpath.findall(query, data) == [] |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.skipif(IREGEXP_AVAILABLE, reason="iregexp_check is available") |
| 26 | +def test_no_iregexp_check() -> None: |
| 27 | + # Character classes are OK. |
| 28 | + query = "$[?match(@, '[0-9]+')]" |
| 29 | + data = ["123", "abc", "abc123"] |
| 30 | + assert jsonpath.findall(query, data) == ["123"] |
| 31 | + |
| 32 | + # Multi character escapes are OK when iregexp_check is not installed. |
| 33 | + query = "$[?match(@, '\\\\d+')]" |
| 34 | + assert jsonpath.findall(query, data) == ["123"] |
0 commit comments