Skip to content

Commit d0e32f3

Browse files
test(tools): add unit tests for check-prose and gen-indexes (#449)
* test(tools): add unit tests for check-prose and gen-indexes Adds missing unit test suites tools/check-prose-test.py and tools/gen-indexes-test.py using Python's standard library unittest framework, and integrates them into Makefile's test target. Co-authored-by: mjmirza <34001140+mjmirza@users.noreply.github.com> * test(tools): add unit tests for check-prose and gen-indexes Adds missing unit test suites tools/check-prose-test.py and tools/gen-indexes-test.py using Python's standard library unittest framework, and integrates them into Makefile's test target. Co-authored-by: mjmirza <34001140+mjmirza@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 5c43cb5 commit d0e32f3

3 files changed

Lines changed: 233 additions & 0 deletions

File tree

‎Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ by-problem-by-language-check: by-problem-by-language
3333
test:
3434
@python3 tools/check-structure-test.py
3535
@python3 tools/check-family-names-test.py
36+
@python3 tools/check-prose-test.py
37+
@python3 tools/gen-indexes-test.py
3638
@python3 tools/gen-by-problem-by-language-test.py
3739
@python3 tools/gen-catalogue-status-test.py
3840
@python3 tools/check-code-test.py

‎tools/check-prose-test.py‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env python3
2+
"""Unit tests for tools/check-prose.py prose validator."""
3+
4+
from __future__ import annotations
5+
6+
import importlib.util
7+
import sys
8+
import tempfile
9+
import unittest
10+
from pathlib import Path
11+
12+
TOOLS = Path(__file__).resolve().parent
13+
sys.path.insert(0, str(TOOLS))
14+
15+
spec = importlib.util.spec_from_file_location(
16+
"check_prose", TOOLS / "check-prose.py"
17+
)
18+
check_prose = importlib.util.module_from_spec(spec)
19+
spec.loader.exec_module(check_prose)
20+
21+
22+
class TestCheckProse(unittest.TestCase):
23+
def setUp(self):
24+
self.td = tempfile.TemporaryDirectory()
25+
self.tmp = Path(self.td.name)
26+
27+
def tearDown(self):
28+
self.td.cleanup()
29+
30+
def test_prose_lines_frontmatter_and_fences(self):
31+
content = (
32+
"---\n"
33+
"name: test-pattern\n"
34+
"---\n"
35+
"Line 4: This is prose.\n"
36+
"```python\n"
37+
"def foo():\n"
38+
" return 'leverage'\n"
39+
"```\n"
40+
"Line 9: More prose.\n"
41+
)
42+
lines = check_prose.prose_lines(content)
43+
self.assertEqual(
44+
lines,
45+
[
46+
(4, "Line 4: This is prose."),
47+
(9, "Line 9: More prose."),
48+
],
49+
)
50+
51+
def test_strip_inline_code(self):
52+
line = "Use `leverage` in code, but leverage in prose."
53+
stripped = check_prose.strip_inline_code(line)
54+
self.assertEqual(stripped, "Use in code, but leverage in prose.")
55+
56+
def test_check_clean_file(self):
57+
test_file = self.tmp / "clean.md"
58+
test_file.write_text(
59+
"---\n"
60+
"name: clean\n"
61+
"---\n"
62+
"## 1. Context\n"
63+
"This pattern provides a straight forward solution to a clear problem.\n"
64+
)
65+
errs = check_prose.check(test_file)
66+
self.assertEqual(errs, [])
67+
68+
def test_check_banned_words_and_phrases(self):
69+
test_file = self.tmp / "slop.md"
70+
test_file.write_text(
71+
"We leverage this robust tool to dive into the problem.\n"
72+
)
73+
errs = check_prose.check(test_file)
74+
self.assertTrue(any("banned word 'leverage'" in e for e in errs))
75+
self.assertTrue(any("banned word 'robust'" in e for e in errs))
76+
self.assertTrue(any("banned phrase 'dive into'" in e for e in errs))
77+
78+
def test_check_proper_noun_collision(self):
79+
test_file = self.tmp / "proper_noun.md"
80+
test_file.write_text(
81+
"According to Foster, we should foster clean architecture.\n"
82+
)
83+
errs = check_prose.check(test_file)
84+
# Capitalized 'Foster' is exempt, but lowercase 'foster' triggers an error.
85+
foster_errs = [e for e in errs if "banned word 'foster'" in e]
86+
self.assertEqual(len(foster_errs), 1)
87+
88+
def test_check_banned_characters_and_emojis(self):
89+
test_file = self.tmp / "chars.md"
90+
test_file.write_text(
91+
"Here is an em dash—and an en dash–and ellipsis… 😀\n"
92+
)
93+
errs = check_prose.check(test_file)
94+
self.assertTrue(any("em dash" in e for e in errs))
95+
self.assertTrue(any("en dash" in e for e in errs))
96+
self.assertTrue(any("ellipsis character" in e for e in errs))
97+
self.assertTrue(any("emoji" in e for e in errs))
98+
99+
def test_check_triple_dash_separator(self):
100+
test_file = self.tmp / "separator.md"
101+
test_file.write_text(
102+
"Section 1\n\n---\n\nSection 2\n"
103+
)
104+
errs = check_prose.check(test_file)
105+
self.assertTrue(any("triple-dash used as separator" in e for e in errs))
106+
107+
def test_collect(self):
108+
files = check_prose.collect()
109+
self.assertIsInstance(files, list)
110+
self.assertGreater(len(files), 0)
111+
self.assertTrue(all(f.suffix == ".md" for f in files))
112+
113+
114+
if __name__ == "__main__":
115+
unittest.main()

‎tools/gen-indexes-test.py‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env python3
2+
"""Unit tests for tools/gen-indexes.py family README index generator."""
3+
4+
from __future__ import annotations
5+
6+
import importlib.util
7+
import json
8+
import sys
9+
import tempfile
10+
import unittest
11+
from pathlib import Path
12+
13+
TOOLS = Path(__file__).resolve().parent
14+
sys.path.insert(0, str(TOOLS))
15+
16+
spec = importlib.util.spec_from_file_location(
17+
"gen_indexes", TOOLS / "gen-indexes.py"
18+
)
19+
gen_indexes = importlib.util.module_from_spec(spec)
20+
spec.loader.exec_module(gen_indexes)
21+
22+
23+
class TestGenIndexes(unittest.TestCase):
24+
def setUp(self):
25+
self.td = tempfile.TemporaryDirectory()
26+
self.tmp = Path(self.td.name)
27+
28+
def tearDown(self):
29+
self.td.cleanup()
30+
31+
def test_field_parsing(self):
32+
block = 'name: "Strategy Pattern"\ncategory: Behavioral\nmaturity: canonical'
33+
self.assertEqual(gen_indexes.field(block, "name"), "Strategy Pattern")
34+
self.assertEqual(gen_indexes.field(block, "category"), "Behavioral")
35+
self.assertEqual(gen_indexes.field(block, "maturity"), "canonical")
36+
self.assertEqual(gen_indexes.field(block, "missing", "default_val"), "default_val")
37+
38+
def test_first_intent_extraction_and_truncation(self):
39+
sample_entry = (
40+
"---\nname: Test Pattern\n---\n"
41+
"## 1. Name, aliases, and lineage\n"
42+
"Lineage text here.\n\n"
43+
"## 2. Problem and context\n"
44+
"This is the core problem statement that needs to be extracted as intent. "
45+
"It explains why the pattern exists and when to use it in practice. "
46+
"Additional sentence to make this summary long enough for truncation testing when exceeding max length limit.\n\n"
47+
"## 3. Forces\n"
48+
"Forces text.\n"
49+
)
50+
intent = gen_indexes.first_intent(sample_entry)
51+
self.assertTrue(intent.startswith("This is the core problem statement"))
52+
self.assertLessEqual(len(intent), 183) # 180 chars + " ..."
53+
54+
def test_first_intent_missing_section_2(self):
55+
sample_entry = "---\nname: Test Pattern\n---\n## 1. Name\nNo section 2 here.\n"
56+
intent = gen_indexes.first_intent(sample_entry)
57+
self.assertEqual(intent, "")
58+
59+
def test_load_planned(self):
60+
queue_file = self.tmp / "AUTHORING-QUEUE.json"
61+
queue_data = [
62+
{"path": "patterns/01-design-patterns-gof/nonexistent-queued-test-pattern.md", "name": "Test Pattern", "reason": "Test reason"},
63+
{"path": "patterns/01-design-patterns-gof/deferred-item.md", "name": "Deferred", "status": "deferred"},
64+
{"path": "invalid-path/item.md", "name": "Invalid"},
65+
]
66+
queue_file.write_text(json.dumps(queue_data), encoding="utf-8")
67+
68+
original_queue = gen_indexes.QUEUE
69+
gen_indexes.QUEUE = queue_file
70+
try:
71+
planned = gen_indexes.load_planned()
72+
self.assertIn("01-design-patterns-gof", planned)
73+
self.assertEqual(planned["01-design-patterns-gof"], [("Test Pattern", "Test reason")])
74+
finally:
75+
gen_indexes.QUEUE = original_queue
76+
77+
def test_build_index(self):
78+
family_dir = self.tmp / "patterns" / "01-design-patterns-gof"
79+
family_dir.mkdir(parents=True)
80+
81+
entry1 = family_dir / "strategy.md"
82+
entry1.write_text(
83+
"---\n"
84+
"name: Strategy\n"
85+
"category: Behavioral\n"
86+
"maturity: canonical\n"
87+
"---\n"
88+
"## 1. Name\nStrategy.\n\n"
89+
"## 2. Problem and context\nDefine a family of algorithms.\n\n"
90+
"## 3. Forces\nFlexibility.\n",
91+
encoding="utf-8",
92+
)
93+
94+
planned_items = [("Visitor", "Classic pattern")]
95+
count = gen_indexes.build(family_dir, planned_items)
96+
self.assertEqual(count, 1)
97+
98+
readme = family_dir / "README.md"
99+
self.assertTrue(readme.exists())
100+
content = readme.read_text(encoding="utf-8")
101+
self.assertIn("# Family 01. Design Patterns", content)
102+
self.assertIn("## Behavioral", content)
103+
self.assertIn("| [Strategy](strategy.md) | canonical |", content)
104+
self.assertIn("## Planned", content)
105+
self.assertIn("- Visitor. Classic pattern", content)
106+
107+
def test_build_empty_family(self):
108+
family_dir = self.tmp / "patterns" / "99-empty"
109+
family_dir.mkdir(parents=True)
110+
count = gen_indexes.build(family_dir, [])
111+
self.assertEqual(count, 0)
112+
self.assertFalse((family_dir / "README.md").exists())
113+
114+
115+
if __name__ == "__main__":
116+
unittest.main()

0 commit comments

Comments
 (0)