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
Parse-test every problems/*.lean file through the agent's pure-Python
|
| 3 |
+
preprocessing helpers in src/langgraph_agent.py.
|
| 4 |
+
|
| 5 |
+
No Lean install required - these tests only exercise text-level helpers:
|
| 6 |
+
- UTF-8 load
|
| 7 |
+
- _count_theorem_blocks
|
| 8 |
+
- _sanitize_imports (must preserve theorem block count)
|
| 9 |
+
- _extract_lean_code (idempotent on raw .lean files with no fences)
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import sys
|
| 14 |
+
import unittest
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
|
| 17 |
+
# Add src to Python path
|
| 18 |
+
sys.path.insert(0, 'src')
|
| 19 |
+
# Also support running from repo root regardless of cwd.
|
| 20 |
+
_REPO_ROOT = Path(__file__).resolve().parent.parent
|
| 21 |
+
sys.path.insert(0, str(_REPO_ROOT / "src"))
|
| 22 |
+
|
| 23 |
+
from langgraph_agent import ( # noqa: E402
|
| 24 |
+
_count_theorem_blocks,
|
| 25 |
+
_extract_lean_code,
|
| 26 |
+
_sanitize_imports,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
_PROBLEMS_DIR = _REPO_ROOT / "problems"
|
| 30 |
+
|
| 31 |
+
_EXPECTED_FILES = [
|
| 32 |
+
"simple_add.lean",
|
| 33 |
+
"test_problem.lean",
|
| 34 |
+
"lecture4_problems.lean",
|
| 35 |
+
"lecture5_problems.lean",
|
| 36 |
+
"lecture6_problems.lean",
|
| 37 |
+
"lecture7_problems.lean",
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _existing_problem_files():
|
| 42 |
+
"""Return the list of problem files that actually exist on disk."""
|
| 43 |
+
files = []
|
| 44 |
+
for name in _EXPECTED_FILES:
|
| 45 |
+
p = _PROBLEMS_DIR / name
|
| 46 |
+
if p.is_file():
|
| 47 |
+
files.append(p)
|
| 48 |
+
# Also pick up anything else in problems/*.lean we don't know about.
|
| 49 |
+
for p in sorted(_PROBLEMS_DIR.glob("*.lean")):
|
| 50 |
+
if p not in files:
|
| 51 |
+
files.append(p)
|
| 52 |
+
return files
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class TestProblemsParse(unittest.TestCase):
|
| 56 |
+
"""One subTest per file per assertion - failures clearly identify the file."""
|
| 57 |
+
|
| 58 |
+
@classmethod
|
| 59 |
+
def setUpClass(cls):
|
| 60 |
+
cls.files = _existing_problem_files()
|
| 61 |
+
|
| 62 |
+
def test_problems_dir_exists(self):
|
| 63 |
+
self.assertTrue(
|
| 64 |
+
_PROBLEMS_DIR.is_dir(),
|
| 65 |
+
f"problems/ directory not found at {_PROBLEMS_DIR}",
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
def test_utf8_load(self):
|
| 69 |
+
"""Each .lean file must be readable as UTF-8."""
|
| 70 |
+
for path in self.files:
|
| 71 |
+
with self.subTest(file=path.name):
|
| 72 |
+
# Must not raise.
|
| 73 |
+
content = path.read_text(encoding="utf-8")
|
| 74 |
+
self.assertIsInstance(content, str)
|
| 75 |
+
|
| 76 |
+
def test_theorem_block_count_positive(self):
|
| 77 |
+
"""_count_theorem_blocks must return >= 1 for every problem file."""
|
| 78 |
+
for path in self.files:
|
| 79 |
+
with self.subTest(file=path.name):
|
| 80 |
+
content = path.read_text(encoding="utf-8")
|
| 81 |
+
count = _count_theorem_blocks(content)
|
| 82 |
+
self.assertGreaterEqual(
|
| 83 |
+
count,
|
| 84 |
+
1,
|
| 85 |
+
f"{path.name}: expected >= 1 theorem block, got {count}",
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
def test_sanitize_imports_preserves_theorem_count(self):
|
| 89 |
+
"""_sanitize_imports must not drop any theorem/example/lemma/def statements."""
|
| 90 |
+
for path in self.files:
|
| 91 |
+
with self.subTest(file=path.name):
|
| 92 |
+
content = path.read_text(encoding="utf-8")
|
| 93 |
+
original = _count_theorem_blocks(content)
|
| 94 |
+
sanitized = _sanitize_imports(content)
|
| 95 |
+
after = _count_theorem_blocks(sanitized)
|
| 96 |
+
self.assertEqual(
|
| 97 |
+
after,
|
| 98 |
+
original,
|
| 99 |
+
f"{path.name}: _sanitize_imports changed block count "
|
| 100 |
+
f"({original} -> {after})",
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
def test_extract_lean_code_idempotent_on_raw_files(self):
|
| 104 |
+
"""Raw .lean files have no code fences, so _extract_lean_code(content)
|
| 105 |
+
should fall through to content.strip()."""
|
| 106 |
+
for path in self.files:
|
| 107 |
+
with self.subTest(file=path.name):
|
| 108 |
+
content = path.read_text(encoding="utf-8")
|
| 109 |
+
# Raw .lean files should not contain markdown code fences.
|
| 110 |
+
self.assertNotIn(
|
| 111 |
+
"```",
|
| 112 |
+
content,
|
| 113 |
+
f"{path.name}: unexpected ``` fence in raw .lean file",
|
| 114 |
+
)
|
| 115 |
+
extracted = _extract_lean_code(content)
|
| 116 |
+
self.assertEqual(
|
| 117 |
+
extracted,
|
| 118 |
+
content.strip(),
|
| 119 |
+
f"{path.name}: _extract_lean_code not idempotent on raw file",
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# Skip individual expected files if missing (defensive — they all exist today).
|
| 124 |
+
for _name in _EXPECTED_FILES:
|
| 125 |
+
_path = _PROBLEMS_DIR / _name
|
| 126 |
+
|
| 127 |
+
def _make_test(p):
|
| 128 |
+
@unittest.skipUnless(p.is_file(), f"{p.name} not present")
|
| 129 |
+
def _test(self):
|
| 130 |
+
content = p.read_text(encoding="utf-8")
|
| 131 |
+
self.assertGreaterEqual(_count_theorem_blocks(content), 1)
|
| 132 |
+
self.assertEqual(
|
| 133 |
+
_count_theorem_blocks(_sanitize_imports(content)),
|
| 134 |
+
_count_theorem_blocks(content),
|
| 135 |
+
)
|
| 136 |
+
self.assertEqual(_extract_lean_code(content), content.strip())
|
| 137 |
+
return _test
|
| 138 |
+
|
| 139 |
+
setattr(
|
| 140 |
+
TestProblemsParse,
|
| 141 |
+
f"test_file_{_name.replace('.', '_')}",
|
| 142 |
+
_make_test(_path),
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
unittest.main()
|