Test markdown-section's main in process, and the end of a section
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import contextlib
|
||||
import io
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -65,6 +67,10 @@ class Section(unittest.TestCase):
|
||||
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_only_blank_lines_are_cut_from_the_end_of_a_section(self):
|
||||
self.assertEqual(main.section("## A\n\nends in X\n\n\n## B\n", "a"), "## A\n\nends in X\n")
|
||||
self.assertEqual(main.section("## A\n\ncode \n\n", "a"), "## A\n\ncode \n")
|
||||
|
||||
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'"))
|
||||
|
||||
@@ -72,6 +78,38 @@ class Section(unittest.TestCase):
|
||||
self.assertEqual(main.section(DOCUMENT, "zeta"), Failure("no heading holds 'zeta'"))
|
||||
|
||||
|
||||
class Main(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.file = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "notes.md"
|
||||
self.file.write_text(DOCUMENT)
|
||||
|
||||
def run_main(self, *args: str) -> tuple[int, str, str]:
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
code = main.main(list(args))
|
||||
return code, out.getvalue(), err.getvalue()
|
||||
|
||||
def test_prints_the_section(self):
|
||||
self.assertEqual(self.run_main(str(self.file), "delta"), (0, "## Delta\n\nDelta text.\n", ""))
|
||||
|
||||
def test_a_failure_is_printed_to_stderr_with_exit_1(self):
|
||||
self.assertEqual(self.run_main(str(self.file), "zeta"), (1, "", "markdown-section: no heading holds 'zeta'\n"))
|
||||
|
||||
def test_an_unreadable_file_exits_1_naming_it(self):
|
||||
code, out, err = self.run_main(str(self.file.with_name("missing.md")), "a")
|
||||
self.assertEqual((code, out), (1, ""))
|
||||
self.assertRegex(err, r"^markdown-section: .*missing\.md")
|
||||
|
||||
def test_wrong_arguments_exit_2_with_the_usage(self):
|
||||
for args in ((), (str(self.file),), (str(self.file), "a", "b")):
|
||||
self.assertEqual(self.run_main(*args), (2, "", "usage: markdown-section <file> <fragment>\n"), args)
|
||||
|
||||
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"))
|
||||
|
||||
|
||||
class BlackBox(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.file = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) / "notes.md"
|
||||
|
||||
Reference in New Issue
Block a user