Files
claude-config/bin/task_bench/test_plant.py
T
2026-11-23 21:31:00 +01:00

98 lines
4.3 KiB
Python

"""task-bench's planter: which files it mutates, which language's tool finds the mutants of each, which mutants it draws and whose test output it reads."""
import pathlib
import sys
import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from task_bench import plant
from task_bench.plant import drawn, killed_mutants, Language, LANGUAGES, place_files, task_text
from task_bench.planted_mutant import Mutant
from failure import Failure
PLACES = """## 2026-11-19T22:33:15Z, PR #646 at a357cc7b
Junior A, hardest first:
- Locality, Shape: `internal/render/pick.go:287` `parked`: three steps held at once
- Shape: `internal/render/pick_test.go:12` `TestPick`: a long table
- Self-sufficiency: `README.md:89` "Round cap": three sections
- Locality: `cli/report.go:41` the comment: points to `cli/gone.go`
Mid A, hardest first:
- Locality: `internal/render/pick.go:354` `resumed`: a loop
- Locality: `data-import/geo.py:3` `load`: a long chain
"""
def mutant(path: str, line: int = 1) -> Mutant:
return Mutant(path, line, f"{path}:{line} x", (b"a + b",), (b"a - b",))
class PlaceFilesTest(unittest.TestCase):
def test_names_each_go_and_python_file_that_holds_a_place_once_and_no_test_file_or_other_language(self):
self.assertEqual(place_files(PLACES), ["cli/report.go", "data-import/geo.py", "internal/render/pick.go"])
def test_names_none_where_the_seats_found_no_places(self):
self.assertEqual(place_files("Seats found no places hard."), [])
self.assertEqual(place_files("- `tests/test_geo.py:3` `load`\n- `geo_test.py:3` `load`\n- `notes.pyc:1` `x`\n"), [])
class DrawnTest(unittest.TestCase):
MUTANTS = [mutant("a.go", line) for line in range(1, 40)]
def test_a_seed_draws_the_same_order_whatever_order_the_tool_listed_the_mutants_in(self):
self.assertEqual(drawn(self.MUTANTS, 7), drawn(self.MUTANTS[::-1], 7))
def test_draws_every_mutant_once(self):
self.assertEqual(sorted(drawn(self.MUTANTS + self.MUTANTS[:3], 7)), self.MUTANTS)
def test_another_seed_draws_another_order(self):
self.assertNotEqual(drawn(self.MUTANTS, 7), drawn(self.MUTANTS, 8))
class LanguagesTest(unittest.TestCase):
def setUp(self):
self.asked: list[tuple] = []
self.found: dict[str, list[Mutant] | Failure] = {"Go": [mutant("b.go"), mutant("a.go")], "Python": [mutant("c.py")]}
languages = tuple(Language(name, suffixes, self.killed(name), f"{name} read {{}}".format) for name, suffixes in (("Go", (".go",)), ("Python", (".py", ".pyi"))))
patched = unittest.mock.patch.object(plant, "LANGUAGES", languages)
patched.start()
self.addCleanup(patched.stop)
def killed(self, name: str):
def killed(top: str, files: list[str], run, jobs: int) -> list[Mutant] | Failure:
self.asked.append((name, top, files, run, jobs))
return self.found[name]
return killed
def test_each_languages_tool_finds_the_mutants_of_the_files_in_that_language(self):
self.assertEqual(killed_mutants("/top", ["a.go", "c.py", "b.go", "d.pyi"], "run", 3), [mutant("b.go"), mutant("a.go"), mutant("c.py")])
self.assertEqual(self.asked, [("Go", "/top", ["a.go", "b.go"], "run", 3), ("Python", "/top", ["c.py", "d.pyi"], "run", 3)])
def test_a_language_without_a_file_runs_no_tool(self):
self.assertEqual(killed_mutants("/top", ["c.py"], "run", 3), [mutant("c.py")])
self.assertEqual([name for name, *_ in self.asked], ["Python"])
def test_a_tool_that_fails_fails_the_whole(self):
self.found["Go"] = Failure("gremlins broke")
self.assertEqual(killed_mutants("/top", ["a.go", "c.py"], "run", 3), Failure("gremlins broke"))
def test_the_language_of_the_bugs_file_reads_the_test_commands_output(self):
self.assertEqual((task_text("pkg/a.go", "output"), task_text("pkg/c.pyi", "output")), ("Go read output", "Python read output"))
self.assertIsNone(task_text("README.md", "output"))
class ShippedLanguagesTest(unittest.TestCase):
def test_go_reads_go_tests_output_and_python_pytests(self):
self.assertEqual([(language.name, language.suffixes) for language in LANGUAGES], [("Go", (".go",)), ("Python", (".py",))])
self.assertEqual(task_text("calc.go", "--- FAIL: TestPick (0.00s)\n pick_test.go:42: got 3, want 4\nFAIL\n"), "got 3, want 4")
self.assertEqual(task_text("calc.py", "==== FAILURES ====\n___ test_pick ___\nE assert 3 == 4\n"), "assert 3 == 4")
if __name__ == "__main__":
unittest.main()