Files
claude-config/bin/task_bench/test_plant.py
T

98 lines
4.9 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
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from task_bench.plant import drawn, killed_mutants, Language, LANGUAGES, place_files, task_text
from failure import Failure
from task_bench.planted_mutant import Mutant
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."), [])
def test_names_none_where_the_places_name_only_test_files_or_another_suffix(self):
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")]}
self.languages = tuple(Language(name, suffixes, self.killed(name), f"{name} read {{}}".format) for name, suffixes in (("Go", (".go",)), ("Python", (".py", ".pyi"))))
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, self.languages), [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, self.languages), [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, self.languages), 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", self.languages), task_text("pkg/c.pyi", "output", self.languages)), ("Go read output", "Python read output"))
self.assertIsNone(task_text("README.md", "output", self.languages))
class ShippedLanguagesTest(unittest.TestCase):
def test_each_language_reads_its_own_test_runners_output(self):
self.assertEqual([(language.name, language.suffixes) for language in LANGUAGES], [("Go", (".go",)), ("PHP", (".php",)), ("Python", (".py",)), ("Rust", (".rs",)), ("TypeScript", (".cjs", ".cts", ".js", ".jsx", ".mjs", ".mts", ".ts", ".tsx"))])
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")
self.assertEqual(task_text("Calc.php", "There was 1 failure:\n\n1) CalcTest::testPicks\ngot 3, want 4\n\n/w/tests/CalcTest.php:9\n"), "got 3, want 4")
self.assertEqual(task_text("calc.ts", "✖ failing tests:\n\ntest at calc.test.ts:5:1\n✖ picks (1.2ms)\n Error: got 3, want 4\n at pick (file:///w/calc.ts:9:5)\n"), "Error: got 3, want 4")
self.assertEqual(task_text("calc.rs", "---- picks stdout ----\nthread 'picks' (7) panicked at src/calc.rs:9:5:\ngot 3, want 4\n"), "got 3, want 4")
if __name__ == "__main__":
unittest.main()