forked from BonsaiDen/twitter-text-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.py
More file actions
555 lines (439 loc) · 29.9 KB
/
tests.py
File metadata and controls
555 lines (439 loc) · 29.9 KB
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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# -*- coding: UTF-8 -*-
# This file is part of twitter-text-python.
#
# twitter-text-python is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# twitter-text-python is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# twitter-text-python. If not, see <http://www.gnu.org/licenses/>.
# twp - Unittests --------------------------------------------------------------
# ------------------------------------------------------------------------------
import unittest
import ttp
class TWPTests(unittest.TestCase):
def setUp(self):
self.parser = ttp.Parser()
# General Tests ------------------------------------------------------------
# --------------------------------------------------------------------------
def test_all_not_allow_amp_without_question(self):
result = self.parser.parse(u'Check out: http://www.github.com/test&@username')
self.assertEqual(result.html, u'Check out: <a href="http://www.github.com/test">http://www.github.com/test</a>&<a href="http://twitter.com/username">@username</a>')
self.assertEqual(result.users, [u'username'])
self.assertEqual(result.urls, [u'http://www.github.com/test'])
def test_all_not_break_url_at(self):
result = self.parser.parse(u'http://www.flickr.com/photos/29674651@N00/4382024406')
self.assertEqual(result.html, u'<a href="http://www.flickr.com/photos/29674651@N00/4382024406">http://www.flickr.com/photo...</a>')
self.assertEqual(result.urls, [u'http://www.flickr.com/photos/29674651@N00/4382024406'])
# URL tests ----------------------------------------------------------------
# --------------------------------------------------------------------------
def test_url_mid(self):
result = self.parser.parse(u'text http://example.com more text')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a> more text')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_unicode(self):
result = self.parser.parse(u'I enjoy Macintosh Brand computers: http://✪df.ws/ejp')
self.assertEqual(result.html, u'I enjoy Macintosh Brand computers: <a href="http://✪df.ws/ejp">http://✪df.ws/ejp</a>')
self.assertEqual(result.urls, [u'http://\u272adf.ws/ejp'])
def test_url_parentheses(self):
result = self.parser.parse(u'text (http://example.com)')
self.assertEqual(result.html, u'text (<a href="http://example.com">http://example.com</a>)')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_underscore(self):
result = self.parser.parse(u'text http://example.com/test/foo_123.jpg')
self.assertEqual(result.html, u'text <a href="http://example.com/test/foo_123.jpg">http://example.com/test/foo...</a>')
self.assertEqual(result.urls, [u'http://example.com/test/foo_123.jpg'])
def test_url_underscore_dot(self):
result = self.parser.parse(u'text http://example.com/test/bla.net_foo_123.jpg')
self.assertEqual(result.html, u'text <a href="http://example.com/test/bla.net_foo_123.jpg">http://example.com/test/bla...</a>')
self.assertEqual(result.urls, [u'http://example.com/test/bla.net_foo_123.jpg'])
def test_url_amp_lang_equals(self):
result = self.parser.parse(u'Check out http://search.twitter.com/search?q=avro&lang=en')
self.assertEqual(result.html, u'Check out <a href="http://search.twitter.com/search?q=avro&lang=en">http://search.twitter.com/s...</a>')
self.assertEqual(result.urls, [u'http://search.twitter.com/search?q=avro&lang=en'])
def test_url_amp_break(self):
result = self.parser.parse(u'Check out http://twitter.com/te?foo&invalid=True')
self.assertEqual(result.html, u'Check out <a href="http://twitter.com/te?foo&invalid=True">http://twitter.com/te?foo...</a>')
self.assertEqual(result.urls, [u'http://twitter.com/te?foo&invalid=True'])
def test_url_dash(self):
result = self.parser.parse(u'Is www.foo-bar.com a valid URL?')
self.assertEqual(result.html, u'Is <a href="http://www.foo-bar.com">www.foo-bar.com</a> a valid URL?')
self.assertEqual(result.urls, [u'www.foo-bar.com'])
def test_url_multiple(self):
result = self.parser.parse(u'http://example.com https://sslexample.com http://sub.example.com')
self.assertEqual(result.html, u'<a href="http://example.com">http://example.com</a> <a href="https://sslexample.com">https://sslexample.com</a> <a href="http://sub.example.com">http://sub.example.com</a>')
self.assertEqual(result.urls, [u'http://example.com', u'https://sslexample.com', u'http://sub.example.com'])
def test_url_raw_domain(self):
result = self.parser.parse(u'See http://example.com example.com')
self.assertEqual(result.html, u'See <a href="http://example.com">http://example.com</a> example.com')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_embed_link(self):
result = self.parser.parse(u'<link rel=\'true\'>http://example.com</link>')
self.assertEqual(result.html, u'<link rel=\'true\'><a href="http://example.com">http://example.com</a></link>')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_trailing(self):
result = self.parser.parse(u'text http://example.com')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_japanese(self):
result = self.parser.parse(u'いまなにしてるhttp://example.comいまなにしてる')
self.assertEqual(result.html, u'いまなにしてる<a href="http://example.com">http://example.com</a>いまなにしてる')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_lots_of_punctuation(self):
result = self.parser.parse(u'text http://xo.com/~matthew+%-,.;x')
self.assertEqual(result.html, u'text <a href="http://xo.com/~matthew+%-,.;x">http://xo.com/~matthew+%-,.;x</a>')
self.assertEqual(result.urls, [u'http://xo.com/~matthew+%-,.;x'])
def test_url_question_numbers(self):
result = self.parser.parse(u'text http://example.com/?77e8fd')
self.assertEqual(result.html, u'text <a href="http://example.com/?77e8fd">http://example.com/?77e8fd</a>')
self.assertEqual(result.urls, [u'http://example.com/?77e8fd'])
def test_url_one_letter_other(self):
result = self.parser.parse(u'text http://u.nu/')
self.assertEqual(result.html, u'text <a href="http://u.nu/">http://u.nu/</a>')
self.assertEqual(result.urls, [u'http://u.nu/'])
result = self.parser.parse(u'text http://u.tv/')
self.assertEqual(result.html, u'text <a href="http://u.tv/">http://u.tv/</a>')
self.assertEqual(result.urls, [u'http://u.tv/'])
def test_url_one_letter_iana(self):
result = self.parser.parse(u'text http://x.com/')
self.assertEqual(result.html, u'text <a href="http://x.com/">http://x.com/</a>')
self.assertEqual(result.urls, [u'http://x.com/'])
result = self.parser.parse(u'text http://Q.com/')
self.assertEqual(result.html, u'text <a href="http://Q.com/">http://Q.com/</a>')
self.assertEqual(result.urls, [u'http://Q.com/'])
result = self.parser.parse(u'text http://z.com/')
self.assertEqual(result.html, u'text <a href="http://z.com/">http://z.com/</a>')
self.assertEqual(result.urls, [u'http://z.com/'])
result = self.parser.parse(u'text http://i.net/')
self.assertEqual(result.html, u'text <a href="http://i.net/">http://i.net/</a>')
self.assertEqual(result.urls, [u'http://i.net/'])
result = self.parser.parse(u'text http://q.net/')
self.assertEqual(result.html, u'text <a href="http://q.net/">http://q.net/</a>')
self.assertEqual(result.urls, [u'http://q.net/'])
result = self.parser.parse(u'text http://X.org/')
self.assertEqual(result.html, u'text <a href="http://X.org/">http://X.org/</a>')
self.assertEqual(result.urls, [u'http://X.org/'])
def test_url_long_hypens(self):
result = self.parser.parse(u'text http://word-and-a-number-8-ftw.domain.tld/')
self.assertEqual(result.html, u'text <a href="http://word-and-a-number-8-ftw.domain.tld/">http://word-and-a-number-8-...</a>')
self.assertEqual(result.urls, [u'http://word-and-a-number-8-ftw.domain.tld/'])
# URL not tests ------------------------------------------------------------
def test_not_url_dotdotdot(self):
result = self.parser.parse(u'Is www...foo a valid URL?')
self.assertEqual(result.html, u'Is www...foo a valid URL?')
self.assertEqual(result.urls, [])
def test_not_url_dash(self):
result = self.parser.parse(u'Is www.-foo.com a valid URL?')
self.assertEqual(result.html, u'Is www.-foo.com a valid URL?')
self.assertEqual(result.urls, [])
def test_not_url_no_tld(self):
result = self.parser.parse(u'Is http://no-tld a valid URL?')
self.assertEqual(result.html, u'Is http://no-tld a valid URL?')
self.assertEqual(result.urls, [])
def test_not_url_tld_too_short(self):
result = self.parser.parse(u'Is http://tld-too-short.x a valid URL?')
self.assertEqual(result.html, u'Is http://tld-too-short.x a valid URL?')
self.assertEqual(result.urls, [])
def test_all_not_break_url_at(self):
result = self.parser.parse(u'http://www.flickr.com/photos/29674651@N00/4382024406')
self.assertEqual(result.html, u'<a href="http://www.flickr.com/photos/29674651@N00/4382024406">http://www.flickr.com/photo...</a>')
self.assertEqual(result.urls, [u'http://www.flickr.com/photos/29674651@N00/4382024406'])
def test_not_url_one_letter_iana(self):
result = self.parser.parse(u'text http://a.com/ http://a.net/ http://a.org/')
self.assertEqual(result.html, u'text http://a.com/ http://a.net/ http://a.org/')
self.assertEqual(result.urls, [])
# URL followed Tests -------------------------------------------------------
def test_url_followed_question(self):
result = self.parser.parse(u'text http://example.com?')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>?')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_colon(self):
result = self.parser.parse(u'text http://example.com:')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>:')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_curly_brace(self):
result = self.parser.parse(u'text http://example.com}')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>}')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_single_quote(self):
result = self.parser.parse(u'text http://example.com')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_dot(self):
result = self.parser.parse(u'text http://example.com.')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>.')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_exclamation(self):
result = self.parser.parse(u'text http://example.com!')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>!')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_comma(self):
result = self.parser.parse(u'text http://example.com,')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>,')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_brace(self):
result = self.parser.parse(u'text http://example.com)')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>)')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_big_brace(self):
result = self.parser.parse(u'text http://example.com]')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>]')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_equals(self):
result = self.parser.parse(u'text http://example.com=')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>=')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_semicolon(self):
result = self.parser.parse(u'text http://example.com;')
self.assertEqual(result.html, u'text <a href="http://example.com">http://example.com</a>;')
self.assertEqual(result.urls, [u'http://example.com'])
def test_url_followed_hypen(self):
result = self.parser.parse(u'text http://domain.tld-that-you-should-have-put-a-space-after')
self.assertEqual(result.html, u'text <a href="http://domain.tld">http://domain.tld</a>-that-you-should-have-put-a-space-after')
self.assertEqual(result.urls, [u'http://domain.tld'])
# URL preceeded Tests -------------------------------------------------------
def test_url_preceeded_colon(self):
result = self.parser.parse(u'text:http://example.com')
self.assertEqual(result.html, u'text:<a href="http://example.com">http://example.com</a>')
self.assertEqual(result.urls, [u'http://example.com'])
def test_not_url_preceeded_equals(self):
result = self.parser.parse(u'text =http://example.com')
self.assertEqual(result.html, u'text =http://example.com')
self.assertEqual(result.urls, [])
# NOT
def test_not_url_preceeded_forwardslash(self):
result = self.parser.parse(u'text /http://example.com')
self.assertEqual(result.html, u'text /http://example.com')
self.assertEqual(result.urls, [])
def test_not_url_preceeded_exclamation(self):
result = self.parser.parse(u'text !http://example.com')
self.assertEqual(result.html, u'text !http://example.com')
self.assertEqual(result.urls, [])
# URL numeric tests --------------------------------------------------------
def test_url_at_numeric(self):
result = self.parser.parse(u'http://www.flickr.com/photos/29674651@N00/4382024406')
self.assertEqual(result.html, u'<a href="http://www.flickr.com/photos/29674651@N00/4382024406">http://www.flickr.com/photo...</a>')
self.assertEqual(result.urls, [u'http://www.flickr.com/photos/29674651@N00/4382024406'])
def test_url_at_non_numeric(self):
result = self.parser.parse(u'http://www.flickr.com/photos/29674651@N00/foobar')
self.assertEqual(result.html, u'<a href="http://www.flickr.com/photos/29674651@N00/foobar">http://www.flickr.com/photo...</a>')
self.assertEqual(result.urls, [u'http://www.flickr.com/photos/29674651@N00/foobar'])
# URL domain tests ---------------------------------------------------------
def test_url_WWW(self):
result = self.parser.parse(u'WWW.EXAMPLE.COM')
self.assertEqual(result.html, u'<a href="http://WWW.EXAMPLE.COM">WWW.EXAMPLE.COM</a>')
self.assertEqual(result.urls, [u'WWW.EXAMPLE.COM'])
def test_url_www(self):
result = self.parser.parse(u'www.example.com')
self.assertEqual(result.html, u'<a href="http://www.example.com">www.example.com</a>')
self.assertEqual(result.urls, [u'www.example.com'])
def test_url_only_domain_query_followed_period(self):
result = self.parser.parse(u'I think it\'s proper to end sentences with a period http://tell.me/why?=because.i.want.it. Even when they contain a URL.')
self.assertEqual(result.html, u'I think it\'s proper to end sentences with a period <a href="http://tell.me/why?=because.i.want.it">http://tell.me/why?=because...</a>. Even when they contain a URL.')
self.assertEqual(result.urls, [u'http://tell.me/why?=because.i.want.it'])
def test_url_only_domain_followed_period(self):
result = self.parser.parse(u'I think it\'s proper to end sentences with a period http://tell.me. Even when they contain a URL.')
self.assertEqual(result.html, u'I think it\'s proper to end sentences with a period <a href="http://tell.me">http://tell.me</a>. Even when they contain a URL.')
self.assertEqual(result.urls, [u'http://tell.me'])
def test_url_only_domain_path_followed_period(self):
result = self.parser.parse(u'I think it\'s proper to end sentences with a period http://tell.me/why. Even when they contain a URL.')
self.assertEqual(result.html, u'I think it\'s proper to end sentences with a period <a href="http://tell.me/why">http://tell.me/why</a>. Even when they contain a URL.')
self.assertEqual(result.urls, [u'http://tell.me/why'])
def test_url_long_tld(self):
result = self.parser.parse(u'http://example.mobi/path')
self.assertEqual(result.html, u'<a href="http://example.mobi/path">http://example.mobi/path</a>')
self.assertEqual(result.urls, [u'http://example.mobi/path'])
def test_url_multiple_protocols(self):
result = self.parser.parse(u'http://foo.com AND https://bar.com AND www.foobar.com')
self.assertEqual(result.html, u'<a href="http://foo.com">http://foo.com</a> AND <a href="https://bar.com">https://bar.com</a> AND <a href="http://www.foobar.com">www.foobar.com</a>')
self.assertEqual(result.urls, [u'http://foo.com', u'https://bar.com', u'www.foobar.com'])
# NOT
def test_not_url_exclamation_domain(self):
result = self.parser.parse(u'badly formatted http://foo!bar.com')
self.assertEqual(result.html, u'badly formatted http://foo!bar.com')
self.assertEqual(result.urls, [])
def test_not_url_under_domain(self):
result = self.parser.parse(u'badly formatted http://foo_bar.com')
self.assertEqual(result.html, u'badly formatted http://foo_bar.com')
self.assertEqual(result.urls, [])
# Hashtag tests ------------------------------------------------------------
# --------------------------------------------------------------------------
def test_hashtag_followed_full_whitespace(self):
result = self.parser.parse(u'#hashtag text')
self.assertEqual(result.html, u'<a href="http://search.twitter.com/search?q=%23hashtag">#hashtag</a> text')
self.assertEqual(result.tags, [u'hashtag'])
def test_hashtag_followed_full_hash(self):
result = self.parser.parse(u'#hashtag')
self.assertEqual(result.html, u'<a href="http://search.twitter.com/search?q=%23hashtag">#hashtag</a>')
self.assertEqual(result.tags, [u'hashtag'])
def test_hashtag_preceeded_full_whitespace(self):
result = self.parser.parse(u'text #hashtag')
self.assertEqual(result.html, u'text <a href="http://search.twitter.com/search?q=%23hashtag">#hashtag</a>')
self.assertEqual(result.tags, [u'hashtag'])
def test_hashtag_number(self):
result = self.parser.parse(u'text #1tag')
self.assertEqual(result.html, u'text <a href="http://search.twitter.com/search?q=%231tag">#1tag</a>')
self.assertEqual(result.tags, [u'1tag'])
def test_not_hashtag_escape(self):
result = self.parser.parse(u'&#nbsp;')
self.assertEqual(result.html, u'&#nbsp;')
self.assertEqual(result.tags, [])
def test_hashtag_japanese(self):
result = self.parser.parse(u'text #hashtagの')
self.assertEqual(result.html, u'text <a href="http://search.twitter.com/search?q=%23hashtag">#hashtag</a>の')
self.assertEqual(result.tags, [u'hashtag'])
def test_hashtag_period(self):
result = self.parser.parse(u'text.#hashtag')
self.assertEqual(result.html, u'text.<a href="http://search.twitter.com/search?q=%23hashtag">#hashtag</a>')
self.assertEqual(result.tags, [u'hashtag'])
def test_hashtag_trailing(self):
result = self.parser.parse(u'text #hashtag')
self.assertEqual(result.html, u'text <a href="http://search.twitter.com/search?q=%23hashtag">#hashtag</a>')
self.assertEqual(result.tags, [u'hashtag'])
def test_not_hashtag_exclamation(self):
result = self.parser.parse(u'text #hashtag!')
self.assertEqual(result.html, u'text <a href="http://search.twitter.com/search?q=%23hashtag">#hashtag</a>!')
self.assertEqual(result.tags, [u'hashtag'])
def test_hashtag_multiple(self):
result = self.parser.parse(u'text #hashtag1 #hashtag2')
self.assertEqual(result.html, u'text <a href="http://search.twitter.com/search?q=%23hashtag1">#hashtag1</a> <a href="http://search.twitter.com/search?q=%23hashtag2">#hashtag2</a>')
self.assertEqual(result.tags, [u'hashtag1', u'hashtag2'])
def test_not_hashtag_number(self):
result = self.parser.parse(u'text #1234')
self.assertEqual(result.html, u'text #1234')
self.assertEqual(result.tags, [])
def test_not_hashtag_text(self):
result = self.parser.parse(u'text#hashtag')
self.assertEqual(result.html, u'text#hashtag')
self.assertEqual(result.tags, [])
def test_hashtag_umlaut(self):
result = self.parser.parse(u'text #hash_tagüäö')
self.assertEqual(result.html, u'text <a href="http://search.twitter.com/search?q=%23hash_tag%C3%BC%C3%A4%C3%B6">#hash_tagüäö</a>')
self.assertEqual(result.tags, [u'hash_tag\xfc\xe4\xf6'])
def test_hashtag_alpha(self):
result = self.parser.parse(u'text #hash0tag')
self.assertEqual(result.html, u'text <a href="http://search.twitter.com/search?q=%23hash0tag">#hash0tag</a>')
self.assertEqual(result.tags, [u'hash0tag'])
def test_hashtag_under(self):
result = self.parser.parse(u'text #hash_tag')
self.assertEqual(result.html, u'text <a href="http://search.twitter.com/search?q=%23hash_tag">#hash_tag</a>')
self.assertEqual(result.tags, [u'hash_tag'])
# Username tests -----------------------------------------------------------
# --------------------------------------------------------------------------
def test_not_username_preceded_letter(self):
result = self.parser.parse(u'meet@the beach')
self.assertEqual(result.html, u'meet@the beach')
self.assertEqual(result.users, [])
def test_username_preceded_punctuation(self):
result = self.parser.parse(u'.@username')
self.assertEqual(result.html, u'.<a href="http://twitter.com/username">@username</a>')
self.assertEqual(result.users, [u'username'])
def test_username_preceded_japanese(self):
result = self.parser.parse(u'あ@username')
self.assertEqual(result.html, u'あ<a href="http://twitter.com/username">@username</a>')
self.assertEqual(result.users, [u'username'])
def test_username_followed_japanese(self):
result = self.parser.parse(u'@usernameの')
self.assertEqual(result.html, u'<a href="http://twitter.com/username">@username</a>の')
self.assertEqual(result.users, [u'username'])
def test_username_surrounded_japanese(self):
result = self.parser.parse(u'あ@usernameの')
self.assertEqual(result.html, u'あ<a href="http://twitter.com/username">@username</a>の')
self.assertEqual(result.users, [u'username'])
def test_username_followed_punctuation(self):
result = self.parser.parse(u'@username&^$%^')
self.assertEqual(result.html, u'<a href="http://twitter.com/username">@username</a>&^$%^')
self.assertEqual(result.users, [u'username'])
def test_not_username_spaced(self):
result = self.parser.parse(u'@ username')
self.assertEqual(result.html, u'@ username')
self.assertEqual(result.users, [])
def test_username_beginning(self):
result = self.parser.parse(u'@username text')
self.assertEqual(result.html, u'<a href="http://twitter.com/username">@username</a> text')
self.assertEqual(result.users, [u'username'])
def test_username_to_long(self):
result = self.parser.parse(u'@username9012345678901')
self.assertEqual(result.html, u'<a href="http://twitter.com/username901234567890">@username901234567890</a>1')
self.assertEqual(result.users, [u'username901234567890'])
def test_username_full_at_sign(self):
result = self.parser.parse(u'@username')
self.assertEqual(result.html, u'<a href="http://twitter.com/username">@username</a>')
self.assertEqual(result.users, [u'username'])
def test_username_trailing(self):
result = self.parser.parse(u'text @username')
self.assertEqual(result.html, u'text <a href="http://twitter.com/username">@username</a>')
self.assertEqual(result.users, [u'username'])
# Replies
def test_username_reply_simple(self):
result = self.parser.parse(u'@username')
self.assertEqual(result.html, u'<a href="http://twitter.com/username">@username</a>')
self.assertEqual(result.users, [u'username'])
self.assertEqual(result.reply, u'username')
def test_username_reply_whitespace(self):
result = self.parser.parse(u' @username')
self.assertEqual(result.html, u' <a href="http://twitter.com/username">@username</a>')
self.assertEqual(result.users, [u'username'])
self.assertEqual(result.reply, u'username')
def test_username_reply_full(self):
result = self.parser.parse(u' @username')
self.assertEqual(result.html, u' <a href="http://twitter.com/username">@username</a>')
self.assertEqual(result.users, [u'username'])
self.assertEqual(result.reply, u'username')
def test_username_non_reply(self):
result = self.parser.parse(u'test @username')
self.assertEqual(result.html, u'test <a href="http://twitter.com/username">@username</a>')
self.assertEqual(result.users, [u'username'])
self.assertEqual(result.reply, None)
# List tests ---------------------------------------------------------------
# --------------------------------------------------------------------------
def test_list_preceeded(self):
result = self.parser.parse(u'text @username/list')
self.assertEqual(result.html, u'text <a href="http://twitter.com/username/list">@username/list</a>')
self.assertEqual(result.lists, [(u'username', u'list')])
def test_list_beginning(self):
result = self.parser.parse(u'@username/list')
self.assertEqual(result.html, u'<a href="http://twitter.com/username/list">@username/list</a>')
self.assertEqual(result.lists, [(u'username', u'list')])
def test_list_preceeded_punctuation(self):
result = self.parser.parse(u'.@username/list')
self.assertEqual(result.html, u'.<a href="http://twitter.com/username/list">@username/list</a>')
self.assertEqual(result.lists, [(u'username', u'list')])
def test_list_followed_punctuation(self):
result = self.parser.parse(u'@username/list&^$%^')
self.assertEqual(result.html, u'<a href="http://twitter.com/username/list">@username/list</a>&^$%^')
self.assertEqual(result.lists, [(u'username', u'list')])
def test_list_not_slash_space(self):
result = self.parser.parse(u'@username/ list')
self.assertEqual(result.html, u'<a href="http://twitter.com/username">@username</a>/ list')
self.assertEqual(result.users, [u'username'])
self.assertEqual(result.lists, [])
def test_list_beginning(self):
result = self.parser.parse(u'@username/list')
self.assertEqual(result.html, u'<a href="http://twitter.com/username/list">@username/list</a>')
self.assertEqual(result.lists, [(u'username', u'list')])
def test_list_not_empty_username(self):
result = self.parser.parse(u'text @/list')
self.assertEqual(result.html, u'text @/list')
self.assertEqual(result.lists, [])
def test_list_not_preceeded_letter(self):
result = self.parser.parse(u'meet@the/beach')
self.assertEqual(result.html, u'meet@the/beach')
self.assertEqual(result.lists, [])
def test_list_long_truncate(self):
result = self.parser.parse(u'@username/list5678901234567890123456789012345678901234567890123456789012345678901234567890A')
self.assertEqual(result.html, u'<a href="http://twitter.com/username/list5678901234567890123456789012345678901234567890123456789012345678901234567890">@username/list5678901234567890123456789012345678901234567890123456789012345678901234567890</a>A')
self.assertEqual(result.lists, [(u'username', u'list5678901234567890123456789012345678901234567890123456789012345678901234567890')])
def test_list_with_dash(self):
result = self.parser.parse(u'text @username/list-foo')
self.assertEqual(result.html, u'text <a href="http://twitter.com/username/list-foo">@username/list-foo</a>')
self.assertEqual(result.lists, [(u'username', u'list-foo')])
# Test it!
if __name__ == '__main__':
unittest.main()