示例工程 11-protocols-generics-overloads

11-protocols-generics-overloads/05-typeis-narrowing/test_typeis_narrowing.py

python · 0.9 KB

"""test_typeis_narrowing.py"""
 
from __future__ import annotations
 
from pathlib import Path
 
import pytest
from typeis_narrowing import first_error, first_upper, main
 
HERE = Path(__file__).parent
 
 
def test_typeguard_negative_branch_is_not_narrowed() -> None:
    text = (HERE / "mypy-typeguard.txt").read_text(encoding="utf-8")
    assert "[operator]" in first_error(text)
 
 
def test_typeis_negative_branch_is_narrowed() -> None:
    text = (HERE / "mypy-typeis.txt").read_text(encoding="utf-8")
    assert "Success: no issues found" in text
 
 
def test_positive_narrowing_works_at_runtime() -> None:
    assert first_upper(["ok", "no"]) == "OK"
    assert first_upper(42) == ""
    assert first_upper(["ok", 1]) == ""
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert "typeguard error:" in out
    assert "typeis success: True" in out