Download tests/test_new_shapes.py from tailot/lingologos-thinkingpython: direct link, hf CLI and curl.
- Browser
- Download file 4.94 kB
-
https://huggingface.co/datasets/tailot/lingologos-thinkingpython/resolve/main/tests/test_new_shapes.py
- Command line
-
hf download hf://datasets/tailot/lingologos-thinkingpython/tests/test_new_shapes.py
-
curl -L -o test_new_shapes.py https://huggingface.co/datasets/tailot/lingologos-thinkingpython/resolve/main/tests/test_new_shapes.py
4.94 kB
| """Every catalog entry has a witness in the lab and a lock that survives the edit. | |
| The 13 shapes added after the first wave are exercised here one by one: the matcher | |
| finds exactly one witness, the edit pair applies, the generated lock test passes on | |
| the edited tree, and the same matcher reports NO_SHAPE afterwards. | |
| """ | |
| import os | |
| import shutil | |
| import subprocess | |
| import sys | |
| import tempfile | |
| import unittest | |
| ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| sys.path.insert(0, ROOT) | |
| from lingologos_thinkingpython import catalog, session, shapes # noqa: E402 | |
| LAB = os.path.join(ROOT, "fixtures", "lab") | |
| NEW_SHAPES = ["unused-local", "unreachable-code", "empty-except", "bare-except", | |
| "bool-prefix", "len-zero", "bool-compare", "dict-get", "redundant-list", | |
| "str-of-string", "open-context", "range-len", "dataclass-init"] | |
| def registry_source(directory: str) -> str: | |
| with open(os.path.join(directory, "registry.py"), "r", encoding="utf-8") as handle: | |
| return handle.read() | |
| class NewShapes(unittest.TestCase): | |
| def setUp(self): | |
| temp = tempfile.mkdtemp(prefix="newshapes_") | |
| self.temp = os.path.join(temp, "lab") | |
| shutil.copytree(LAB, self.temp) | |
| def tearDown(self): | |
| shutil.rmtree(os.path.dirname(self.temp), ignore_errors=True) | |
| def _plan(self, shape: str): | |
| source = registry_source(self.temp) | |
| hits, reason = shapes.match(shape, source) | |
| self.assertTrue(hits, "%s: no witness (%s)" % (shape, reason)) | |
| target = hits[0] | |
| edits = shapes.edits(shape, "registry.py", source, target) | |
| self.assertTrue(edits, "%s: empty edit" % shape) | |
| for edit in edits: | |
| self.assertIn(edit["search"], source, "%s: dishonest search" % shape) | |
| return {"frame": shape, "file": "registry.py", "line": target["line"], | |
| "end": target["end"], "symbol": target["symbol"], "edits": edits, | |
| "lock": shapes.lock(shape, target)}, hits, source | |
| def _lock_suite_is_green(self): | |
| proc = subprocess.run([sys.executable, "-m", "pytest", "-q", | |
| os.path.join(self.temp, "tests")], | |
| capture_output=True, text=True, timeout=300, | |
| cwd=self.temp) | |
| return proc.returncode, proc.stdout + proc.stderr | |
| def test_every_new_shape_is_in_the_catalog(self): | |
| entries = {entry["id"]: entry for entry in catalog.load_entries()} | |
| for shape in NEW_SHAPES: | |
| self.assertIn(shape, entries) | |
| entry = entries[shape] | |
| self.assertEqual(entry["shape"], shape) | |
| self.assertTrue(entry["effect"] and entry["lock"]) | |
| self.assertGreaterEqual(len(entry["refusals"]), 2) | |
| def test_every_catalog_entry_has_a_working_code_path(self): | |
| source = registry_source(self.temp) | |
| for shape in shapes.shapes(): | |
| hits, reason = shapes.match(shape, source) | |
| self.assertTrue(hits, "%s: the lab has no witness (%s)" % (shape, reason)) | |
| for hit in hits: | |
| self.assertTrue(shapes.lock(shape, hit), "%s: no lock text" % shape) | |
| edits = shapes.edits(shape, "registry.py", source, hit) | |
| self.assertTrue(edits, "%s: no edit for line %s" % (shape, hit["line"])) | |
| def test_edit_pair_applies_and_the_lock_holds(self): | |
| for shape in NEW_SHAPES: | |
| self.setUp() | |
| try: | |
| plan, hits, before_source = self._plan(shape) | |
| receipt = session.apply_pair(self.temp, plan) | |
| self.assertTrue(receipt["applied"], "%s: %s" % (shape, receipt)) | |
| code, log = self._lock_suite_is_green() | |
| self.assertEqual(code, 0, "%s: lock suite red\n%s" % (shape, log)) | |
| edited = registry_source(self.temp) | |
| self.assertNotEqual(edited, before_source, "%s: nothing was written" % shape) | |
| compile(edited, "registry.py", "exec") | |
| after, _ = shapes.match(shape, edited) | |
| self.assertLess(len(after), len(hits), | |
| "%s: the witness is still reported" % shape) | |
| finally: | |
| self.tearDown() | |
| def test_each_shape_matches_only_its_own_witness(self): | |
| source = registry_source(self.temp) | |
| for shape in NEW_SHAPES: | |
| hits, _ = shapes.match(shape, source) | |
| self.assertGreaterEqual(len(hits), 1, shape) | |
| self.assertTrue(shapes.lock(shape, hits[0]).startswith("def test_"), shape) | |
| def test_matchers_are_quiet_on_clean_code(self): | |
| for shape in NEW_SHAPES: | |
| hits, reason = shapes.match(shape, "def clean(value):\n return value\n") | |
| self.assertFalse(hits, "%s: %s" % (shape, hits)) | |
| self.assertEqual(reason, "NO_SHAPE", shape) | |
| if __name__ == "__main__": | |
| unittest.main() | |