116 lines
4.1 KiB
Python
116 lines
4.1 KiB
Python
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from failure import Failure
|
|
from markdown_section import main
|
|
|
|
TOOL = pathlib.Path(__file__).resolve().parents[1] / "markdown-section"
|
|
|
|
DOCUMENT = """# Title
|
|
|
|
Intro.
|
|
|
|
## Alpha
|
|
|
|
Alpha text.
|
|
|
|
### Alpha detail
|
|
|
|
Detail text.
|
|
|
|
## Beta Gamma
|
|
|
|
Beta text.
|
|
|
|
```
|
|
## Not a heading
|
|
```
|
|
|
|
## Delta
|
|
|
|
Delta text.
|
|
"""
|
|
|
|
|
|
class Section(unittest.TestCase):
|
|
def test_the_fragment_is_matched_against_the_heading_text_without_its_hashes(self):
|
|
self.assertEqual(main.section(DOCUMENT, "Alpha text"), Failure("no heading holds 'Alpha text'"))
|
|
self.assertEqual(main.section(DOCUMENT, "## beta"), Failure("no heading holds '## beta'"))
|
|
|
|
def test_holds_its_sub_sections(self):
|
|
self.assertEqual(main.section(DOCUMENT, "Alpha"), "## Alpha\n\nAlpha text.\n\n### Alpha detail\n\nDetail text.\n")
|
|
|
|
def test_a_sub_section_stops_at_a_higher_level_heading(self):
|
|
self.assertEqual(main.section(DOCUMENT, "alpha detail"), "### Alpha detail\n\nDetail text.\n")
|
|
|
|
def test_the_fragment_matches_any_part_of_the_heading_whatever_its_case(self):
|
|
self.assertTrue(str(main.section(DOCUMENT, "gamma")).startswith("## Beta Gamma\n"))
|
|
|
|
def test_a_fenced_line_that_looks_like_a_heading_neither_ends_nor_matches(self):
|
|
self.assertEqual(main.section(DOCUMENT, "Beta"), "## Beta Gamma\n\nBeta text.\n\n```\n## Not a heading\n```\n")
|
|
self.assertEqual(main.section(DOCUMENT, "not a heading"), Failure("no heading holds 'not a heading'"))
|
|
|
|
def test_the_last_section_runs_to_the_end_of_the_file(self):
|
|
self.assertEqual(main.section(DOCUMENT, "delta"), "## Delta\n\nDelta text.\n")
|
|
|
|
def test_a_level_one_heading_holds_the_rest_of_the_document(self):
|
|
self.assertEqual(main.section(DOCUMENT, "title"), DOCUMENT)
|
|
|
|
def test_several_matching_headings_fail_listing_each_with_its_line(self):
|
|
listed = main.section("## Plan one\n\ntext\n\n## Plan two\n", "plan")
|
|
self.assertEqual(listed, Failure("2 headings hold 'plan', so use more of one:\n1: ## Plan one\n5: ## Plan two"))
|
|
|
|
def test_a_heading_needs_a_space_after_its_hashes(self):
|
|
self.assertEqual(main.section("#hashtag\n\n## Real\n", "hashtag"), Failure("no heading holds 'hashtag'"))
|
|
|
|
def test_no_heading_is_a_failure(self):
|
|
self.assertEqual(main.section(DOCUMENT, "zeta"), Failure("no heading holds 'zeta'"))
|
|
|
|
|
|
class BlackBox(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.file = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "notes.md"
|
|
self.file.write_text(DOCUMENT)
|
|
|
|
def run_tool(self, *args: str) -> subprocess.CompletedProcess[str]:
|
|
return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, text=True)
|
|
|
|
def test_prints_the_section(self):
|
|
ran = self.run_tool(str(self.file), "delta")
|
|
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, "## Delta\n\nDelta text.\n", ""))
|
|
|
|
def test_exits_1_saying_why_where_no_heading_matches(self):
|
|
ran = self.run_tool(str(self.file), "zeta")
|
|
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (1, "", "markdown-section: no heading holds 'zeta'\n"))
|
|
|
|
def test_exits_1_listing_the_headings_where_several_match(self):
|
|
ran = self.run_tool(str(self.file), "a")
|
|
self.assertEqual((ran.returncode, ran.stdout), (1, ""))
|
|
self.assertIn("headings hold 'a'", ran.stderr)
|
|
self.assertIn("5: ## Alpha", ran.stderr)
|
|
|
|
def test_exits_1_where_the_file_cant_be_read(self):
|
|
ran = self.run_tool(str(self.file.with_name("missing.md")), "alpha")
|
|
self.assertEqual((ran.returncode, ran.stdout), (1, ""))
|
|
self.assertIn("markdown-section: ", ran.stderr)
|
|
|
|
def test_exits_2_with_the_usage_on_wrong_arguments(self):
|
|
for args in ([], [str(self.file)], [str(self.file), "a", "b"]):
|
|
ran = self.run_tool(*args)
|
|
self.assertEqual((ran.returncode, ran.stdout), (2, ""), args)
|
|
self.assertIn("usage: markdown-section <file> <fragment>", ran.stderr)
|
|
|
|
def test_help_prints_the_usage(self):
|
|
ran = self.run_tool("--help")
|
|
self.assertEqual(ran.returncode, 0)
|
|
self.assertIn("usage: markdown-section <file> <fragment>", ran.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|