lingologos-thinkingpython / tests /test_reasons.py
tailot's picture
thinking-python: reference implementation, corpus and gates
35d4b41 verified
Raw History Blame Contribute Delete
2.57 kB
"""Every reason code the code can raise is registered in the registry of section 9."""
import ast
import os
import sys
import unittest
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
from lingologos_thinkingpython import reasons # noqa: E402
SCAN = ["lingologos_thinkingpython", "nodes", "tools"]
# Status words are not reason codes; they are verdicts, gate labels and exit markers.
NOT_A_CODE = {"PASS", "FAIL", "GREEN", "RED", "UNKNOWN", "VERIFIED", "REFUSED", "PARTIAL",
"ALL_GATES_PASS", "NO_SHAPE", "UNPARSEABLE_TARGET", "OFF", "NONE", "INFO",
"WARN", "ERROR", "NOT_FOUND", "NOT_UNDERSTOOD_OK", "L1", "L2", "L3", "L4",
"L5", "OK", "YES", "NOLANG", "JSON", "UTF8", "EOL", "LF", "CRLF",
"PYTHONPATH", "GATES_FAILED", "SINGLE_PROVER", "MISSING"}
def literals(directory: str):
for name in sorted(os.listdir(directory)):
path = os.path.join(directory, name)
if name.endswith(".py"):
with open(path, "r", encoding="utf-8") as handle:
tree = ast.parse(handle.read(), filename=path)
for node in ast.walk(tree):
if isinstance(node, ast.Constant) and isinstance(node.value, str):
yield path, node.value
elif os.path.isdir(path) and name != "__pycache__":
for item in literals(path):
yield item
class ReasonRegistry(unittest.TestCase):
def test_groups_are_disjoint(self):
overlap = set(reasons.SESSION_CODES) & set(reasons.PREFLIGHT_CODES)
overlap |= set(reasons.SESSION_CODES) & set(reasons.GATE_CODES)
overlap |= set(reasons.PREFLIGHT_CODES) & set(reasons.GATE_CODES)
self.assertEqual(overlap, set())
def test_every_emitted_code_is_registered(self):
unregistered = set()
for directory in SCAN:
for _path, value in literals(os.path.join(ROOT, directory)):
if (len(value) >= 4 and value.isupper()
and value.replace("_", "").isalpha()
and value not in NOT_A_CODE
and not reasons.is_known(value)):
unregistered.add(value)
self.assertEqual(unregistered, set())
def test_group_lookup(self):
self.assertEqual(reasons.group_of("NO_TEST_FILE"), "preflight")
self.assertEqual(reasons.group_of("SUITE_RED"), "gate")
self.assertEqual(reasons.group_of("NOT_A_CODE"), "unknown")
if __name__ == "__main__":
unittest.main()