Add ReplyBuilder tests

The ReplyBuilder does stuff like parsing >quotes, >>replies, and
[spoiler]spoiler tags[/spoiler]

We mostly want to track consistent behavior with this

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-07-23 18:12:45 -07:00
parent 5164842289
commit 939227e1f0

105
tests/test_replybuilder.py Normal file
View File

@@ -0,0 +1,105 @@
from datetime import timedelta
from django.test import TestCase
from board.models import Board, Post
from board.templatetags.post_body import ReplyBuilder
class ReplyBuilderTestCase(TestCase):
def setUp(self):
Board.objects.create(
url="test", name="test", post_cooldown=timedelta(seconds=0)
)
def test_quote(self):
expected = [
(
"this is some text that shouldn't be quoted.",
"this is some text that shouldn't be quoted.",
),
(
"&gt;implying this quotes text correctly",
'<span class="quote">&gt;implying this quotes text correctly</span>',
),
(
"&gt;implying this &gt;quotes &gt;text correctly",
'<span class="quote">&gt;implying this &gt;quotes &gt;text correctly</span>',
),
(
"""
&gt;implying this quotes text correctly
nothing ever changes
""",
'<span class="quote">&gt;implying this quotes text correctly</span><br/>nothing ever changes',
),
(
"""
what happened to the 1 2 3 4 5 6 7 8 9
&gt;implying this quotes text correctly
nothing ever changes
""",
'what happened to the 1 2 3 4 5 6 7 8 9<br/><span class="quote">&gt;implying this quotes text correctly</span><br/>nothing ever changes',
),
(
"""
nothing ever changes
&gt;implying this quotes text correctly
what happened to the 1 2 3 4 5 6 7 8 9
""",
'nothing ever changes<br/><span class="quote">&gt;implying this quotes text correctly</span><br/>what happened to the 1 2 3 4 5 6 7 8 9',
),
]
for input, output in expected:
rb = ReplyBuilder(input.strip())
self.assertEquals(rb.build(), output)
def test_reply(self):
board = Board.objects.get(url="test")
post1 = Post.objects.create(board=board, text="test 1", ip="127.0.0.1")
post2 = Post.objects.create(board=board, text="test 1", ip="127.0.0.1")
post3 = Post.objects.create(board=board, text="test 1", ip="127.0.0.1")
expected = [
(
f"&gt;&gt;{post1.id}",
f'<a class="post_link" href="{post1.get_absolute_url()}">&gt;&gt;{post1.id}</a>',
),
(
f"&gt;&gt;{post2.id}",
f'<a class="post_link" href="{post2.get_absolute_url()}">&gt;&gt;{post2.id}</a>',
),
(
f"&gt;&gt;{post1.id} &gt;&gt;{post2.id} &gt;&gt;{post3.id}",
f'<a class="post_link" href="{post1.get_absolute_url()}">&gt;&gt;{post1.id}</a> '
f'<a class="post_link" href="{post2.get_absolute_url()}">&gt;&gt;{post2.id}</a> '
f'<a class="post_link" href="{post3.get_absolute_url()}">&gt;&gt;{post3.id}</a>',
),
(
f"&gt;&gt;999999",
f'<span class="post_link_broken">&gt;&gt;999999</span>',
),
]
for input, output in expected:
rb = ReplyBuilder(input.strip())
self.assertEquals(rb.build(), output)
def test_spoiler(self):
expected = [
(
"[spoiler]This is a spoiler[/spoiler]",
'<span class="spoiler">This is a spoiler</span>',
),
(
r"\[spoiler]This is an escaped spoiler[/spoiler]",
"[spoiler]This is an escaped spoiler[/spoiler]",
),
(
"[spoiler]This is an unfinished spoiler",
'<span class="spoiler">This is an unfinished spoiler</span>',
),
]
for input, output in expected:
rb = ReplyBuilder(input.strip(), parse_spoilers=True)
self.assertEquals(rb.build(), output)