wapdown/tests/test_golden.py
2026-09-26 13:08:50 +03:00

69 lines
2.7 KiB
Python

"""Byte-for-byte comparison against committed reference output.
These catch unintended drift in whole-document output, which unit tests on
individual constructs cannot. When a change is deliberate, regenerate with
`uv run python tests/regenerate_golden.py` and review the resulting diff.
"""
from __future__ import annotations
from xml.etree import ElementTree
import pytest
from conftest import EXAMPLES, GOLDEN, render_file
from golden_cases import CASES, MULTI_CASES, XHTMLMP_CASES
REGENERATE_HINT = (
"Output changed. If that was intended, run "
"`uv run python tests/regenerate_golden.py` and review the diff."
)
@pytest.mark.parametrize("name", sorted(CASES))
def test_matches_golden(name):
source, options = CASES[name]
golden = GOLDEN / f"{name}.wml"
assert golden.exists(), f"missing golden for '{name}'; {REGENERATE_HINT}"
actual = render_file(EXAMPLES / source, **options).text
assert actual == golden.read_text(encoding="utf-8"), REGENERATE_HINT
@pytest.mark.parametrize("name", sorted(MULTI_CASES))
def test_matches_golden_directory(name):
source, options = MULTI_CASES[name]
directory = GOLDEN / name
assert directory.is_dir(), f"missing golden dir for '{name}'; {REGENERATE_HINT}"
documents = {
doc.name or "index.wml": doc.text
for doc in render_file(EXAMPLES / source, **options).documents
}
expected = {path.name: path.read_text(encoding="utf-8") for path in directory.iterdir()}
assert sorted(documents) == sorted(expected), REGENERATE_HINT
for filename, text in documents.items():
assert text == expected[filename], f"{filename}: {REGENERATE_HINT}"
@pytest.mark.parametrize("path", sorted(GOLDEN.glob("**/*.wml")), ids=lambda p: p.name)
def test_golden_files_are_well_formed(path):
ElementTree.fromstring(path.read_text(encoding="utf-8"))
@pytest.mark.parametrize("name", sorted(XHTMLMP_CASES))
def test_matches_golden_xhtmlmp(name):
source, options = XHTMLMP_CASES[name]
golden = GOLDEN / f"{name}.xhtml"
assert golden.exists(), f"missing golden for '{name}'; {REGENERATE_HINT}"
actual = render_file(EXAMPLES / source, renderer_name="xhtmlmp", **options)
assert actual == golden.read_text(encoding="utf-8"), REGENERATE_HINT
@pytest.mark.parametrize("path", sorted(GOLDEN.glob("**/*.xhtml")), ids=lambda p: p.name)
def test_xhtmlmp_golden_files_are_well_formed(path):
ElementTree.fromstring(path.read_text(encoding="utf-8"))
def test_every_example_has_a_case():
"""A new examples/*.md without a golden case would go unverified."""
covered = {source for source, _ in list(CASES.values()) + list(MULTI_CASES.values())}
assert {path.name for path in EXAMPLES.glob("*.md")} == covered