| 1 |
|
| 2 |
|
| 3 |
"""Test the evaluation of some simple expressions.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import dataclasses |
| 8 |
from typing import NamedTuple |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
import parse_stages as pst |
| 13 |
|
| 14 |
|
| 15 |
@dataclasses.dataclass(frozen=True) |
| 16 |
class Environment(pst.TaggedFrozen): |
| 17 |
"""Specify an environment to be matched by a spec (or not).""" |
| 18 |
|
| 19 |
|
| 20 |
class Case(NamedTuple): |
| 21 |
"""Specify a single test case: a string to parse, results.""" |
| 22 |
|
| 23 |
spec: str |
| 24 |
matched: list[str] |
| 25 |
|
| 26 |
|
| 27 |
_ALL = [ |
| 28 |
Environment(name="t-black", tags=["check"]), |
| 29 |
Environment(name="t-black-reformat", tags=["do", "reformat"]), |
| 30 |
Environment(name="t-pep8", tags=["check"]), |
| 31 |
Environment(name="t-mypy", tags=["check"]), |
| 32 |
Environment(name="t-pylint", tags=["check"]), |
| 33 |
Environment(name="t-unit-tests", tags=["tests"]), |
| 34 |
Environment(name="t-runner-pep8", tags=["check", "runner"]), |
| 35 |
] |
| 36 |
|
| 37 |
_TESTS = [ |
| 38 |
Case(spec="@check", matched=["t-black", "t-pep8", "t-mypy", "t-pylint", "t-runner-pep8"]), |
| 39 |
Case(spec="@tests", matched=["t-unit-tests"]), |
| 40 |
Case(spec="@check and not pep8", matched=["t-black", "t-mypy", "t-pylint"]), |
| 41 |
Case(spec="not pep8 and @check", matched=["t-black", "t-mypy", "t-pylint"]), |
| 42 |
Case(spec="@check and pep8 or @tests", matched=["t-pep8", "t-unit-tests", "t-runner-pep8"]), |
| 43 |
Case(spec="black", matched=["t-black", "t-black-reformat"]), |
| 44 |
Case(spec="black and not black-reformat", matched=["t-black"]), |
| 45 |
Case(spec="black and not black-reformat", matched=["t-black"]), |
| 46 |
Case(spec="(black or @runner) and @check", matched=["t-black", "t-runner-pep8"]), |
| 47 |
Case(spec="not (@check or @tests)", matched=["t-black-reformat"]), |
| 48 |
] |
| 49 |
|
| 50 |
|
| 51 |
@pytest.mark.parametrize("case", _TESTS) |
| 52 |
def test_basic(case: Case) -> None: |
| 53 |
"""Make sure evaluation works more or less correctly.""" |
| 54 |
expr = pst.parse_spec(case.spec) |
| 55 |
assert [env.name for env in _ALL if expr.evaluate(env)] == case.matched |