|
| 1 | +"""The review profile (schema-dist ``profiles/review.md``): ``proposed_mark_of`` (the mark as a reader that shows |
| 2 | +a world shows it, rules 1, 2 and 4) and ``hands_on_as_proposed`` (what a reader that hands a world on leaves out, |
| 3 | +rules 1 and 4). The profile has no fixture yet; these cases are the npm SDK's too (``test/review.test.mjs``), |
| 4 | +case for case.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from onlyworlds import PROPOSED_MARK, hands_on_as_proposed, proposed_mark_of |
| 13 | +from onlyworlds.pack import is_proposed |
| 14 | + |
| 15 | +TODAY = "2026-10-26" |
| 16 | +_NO_KEY = object() |
| 17 | + |
| 18 | +FS = chr(0x1C) |
| 19 | +BOM = chr(0xFEFF) |
| 20 | +ARABIC_DAY = "".join(chr(c) for c in (0x662, 0x660, 0x662, 0x666)) + "-10-01" |
| 21 | + |
| 22 | +# (what, the mark (_NO_KEY: no key), proposed_mark_of's answer, hands_on_as_proposed's answer) |
| 23 | +CASES: list[tuple[str, Any, Any, bool]] = [ |
| 24 | + ("no key", _NO_KEY, None, False), |
| 25 | + ("null: settled", None, None, False), |
| 26 | + ("a string: not a mark, a doubt", "yes", None, True), |
| 27 | + ("an array: not a mark, a doubt", [], None, True), |
| 28 | + ("false: a value, a doubt", False, None, True), |
| 29 | + ("zero: a value, a doubt", 0, None, True), |
| 30 | + ("an empty object", {}, {"by": None, "expires": None, "lapsed": False}, True), |
| 31 | + ( |
| 32 | + "a whole mark, open", |
| 33 | + {"by": "m1", "evidence": "seen", "expires": "2026-11-01"}, |
| 34 | + {"by": "m1", "expires": "2026-11-01", "lapsed": False}, |
| 35 | + True, |
| 36 | + ), |
| 37 | + ( |
| 38 | + "trimmed by and expires", |
| 39 | + {"by": " m1 ", "expires": " 2026-11-01 "}, |
| 40 | + {"by": "m1", "expires": "2026-11-01", "lapsed": False}, |
| 41 | + True, |
| 42 | + ), |
| 43 | + ("lapses on its own day", {"by": "m1", "expires": TODAY}, {"by": "m1", "expires": TODAY, "lapsed": True}, True), |
| 44 | + ( |
| 45 | + "lapsed before today", |
| 46 | + {"by": "m1", "expires": "2026-10-01"}, |
| 47 | + {"by": "m1", "expires": "2026-10-01", "lapsed": True}, |
| 48 | + True, |
| 49 | + ), |
| 50 | + ( |
| 51 | + "an expires not shaped as a day", |
| 52 | + {"by": "m1", "expires": "2026-1-1"}, |
| 53 | + {"by": "m1", "expires": None, "lapsed": False}, |
| 54 | + True, |
| 55 | + ), |
| 56 | + ("a blank or non-string by", {"by": " ", "expires": 7}, {"by": None, "expires": None, "lapsed": False}, True), |
| 57 | + # The two languages' own defaults disagree here (see the npm test): both trim by White_Space and read ASCII digits. |
| 58 | + ( |
| 59 | + "a by wrapped in U+001C keeps it", |
| 60 | + {"by": f"{FS}m1{FS}"}, |
| 61 | + {"by": f"{FS}m1{FS}", "expires": None, "lapsed": False}, |
| 62 | + True, |
| 63 | + ), |
| 64 | + ( |
| 65 | + "a by wrapped in a byte order mark keeps it", |
| 66 | + {"by": f"{BOM}m1"}, |
| 67 | + {"by": f"{BOM}m1", "expires": None, "lapsed": False}, |
| 68 | + True, |
| 69 | + ), |
| 70 | + ( |
| 71 | + "an expires in Arabic-Indic digits is no day", |
| 72 | + {"by": "m1", "expires": ARABIC_DAY}, |
| 73 | + {"by": "m1", "expires": None, "lapsed": False}, |
| 74 | + True, |
| 75 | + ), |
| 76 | + ( |
| 77 | + "an expires wrapped in a no-break space is trimmed", |
| 78 | + {"expires": chr(0xA0) + "2026-10-01"}, |
| 79 | + {"by": None, "expires": "2026-10-01", "lapsed": True}, |
| 80 | + True, |
| 81 | + ), |
| 82 | +] |
| 83 | + |
| 84 | + |
| 85 | +def _el(mark: Any) -> dict[str, Any]: |
| 86 | + return {"id": "a", "name": "A"} if mark is _NO_KEY else {"id": "a", "name": "A", PROPOSED_MARK: mark} |
| 87 | + |
| 88 | + |
| 89 | +def test_the_mark_key_is_the_profiles() -> None: |
| 90 | + assert PROPOSED_MARK == "x_review_proposed" |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize(("what", "mark", "shown", "handed_on"), CASES, ids=[c[0] for c in CASES]) |
| 94 | +def test_review_case(what: str, mark: Any, shown: Any, handed_on: bool) -> None: |
| 95 | + assert proposed_mark_of(_el(mark), TODAY) == shown |
| 96 | + assert hands_on_as_proposed(_el(mark)) is handed_on |
| 97 | + assert is_proposed(_el(mark)) is handed_on # the pack's reader is the same rule |
| 98 | + |
| 99 | + |
| 100 | +def test_not_an_element_reads_as_settled() -> None: |
| 101 | + for v in (None, "x", 3, ["a"]): |
| 102 | + assert proposed_mark_of(v, TODAY) is None |
| 103 | + assert hands_on_as_proposed(v) is False |
| 104 | + |
| 105 | + |
| 106 | +def test_today_must_be_a_day() -> None: |
| 107 | + with pytest.raises(TypeError): |
| 108 | + proposed_mark_of(_el({}), "26-10-2026") |
| 109 | + with pytest.raises(TypeError): |
| 110 | + proposed_mark_of(_el({}), 20261026) # type: ignore[arg-type] |
| 111 | + with pytest.raises(TypeError): |
| 112 | + proposed_mark_of(_el({}), ARABIC_DAY) |
| 113 | + assert proposed_mark_of(_el({})) is not None # today in UTC by default |
0 commit comments