diff --git a/src/jsonyx/_encoder.py b/src/jsonyx/_encoder.py index bea7bb4..3c6ac94 100644 --- a/src/jsonyx/_encoder.py +++ b/src/jsonyx/_encoder.py @@ -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): diff --git a/src/jsonyx/test/__init__.py b/src/jsonyx/test/__init__.py index 0176e38..63857f9 100644 --- a/src/jsonyx/test/__init__.py +++ b/src/jsonyx/test/__init__.py @@ -1,5 +1,4 @@ # Copyright (C) 2024 Nice Zombies -# TODO(Nice Zombies): test jsonyx.dumps """Get JSON module.""" from __future__ import annotations diff --git a/src/jsonyx/test/test_dumps.py b/src/jsonyx/test/test_dumps.py new file mode 100644 index 0000000..12b55a1 --- /dev/null +++ b/src/jsonyx/test/test_dumps.py @@ -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 diff --git a/src/jsonyx/test/test_jsonyx.py b/src/jsonyx/test/test_jsonyx.py index 1c3273d..9047ea9 100644 --- a/src/jsonyx/test/test_jsonyx.py +++ b/src/jsonyx/test/test_jsonyx.py @@ -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: diff --git a/src/jsonyx/test/test_loads.py b/src/jsonyx/test/test_loads.py index 889c753..c0771f2 100644 --- a/src/jsonyx/test/test_loads.py +++ b/src/jsonyx/test/test_loads.py @@ -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( @@ -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: @@ -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) diff --git a/src/jsonyx/test/test_write.py b/src/jsonyx/test/test_write.py index ca037be..2c4732a 100644 --- a/src/jsonyx/test/test_write.py +++ b/src/jsonyx/test/test_write.py @@ -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