Files
claude-config/bin/test_rank_todo.py
T

126 lines
4.6 KiB
Python

import contextlib
import io
import pathlib
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
import rank_todo
from test_python_floor import ROOT
README = "# P\n\n## Goals\n\n1. Fast.\n2. Correct.\n 1. Tested.\n\n## Audience\n\nMe.\n"
TODO = """# todo
## Scoring
`Score = -R - S/4 + 2*A + 2*G*W`
`Bar = 9`
| Goal | W |
|---|---|
| 1 | 1.00 |
| 2 | 0.67 |
| 2.1 | 0.33 |
## Items
| ID | Release | Exempt | Item | R | S | A | G | Goals | Score |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0.2.0 | | **Ship the later thing.** | 1 | 1 | 9 | 9 | 1 | 0 |
| 2 | 0.1.0 | | **Tighten the tests.** | 2 | 4 | 5 | 5 | 2.1 | 0 |
| 3 | 0.1.0 | | **Make it fast, `a \| b`.** | 3 | 8 | 6 | 8 | 2.1, 1 | 0 |
| 4 | 0.1.0 | security | **Rotate the leaked key.** | 9 | 2 | 1 | 1 | 2.1 | 0 |
## Details
### 1. stale heading
Later detail.
### 3. Make it fast.
Fast detail.
## Notes
Kept as written.
"""
class Rank(unittest.TestCase):
def ranked(self, todo=TODO, readme=README):
return rank_todo.rank(todo, rank_todo.readme_goals(readme))
def test_scores_and_sorts_by_release_then_score_and_orders_the_details_to_match(self):
text, under_bar = self.ranked()
self.assertIn("""| 4 | 0.1.0 | security | **Rotate the leaked key.** | 9 | 2 | 1 | 1 | 2.1 | -6.8 |
| 3 | 0.1.0 | | **Make it fast, `a \\| b`.** | 3 | 8 | 6 | 8 | 2.1, 1 | 23.0 |
| 2 | 0.1.0 | | **Tighten the tests.** | 2 | 4 | 5 | 5 | 2.1 | 10.3 |
| 1 | 0.2.0 | | **Ship the later thing.** | 1 | 1 | 9 | 9 | 1 | 34.8 |
""", text)
self.assertTrue(text.endswith("## Details\n\n### 3. Make it fast, `a \\| b`.\n\nFast detail.\n\n### 1. Ship the later thing.\n\nLater detail.\n\n## Notes\n\nKept as written.\n"))
self.assertEqual(under_bar, [])
self.assertEqual(self.ranked(text), (text, []))
def test_names_each_item_under_the_bar(self):
_, under_bar = self.ranked(TODO.replace("`Bar = 9`", "`Bar = 11`"))
self.assertEqual(under_bar, ["item 2 scores 10.3, under the bar 11; rescore it or delete it"])
def test_reads_goals_and_sub_goals_in_reading_order(self):
self.assertEqual(rank_todo.readme_goals(README), ["1", "2", "2.1"])
self.assertEqual(rank_todo.readme_goals(README.replace(" 1.", "\t1.")), ["1", "2", "2.1"])
with self.assertRaises(rank_todo.Malformed):
rank_todo.readme_goals(README.replace("2. Correct.", "1. Correct."))
def test_prints_the_positional_weights_when_they_stop_listing_the_readmes_goals(self):
with self.assertRaises(rank_todo.Malformed) as raised:
self.ranked(readme=README.replace(" 1. Tested.", " 1. Tested.\n3. Small."))
self.assertIn("| 1 | 1.00 |\n| 2 | 0.75 |\n| 2.1 | 0.50 |\n| 3 | 0.25 |", str(raised.exception))
def test_refuses_a_formula_that_is_more_than_arithmetic_over_the_scores(self):
for formula in ("__import__('os')", "R.real", "X + 1", "R ** 2", "R +"):
with self.subTest(formula=formula), self.assertRaises(rank_todo.Malformed):
self.ranked(TODO.replace("-R - S/4 + 2*A + 2*G*W", formula))
def test_refuses_a_row_or_detail_off_the_format(self):
row = "| 2 | 0.1.0 | | **Tighten the tests.** | 2 | 4 | 5 | 5 | 2.1 | 0 |"
broken = {
row: row.replace("0.1.0", "0.1"),
row + "\n": row.replace("5 | 5", "5 | ²") + "\n",
"| 2.1 | 0 |\n\n## Details": "| 2.1 | 0 |\n\n| 9 | 0.1.0 | | **Stray.** | 1 | 1 | 1 | 1 | 1 | 0 |\n\n## Details",
"| 4 | 0.1.0 | security |": "| 4 | 0.1.0 | later |",
"Fast detail.": "```\n## Goals\n```",
"### 3. Make it fast.": "### 5. Make it fast.",
"`Bar = 9`": "`Bar = nan`",
"-R - S/4 + 2*A + 2*G*W": "R / (S - 2)",
}
for old, new in broken.items():
with self.subTest(new=new), self.assertRaises(rank_todo.Malformed):
self.ranked(TODO.replace(old, new))
class Main(unittest.TestCase):
def test_ranks_the_file_in_place_and_check_writes_nothing(self):
with tempfile.TemporaryDirectory() as temp:
todo = pathlib.Path(temp, "todo.md")
todo.write_text(TODO)
pathlib.Path(temp, "README.md").write_text(README)
self.assertEqual(rank_todo.main(["--check", str(todo)]), 1)
self.assertEqual(todo.read_text(), TODO)
self.assertEqual(rank_todo.main([str(todo)]), 0)
self.assertEqual(rank_todo.main(["--check", str(todo)]), 0)
pathlib.Path(temp, "README.md").unlink()
with contextlib.redirect_stderr(io.StringIO()) as said:
self.assertEqual(rank_todo.main([str(todo)]), 1)
self.assertIn("README.md", said.getvalue())
class ThisRepo(unittest.TestCase):
def test_todo_md_is_ranked_with_nothing_under_the_bar(self):
text = (ROOT / "todo.md").read_text(encoding="utf-8")
self.assertEqual(rank_todo.rank(text, rank_todo.readme_goals((ROOT / "README.md").read_text(encoding="utf-8"))), (text, []))