"""Tests for composing-html's deterministic checker.
Run with: python composing-html/tests/test_checker.py (no pytest needed)
or python -m pytest composing-html/tests -q
"""
from __future__ import annotations
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / "scripts"))
from checker import check_html, has_errors
def _rules(html, **kw):
return {f.rule for f in check_html(html, **kw)}
_PASS = 0
_FAIL = 0
def ok(cond, label):
global _PASS, _FAIL
if cond:
_PASS += 1
else:
_FAIL += 1
print(f" FAIL: {label}")
# --- clean fragment: no findings ------------------------------------------- #
clean = """
All goodStatus
hi
', fragment=True), "chrome-leak fires on / in fragment") ok("chrome-leak" not in _rules('x'), "chrome-leak silent on a full artifact") # --- undefined token ------------------------------------------------------- # ok("undefined-token" in _rules('typo
', fragment=True), "undefined-token catches var(--cley) typo") ok("undefined-token" not in _rules('ok
', fragment=True), "undefined-token silent on valid token") ok("undefined-token" not in _rules( '', fragment=True), "undefined-token silent on --bind-* created by data-bind") ok("undefined-token" not in _rules( '', fragment=True), "undefined-token silent on author-declared custom property") # --- hardcoded colour ------------------------------------------------------ # ok("hardcoded-color" in _rules('red
', fragment=True), "hardcoded-color catches hex literal") ok("hardcoded-color" in _rules('x
', fragment=True), "hardcoded-color catches rgb()") # --- inline typography ----------------------------------------------------- # ok("inline-typography" in _rules('x
', fragment=True), "inline-typography catches font-family") ok("inline-typography" in _rules('x
', fragment=True), "inline-typography catches font-size") # --- nested card ----------------------------------------------------------- # ok("nested-card" in _rules('
', fragment=True),
"img-no-alt catches missing alt")
ok("img-no-alt" not in _rules('
', fragment=True),
"img-no-alt silent on empty alt")
# --- severity contract ----------------------------------------------------- #
ok(has_errors(check_html('x
', fragment=True)), "undefined-token is error severity") ok(not has_errors(check_html('