summaryrefslogtreecommitdiff
path: root/tests/test_matrix_style_parser.py
blob: b8ed1121d8143006e643c9887e0436976b1ad1d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from slidge_style_parser import format_body

MATRIX_FORMATS = {
    "_": ("<em>", "</em>"),
    "*": ("<strong>", "</strong>"),
    "~": ("<strike>", "</strike>"),
    "`": ("<code>", "</code>"),
    "```": ("<pre><code>", "</code></pre>"),
    "```language": ("<pre><code class=\"language-{}\">", "</code></pre>"),
    ">": ("<blockquote>", "</blockquote>"),
    "||": ("<span data-mx-spoiler>", "</span>"),
    "\n": ("<br>", "")
}

def test_basic():
    test = "_underline_"
    formatted_body = "<em>underline</em>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "*bold*"
    formatted_body = "<strong>bold</strong>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "~strikethrough~"
    formatted_body = "<strike>strikethrough</strike>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "`code span`"
    formatted_body = "<code>code span</code>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = """
    ```python
        def test_basic():
            test = "_underline_"
            formatted_body = "<em>underline</em>"
            assert(format_body(test, MATRIX_FORMATS) == formatted_body)
    ```
    """
    formatted_body = test = """<pre><code class="language-python">def test_basic():<br>        test = "_underline_"<br>        formatted_body = "<em>underline</em>"<br>        assert(format_body(test, MATRIX_FORMATS) == (test, formatted_body))</pre></code><br>"""
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "```\ncode block\n```"
    formatted_body = "<pre><code>code block</code></pre>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "||this message contains a spoiler||"
    formatted_body = "<span data-mx-spoiler>this message contains a spoiler</span>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

def test_quotes():
    test = ">single"
    formatted_body = "<blockquote>single</blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">single arrow ->"
    formatted_body = "<blockquote>single arrow -></blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">single\n>grouped"
    formatted_body = "<blockquote>single<br>grouped</blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">>double"
    formatted_body = "<blockquote><blockquote>double</blockquote></blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">>double\n>>double"
    formatted_body = "<blockquote><blockquote>double<br>double</blockquote></blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">>double\n&>not quote"
    formatted_body = "<blockquote><blockquote>double</blockquote></blockquote><br>&>not quote"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">>double\n>grouped single"
    formatted_body = "<blockquote><blockquote>double</blockquote><br>grouped single</blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">>>tripple\n>single\n>>double"
    formatted_body = "<blockquote><blockquote><blockquote>tripple</blockquote></blockquote><br>single<br><blockquote>double</blockquote></blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

def test_code_blocks():
    test = "```\nhacker\ncode\n```"
    formatted_body = "<pre><code>hacker<br>code</code></pre>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "```python\nhacker code\n```"
    formatted_body = "<pre><code class=\"language-python\">hacker code</code></pre>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "```python\nhacker code\n```\nnormal text"
    formatted_body = "<pre><code class=\"language-python\">hacker code</code></pre><br>normal text"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "```python\nhacker code\n```\nnormal text\n```java\npublic static void main(String [])\n```"
    formatted_body = "<pre><code class=\"language-python\">hacker code</code></pre><br>normal text<br><pre><code class=\"language-java\">public static void main(String [])</code></pre>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">```java\n>why are you quoting a code block\n>```"
    formatted_body = "<blockquote><pre><code class=\"language-java\">why are you quoting a code block</code></pre></blockquote>"
    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 = "<blockquote><blockquote><pre><code>double quote code block</code></pre></blockquote><br>single quote not in code block</blockquote><br>normal text"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">```\n>please stop trying to break my parser ;-;"
    formatted_body = "<blockquote><pre><code>please stop trying to break my parser ;-;</code></pre></blockquote>"
    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 = "<blockquote><blockquote><pre><code>>>double quote code block</code></pre></blockquote><br>single quote not in code block</blockquote><br>normal text"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "_```_ignored\ninvalid code block\n```"
    formatted_body = "<em>```</em>ignored<br>invalid code block<br>```"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)


def test_escaped():
    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 = "<blockquote><blockquote><blockquote>tripple</blockquote></blockquote></blockquote><br>>none<br><blockquote><blockquote>double</blockquote></blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

def test_nested():
    test = "`*~_code span_~*`"
    formatted_body = "<code>*~_code span_~*</code>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "*_~`code span`~_*"
    formatted_body = "<strong><em><strike><code>code span</code></strike></em></strong>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">*_~`code span`~_*"
    formatted_body = "<blockquote><strong><em><strike><code>code span</code></strike></em></strong></blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "*bold star >*< star bold*"
    formatted_body = "<strong>bold star >*< star bold</strong>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "*_bold*_"
    formatted_body = "<strong>_bold</strong>_"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "__underlined__"
    formatted_body = "<em><em>underlined</em></em>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

def test_no_changes():
    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 = "||<br>also<br>not<br>a<br>spoiler||"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "`no code\nblock here`"
    formatted_body = "`no code<br>block here`"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "invalid ```\ncode block\n```"
    formatted_body = "invalid ```<br>code block<br>```"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "```\ncode block\ninvalid```"
    formatted_body = "```<br>code block<br>invalid```"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "```\ncode block\n```invalid"
    formatted_body = "```<br>code block<br>```invalid"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

def test_assorted():
    test = "\n"
    formatted_body = "<br>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "at the ||end||"
    formatted_body = "at the <span data-mx-spoiler>end</span>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "in the ~middle~ here"
    formatted_body = "in the <strike>middle</strike> 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 = "<em>underline</em> <strong>bold</strong> <strike>strikethrough</strike> >not quote <span data-mx-spoiler>spoiler</span><br><blockquote>quote</blockquote><br>nothing<br>nothing<br><blockquote><blockquote><blockquote><blockquote>another quote with <span data-mx-spoiler><strike><em><strong>```four```</strong></em></strike></span></blockquote></blockquote></blockquote></blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">```\n>do be do be dooo ba do be do be do ba\n>>>"
    formatted_body = "<blockquote><pre><code>do be do be dooo ba do be do be do ba<br>>></code></pre></blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "\n\n>```\n>do be do be dooo ba do be do be do ba\na\n\n\naoeu\n"
    formatted_body = "<br><br><blockquote><pre><code>do be do be dooo ba do be do be do ba</code></pre></blockquote><br>a<br><br><br>aoeu<br>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">```\n>do be do be dooo ba do be do be do ba\n>\n>\n>aoeu"
    formatted_body = "<blockquote><pre><code>do be do be dooo ba do be do be do ba<br><br><br>aoeu</code></pre></blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = ">```\n>code block\n>```invalid end\n"
    formatted_body = "<blockquote><pre><code>code block<br>```invalid end</code></pre></blockquote><br>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "invalid ```\ncode block\n*bold*\n```"
    formatted_body = "invalid ```<br>code block<br><strong>bold</strong><br>```"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

def test_weird_utf8():
    test = "โค๏ธ๐Ÿ’“๐Ÿ’•๐Ÿ’–๐Ÿ’— ||๐Ÿ’™๐Ÿ’š๐Ÿ’›๐Ÿ’œ๐Ÿ–ค|| ๐Ÿ’๐Ÿ’ž๐Ÿ’Ÿโฃ๏ธ"
    formatted_body = "โค๏ธ๐Ÿ’“๐Ÿ’•๐Ÿ’–๐Ÿ’— <span data-mx-spoiler>๐Ÿ’™๐Ÿ’š๐Ÿ’›๐Ÿ’œ๐Ÿ–ค</span> ๐Ÿ’๐Ÿ’ž๐Ÿ’Ÿโฃ๏ธ"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง _underline_๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ง"
    formatted_body = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง <em>underline</em>๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ง"
    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 = "<blockquote>\u202eRight to left quote?</blockquote>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "_Invisible\u200bseparator_"
    formatted_body = "<em>Invisible\u200bseparator</em>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

    test = "~\u200b~"
    formatted_body = "<strike>\u200b</strike>"
    assert(format_body(test, MATRIX_FORMATS) == formatted_body)

LIMITED_FORMATS = {
    "_": ("<em>", "</em>"),
    "~": ("<strike>", "</strike>"),
    "`": ("<code>", "</code>")
}

def test_limited():
    test = "_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||"
    formatted_body = "<em>underline</em> *bold* <strike>strikethrough</strike> >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||<strike><em>*```four```*</em></strike>||"
    assert(format_body(test, LIMITED_FORMATS) == formatted_body)