test_twtxtparser.py 18.1 KB
Newer Older
Lysander Trischler's avatar
Lysander Trischler committed
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
import unittest
from twtxtparser import (
        _find_whitespace,
        _extract_between,
        _parse_mention,
        _parse_subject_hash,
        _parse_subject,
        _parse_markdown_link,
        _parse_plain_link,
        parse_twt_text,
        Mention,
        SubjectHash,
        TwtxtLink,
        MarkdownLink,
        PlainLink,
        Text,
)


class FindWhitespaceTest(unittest.TestCase):

    def test_no_whitespace(self):
        self.assertEqual(0, _find_whitespace('', 0))
        self.assertEqual(1, _find_whitespace('a', 0))
        self.assertEqual(3, _find_whitespace(' ab', 1))

    def test_whitespace(self):
        self.assertEqual(1, _find_whitespace('a ', 0))
        self.assertEqual(2, _find_whitespace('ab ', 0))
        self.assertEqual(2, _find_whitespace('ab cd', 0))
        self.assertEqual(7, _find_whitespace('ab cdef gh', 3))


class ExtractBetweenTest(unittest.TestCase):

    def test_closing_not_found(self):
        self.assertEqual((None, -1), _extract_between("(missing parenthesis", 0, None, "(", ")"))
        self.assertEqual((None, -1), _extract_between("(closing parenthesis)( before start_pos", 21, None, "(", ")"))
        self.assertEqual((None, -1), _extract_between("(closing parenthesis) after end_pos", 0, 20, "(", ")"))

    def test_extract_data(self):
        self.assertEqual(("foo", 5), _extract_between("(foo)bar", 0, None, "(", ")"))
        self.assertEqual(("foo", 7), _extract_between("({foo})bar", 0, None, "({", "})"))
        self.assertEqual(("foo", 7), _extract_between("({foo})", 0, None, "({", "})"))


class ParseMentionTest(unittest.TestCase):

    def test_missing_closing_angle_bracket(self):
        self.assertIsNone(_parse_mention("@<missing bracket", 0))
        self.assertIsNone(_parse_mention("@<missing bracket]})", 0))

    def test_nick_and_url(self):
        self.assertEqual(Mention(start_pos=0, end_pos=37, nick="nick", url="https://example.com/twtxt.txt"),
                         _parse_mention("@<nick https://example.com/twtxt.txt>", 0))
        self.assertEqual(Mention(start_pos=0, end_pos=37, nick="nick", url="https://example.com/twtxt.txt"),
                         _parse_mention("@<nick https://example.com/twtxt.txt> more text", 0))
        self.assertEqual(Mention(start_pos=0, end_pos=43, nick="nick", url="https://example.com/twtxt.txt"),
                         _parse_mention("@<  nick   https://example.com/twtxt.txt  > more text", 0))

    def test_only_nick(self):
        self.assertEqual(Mention(start_pos=0, end_pos=7, nick="nick", url=None),
                         _parse_mention("@<nick>", 0))
        self.assertEqual(Mention(start_pos=0, end_pos=7, nick="nick", url=None),
                         _parse_mention("@<nick> more text", 0))
        self.assertEqual(Mention(start_pos=0, end_pos=11, nick="nick", url=None),
                         _parse_mention("@< nick   > more text", 0))

    def test_only_url(self):
        self.assertEqual(Mention(start_pos=0, end_pos=32, nick=None, url="https://example.com/twtxt.txt"),
                         _parse_mention("@<https://example.com/twtxt.txt>", 0))
        self.assertEqual(Mention(start_pos=0, end_pos=32, nick=None, url="https://example.com/twtxt.txt"),
                         _parse_mention("@<https://example.com/twtxt.txt> more text", 0))
        self.assertEqual(Mention(start_pos=0, end_pos=36, nick=None, url="https://example.com/twtxt.txt"),
                         _parse_mention("@<   https://example.com/twtxt.txt > more text", 0))


class ParseSubjectHashTest(unittest.TestCase):

    def test_missing_closing_angle_bracket(self):
        self.assertIsNone(_parse_subject_hash("#<abcdefg missing-bracket", 0))
        self.assertIsNone(_parse_subject_hash("#<abcdefg missing-bracket]})", 0))

    def test_non_7_chars_hash(self):
        self.assertEqual(SubjectHash(start_pos=0, end_pos=30, hash="123456", url="https://example.com/"),
                         _parse_subject_hash("#<123456 https://example.com/>", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=9, hash="123456", url=None),
                         _parse_subject_hash("#<123456>", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=32, hash="12345678", url="https://example.com/"),
                         _parse_subject_hash("#<12345678 https://example.com/>", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=11, hash="12345678", url=None),
                         _parse_subject_hash("#<12345678>", 0))

    def test_hash_and_url(self):
        self.assertEqual(SubjectHash(start_pos=0, end_pos=31, hash="abcd567", url="https://example.com/"),
                         _parse_subject_hash("#<abcd567 https://example.com/>", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=31, hash="abcd567", url="https://example.com/"),
                         _parse_subject_hash("#<abcd567 https://example.com/> more text", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=36, hash="abcd567", url="https://example.com/"),
                         _parse_subject_hash("#<  abcd567   https://example.com/ > more text", 0))

    def test_only_hash(self):
        self.assertEqual(SubjectHash(start_pos=0, end_pos=10, hash="abcd567", url=None),
                         _parse_subject_hash("#<abcd567>", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=10, hash="abcd567", url=None),
                         _parse_subject_hash("#<abcd567> more text", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=13, hash="abcd567", url=None),
                         _parse_subject_hash("#<  abcd567 > more text", 0))

    def test_only_url(self):
        self.assertEqual(SubjectHash(start_pos=0, end_pos=23, hash=None, url="https://example.com/"),
                         _parse_subject_hash("#<https://example.com/>", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=23, hash=None, url="https://example.com/"),
                         _parse_subject_hash("#<https://example.com/> more text", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=26, hash=None, url="https://example.com/"),
                         _parse_subject_hash("#<  https://example.com/ > more text", 0))


class ParseSubjectTest(unittest.TestCase):

    def test_missing_closing_angle_bracket(self):
        self.assertIsNone(_parse_subject("(#<abcdefg missing-bracket>", 0))
        self.assertIsNone(_parse_subject("(#<abcdefg missing-bracket>]}", 0))
        self.assertIsNone(_parse_subject("(#<abcdefg missing-bracket]}", 0))
        self.assertIsNone(_parse_subject("(#<abcdefg missing-bracket]})", 0))

    def test_other_text_after_subject_hash(self):
        self.assertIsNone(_parse_subject("(#<abcd567 https://example.com/> a)", 0))

    def test_hash_and_url(self):
        self.assertEqual(SubjectHash(start_pos=0, end_pos=33, hash="abcd567", url="https://example.com/"),
                         _parse_subject("(#<abcd567 https://example.com/>)", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=33, hash="abcd567", url="https://example.com/"),
                         _parse_subject("(#<abcd567 https://example.com/>) more text", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=36, hash="abcd567", url="https://example.com/"),
                         _parse_subject("( #<abcd567 https://example.com/>  ) more text", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=44, hash="abcd567", url="https://example.com/"),
                         _parse_subject("(  #<  abcd567   https://example.com/ >    ) more text", 0))

    def test_only_hash(self):
        self.assertEqual(SubjectHash(start_pos=0, end_pos=12, hash="abcd567", url=None),
                         _parse_subject("(#<abcd567>)", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=12, hash="abcd567", url=None),
                         _parse_subject("(#<abcd567>) more text", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=15, hash="abcd567", url=None),
                         _parse_subject("( #<abcd567>  ) more text", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=18, hash="abcd567", url=None),
                         _parse_subject("(  #<  abcd567 > ) more text", 0))

    def test_only_url(self):
        self.assertEqual(SubjectHash(start_pos=0, end_pos=25, hash=None, url="https://example.com/"),
                         _parse_subject("(#<https://example.com/>)", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=25, hash=None, url="https://example.com/"),
                         _parse_subject("(#<https://example.com/>) more text", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=28, hash=None, url="https://example.com/"),
                         _parse_subject("( #<https://example.com/>  ) more text", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=31, hash=None, url="https://example.com/"),
                         _parse_subject("(  #<  https://example.com/ > ) more text", 0))

160
161
162
163
164
165
166
167
    def test_only_url_hash_extracted_from_url(self):
        self.assertEqual(SubjectHash(start_pos=0, end_pos=34, hash="hfp2hca", url="https://twtxt.net/twt/hfp2hca"),
                         _parse_subject("(#<https://twtxt.net/twt/hfp2hca>)", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=35, hash="hfp2hca", url="https://twtxt.net/conv/hfp2hca"),
                         _parse_subject("(#<https://twtxt.net/conv/hfp2hca>)", 0))
        self.assertEqual(SubjectHash(start_pos=0, end_pos=43, hash="5jqioeq", url="https://txt.sour.is/search?tag=5jqioeq"),
                         _parse_subject("(#<https://txt.sour.is/search?tag=5jqioeq>)", 0))

Lysander Trischler's avatar
Lysander Trischler committed
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

class ParseMarkdownLink(unittest.TestCase):

    def test_missing_closing_closing_bracket(self):
        self.assertIsNone(_parse_markdown_link("[link title(https://example.com)", 0))
        self.assertIsNone(_parse_markdown_link("[link title", 0))

    def test_missing_opening_parenthesis(self):
        self.assertIsNone(_parse_markdown_link("[link title]https://example.com)", 0))
        self.assertIsNone(_parse_markdown_link("[link title]", 0))

    def test_missing_closing_parenthesis(self):
        self.assertIsNone(_parse_markdown_link("[link title](https://example.com", 0))
        self.assertIsNone(_parse_markdown_link("[link title](", 0))

    def test_missing_closing_parenthesis_or_after_text(self):
        self.assertIsNone(_parse_markdown_link("[link title](https://example.com more)", 0))
        self.assertIsNone(_parse_markdown_link("[link title](https://example.com more", 0))

    def test_chars_between_closing_bracket_and_opening_parenthesis(self):
        self.assertIsNone(_parse_markdown_link("[link title] (https://example.com)", 0))
        self.assertIsNone(_parse_markdown_link("[link title]a(https://example.com)", 0))

    def test_markdown_link(self):
        self.assertEqual(MarkdownLink(start_pos=0, end_pos=34, title="link title", url="https://example.com/"),
                         _parse_markdown_link("[link title](https://example.com/)", 0))
        self.assertEqual(MarkdownLink(start_pos=0, end_pos=34, title="link title", url="https://example.com/"),
                         _parse_markdown_link("[link title](https://example.com/) more text", 0))
        self.assertEqual(MarkdownLink(start_pos=0, end_pos=29, title="title", url="https://example.com/"),
                         _parse_markdown_link("[title](https://example.com/)", 0))
        self.assertEqual(MarkdownLink(start_pos=0, end_pos=29, title="title", url="https://example.com/"),
                         _parse_markdown_link("[title](https://example.com/) more text", 0))


class PlainLinkTest(unittest.TestCase):

    def test_http_link(self):
        self.assertEqual(PlainLink(start_pos=0, end_pos=19, url="http://example.com/"),
                         _parse_plain_link("http://example.com/", 0))
        self.assertEqual(PlainLink(start_pos=0, end_pos=19, url="http://example.com/"),
                         _parse_plain_link("http://example.com/ more text", 0))

    def test_https_link(self):
        self.assertEqual(PlainLink(start_pos=0, end_pos=20, url="https://example.com/"),
                         _parse_plain_link("https://example.com/", 0))
        self.assertEqual(PlainLink(start_pos=0, end_pos=20, url="https://example.com/"),
                         _parse_plain_link("https://example.com/ more text", 0))

    def test_closing_parenthesis_at_the_end_is_not_part_of_url(self):
        self.assertEqual(PlainLink(start_pos=0, end_pos=20, url="https://example.com/"),
                         _parse_plain_link("https://example.com/)", 0))


class ParseTwtTextTest(unittest.TestCase):

    def check(self, text, *expected_tokens):
        actual = tuple(parse_twt_text(text))
        self.assertEqual(expected_tokens, actual)

    def test_only_text(self):
        self.check("Hello world.", Text(start_pos=0, end_pos=12, text="Hello world."))

    def test_only_mention(self):
        self.check("@<hugo https://example.com/twtxt.txt>",
                   Mention(start_pos=0, end_pos=37, nick="hugo", url="https://example.com/twtxt.txt"))
        self.check("@<hugo>",
                   Mention(start_pos=0, end_pos=7, nick="hugo", url=None))
        self.check("@<https://example.com/twtxt.txt>",
                   Mention(start_pos=0, end_pos=32, nick=None, url="https://example.com/twtxt.txt"))

    def test_text_and_mention(self):
        self.check("Hello world. @<hugo https://example.com/twtxt.txt>",
                   Text(start_pos=0, end_pos=13, text="Hello world. "),
                   Mention(start_pos=13, end_pos=50, nick="hugo", url="https://example.com/twtxt.txt"))

    def test_only_subject(self):
        self.check("(#<123defg https://example.com/>)",
                   SubjectHash(start_pos=0, end_pos=33, hash="123defg", url="https://example.com/"))
        self.check("(#<123defg>)",
                   SubjectHash(start_pos=0, end_pos=12, hash="123defg", url=None))
        self.check("(#<https://example.com/>)",
                   SubjectHash(start_pos=0, end_pos=25, hash=None, url="https://example.com/"))

    def test_subject_with_text_after_subject_hash_is_text_token_and_twtxt_link_and_text(self):
        self.check("(#<123defg https://example.com/> some text)",
                   Text(start_pos=0, end_pos=1, text="("),
                   TwtxtLink(start_pos=1, end_pos=32, title="123defg", url="https://example.com/"),
                   Text(start_pos=32, end_pos=43, text=" some text)"))

    def test_subject_text_and_mention_and_text(self):
        self.check("(#<abc4567 https://example.com/>) @<hugo https://example.com/twtxt.txt> Great!",
                   SubjectHash(start_pos=0, end_pos=33, hash="abc4567", url="https://example.com/"),
                   Text(start_pos=33, end_pos=34, text=" "),
                   Mention(start_pos=34, end_pos=71, nick="hugo", url="https://example.com/twtxt.txt"),
                   Text(start_pos=71, end_pos=78, text=" Great!"))

    def test_mention_and_text_and_mention_and_subject_and_text_and_mention_and_text(self):
        self.check("@<hugo https://example.com/hugo> @<joe> (#<1234567 https://example.com/>) "
                   "@<https://example.com/kate> This is wonderful.",
                   Mention(start_pos=0, end_pos=32, nick="hugo", url="https://example.com/hugo"),
                   Text(start_pos=32, end_pos=33, text=" "),
                   Mention(start_pos=33, end_pos=39, nick="joe", url=None),
                   Text(start_pos=39, end_pos=40, text=" "),
                   SubjectHash(start_pos=40, end_pos=73, hash="1234567", url="https://example.com/"),
                   Text(start_pos=73, end_pos=74, text=" "),
                   Mention(start_pos=74, end_pos=101, nick=None, url="https://example.com/kate"),
                   Text(start_pos=101, end_pos=120, text=" This is wonderful."))

    def test_only_twtxt_link(self):
        self.check("#<eggs https://example.com/eggs>",
                   TwtxtLink(start_pos=0, end_pos=32, title="eggs", url="https://example.com/eggs"))

    def test_only_markdown_link(self):
        self.check("[link title](https://example.com/)",
                  MarkdownLink(start_pos=0, end_pos=34, title="link title", url="https://example.com/"))

    def test_mention_text_subject_text_mention_text_markdown_link_text_mention_text_mention(self):
        self.check("@<hugo https://example.com/hugo> (#<1234567 https://example.com/>) "
                   "What a great thing I've discovered: [Check this out](https://example.com/), "
                   "@<kate> and @<joe>",
                   Mention(start_pos=0, end_pos=32, nick="hugo", url="https://example.com/hugo"),
                   Text(start_pos=32, end_pos=33, text=" "),
                   SubjectHash(start_pos=33, end_pos=66, hash="1234567", url="https://example.com/"),
                   Text(start_pos=66, end_pos=103, text=" What a great thing I've discovered: "),
                   MarkdownLink(start_pos=103, end_pos=141, title="Check this out", url="https://example.com/"),
                   Text(start_pos=141, end_pos=143, text=", "),
                   Mention(start_pos=143, end_pos=150, nick="kate", url=None),
                   Text(start_pos=150, end_pos=155, text=" and "),
                   Mention(start_pos=155, end_pos=161, nick="joe", url=None))

    def test_input_crashing_parser(self):
        self.check("Over two days, the upload feature is busy. "
                   "!(https://twtxt.net/media/KuGwrmFJvpahhaJJsFPXKK) No comments in console.",
                   Text(start_pos=0, end_pos=45, text="Over two days, the upload feature is busy. !("),
                   PlainLink(start_pos=45, end_pos=91, url="https://twtxt.net/media/KuGwrmFJvpahhaJJsFPXKK"),
                   Text(start_pos=91, end_pos=116, text=") No comments in console."))

    def test_only_plain_link(self):
        self.check("https://example.com/",
                   PlainLink(start_pos=0, end_pos=20, url="https://example.com/"))

    def test_mention_plain_link_text(self):
        self.check("@<hugo https://example.com/hugo>https://example.com/foo/bar crazy!!1",
                   Mention(start_pos=0, end_pos=32, nick="hugo", url="https://example.com/hugo"),
                   PlainLink(start_pos=32, end_pos=59, url="https://example.com/foo/bar"),
                   Text(start_pos=59, end_pos=68, text=" crazy!!1"))

if __name__ == "__main__":
    unittest.main()