-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c29cd76
commit 6e74846
Showing
2 changed files
with
45 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright (C) 2024 Nice Zombies | ||
"""JSON write tests.""" | ||
from __future__ import annotations | ||
|
||
__all__: list[str] = [] | ||
|
||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
from jsonyx.allow import SURROGATES | ||
# pylint: disable-next=W0611 | ||
from jsonyx.test import get_json # type: ignore # noqa: F401 | ||
|
||
if TYPE_CHECKING: | ||
from types import ModuleType | ||
|
||
|
||
def test_value(json: ModuleType) -> None: | ||
"""Test JSON value.""" | ||
with TemporaryDirectory() as tmpdir: | ||
filename: Path = Path(tmpdir) / "file.json" | ||
json.write(0, filename) | ||
assert filename.read_text("utf_8") == "0\n" | ||
|
||
|
||
@pytest.mark.parametrize(("s", "expected"), [ | ||
("\ud800", '"\ud800"\n'), | ||
("\ud800$", '"\ud800$"\n'), | ||
("\udf48", '"\udf48"\n'), # noqa: PT014 | ||
]) | ||
def test_surrogates(json: ModuleType, s: str, expected: str) -> None: | ||
"""Test surrogates.""" | ||
with TemporaryDirectory() as tmpdir: | ||
filename: Path = Path(tmpdir) / "file.json" | ||
json.write(s, filename, allow=SURROGATES) | ||
assert filename.read_text("utf_8", "surrogatepass") == expected | ||
|
||
|
||
@pytest.mark.parametrize("s", ["\ud800", "\ud800$", "\udf48"]) # noqa: PT014 | ||
def test_surrogates_if_not_allowed(json: ModuleType, s: str) -> None: | ||
"""Test surrogates.""" | ||
with TemporaryDirectory() as tmpdir, pytest.raises(UnicodeEncodeError): | ||
json.write(s, Path(tmpdir) / "file.json") |