示例工程 40-type-checker-in-python

40-type-checker-in-python/04-test-suite/test_test_suite.py

python · 0.9 KB

"""test_test_suite.py"""
 
from __future__ import annotations
 
import pytest
from test_suite import CASES, check, main
 
 
@pytest.mark.parametrize(
    ("name", "source", "expected"), [(name, *case) for name, case in CASES.items()]
)
def test_cases(name: str, source: str, expected: list[str]) -> None:
    assert check(source) == expected, name
 
 
@pytest.mark.parametrize(
    ("source", "expected"),
    [
        ("x = 1 + 2\n", []),
        ("x = 'a' + 'b'\n", []),
        ("x = 1 + 'b'\n", ["E001"]),
        ("x = nope\n", ["E002"]),
        ("x = 1\nx = False\n", ["E003"]),
    ],
)
def test_inline_cases(source: str, expected: list[str]) -> None:
    assert check(source) == expected
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert out.count("ok") == len(CASES)
    assert "FAIL" not in out