示例工程 31-io-files-struct-socket-sqlite
31-io-files-struct-socket-sqlite/01-encoding-mistakes/test_encoding_mistakes.py
"""test_encoding_mistakes.py"""
from __future__ import annotations
import pytest
from encoding_mistakes import main
def test_utf8_and_ascii_error() -> None:
text = "你好"
data = text.encode("utf-8")
assert len(data) == 6
assert data.decode("utf-8") == text
with pytest.raises(UnicodeDecodeError):
data.decode("ascii")
assert len(data.decode("ascii", errors="replace")) == 6
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
main()
out = capsys.readouterr().out
assert "utf8_bytes: 6" in out
assert "roundtrip: 你好" in out