Files
claude-config/bin/text_files/test_markdown_text.py
T

44 lines
2.1 KiB
Python

import pathlib
import sys
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from text_files import markdown_text
class Sections(unittest.TestCase):
def test_keeps_every_headed_section_with_its_bodies(self):
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority\n\n## Empty\n\n## Audience\nme\n## Audience\nagain\n# Appendix\nnot audience\n"
self.assertEqual(markdown_text.sections(text), {"Goals": ["1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority"], "Empty": [""], "Audience": ["me", "again"]})
def test_closes_a_fence_only_on_its_own_marker(self):
text = "## Goals\n````md\n~~~\n## Quoted\n```\n````\n ~~~\n## Also quoted\n ~~~\n1. one\n## Audience\nme\n"
self.assertEqual(list(markdown_text.sections(text)), ["Goals", "Audience"])
self.assertEqual(list(markdown_text.sections("```\nquoted:\n```python\n## Inside\n```\n## Goals\n1. one\n")), ["Goals"])
def test_quotes_every_line_of_a_passage_blank_ones_included(self):
self.assertEqual(markdown_text.quoted("merged\n\nPR #1"), "> merged\n> \n> PR #1")
class Section(unittest.TestCase):
def test_holds_deeper_headings_and_fenced_lines_and_ends_at_its_own_level(self):
text = "# P\n\n## Scoring run\n\na\n\n### Sub\n\n```\n## fenced\n```\n\n## Next\n\nb\n"
self.assertEqual(markdown_text.section(text, "Scoring run"), ["", "a", "", "### Sub", "", "```", "## fenced", "```", ""])
self.assertIsNone(markdown_text.section(text, "Missing"))
def test_a_fence_closes_only_on_its_own_marker_here_too(self):
text = "## A\n````md\n```\n## Quoted\n````\n## B\n"
self.assertEqual(markdown_text.section(text, "A"), ["````md", "```", "## Quoted", "````"])
class Listed(unittest.TestCase):
def test_joins_names_with_commas_and_the_conjunction_before_the_last(self):
self.assertEqual(markdown_text.listed(["a"], "and"), "a")
self.assertEqual(markdown_text.listed(["a", "b"], "and"), "a and b")
self.assertEqual(markdown_text.listed(("a", "b", "c"), "or"), "a, b or c")
if __name__ == "__main__":
unittest.main()