Files
interchan/tests/test_replybuilder.py

118 lines
4.4 KiB
Python
Raw Normal View History

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.",
),
(
">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)
def test_youtube_embed(self):
expected = [
(
"https://www.youtube.com/watch?v=O_3_-UrhZH0",
'<span class="youtube_embed"><span class="youtube_embed_container">https://www.youtube.com/watch?v=O_3_-UrhZH0 [<a class="youtube_embed_link" data-yt-hash="O_3_-UrhZH0" href="https://www.youtube.com/watch?v=O_3_-UrhZH0">Embed</a>]</span></span>',
),
]
for input, output in expected:
rb = ReplyBuilder(input.strip())
self.assertEquals(rb.build(), output)