From eb2942a595b5f6f1e7eed711659ceba8821be3a3 Mon Sep 17 00:00:00 2001 From: SavagePeanut Date: Wed, 2 Aug 2023 17:45:21 -0500 Subject: fix code blocks --- tests/test_style_parser.py | 280 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 240 insertions(+), 40 deletions(-) (limited to 'tests') diff --git a/tests/test_style_parser.py b/tests/test_style_parser.py index 671af64..fd02597 100644 --- a/tests/test_style_parser.py +++ b/tests/test_style_parser.py @@ -6,67 +6,267 @@ MATRIX_FORMATS = { "~": ("", ""), "`": ("", ""), "```": ("
", "
"), + "```language": ("
", "
"), ">": ("
", "
"), "||": ("", "") } def test_basic(): - assert(format_body("_underline_", MATRIX_FORMATS) == "underline") - assert(format_body("*bold*", MATRIX_FORMATS) == "bold") - assert(format_body("~strikethrough~", MATRIX_FORMATS) == "strikethrough") - assert(format_body("`code span`", MATRIX_FORMATS) == "code span") - assert(format_body("```code\nblock```", MATRIX_FORMATS) == "
code\nblock
") - assert(format_body("||spoiler||", MATRIX_FORMATS) == "spoiler") + test = "_underline_" + formatted_body = "underline" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "*bold*" + formatted_body = "bold" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "~strikethrough~" + formatted_body = "strikethrough" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "`code span`" + formatted_body = "code span" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = """ + ```python + def test_basic(): + test = "_underline_" + formatted_body = "underline" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + ``` + """ + formatted_body = test = """ +
def test_basic():
+        test = "_underline_"
+        formatted_body = "underline"
+        assert(format_body(test, MATRIX_FORMATS) == (test, formatted_body))
+ """ + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "```\ncode block\n```" + formatted_body = "
code block
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "||this message contains a spoiler||" + formatted_body = "this message contains a spoiler" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) def test_quotes(): - assert(format_body(">single", MATRIX_FORMATS) == "
single
") - assert(format_body(">single\n>grouped", MATRIX_FORMATS) == "
single\ngrouped
") - assert(format_body(">>double", MATRIX_FORMATS) == "
double
") - assert(format_body(">>double\n>grouped single", MATRIX_FORMATS) == "
double
\ngrouped single
") - assert(format_body(">>>tripple\n>single\n>>double", MATRIX_FORMATS) == "
tripple
\nsingle\n
double
") + test = ">single" + formatted_body = "
single
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">single arrow ->" + formatted_body = "
single arrow ->
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">single\n>grouped" + formatted_body = "
single\ngrouped
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">>double" + formatted_body = "
double
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">>double\n>>double" + formatted_body = "
double\ndouble
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">>double\n&>not quote" + formatted_body = "
double
\n&>not quote" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">>double\n>grouped single" + formatted_body = "
double
\ngrouped single
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">>>tripple\n>single\n>>double" + formatted_body = "
tripple
\nsingle\n
double
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + +CODE_BLOCK_TEST_CASE = \ +""" +Code test +```python3 +def who_is_awesome(): + return "you!" +``` +Nope +""" + +CODE_BLOCK_TEST_CASE_OUTPUT = \ +""" +Code test +

+def who_is_awesome():
+    return \"you!\"
+
+Nope +""" + +def test_code_blocks(): + test = "```\nhacker\ncode\n```" + formatted_body = "
hacker\ncode
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "```python\nhacker code\n```" + formatted_body = "
hacker code
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">```java\n>why are you quoting a code block\n>```" + formatted_body = "
why are you quoting a code block
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">```\n>please stop trying to break my parser ;-;\n>```" + formatted_body = "
please stop trying to break my parser ;-;
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">>```\n>>double quote code block\n>single quote not in code block\nnormal text" + formatted_body = "
double quote code block
\nsingle quote not in code block
\nnormal text" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">>```\n>>>>double quote code block\n>single quote not in code block\nnormal text" + formatted_body = "
>>double quote code block
\nsingle quote not in code block
\nnormal text" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "_```_ignored\ninvalid code block\n```" + formatted_body = "```ignored\ninvalid code block\n```" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + def test_escaped(): - assert(format_body("\\_no underline_", MATRIX_FORMATS) == "_no underline_") - assert(format_body("\\\\_no underline_", MATRIX_FORMATS) == "\\_no underline_") - assert(format_body(">>>tripple\n\\>none\n>>double", MATRIX_FORMATS) == "
tripple
\n>none\n
double
") + test = "\\_no underline_" + formatted_body = "_no underline_" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "\\\\_no underline_" + formatted_body = "\\_no underline_" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">>>tripple\n\\>none\n>>double" + formatted_body = "
tripple
\n>none\n
double
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) def test_nested(): - assert(format_body("`*~_code span_~*`", MATRIX_FORMATS) == "*~_code span_~*") - assert(format_body("*_~`code span`~_*", MATRIX_FORMATS) == "code span") - assert(format_body(">*_~`code span`~_*", MATRIX_FORMATS) == "
code span
") - assert(format_body("*bold star >*< star bold*", MATRIX_FORMATS) == "bold star >*< star bold") - assert(format_body("*_bold*_", MATRIX_FORMATS) == "_bold_") - assert(format_body("__underlined__", MATRIX_FORMATS) == "underlined") + test = "`*~_code span_~*`" + formatted_body = "*~_code span_~*" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "*_~`code span`~_*" + formatted_body = "code span" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">*_~`code span`~_*" + formatted_body = "
code span
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "*bold star >*< star bold*" + formatted_body = "bold star >*< star bold" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "*_bold*_" + formatted_body = "_bold_" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "__underlined__" + formatted_body = "underlined" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) def test_no_changes(): - assert(format_body("", MATRIX_FORMATS) == "") - assert(format_body("~~ empty `````` styles **", MATRIX_FORMATS) == "~~ empty `````` styles **") - assert(format_body("this is not an empty string", MATRIX_FORMATS) == "this is not an empty string") - assert(format_body("arrow ->", MATRIX_FORMATS) == "arrow ->") - assert(format_body(" > no quote", MATRIX_FORMATS) == " > no quote") - assert(format_body("_not underlined", MATRIX_FORMATS) == "_not underlined") - assert(format_body("|not a spoiler|", MATRIX_FORMATS) == "|not a spoiler|") - assert(format_body("`no code\nblock here`", MATRIX_FORMATS) == "`no code\nblock here`") + test = "" + formatted_body = "" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "~~ empty `````` styles **" + formatted_body = "~~ empty `````` styles **" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "this is not an empty string" + formatted_body = "this is not an empty string" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "arrow ->" + formatted_body = "arrow ->" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = " > no quote" + formatted_body = " > no quote" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "_not underlined" + formatted_body = "_not underlined" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "|not a spoiler|" + formatted_body = "|not a spoiler|" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "||\nalso\nnot\na\nspoiler||" + formatted_body = "||\nalso\nnot\na\nspoiler||" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "`no code\nblock here`" + formatted_body = "`no code\nblock here`" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "invalid ```\ncode block\n```" + formatted_body = "invalid ```\ncode block\n```" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "```\ncode block\ninvalid```" + formatted_body = "```\ncode block\ninvalid```" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "```\ncode block\n```invalid" + formatted_body = "```\ncode block\n```invalid" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) def test_assorted(): - assert(format_body("at the ```end```", MATRIX_FORMATS) == "at the
end
") - assert(format_body("in the ~middle~ here", MATRIX_FORMATS) == "in the middle here") - assert(format_body("_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||", MATRIX_FORMATS) == "underline bold strikethrough >not quote spoiler\n
quote
\nnothing\nnothing\n
another quote with
four
") + test = "at the ||end||" + formatted_body = "at the end" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "in the ~middle~ here" + formatted_body = "in the middle here" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||" + formatted_body = "underline bold strikethrough >not quote spoiler\n
quote
\nnothing\nnothing\n
another quote with ```four```
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) def test_weird_utf8(): - assert(format_body("โค๏ธ๐Ÿ’“๐Ÿ’•๐Ÿ’–๐Ÿ’— ```๐Ÿ’™๐Ÿ’š๐Ÿ’›๐Ÿ’œ๐Ÿ–ค``` ๐Ÿ’๐Ÿ’ž๐Ÿ’Ÿโฃ๏ธ", MATRIX_FORMATS) == "โค๏ธ๐Ÿ’“๐Ÿ’•๐Ÿ’–๐Ÿ’—
๐Ÿ’™๐Ÿ’š๐Ÿ’›๐Ÿ’œ๐Ÿ–ค
๐Ÿ’๐Ÿ’ž๐Ÿ’Ÿโฃ๏ธ") - assert(format_body("๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง _underline_๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ง", MATRIX_FORMATS) == "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง underline๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ง") - assert(format_body("\u202eRight to left", MATRIX_FORMATS) == "\u202eRight to left") - assert(format_body(">\u202eRight to left quote?", MATRIX_FORMATS) == "
\u202eRight to left quote?
") - assert(format_body("_Invisible\u200bseparator_", MATRIX_FORMATS) == "Invisible\u200bseparator") - assert(format_body("~\u200b~", MATRIX_FORMATS) == "\u200b") + test = "โค๏ธ๐Ÿ’“๐Ÿ’•๐Ÿ’–๐Ÿ’— ||๐Ÿ’™๐Ÿ’š๐Ÿ’›๐Ÿ’œ๐Ÿ–ค|| ๐Ÿ’๐Ÿ’ž๐Ÿ’Ÿโฃ๏ธ" + formatted_body = "โค๏ธ๐Ÿ’“๐Ÿ’•๐Ÿ’–๐Ÿ’— ๐Ÿ’™๐Ÿ’š๐Ÿ’›๐Ÿ’œ๐Ÿ–ค ๐Ÿ’๐Ÿ’ž๐Ÿ’Ÿโฃ๏ธ" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง _underline_๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ง" + formatted_body = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง underline๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ง" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "\u202eRight to left" + formatted_body = "\u202eRight to left" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">\u202eRight to left quote?" + formatted_body = "
\u202eRight to left quote?
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "_Invisible\u200bseparator_" + formatted_body = "Invisible\u200bseparator" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = "~\u200b~" + formatted_body = "\u200b" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) LIMITED_FORMATS = { "_": ("", ""), "~": ("", ""), - "`": ("", ""), - "||": ("", "") + "`": ("", "") } def test_limited(): - assert(format_body("_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||", LIMITED_FORMATS) == "underline *bold* strikethrough >not quote spoiler\n>quote\nnothing\nnothing\n>>>>another quote with *```four```*") + test = "_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||" + formatted_body = "underline *bold* strikethrough >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||*```four```*||" + assert(format_body(test, LIMITED_FORMATS) == formatted_body) -- cgit v1.2.3