Kill markdown-section's help and end-of-section mutants
This commit is contained in:
@@ -56,8 +56,7 @@ def section(text: str, fragment: str) -> str | Failure:
|
||||
found = headings(text)
|
||||
wanted = fragment.strip().lower()
|
||||
matching = [heading for heading in found if wanted in heading[2].lower()]
|
||||
if len(matching) > 1:
|
||||
matching = [heading for heading in matching if heading[2].lower() == wanted] or matching
|
||||
matching = [heading for heading in matching if heading[2].lower() == wanted] or matching
|
||||
if not matching:
|
||||
return Failure(f"no heading holds '{fragment}'")
|
||||
lines = text.splitlines()
|
||||
@@ -65,5 +64,5 @@ def section(text: str, fragment: str) -> str | Failure:
|
||||
listed = "\n".join(f"{index + 1}: {lines[index]}" for index, _, _ in matching)
|
||||
return Failure(f"{len(matching)} headings hold '{fragment}', so use more of one:\n{listed}")
|
||||
start, level, _ = matching[0]
|
||||
end = next((index for index, deeper, _ in found if index > start and deeper <= level), len(lines))
|
||||
end = next((index for index, deeper, _ in found if index > start and deeper <= level), None)
|
||||
return "\n".join(lines[start:end]).rstrip("\n") + "\n"
|
||||
|
||||
@@ -118,10 +118,12 @@ class Main(unittest.TestCase):
|
||||
with self.subTest(args):
|
||||
self.assertEqual(self.run_main(*args), (2, "", "usage: markdown-section <file> <fragment>\n"))
|
||||
|
||||
def test_help_prints_the_usage_and_exits_0(self):
|
||||
code, out, err = self.run_main("-h")
|
||||
self.assertEqual((code, err), (0, ""))
|
||||
self.assertTrue(out.startswith("Print one section of a markdown file"))
|
||||
def test_help_prints_the_usage_and_exits_0_whatever_follows_it(self):
|
||||
for args in (("-h",), ("--help",), ("--help", "x")):
|
||||
with self.subTest(args):
|
||||
code, out, err = self.run_main(*args)
|
||||
self.assertEqual((code, err), (0, ""))
|
||||
self.assertTrue(out.startswith("Print one section of a markdown file"))
|
||||
|
||||
|
||||
class Launcher(unittest.TestCase):
|
||||
|
||||
Reference in New Issue
Block a user