diff --git a/test/python/ast/test_python_cst_node.py b/test/python/ast/test_python_cst_node.py index 97033305..39d5e4cc 100644 --- a/test/python/ast/test_python_cst_node.py +++ b/test/python/ast/test_python_cst_node.py @@ -98,6 +98,23 @@ def test_load_file(self) -> None: atu = PythonCstNode.load(Path(targets.__file__).parent / "demo.py") assert_that(atu, is_not(None)) + @pytest.mark.parametrize( + "source", + [ + pytest.param('x = "“quoted”"\n', id="undecodable-in-cp1252"), + pytest.param('x = "café"\n', id="two-byte-character"), + pytest.param('x = "\U0001f600"\n', id="four-byte-character"), + ], + ) + def test_load_file_decodes_utf8(self, tmp_path: Path, source: str) -> None: + """Verify load reads the file as UTF-8, whatever the locale's default encoding is.""" + path = tmp_path / "utf8.py" + path.write_bytes(source.encode("utf-8")) + + atu = PythonCstNode.load(path) + + assert_that(atu.signature, is_(source)) + def test_load_invalid_file(self) -> None: """AI: Verify loading a syntactically invalid Python file raises a ParserSyntaxError.""" with pytest.raises(ParserSyntaxError, match="Syntax Error"): diff --git a/test/python/ast/test_python_rst_node.py b/test/python/ast/test_python_rst_node.py index e43271fb..3b80bbd0 100644 --- a/test/python/ast/test_python_rst_node.py +++ b/test/python/ast/test_python_rst_node.py @@ -168,6 +168,23 @@ def test_load_file(self) -> None: atu = PythonRstNode.load(Path(targets.__file__).parent / "demo.py") assert_that(atu.translation_unit.atu.type_ignores, is_(empty())) + @pytest.mark.parametrize( + "source", + [ + pytest.param('x = "“quoted”"\n', id="undecodable-in-cp1252"), + pytest.param('x = "café"\n', id="two-byte-character"), + pytest.param('x = "\U0001f600"\n', id="four-byte-character"), + ], + ) + def test_load_file_decodes_utf8(self, tmp_path: Path, source: str) -> None: + """Verify load reads the file as UTF-8, whatever the locale's default encoding is.""" + path = tmp_path / "utf8.py" + path.write_bytes(source.encode("utf-8")) + + atu = PythonRstNode.load(path) + + assert_that(atu.signature, is_(source)) + def test_load_invalid_file(self) -> None: """AI: Verify loading a syntactically invalid Python file raises an IndentationError.""" with pytest.raises(IndentationError, match="unexpected indent"):