From 939227e1f08146ba09824a9b7ee1fc6f97fd0ff4 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Sat, 23 Jul 2022 18:12:45 -0700 Subject: [PATCH] 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 --- tests/test_replybuilder.py | 105 +++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 tests/test_replybuilder.py diff --git a/tests/test_replybuilder.py b/tests/test_replybuilder.py new file mode 100644 index 0000000..9266e75 --- /dev/null +++ b/tests/test_replybuilder.py @@ -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.", + ), + ( + ">implying this quotes text correctly", + '>implying this quotes text correctly', + ), + ( + ">implying this >quotes >text correctly", + '>implying this >quotes >text correctly', + ), + ( + """ +>implying this quotes text correctly +nothing ever changes +""", + '>implying this quotes text correctly
nothing ever changes', + ), + ( + """ +what happened to the 1 2 3 4 5 6 7 8 9 +>implying this quotes text correctly +nothing ever changes +""", + 'what happened to the 1 2 3 4 5 6 7 8 9
>implying this quotes text correctly
nothing ever changes', + ), + ( + """ +nothing ever changes +>implying this quotes text correctly +what happened to the 1 2 3 4 5 6 7 8 9 +""", + 'nothing ever changes
>implying this quotes text correctly
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">>{post1.id}", + f'>>{post1.id}', + ), + ( + f">>{post2.id}", + f'>>{post2.id}', + ), + ( + f">>{post1.id} >>{post2.id} >>{post3.id}", + f'>>{post1.id} ' + f'>>{post2.id} ' + f'>>{post3.id}', + ), + ( + f">>999999", + f'>>999999', + ), + ] + + 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]", + 'This is a spoiler', + ), + ( + r"\[spoiler]This is an escaped spoiler[/spoiler]", + "[spoiler]This is an escaped spoiler[/spoiler]", + ), + ( + "[spoiler]This is an unfinished spoiler", + 'This is an unfinished spoiler', + ), + ] + + for input, output in expected: + rb = ReplyBuilder(input.strip(), parse_spoilers=True) + self.assertEquals(rb.build(), output)