Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions test/python/ast/test_python_cst_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
17 changes: 17 additions & 0 deletions test/python/ast/test_python_rst_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
Loading