| 1 |
|
| 2 |
|
| 3 |
"""Test some basic parsing functionality.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from typing import NamedTuple |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
import parse_stages as pst |
| 12 |
|
| 13 |
|
| 14 |
class Case(NamedTuple): |
| 15 |
"""Specify a single test case: a string to parse, results.""" |
| 16 |
|
| 17 |
spec: str |
| 18 |
res: pst.BoolExpr |
| 19 |
|
| 20 |
|
| 21 |
class IdCase(NamedTuple): |
| 22 |
"""Specify a single test case for stage IDs: a string to parse, results.""" |
| 23 |
|
| 24 |
spec: str |
| 25 |
res: list[int] |
| 26 |
empty_set_specs: list[str] | None |
| 27 |
|
| 28 |
|
| 29 |
_DEFS = [ |
| 30 |
Case(spec="@check", res=pst.TagExpr(tag="check")), |
| 31 |
Case(spec="pep8", res=pst.KeywordExpr(keyword="pep8")), |
| 32 |
Case(spec="not @check", res=pst.NotExpr(child=pst.TagExpr(tag="check"))), |
| 33 |
Case( |
| 34 |
spec="@check and not pep8", |
| 35 |
res=pst.AndExpr( |
| 36 |
children=[ |
| 37 |
pst.TagExpr(tag="check"), |
| 38 |
pst.NotExpr(child=pst.KeywordExpr(keyword="pep8")), |
| 39 |
], |
| 40 |
), |
| 41 |
), |
| 42 |
Case( |
| 43 |
spec="not pep8 and @check or @tests or something", |
| 44 |
res=pst.OrExpr( |
| 45 |
children=[ |
| 46 |
pst.AndExpr( |
| 47 |
children=[ |
| 48 |
pst.NotExpr(child=pst.KeywordExpr(keyword="pep8")), |
| 49 |
pst.TagExpr(tag="check"), |
| 50 |
], |
| 51 |
), |
| 52 |
pst.TagExpr(tag="tests"), |
| 53 |
pst.KeywordExpr(keyword="something"), |
| 54 |
], |
| 55 |
), |
| 56 |
), |
| 57 |
Case( |
| 58 |
spec="black and not black-reformat", |
| 59 |
res=pst.AndExpr( |
| 60 |
children=[ |
| 61 |
pst.KeywordExpr(keyword="black"), |
| 62 |
pst.NotExpr(child=pst.KeywordExpr(keyword="black-reformat")), |
| 63 |
], |
| 64 |
), |
| 65 |
), |
| 66 |
Case( |
| 67 |
spec="not (@check or @docs) and @manual", |
| 68 |
res=pst.AndExpr( |
| 69 |
children=[ |
| 70 |
pst.NotExpr( |
| 71 |
child=pst.OrExpr(children=[pst.TagExpr(tag="check"), pst.TagExpr(tag="docs")]), |
| 72 |
), |
| 73 |
pst.TagExpr(tag="manual"), |
| 74 |
], |
| 75 |
), |
| 76 |
), |
| 77 |
] |
| 78 |
|
| 79 |
_IDS = [ |
| 80 |
IdCase(spec="", res=[], empty_set_specs=None), |
| 81 |
IdCase(spec="0", res=[], empty_set_specs=None), |
| 82 |
IdCase(spec="none", res=[], empty_set_specs=None), |
| 83 |
IdCase(spec="empty", res=[], empty_set_specs=["", "empty"]), |
| 84 |
IdCase(spec="1", res=[0], empty_set_specs=None), |
| 85 |
IdCase(spec="2,6,4", res=[1, 5, 3], empty_set_specs=None), |
| 86 |
IdCase(spec="1-3,4-6", res=[0, 1, 2, 3, 4, 5], empty_set_specs=None), |
| 87 |
IdCase(spec="1-3,5-6", res=[0, 1, 2, 4, 5], empty_set_specs=None), |
| 88 |
IdCase(spec="1-3,7-10,4", res=[0, 1, 2, 6, 7, 8, 9, 3], empty_set_specs=None), |
| 89 |
] |
| 90 |
|
| 91 |
|
| 92 |
@pytest.mark.parametrize("case", _DEFS) |
| 93 |
def test_basic(case: Case) -> None: |
| 94 |
"""Make sure we parse a specification correctly.""" |
| 95 |
assert pst.parse_spec(case.spec) == case.res |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.parametrize("case", _IDS) |
| 99 |
def test_ids_basic(case: IdCase) -> None: |
| 100 |
"""Make sure we parse a set of stage IDs correctly.""" |
| 101 |
assert pst.parse_stage_ids(case.spec, empty_set_specs=case.empty_set_specs) == case.res |