From 13a803c440023900f2fbd9abfb30c3d59fb245a8 Mon Sep 17 00:00:00 2001 From: frcroth Date: Sun, 15 Oct 2023 11:59:25 +0200 Subject: [PATCH] Add markdown extension tests --- myhpi/core/markdown/extensions.py | 13 +-- myhpi/tests/test_markdown_extensions.py | 100 ++++++++++++++++++++++++ myhpi/tests/test_minutes_extensions.py | 29 ------- 3 files changed, 108 insertions(+), 34 deletions(-) create mode 100644 myhpi/tests/test_markdown_extensions.py delete mode 100644 myhpi/tests/test_minutes_extensions.py diff --git a/myhpi/core/markdown/extensions.py b/myhpi/core/markdown/extensions.py index 15ec98e9..f0392879 100644 --- a/myhpi/core/markdown/extensions.py +++ b/myhpi/core/markdown/extensions.py @@ -75,7 +75,7 @@ def breakify(self, match): ) -class QuorumPrepocessor(MinutesBasePreprocessor): +class QuorumPreprocessor(MinutesBasePreprocessor): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.patterns = [ @@ -138,11 +138,11 @@ class HeadingLevelPreprocessor(MinutesBasePreprocessor): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.patterns = [ - (r"^#{1,5}", self.decrease), + (r"^#{1,5} ", self.decrease), ] def decrease(self, match): - return f"{match.group(0)}#" + return f"#{match.group(0)}" class InternalLinkPattern(LinkInlineProcessor): @@ -158,6 +158,9 @@ def handleMatch(self, m, data=None): def url(self, id): return Page.objects.get(id=id).localized.get_url() + def default_pattern(): + return r"\[(?P[^\[]+)\]\(page:(?P<id>\d+)\)" + class ImagePattern(LinkInlineProcessor): def handleMatch(self, m, data=None): @@ -181,11 +184,11 @@ def extendMarkdown(self, md): md.preprocessors.register(VotePreprocessor(md), "votify", 200) md.preprocessors.register(StartEndPreprocessor(md), "start_or_endify", 200) md.preprocessors.register(BreakPreprocessor(md), "breakify", 200) - md.preprocessors.register(QuorumPrepocessor(md), "quorumify", 200) + md.preprocessors.register(QuorumPreprocessor(md), "quorumify", 200) md.preprocessors.register(EnterLeavePreprocessor(md), "enter_or_leavify", 200) md.preprocessors.register(HeadingLevelPreprocessor(md), "decrease", 200) md.inlinePatterns.register( - InternalLinkPattern(r"\[(?P<title>[^\[]+)\]\(page:(?P<id>\d+)\)", md), + InternalLinkPattern(InternalLinkPattern.default_pattern(), md), "InternalLinkPattern", 200, ) diff --git a/myhpi/tests/test_markdown_extensions.py b/myhpi/tests/test_markdown_extensions.py new file mode 100644 index 00000000..1a65e74f --- /dev/null +++ b/myhpi/tests/test_markdown_extensions.py @@ -0,0 +1,100 @@ +import django +from django.test import TestCase +from django.utils.translation import activate +from wagtail.core.models import Page + +django.setup() + +from myhpi.core.markdown.extensions import ( + BreakPreprocessor, + EnterLeavePreprocessor, + HeadingLevelPreprocessor, + InternalLinkPattern, + QuorumPreprocessor, + StartEndPreprocessor, + VotePreprocessor, +) + + +class TestMarkdownExtensions(TestCase): + def test_vote_preprocessor(self): + vp = VotePreprocessor() + text = ["[2|3|4]", "[a|b|c]"] # Second line is not processed + result = vp.run(text) + self.assertEqual(result, ["**[2|3|4]**", "[a|b|c]"]) + + def test_start_end_preprocessor(self): + activate("en") + sep = StartEndPreprocessor() + text = ["|start|(12:00)", "|end|(16:00)"] + result = sep.run(text) + self.assertEqual(result, ["*Begin of meeting: 12:00* ", "*End of meeting: 16:00* "]) + + def test_break_preprocessor(self): + activate("en") + bp = BreakPreprocessor() + text = ["|break|(12:00)(13:00)"] + result = bp.run(text) + self.assertEqual(result, ["*Meeting break: 12:00 – 13:00*"]) + + def test_quorum_preprocessor(self): + activate("en") + qp = QuorumPreprocessor() + text = ["|quorum|(3/8)", "|quorum|(4/8)"] + result = qp.run(text) + self.assertEqual(result, ["*3/8 present → not quorate* ", "*4/8 present → quorate* "]) + + def test_enter_leave_preprocessor(self): + activate("en") + elp = EnterLeavePreprocessor() + text = [ + "|enter|(12:00)(First Last)", + "|enter|(12:00)(Prof. First Last)", + "|enter|(12:00)(Prof. First Last)(Means)", + "|leave|(12:00)(Prof. First Last)", + ] + result = elp.run(text) + self.assertEqual( + result, + [ + "*12:00: First Last enters the meeting* ", + "*12:00: Prof. First Last enters the meeting* ", + "*12:00: Prof. First Last enters the meeting via Means* ", + "*12:00: Prof. First Last leaves the meeting* ", + ], + ) + + def test_heading_level_preprocessor(self): + hlp = HeadingLevelPreprocessor() + text = [ + "# Heading 1", + "## Heading 2", + "### Heading 3", + "#### Heading 4", + "##### Heading 5", + "###### Heading 6", + ] + result = hlp.run(text) + self.assertEqual( + result, + [ + "## Heading 1", + "### Heading 2", + "#### Heading 3", + "##### Heading 4", + "###### Heading 5", + "###### Heading 6", + ], + ) + + def test_internal_link_preprocessor(self): + ilp = InternalLinkPattern(InternalLinkPattern.default_pattern()) + import re + + from myhpi.tests.core.setup import setup_data + + test_data = setup_data() + test_page = test_data["pages"][0] + text = f"[Page title](page:{test_page.id})" + el, _, _ = ilp.handleMatch(re.match(ilp.pattern, text)) + self.assertEqual(el.attrib["href"], test_page.localized.get_url()) diff --git a/myhpi/tests/test_minutes_extensions.py b/myhpi/tests/test_minutes_extensions.py deleted file mode 100644 index 0bb0e118..00000000 --- a/myhpi/tests/test_minutes_extensions.py +++ /dev/null @@ -1,29 +0,0 @@ -import django -from django.test import TestCase -from django.utils.translation import activate - -django.setup() - -from myhpi.core.markdown.extensions import EnterLeavePreprocessor - - -class TestMinuteExtensions(TestCase): - def test_enter(self): - activate("en") - elp = EnterLeavePreprocessor() - text = [ - "|enter|(12:00)(First Last)", - "|enter|(12:00)(Prof. First Last)", - "|enter|(12:00)(Prof. First Last)(Means)", - "|leave|(12:00)(Prof. First Last)", - ] - result = elp.run(text) - self.assertEqual( - result, - [ - "*12:00: First Last enters the meeting* ", - "*12:00: Prof. First Last enters the meeting* ", - "*12:00: Prof. First Last enters the meeting via Means* ", - "*12:00: Prof. First Last leaves the meeting* ", - ], - )