示例工程 03-numbers-strings-unicode
另有 1 个文件未随站点发布(平台工程、二进制或超过 200KB)。
03-numbers-strings-unicode/03-encode-decode-errors/test_encode_decode_errors.py
"""test_encode_decode_errors.py"""
from __future__ import annotations
import pytest
from encode_decode_errors import decode_with, truncated_last_byte, utf8_bytes
def test_utf8_length() -> None:
assert utf8_bytes() == b"\xe4\xb8\xad\xe6\x96\x87"
assert len(utf8_bytes()) == 6
def test_strict_raises_and_replace_recovers() -> None:
with pytest.raises(UnicodeDecodeError, match="unexpected end of data"):
truncated_last_byte().decode("utf-8")
assert decode_with("replace") == "中\ufffd"
assert decode_with("ignore") == "中"