Skip to content

Commit

Permalink
Test Python singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Aug 1, 2024
1 parent 6e74846 commit ff4cc89
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
11 changes: 6 additions & 5 deletions src/jsonyx/_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ def write_value(
write(encode_string(obj))
elif obj is None:
write("null")
elif obj is True:
write("true")
elif obj is False:
write("false")
elif isinstance(obj, int):
write(int_repr(obj))
if obj is True:
write("true")
elif obj is False:
write("false")
else:
write(int_repr(obj))
elif isinstance(obj, float):
write(floatstr(obj))
elif isinstance(obj, list):
Expand Down
1 change: 0 additions & 1 deletion src/jsonyx/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Copyright (C) 2024 Nice Zombies
# TODO(Nice Zombies): test jsonyx.dumps
"""Get JSON module."""
from __future__ import annotations

Expand Down
27 changes: 27 additions & 0 deletions src/jsonyx/test/test_dumps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (C) 2024 Nice Zombies
# TODO(Nice Zombies): add more tests
"""JSON dumps tests."""
from __future__ import annotations

__all__: list[str] = []

from typing import TYPE_CHECKING

import pytest
# pylint: disable-next=W0611
from jsonyx.test import get_json # type: ignore # noqa: F401

if TYPE_CHECKING:
from types import ModuleType


@pytest.mark.parametrize(("obj", "expected"), [
(True, "true"),
(False, "false"),
(None, "null"),
])
def test_singletons(
json: ModuleType, obj: bool | None, expected: str, # noqa: FBT001
) -> None:
"""Test Python singletons."""
assert json.dumps(obj, end="") == expected
4 changes: 2 additions & 2 deletions src/jsonyx/test/test_jsonyx.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def test_format_syntax_error(
def test_dump(json: ModuleType) -> None:
"""Test JSON dump."""
fp: StringIO = StringIO()
json.dump(0, fp)
assert fp.getvalue() == "0\n"
json.dump(0, fp, end="")
assert fp.getvalue() == "0"


def test_load(json: ModuleType) -> None:
Expand Down
12 changes: 7 additions & 5 deletions src/jsonyx/test/test_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ def test_utf8_bom(json: ModuleType) -> None:
("false", False),
("null", None),
])
def test_keywords(json: ModuleType, s: str, *, expected: bool | None) -> None:
"""Test JSON keywords."""
def test_literal_names(
json: ModuleType, s: str, expected: bool | None, # noqa: FBT001
) -> None:
"""Test literal names."""
assert json.loads(s) is expected


@pytest.mark.parametrize("s", ["NaN", "Infinity", "-Infinity"])
@pytest.mark.parametrize("use_decimal", [True, False])
def test_nan_and_infinity(
json: ModuleType, s: str, *, use_decimal: bool,
json: ModuleType, s: str, use_decimal: bool, # noqa: FBT001
) -> None:
"""Test NaN and infinity with decimal and float."""
obj: object = json.loads(
Expand All @@ -95,7 +97,7 @@ def test_nan_and_infinity(
@pytest.mark.parametrize("s", ["NaN", "Infinity", "-Infinity"])
@pytest.mark.parametrize("use_decimal", [True, False])
def test_nan_and_infinity_not_allowed(
json: ModuleType, s: str, *, use_decimal: bool,
json: ModuleType, s: str, use_decimal: bool, # noqa: FBT001
) -> None:
"""Test NaN and infinity with decimal and float if not allowed."""
with pytest.raises(json.JSONSyntaxError) as exc_info:
Expand Down Expand Up @@ -141,7 +143,7 @@ def test_int(json: ModuleType, s: str) -> None:
])
@pytest.mark.parametrize("use_decimal", [True, False])
def test_decimal_and_float(
json: ModuleType, s: str, *, use_decimal: bool,
json: ModuleType, s: str, use_decimal: bool, # noqa: FBT001
) -> None:
"""Test decimal and float."""
obj: object = json.loads(s, use_decimal=use_decimal)
Expand Down
12 changes: 6 additions & 6 deletions src/jsonyx/test/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ 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"
json.write(0, filename, end="")
assert filename.read_text("utf_8") == "0"


@pytest.mark.parametrize(("s", "expected"), [
("\ud800", '"\ud800"\n'),
("\ud800$", '"\ud800$"\n'),
("\udf48", '"\udf48"\n'), # noqa: PT014
("\ud800", '"\ud800"'),
("\ud800$", '"\ud800$"'),
("\udf48", '"\udf48"'), # 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)
json.write(s, filename, allow=SURROGATES, end="")
assert filename.read_text("utf_8", "surrogatepass") == expected


Expand Down

0 comments on commit ff4cc89

Please sign in to comment.