tokenfabrikker/tests/test_kart.py
Kjell Tore Guttormsen aff6624c5e
feat(kart): the map from equation to function, generated from the code (clarification 131)
Every S-number of the specification points to the functions named after it
and, where it is put together over the years or the draws, to the functions
that do so. A test holds the stored map against the code.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-13 20:53:31 +01:00

106 lines
3.5 KiB
Python

"""The map from equation to function covers the specification and is stored as the code makes it.
Clarification 131: every S-number tagged in the specification has at least one
function, no function carries a number the specification does not have, and
the stored ``modell/kart.md`` equals the one the code makes. Nothing is run.
"""
from __future__ import annotations
import importlib
import re
from typing import TYPE_CHECKING
import pytest
from tokenfabrikker import kart
if TYPE_CHECKING:
from pathlib import Path
MERKET = kart.merkede_numre()
def test_the_specification_tags_s1_to_s54() -> None:
assert MERKET == tuple(range(1, 55))
@pytest.mark.parametrize("nummer", MERKET, ids=[f"S{n}" for n in MERKET])
def test_every_tagged_number_has_a_function(nummer: int) -> None:
assert kart.kart(MERKET)[nummer], f"S{nummer} has no function in the map"
def test_no_numbered_function_without_a_tag() -> None:
assert kart.uten_merke(MERKET) == ()
def test_numbers_in_the_running_text_are_tagged() -> None:
assert set(kart.numre_i_teksten()) <= set(MERKET)
def test_numbered_functions_carry_their_number() -> None:
for nummer, funksjoner in kart.nummererte_funksjoner().items():
for f in funksjoner:
assert re.match(rf"s{nummer}_", f.navn)
assert not f.sammensatt
def test_every_composed_entry_exists_and_is_callable() -> None:
for nummer, oppforinger in kart.SAMMENSATT.items():
assert nummer in MERKET
for modulnavn, navn in oppforinger:
modul = importlib.import_module(f"tokenfabrikker.{modulnavn}")
objekt = getattr(modul, navn)
assert callable(objekt)
assert objekt.__module__ == modul.__name__
assert kart.sammensatte_funksjoner(MERKET).keys() == kart.SAMMENSATT.keys()
def test_a_composed_entry_that_does_not_exist_is_an_error(monkeypatch: pytest.MonkeyPatch) -> None:
feil = {**kart.SAMMENSATT, 26: (("kontantstrom", "finnes_ikke"),)}
monkeypatch.setattr(kart, "SAMMENSATT", feil)
with pytest.raises(kart.KartError, match="finnes_ikke"):
kart.sammensatte_funksjoner(MERKET)
def test_a_composed_entry_for_an_untagged_number_is_an_error(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
kart, "SAMMENSATT", {**kart.SAMMENSATT, 99: (("kontantstrom", "lag_plan"),)}
)
with pytest.raises(kart.KartError, match="S99"):
kart.sammensatte_funksjoner(MERKET)
def test_numbered_functions_come_before_composed_ones() -> None:
for funksjoner in kart.kart(MERKET).values():
slag = [f.sammensatt for f in funksjoner]
assert slag == sorted(slag)
def test_rows_are_in_numeric_order_and_each_number_once() -> None:
rader = re.findall(r"^\| S(\d+) \|", kart.lag(), flags=re.MULTILINE)
assert [int(n) for n in rader] == list(MERKET)
def test_the_text_is_deterministic_with_lf_and_one_trailing_newline() -> None:
tekst = kart.lag()
assert tekst == kart.lag()
assert "\r" not in tekst
assert tekst.endswith("\n")
assert not tekst.endswith("\n\n")
def test_the_stored_map_equals_the_one_the_code_makes() -> None:
assert kart.KARTFIL.read_bytes() == kart.lag().encode("utf-8")
assert kart.kontroller() == "identisk"
def test_a_missing_or_different_file_is_changed(tmp_path: Path) -> None:
fil = tmp_path / "kart.md"
assert kart.kontroller(fil) == "endret"
fil.write_bytes(kart.lag().encode("utf-8") + b"\n")
assert kart.kontroller(fil) == "endret"
fil.write_bytes(kart.lag().encode("utf-8"))
assert kart.kontroller(fil) == "identisk"