-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gen_architecture.py
More file actions
125 lines (103 loc) · 4.27 KB
/
Copy pathtest_gen_architecture.py
File metadata and controls
125 lines (103 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
# FLOOR
"""test_gen_architecture.py - deterministic red floor for gen_architecture.py (G10).
Proves the inventory generator's teeth on isolated fixtures, one behaviour per
decidable claim (METHOD §2; "every generator ships its proof", §2/v0.21):
- render: a CODES tool and a no-codes tool each get the right row.
- order : tools are path-sorted, codes are sorted (diff-stable output).
- summary: the footer counts tools and declared codes.
- round-trip: write_block fills the marked block, then check_block passes.
- drift : a hand-edited block makes check_block fail (the live guard bites).
- markers: a doc without markers fails check_block / write_block, and
extract_block returns None.
It carries the ``# FLOOR`` marker, so discover_floors finds and runs it. Exit 0
= every behaviour holds; exit 1 = a regression.
"""
from __future__ import annotations
import sys
import tempfile
from pathlib import Path
import gen_architecture as ga
_passed = 0
_failed = 0
def _check(label: str, cond: bool) -> None:
global _passed, _failed
if cond:
_passed += 1
print(f" [OK] {label}")
else:
_failed += 1
print(f" [FAIL] {label}")
def _root(files: dict[str, str]) -> Path:
"""Write {relpath: content} into a fresh temp root and return it (kept alive)."""
tmp = tempfile.mkdtemp()
root = Path(tmp)
for rel, content in files.items():
target = root / rel
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(content, encoding="utf-8")
return root
DOC_TEMPLATE = (
"# ARCHITECTURE\n\nintro prose\n\n"
f"{ga.BEGIN}\nplaceholder - to be filled\n{ga.END}\n\ntail prose\n"
)
print("[test floor : architecture inventory generator]\n")
# --- render + order + summary -------------------------------------------------
root = _root(
{
"atool.py": 'CODES = frozenset({"X2", "X1"})\nFLOOR = "f.py"\n',
"btool.py": 'CODES = frozenset()\nFLOOR = "g.py"\n',
}
)
block = ga.render_block(root)
_check(
"render: codes tool row, codes sorted",
"| `atool.py` | `X1`, `X2` | `f.py` |" in block,
)
_check(
"render: no-codes tool row shows (no codes)",
"| `btool.py` | (no codes) | `g.py` |" in block,
)
_check(
"order: atool row precedes btool row (path-sorted)",
block.index("`atool.py`") < block.index("`btool.py`"),
)
_check("summary: counts 2 tools, 2 declared codes", "2 tool(s), 2 declared code(s)" in block)
_check(
"render: markers present and ordered",
block.startswith(ga.BEGIN) and block.rstrip().endswith(ga.END),
)
# --- no-codes tool with no FLOOR shows (none) ---------------------------------
root_nofloor = _root({"ctool.py": "CODES = frozenset()\n"})
_check(
"render: missing FLOOR shows (none)",
"| `ctool.py` | (no codes) | (none) |" in ga.render_block(root_nofloor),
)
# --- round-trip: write_block fills, check_block then passes --------------------
doc = root / "ARCHITECTURE.md"
doc.write_text(DOC_TEMPLATE, encoding="utf-8")
rc_write = ga.write_block(doc, root)
_check("round-trip: write_block returns 0", rc_write == 0)
_check(
"round-trip: placeholder replaced by real rows", "`atool.py`" in doc.read_text(encoding="utf-8")
)
_check("round-trip: check_block passes on a freshly written doc", ga.check_block(doc, root) == 0)
# --- drift: a hand-edited block fails check_block ------------------------------
tampered = doc.read_text(encoding="utf-8").replace("`atool.py`", "`hand_edited.py`")
doc.write_text(tampered, encoding="utf-8")
_check("drift: check_block returns 1 on a tampered block", ga.check_block(doc, root) == 1)
# --- markers missing: check / write fail, extract returns None ----------------
nomark = root / "NOMARK.md"
nomark.write_text("# doc with no markers\n\njust prose\n", encoding="utf-8")
_check(
"markers: extract_block returns None when markers absent",
ga.extract_block(nomark.read_text(encoding="utf-8")) is None,
)
_check("markers: check_block returns 1 when markers absent", ga.check_block(nomark, root) == 1)
_check("markers: write_block returns 1 when markers absent", ga.write_block(nomark, root) == 1)
print(f"\nran {_passed + _failed} assertions, {_failed} failed")
if _failed:
print("RESULT : FAIL (exit 1)")
sys.exit(1)
print("FLOOR GREEN - all checks pass.")
sys.exit(0)