Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
31 changes: 26 additions & 5 deletions tests/test_toml_tests.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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


Expand All @@ -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,
}


Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading