Files
felhom.eu/scripts/test_hu_grep.py
T

52 lines
2.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""R-364: hu_grep.py never reports an untested zero for an accented pattern.
Each case asserts the CONSEQUENCE an operator would act on: a present string is counted; a transformed
pattern, a blind instrument, or a normal-form mismatch is REFUSED (exit 2), never a quiet 0; and a real
absence, with every control behaving, is a 0 (exit 1). Pure Python — runs on the BusyBox CI runner.
Run: python3 scripts/test_hu_grep.py"""
import os
import shutil
import sys
import tempfile
import unicodedata
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import hu_grep # noqa: E402
fails = []
def case(label, want_rc, pattern, anchor, files, want_in=""):
tmp = tempfile.mkdtemp(prefix="hu-grep-")
try:
for name, body in files.items():
with open(os.path.join(tmp, name), "w", encoding="utf-8") as fh:
fh.write(body)
rc, msg, _ = hu_grep.judge(pattern, anchor, hu_grep.DEFAULT_NEGATIVE, [tmp])
finally:
shutil.rmtree(tmp, ignore_errors=True)
if rc != want_rc or want_in not in msg:
fails.append("%s: rc=%d msg=%r, want rc=%d containing %r" % (label, rc, msg, want_rc, want_in))
else:
print("ok %-34s rc=%d %s" % (label, rc, msg[:70]))
PAGE = {"page.html": "<p>Ha lejárt, kérj újat az alábbi gombbal.</p>\n<p>Felhom</p>\n"}
case("present accented string counted", 0, "kérj újat", "Felhom", PAGE, "1 line")
case("octal-escaped pattern REFUSED", 2, "k\\303\\251rj", "Felhom", PAGE, "TRANSFORMED")
case("U+FFFD pattern REFUSED", 2, "k�rj", "Felhom", PAGE, "TRANSFORMED")
case("blind instrument REFUSED", 2, "ügyfélszolgálat", "NotInTheFile", PAGE, "anchor")
case("no anchor given REFUSED", 2, "ügyfélszolgálat", "", PAGE, "--anchor")
case("NFD file, NFC pattern REFUSED", 2, "ügyfélszolgálat", "Felhom",
{"nfd.txt": unicodedata.normalize("NFD", "Felhom ügyfélszolgálat\n")}, "NFD form")
case("tested zero is a zero", 1, "ügyfélszolgálat", "Felhom", PAGE, "TESTED zero")
case("ascii pattern plain zero", 1, "nothere", "", PAGE, "0 line")
if fails:
print("\nFAIL:\n " + "\n ".join(fails))
sys.exit(1)
print("\nhu_grep: OK — no untested zero for an accented pattern")