Skip to content

Commit

Permalink
Test write surrogates
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Aug 1, 2024
1 parent c29cd76 commit 6e74846
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
8 changes: 0 additions & 8 deletions src/jsonyx/test/test_jsonyx.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,3 @@ def test_read(json: ModuleType) -> None:
filename: Path = Path(tmpdir) / "file.json"
filename.write_text("0", "utf_8")
assert json.read(filename) == 0


def test_write(json: ModuleType) -> None:
"""Test JSON write."""
with TemporaryDirectory() as tmpdir:
filename: Path = Path(tmpdir) / "file.json"
json.write(0, filename)
assert filename.read_text("utf_8") == "0\n"
45 changes: 45 additions & 0 deletions src/jsonyx/test/test_write.py
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")

0 comments on commit 6e74846

Please sign in to comment.