Skip to content

Commit

Permalink
Add markdown extension tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frcroth committed Oct 15, 2023
1 parent bfb430a commit 13a803c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 34 deletions.
13 changes: 8 additions & 5 deletions myhpi/core/markdown/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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):
Expand All @@ -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<title>[^\[]+)\]\(page:(?P<id>\d+)\)"


class ImagePattern(LinkInlineProcessor):
def handleMatch(self, m, data=None):
Expand All @@ -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,
)
Expand Down
100 changes: 100 additions & 0 deletions myhpi/tests/test_markdown_extensions.py
Original file line number Diff line number Diff line change
@@ -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())
29 changes: 0 additions & 29 deletions myhpi/tests/test_minutes_extensions.py

This file was deleted.

0 comments on commit 13a803c

Please sign in to comment.