Code

Ticket #1919: truncate_tests.diff

File truncate_tests.diff, 2.0 KB (added by deryck, 7 years ago)

Tests for django.utils.text.trunacte_*.

Line 
1Index: tests/regressiontests/utils/tests.py
2===================================================================
3--- tests/regressiontests/utils/tests.py        (revision 6401)
4+++ tests/regressiontests/utils/tests.py        (working copy)
5@@ -7,6 +7,7 @@
6 from django.utils import html
7 
8 from timesince import timesince_tests
9+from truncate import truncate_tests
10 
11 class TestUtilsHtml(TestCase):
12 
13@@ -118,6 +119,7 @@
14 
15 __test__ = {
16     'timesince_tests': timesince_tests,
17+    'truncate_tests': truncate_tests,
18 }
19 
20 if __name__ == "__main__":
21Index: tests/regressiontests/utils/truncate.py
22===================================================================
23--- tests/regressiontests/utils/truncate.py     (revision 0)
24+++ tests/regressiontests/utils/truncate.py     (revision 0)
25@@ -0,0 +1,32 @@
26+truncate_tests = """
27+>>> from django.utils.text import truncate_words, truncate_html_words
28+
29+>>> text = \"\"\"modo suscipit seacula molestie legere
30+... duis odio in demonstraverunt eu
31+... iusto eum elit ea anteposuerit\"\"\"
32+>>>
33+>>> html = \"\"\"<p>modo suscipit <a href="/somelink/">seacula</a> molestie legere
34+... duis odio in demonstraverunt <strong>eu</strong>
35+... iusto eum elit ea anteposuerit\"\"\"
36+>>>
37+
38+# truncate_words normalizes line endings to a single space
39+>>> print truncate_words(text, 10)
40+modo suscipit seacula molestie legere duis odio in demonstraverunt eu ...
41+
42+# truncate_words uses the space to determine word count
43+# and doesn't deal with html.
44+>>> print truncate_words(html, 10)
45+<p>modo suscipit <a href="/somelink/">seacula</a> molestie legere duis odio in demonstraverunt ...
46+
47+# tuncate_html_words preserves line endings and html.  Tags
48+# are closed at the end of the newly tuncated text.
49+>>> print truncate_html_words(text, 10)
50+modo suscipit seacula molestie legere
51+duis odio in demonstraverunt eu ...
52+>>>
53+>>> print truncate_html_words(html, 10)
54+<p>modo suscipit <a href="/somelink/">seacula</a> molestie legere
55+duis odio in demonstraverunt <strong>eu ...</strong></p>
56+>>>
57+"""