diff --git a/tests/test_items.py b/tests/test_items.py index cf6134aa..3c0ab5d7 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -113,11 +113,11 @@ def test_item_base_has_no_unwrap() -> None: def test_integer_unwrap() -> None: - elementary_test(item(666), int) + elementary_test(item(666), int, 666) def test_float_unwrap() -> None: - elementary_test(item(2.78), float) + elementary_test(item(2.78), float, 2.78) @pytest.mark.skipif( @@ -133,25 +133,25 @@ def test_float_is_not_a_sequence() -> None: def test_false_unwrap() -> None: - elementary_test(item(False), bool) + elementary_test(item(False), bool, False) def test_true_unwrap() -> None: - elementary_test(item(True), bool) + elementary_test(item(True), bool, True) def test_datetime_unwrap() -> None: dt = datetime.now(tz=timezone.utc) - elementary_test(item(dt), datetime) + elementary_test(item(dt), datetime, dt) def test_string_unwrap() -> None: - elementary_test(item("hello"), str) + elementary_test(item("hello"), str, "hello") def test_null_unwrap() -> None: n = Null() - elementary_test(n, type(None)) + elementary_test(n, type(None), None) def test_aot_unwrap() -> None: @@ -181,12 +181,12 @@ def test_aot_set_item() -> None: def test_time_unwrap() -> None: t = time(3, 8, 14) - elementary_test(item(t), time) + elementary_test(item(t), time, t) def test_date_unwrap() -> None: d = date.today() - elementary_test(item(d), date) + elementary_test(item(d), date, d) def test_array_unwrap() -> None: diff --git a/tests/test_toml_tests.py b/tests/test_toml_tests.py index 667353ee..40d36d65 100644 --- a/tests/test_toml_tests.py +++ b/tests/test_toml_tests.py @@ -1,6 +1,11 @@ +from __future__ import annotations + import json import os +from datetime import date +from datetime import datetime +from datetime import time from typing import Any from typing import Callable @@ -9,7 +14,6 @@ from tomlkit import load from tomlkit import parse from tomlkit._compat import decode -from tomlkit._utils import parse_rfc3339 from tomlkit.exceptions import TOMLKitError @@ -23,15 +27,32 @@ def to_bool(s: str) -> bool: return s == "true" +def expected_temporal(s: str) -> datetime | date | time: + """Parse toml-test JSON temporal values without using tomlkit's parser. + + The corpus values are RFC 3339 / ISO 8601. Using stdlib constructors here + keeps the compliance tests an independent oracle for ``parse_rfc3339``. + """ + if len(s) == 10 and s[4] == "-" and s[7] == "-": + return date.fromisoformat(s) + if len(s) >= 3 and s[0].isdigit() and s[2] == ":": + return time.fromisoformat(s) + + normalized = s.replace(" ", "T", 1) + if normalized.endswith(("Z", "z")): + normalized = normalized[:-1] + "+00:00" + return datetime.fromisoformat(normalized) + + stypes: dict[str, Callable[[str], Any]] = { "string": str, "bool": to_bool, "integer": int, "float": float, - "datetime": parse_rfc3339, - "datetime-local": parse_rfc3339, - "date-local": parse_rfc3339, - "time-local": parse_rfc3339, + "datetime": expected_temporal, + "datetime-local": expected_temporal, + "date-local": expected_temporal, + "time-local": expected_temporal, } diff --git a/tests/test_utils.py b/tests/test_utils.py index c4f96cf1..d80cc9b1 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -33,6 +33,18 @@ tzinfo=tz(td(seconds=-7 * 3600), "-07:00"), ), ), + ( + "1979-05-27T07:32:00+05:30", + dt( + 1979, + 5, + 27, + 7, + 32, + 0, + tzinfo=tz(td(seconds=5 * 3600 + 30 * 60), "+05:30"), + ), + ), ], ) def test_parse_rfc3339_datetime(string: str, expected: Union[dt, date, time]) -> None: diff --git a/tests/util.py b/tests/util.py index 83cf5d9f..0f5c4694 100644 --- a/tests/util.py +++ b/tests/util.py @@ -52,6 +52,11 @@ def assert_is_ppo(v_unwrapped: object, unwrapped_type: type) -> None: assert isinstance(v_unwrapped, unwrapped_type) -def elementary_test(v: Item, unwrapped_type: type) -> None: +_MISSING = object() + + +def elementary_test(v: Item, unwrapped_type: type, expected: object = _MISSING) -> None: v_unwrapped = v.unwrap() assert_is_ppo(v_unwrapped, unwrapped_type) + if expected is not _MISSING: + assert v_unwrapped == expected