forked from eentzel/htmltruncate.py
/
tests.py
executable file
·46 lines (38 loc) · 2.9 KB
/
tests.py
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
#!/usr/bin/python
import htmltruncate
import unittest
class TruncateTest(unittest.TestCase):
cases = ( ('this <b>word</b> is bolded', 4, "this"),
('this <b>word</b> is bolded', 6, "this <b>w</b>"),
('this <b>word</b> is bolded', 8, "this <b>wor</b>"),
('this <b>word</b> is bolded', 10, "this <b>word</b> "),
('this <b>word</b> is bolded', 700, "this <b>word</b> is bolded"),
('the second tag <span class="hello">is closed, but is the <a href="/test.html">first one</a> closed too?</span>', 52,
'the second tag <span class="hello">is closed, but is the <a href="/test.html">first one</a> close</span>'),
("This is a test of the truncating feature in EDCZ™ please use with caution.", 65,
"This is a test of the truncating feature in EDCZ™ please use with"),
("<p>Well here's another test of truncation <span>with</span> a little bit o markup and a bunch more stuff</p>", 65,
"<p>Well here's another test of truncation <span>with</span> a little bit o markup</p>"),
("This is a test of the truncating feature in EDCZ™ please <span>use</span> with caution.", 65,
"This is a test of the truncating feature in EDCZ™ please <span>use</span> with"),
("This is a test of the truncating features in EDCZ please use <span>with</span> caution <span>more with</span>.", 65,
"This is a test of the truncating features in EDCZ please use <span>with</span>"),
("This is a test of the truncating features in EDCZ please use <span>with caution</span>", 65,
"This is a test of the truncating features in EDCZ please use <span>with</span>"),
("<span>This</span> is a test of the truncating features in EDCZ please use <span>with caution</span>", 65,
"<span>This</span> is a test of the truncating features in EDCZ please use <span>with</span>"),
("And this baby right here is the special last line that get's chopped a little shorter", 55,
"And this baby right here is the special last line that ") )
def testTruncation(self):
for input, count, output in self.cases:
self.assertEqual( htmltruncate.truncate(input, count), output )
def testUnbalanced(self):
self.assertRaises( htmltruncate.UnbalancedError, htmltruncate.truncate, 'I am a <b>bad</strong> little string with unbalanced tags', 20 )
def testEntity(self):
self.assertEqual( htmltruncate.truncate( "I'm one", 3 ), "I'm" )
def testSelfClosing(self):
self.assertEqual( htmltruncate.truncate( "I need<br /> a break", 11 ), "I need<br /> a br" )
def testEllipsis(self):
self.assertEqual( htmltruncate.truncate('this <b>word</b> is bolded', 10, '...' ), "this <b>word</b> ...")
if __name__ == "__main__":
unittest.main()