diff options
| author | SavagePeanut <sourcehut@lazytapir.com> | 2023-07-28 16:00:22 -0500 |
|---|---|---|
| committer | SavagePeanut <sourcehut@lazytapir.com> | 2023-07-30 15:23:11 -0500 |
| commit | f290f20011d6ccafe8a4f0adaaab0c157a2c0107 (patch) | |
| tree | f07c68ebbd8bbb21bf19d92523d1c58308f850aa /tests | |
style outgoing messages
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_style_parser.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/test_style_parser.py b/tests/test_style_parser.py new file mode 100644 index 0000000..49f838b --- /dev/null +++ b/tests/test_style_parser.py @@ -0,0 +1,72 @@ +from style_parser import format_body + +MATRIX_FORMATS = { + "_": ("<em>", "</em>"), + "*": ("<strong>", "</strong>"), + "~": ("<strike>", "</strike>"), + "`": ("<code>", "</code>"), + "```": ("<pre><code>", "</code></pre>"), + ">": ("<blockquote>", "</blockquote>"), + "||": ("<span data-mx-spoiler>", "</span>") +} + +def test_basic(): + assert(format_body("_underline_", MATRIX_FORMATS) == "<em>underline</em>") + assert(format_body("*bold*", MATRIX_FORMATS) == "<strong>bold</strong>") + assert(format_body("~strikethrough~", MATRIX_FORMATS) == "<strike>strikethrough</strike>") + assert(format_body("`code span`", MATRIX_FORMATS) == "<code>code span</code>") + assert(format_body("```code\nblock```", MATRIX_FORMATS) == "<pre><code>code\nblock</code></pre>") + assert(format_body("||spoiler||", MATRIX_FORMATS) == "<span data-mx-spoiler>spoiler</span>") + +def test_quotes(): + assert(format_body(">single", MATRIX_FORMATS) == "<blockquote>single</blockquote>") + assert(format_body(">single\n>grouped", MATRIX_FORMATS) == "<blockquote>single\ngrouped</blockquote>") + assert(format_body(">>double", MATRIX_FORMATS) == "<blockquote><blockquote>double</blockquote></blockquote>") + assert(format_body(">>double\n>grouped single", MATRIX_FORMATS) == "<blockquote><blockquote>double</blockquote>\ngrouped single</blockquote>") + assert(format_body(">>>tripple\n>single\n>>double", MATRIX_FORMATS) == "<blockquote><blockquote><blockquote>tripple</blockquote></blockquote>\nsingle\n<blockquote>double</blockquote></blockquote>") + +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) == "<blockquote><blockquote><blockquote>tripple</blockquote></blockquote></blockquote>\n>none\n<blockquote><blockquote>double</blockquote></blockquote>") + +def test_nested(): + assert(format_body("`*~_code span_~*`", MATRIX_FORMATS) == "<code>*~_code span_~*</code>") + assert(format_body("*_~`code span`~_*", MATRIX_FORMATS) == "<strong><em><strike><code>code span</code></strike></em></strong>") + assert(format_body(">*_~`code span`~_*", MATRIX_FORMATS) == "<blockquote><strong><em><strike><code>code span</code></strike></em></strong></blockquote>") + assert(format_body("*bold*not bold*", MATRIX_FORMATS) == "<strong>bold</strong>not bold*") + assert(format_body("*_bold*_", MATRIX_FORMATS) == "<strong>_bold</strong>_") + +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("__not underlined__", MATRIX_FORMATS) == "__not underlined__") + assert(format_body("`no code\nblock here`", MATRIX_FORMATS) == "`no code\nblock here`") + +def test_assorted(): + assert(format_body("at the ```end```", MATRIX_FORMATS) == "at the <pre><code>end</code></pre>") + assert(format_body("in the ~middle~ here", MATRIX_FORMATS) == "in the <strike>middle</strike> here") + assert(format_body("_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||", MATRIX_FORMATS) == "<em>underline</em> <strong>bold</strong> <strike>strikethrough</strike> >not quote <span data-mx-spoiler>spoiler</span>\n<blockquote>quote</blockquote>\nnothing\nnothing\n<blockquote><blockquote><blockquote><blockquote>another quote with <span data-mx-spoiler><strike><em><strong><pre><code>four</code></pre></strong></em></strike></span></blockquote></blockquote></blockquote></blockquote>") + +def test_weird_utf8(): + assert(format_body("โค๏ธ๐๐๐๐ ```๐๐๐๐๐ค``` ๐๐๐โฃ๏ธ", MATRIX_FORMATS) == "โค๏ธ๐๐๐๐ <pre><code>๐๐๐๐๐ค</code></pre> ๐๐๐โฃ๏ธ") + assert(format_body("๐จโ๐ฉโ๐งโ๐ง _underline_๐ฉโ๐ฉโ๐ฆโ๐ง", MATRIX_FORMATS) == "๐จโ๐ฉโ๐งโ๐ง <em>underline</em>๐ฉโ๐ฉโ๐ฆโ๐ง") + assert(format_body("\u202eRight to left", MATRIX_FORMATS) == "\u202eRight to left") + assert(format_body(">\u202eRight to left quote?", MATRIX_FORMATS) == "<blockquote>\u202eRight to left quote?</blockquote>") + assert(format_body("_Invisible\u200bseparator_", MATRIX_FORMATS) == "<em>Invisible\u200bseparator</em>") + assert(format_body("~\u200b~", MATRIX_FORMATS) == "<strike>\u200b</strike>") + +LIMITED_FORMATS = { + "_": ("<em>", "</em>"), + "~": ("<strike>", "</strike>"), + "`": ("<code>", "</code>"), + "||": ("<span data-mx-spoiler>", "</span>") +} + +def test_limited(): + assert(format_body("_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||", LIMITED_FORMATS) == "<em>underline</em> *bold* <strike>strikethrough</strike> >not quote <span data-mx-spoiler>spoiler</span>\n>quote\nnothing\nnothing\n>>>>another quote with <span data-mx-spoiler><strike><em>*```four```*</em></strike></span>") |
