File: test_text.py

package info (click to toggle)
python-django 3%3A5.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,236 kB
  • sloc: python: 361,585; javascript: 19,250; xml: 211; makefile: 182; sh: 28
file content (465 lines) | stat: -rw-r--r-- 19,913 bytes parent folder | download
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
import json
import sys
from unittest.mock import patch

from django.core.exceptions import SuspiciousFileOperation
from django.test import SimpleTestCase
from django.utils import text
from django.utils.functional import lazystr
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy, override

IS_WIDE_BUILD = len("\U0001f4a9") == 1


class TestUtilsText(SimpleTestCase):
    def test_get_text_list(self):
        self.assertEqual(text.get_text_list(["a", "b", "c", "d"]), "a, b, c or d")
        self.assertEqual(text.get_text_list(["a", "b", "c"], "and"), "a, b and c")
        self.assertEqual(text.get_text_list(["a", "b"], "and"), "a and b")
        self.assertEqual(text.get_text_list(["a"]), "a")
        self.assertEqual(text.get_text_list([]), "")
        with override("ar"):
            self.assertEqual(text.get_text_list(["a", "b", "c"]), "a، b أو c")

    def test_smart_split(self):
        testdata = [
            ('This is "a person" test.', ["This", "is", '"a person"', "test."]),
            ('This is "a person\'s" test.', ["This", "is", '"a person\'s"', "test."]),
            ('This is "a person\\"s" test.', ["This", "is", '"a person\\"s"', "test."]),
            ("\"a 'one", ['"a', "'one"]),
            ("all friends' tests", ["all", "friends'", "tests"]),
            (
                'url search_page words="something else"',
                ["url", "search_page", 'words="something else"'],
            ),
            (
                "url search_page words='something else'",
                ["url", "search_page", "words='something else'"],
            ),
            (
                'url search_page words "something else"',
                ["url", "search_page", "words", '"something else"'],
            ),
            (
                'url search_page words-"something else"',
                ["url", "search_page", 'words-"something else"'],
            ),
            ("url search_page words=hello", ["url", "search_page", "words=hello"]),
            (
                'url search_page words="something else',
                ["url", "search_page", 'words="something', "else"],
            ),
            ("cut:','|cut:' '", ["cut:','|cut:' '"]),
            (lazystr("a b c d"), ["a", "b", "c", "d"]),  # Test for #20231
        ]
        for test, expected in testdata:
            with self.subTest(value=test):
                self.assertEqual(list(text.smart_split(test)), expected)

    def test_truncate_chars(self):
        truncator = text.Truncator("The quick brown fox jumped over the lazy dog.")
        self.assertEqual(
            "The quick brown fox jumped over the lazy dog.", truncator.chars(100)
        ),
        self.assertEqual("The quick brown fox …", truncator.chars(21))
        self.assertEqual("The quick brown fo.....", truncator.chars(23, "....."))
        self.assertEqual(".....", truncator.chars(4, "....."))

        nfc = text.Truncator("o\xfco\xfco\xfco\xfc")
        nfd = text.Truncator("ou\u0308ou\u0308ou\u0308ou\u0308")
        self.assertEqual("oüoüoüoü", nfc.chars(8))
        self.assertEqual("oüoüoüoü", nfd.chars(8))
        self.assertEqual("oü…", nfc.chars(3))
        self.assertEqual("oü…", nfd.chars(3))

        # Ensure the final length is calculated correctly when there are
        # combining characters with no precomposed form, and that combining
        # characters are not split up.
        truncator = text.Truncator("-B\u030aB\u030a----8")
        self.assertEqual("-B\u030a…", truncator.chars(3))
        self.assertEqual("-B\u030aB\u030a-…", truncator.chars(5))
        self.assertEqual("-B\u030aB\u030a----8", truncator.chars(8))

        # Ensure the length of the end text is correctly calculated when it
        # contains combining characters with no precomposed form.
        truncator = text.Truncator("-----")
        self.assertEqual("---B\u030a", truncator.chars(4, "B\u030a"))
        self.assertEqual("-----", truncator.chars(5, "B\u030a"))

        # Make a best effort to shorten to the desired length, but requesting
        # a length shorter than the ellipsis shouldn't break
        self.assertEqual("...", text.Truncator("asdf").chars(1, truncate="..."))
        # lazy strings are handled correctly
        self.assertEqual(
            text.Truncator(lazystr("The quick brown fox")).chars(10), "The quick…"
        )

    def test_truncate_chars_html(self):
        truncator = text.Truncator(
            '<p id="par"><strong><em>The quick brown fox jumped over the lazy dog.</em>'
            "</strong></p>"
        )
        self.assertEqual(
            '<p id="par"><strong><em>The quick brown fox jumped over the lazy dog.</em>'
            "</strong></p>",
            truncator.chars(80, html=True),
        )
        self.assertEqual(
            '<p id="par"><strong><em>The quick brown fox jumped over the lazy dog.</em>'
            "</strong></p>",
            truncator.chars(46, html=True),
        )
        self.assertEqual(
            '<p id="par"><strong><em>The quick brown fox jumped over the lazy dog…</em>'
            "</strong></p>",
            truncator.chars(45, html=True),
        )
        self.assertEqual(
            '<p id="par"><strong><em>The quick…</em></strong></p>',
            truncator.chars(10, html=True),
        )
        self.assertEqual(
            '<p id="par"><strong><em>…</em></strong></p>',
            truncator.chars(1, html=True),
        )
        self.assertEqual("", truncator.chars(0, html=True))
        self.assertEqual("", truncator.chars(-1, html=True))
        self.assertEqual(
            '<p id="par"><strong><em>The qu....</em></strong></p>',
            truncator.chars(10, "....", html=True),
        )
        self.assertEqual(
            '<p id="par"><strong><em>The quick </em></strong></p>',
            truncator.chars(10, "", html=True),
        )
        truncator = text.Truncator("foo</p>")
        self.assertEqual("foo</p>", truncator.chars(5, html=True))

    @patch("django.utils.text.Truncator.MAX_LENGTH_HTML", 10_000)
    def test_truncate_chars_html_size_limit(self):
        max_len = text.Truncator.MAX_LENGTH_HTML
        bigger_len = text.Truncator.MAX_LENGTH_HTML + 1
        valid_html = "<p>Joel is a slug</p>"  # 14 chars
        perf_test_values = [
            ("</a" + "\t" * (max_len - 6) + "//>", "</a>"),
            ("</p" + "\t" * bigger_len + "//>", "</p>"),
            ("&" * bigger_len, ""),
            ("_X<<<<<<<<<<<>", "_X&lt;&lt;&lt;&lt;&lt;&lt;&lt;…"),
            (valid_html * bigger_len, "<p>Joel is a…</p>"),  # 10 chars
        ]
        for value, expected in perf_test_values:
            with self.subTest(value=value):
                truncator = text.Truncator(value)
                self.assertEqual(expected, truncator.chars(10, html=True))

    def test_truncate_chars_html_with_newline_inside_tag(self):
        truncator = text.Truncator(
            '<p>The quick <a href="xyz.html"\n id="mylink">brown fox</a> jumped over '
            "the lazy dog.</p>"
        )
        self.assertEqual(
            '<p>The quick <a href="xyz.html"\n id="mylink">brow…</a></p>',
            truncator.chars(15, html=True),
        )
        self.assertEqual(
            "<p>Th…</p>",
            truncator.chars(3, html=True),
        )

    def test_truncate_chars_html_with_void_elements(self):
        truncator = text.Truncator(
            "<br/>The <hr />quick brown fox jumped over the lazy dog."
        )
        self.assertEqual("<br/>The <hr />quick brown…", truncator.chars(16, html=True))
        truncator = text.Truncator(
            "<br>The <hr/>quick <em>brown fox</em> jumped over the lazy dog."
        )
        self.assertEqual(
            "<br>The <hr/>quick <em>brown…</em>", truncator.chars(16, html=True)
        )
        self.assertEqual("<br>The <hr/>q…", truncator.chars(6, html=True))
        self.assertEqual("<br>The <hr/>…", truncator.chars(5, html=True))
        self.assertEqual("<br>The…", truncator.chars(4, html=True))
        self.assertEqual("<br>Th…", truncator.chars(3, html=True))

    def test_truncate_chars_html_with_html_entities(self):
        truncator = text.Truncator(
            "<i>Buenos d&iacute;as! &#x00bf;C&oacute;mo est&aacute;?</i>"
        )
        self.assertEqual(
            "<i>Buenos días! ¿Cómo está?</i>",
            truncator.chars(40, html=True),
        )
        self.assertEqual(
            "<i>Buenos días…</i>",
            truncator.chars(12, html=True),
        )
        self.assertEqual(
            "<i>Buenos días! ¿Cómo está…</i>",
            truncator.chars(24, html=True),
        )
        truncator = text.Truncator("<p>I &lt;3 python, what about you?</p>")
        self.assertEqual("<p>I &lt;3 python, wh…</p>", truncator.chars(16, html=True))

    def test_truncate_words(self):
        truncator = text.Truncator("The quick brown fox jumped over the lazy dog.")
        self.assertEqual(
            "The quick brown fox jumped over the lazy dog.", truncator.words(10)
        )
        self.assertEqual("The quick brown fox…", truncator.words(4))
        self.assertEqual("The quick brown fox[snip]", truncator.words(4, "[snip]"))
        # lazy strings are handled correctly
        truncator = text.Truncator(
            lazystr("The quick brown fox jumped over the lazy dog.")
        )
        self.assertEqual("The quick brown fox…", truncator.words(4))
        self.assertEqual("", truncator.words(0))
        self.assertEqual("", truncator.words(-1))

    def test_truncate_html_words(self):
        truncator = text.Truncator(
            '<p id="par"><strong><em>The quick brown fox jumped over the lazy dog.</em>'
            "</strong></p>"
        )
        self.assertEqual(
            '<p id="par"><strong><em>The quick brown fox jumped over the lazy dog.</em>'
            "</strong></p>",
            truncator.words(10, html=True),
        )
        self.assertEqual(
            '<p id="par"><strong><em>The quick brown fox…</em></strong></p>',
            truncator.words(4, html=True),
        )
        self.assertEqual(
            "",
            truncator.words(0, html=True),
        )
        self.assertEqual(
            '<p id="par"><strong><em>The quick brown fox....</em></strong></p>',
            truncator.words(4, "....", html=True),
        )
        self.assertEqual(
            '<p id="par"><strong><em>The quick brown fox</em></strong></p>',
            truncator.words(4, "", html=True),
        )

        truncator = text.Truncator(
            "<p>The  quick \t brown fox jumped over the lazy dog.</p>"
        )
        self.assertEqual(
            "<p>The quick brown fox…</p>",
            truncator.words(4, html=True),
        )

        # Test with new line inside tag
        truncator = text.Truncator(
            '<p>The quick <a href="xyz.html"\n id="mylink">brown fox</a> jumped over '
            "the lazy dog.</p>"
        )
        self.assertEqual(
            '<p>The quick <a href="xyz.html"\n id="mylink">brown…</a></p>',
            truncator.words(3, html=True),
        )
        self.assertEqual(
            "<p>The…</p>",
            truncator.words(1, html=True),
        )

        # Test self-closing tags
        truncator = text.Truncator(
            "<br/>The <hr />quick brown fox jumped over the lazy dog."
        )
        self.assertEqual("<br/>The <hr />quick brown…", truncator.words(3, html=True))
        truncator = text.Truncator(
            "<br>The <hr/>quick <em>brown fox</em> jumped over the lazy dog."
        )
        self.assertEqual(
            "<br>The <hr/>quick <em>brown…</em>", truncator.words(3, html=True)
        )

        # Test html entities
        truncator = text.Truncator(
            "<i>Buenos d&iacute;as! &#x00bf;C&oacute;mo est&aacute;?</i>"
        )
        self.assertEqual(
            "<i>Buenos días! ¿Cómo…</i>",
            truncator.words(3, html=True),
        )
        truncator = text.Truncator("<p>I &lt;3 python, what about you?</p>")
        self.assertEqual("<p>I &lt;3 python,…</p>", truncator.words(3, html=True))

        truncator = text.Truncator("foo</p>")
        self.assertEqual("foo</p>", truncator.words(3, html=True))

        # Only open brackets.
        truncator = text.Truncator("<" * 60_000)
        self.assertEqual(truncator.words(1, html=True), "&lt;…")

        # Tags with special chars in attrs.
        truncator = text.Truncator(
            """<i style="margin: 5%; font: *;">Hello, my dear lady!</i>"""
        )
        self.assertEqual(
            """<i style="margin: 5%; font: *;">Hello, my dear…</i>""",
            truncator.words(3, html=True),
        )

        # Tags with special non-latin chars in attrs.
        truncator = text.Truncator("""<p data-x="א">Hello, my dear lady!</p>""")
        self.assertEqual(
            """<p data-x="א">Hello, my dear…</p>""",
            truncator.words(3, html=True),
        )

        # Misplaced brackets.
        truncator = text.Truncator("hello >< world")
        self.assertEqual(truncator.words(1, html=True), "hello…")
        self.assertEqual(truncator.words(2, html=True), "hello &gt;…")
        self.assertEqual(truncator.words(3, html=True), "hello &gt;&lt;…")
        self.assertEqual(truncator.words(4, html=True), "hello &gt;&lt; world")

    @patch("django.utils.text.Truncator.MAX_LENGTH_HTML", 10_000)
    def test_truncate_words_html_size_limit(self):
        max_len = text.Truncator.MAX_LENGTH_HTML
        bigger_len = text.Truncator.MAX_LENGTH_HTML + 1
        valid_html = "<p>Joel is a slug</p>"  # 4 words
        perf_test_values = [
            ("</a" + "\t" * (max_len - 6) + "//>", "</a>"),
            ("</p" + "\t" * bigger_len + "//>", "</p>"),
            ("&" * max_len, ""),
            ("&" * bigger_len, ""),
            ("_X<<<<<<<<<<<>", "_X&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&gt;"),
            (valid_html * bigger_len, valid_html * 12 + "<p>Joel is…</p>"),  # 50 words
        ]
        for value, expected in perf_test_values:
            with self.subTest(value=value):
                truncator = text.Truncator(value)
                self.assertEqual(expected, truncator.words(50, html=True))

    def test_wrap(self):
        digits = "1234 67 9"
        self.assertEqual(text.wrap(digits, 100), "1234 67 9")
        self.assertEqual(text.wrap(digits, 9), "1234 67 9")
        self.assertEqual(text.wrap(digits, 8), "1234 67\n9")

        self.assertEqual(text.wrap("short\na long line", 7), "short\na long\nline")
        self.assertEqual(
            text.wrap("do-not-break-long-words please? ok", 8),
            "do-not-break-long-words\nplease?\nok",
        )

        long_word = "l%sng" % ("o" * 20)
        self.assertEqual(text.wrap(long_word, 20), long_word)
        self.assertEqual(
            text.wrap("a %s word" % long_word, 10), "a\n%s\nword" % long_word
        )
        self.assertEqual(text.wrap(lazystr(digits), 100), "1234 67 9")

    def test_normalize_newlines(self):
        self.assertEqual(
            text.normalize_newlines("abc\ndef\rghi\r\n"), "abc\ndef\nghi\n"
        )
        self.assertEqual(text.normalize_newlines("\n\r\r\n\r"), "\n\n\n\n")
        self.assertEqual(text.normalize_newlines("abcdefghi"), "abcdefghi")
        self.assertEqual(text.normalize_newlines(""), "")
        self.assertEqual(
            text.normalize_newlines(lazystr("abc\ndef\rghi\r\n")), "abc\ndef\nghi\n"
        )

    def test_phone2numeric(self):
        numeric = text.phone2numeric("0800 flowers")
        self.assertEqual(numeric, "0800 3569377")
        lazy_numeric = lazystr(text.phone2numeric("0800 flowers"))
        self.assertEqual(lazy_numeric, "0800 3569377")

    def test_slugify(self):
        items = (
            # given - expected - Unicode?
            ("Hello, World!", "hello-world", False),
            ("spam & eggs", "spam-eggs", False),
            (" multiple---dash and  space ", "multiple-dash-and-space", False),
            ("\t whitespace-in-value \n", "whitespace-in-value", False),
            ("underscore_in-value", "underscore_in-value", False),
            ("__strip__underscore-value___", "strip__underscore-value", False),
            ("--strip-dash-value---", "strip-dash-value", False),
            ("__strip-mixed-value---", "strip-mixed-value", False),
            ("_ -strip-mixed-value _-", "strip-mixed-value", False),
            ("spam & ıçüş", "spam-ıçüş", True),
            ("foo ıç bar", "foo-ıç-bar", True),
            ("    foo ıç bar", "foo-ıç-bar", True),
            ("你好", "你好", True),
            ("İstanbul", "istanbul", True),
        )
        for value, output, is_unicode in items:
            with self.subTest(value=value):
                self.assertEqual(text.slugify(value, allow_unicode=is_unicode), output)
        # Interning the result may be useful, e.g. when fed to Path.
        with self.subTest("intern"):
            self.assertEqual(sys.intern(text.slugify("a")), "a")

    def test_unescape_string_literal(self):
        items = [
            ('"abc"', "abc"),
            ("'abc'", "abc"),
            ('"a "bc""', 'a "bc"'),
            ("''ab' c'", "'ab' c"),
        ]
        for value, output in items:
            with self.subTest(value=value):
                self.assertEqual(text.unescape_string_literal(value), output)
                self.assertEqual(text.unescape_string_literal(lazystr(value)), output)

    def test_unescape_string_literal_invalid_value(self):
        items = ["", "abc", "'abc\""]
        for item in items:
            msg = f"Not a string literal: {item!r}"
            with self.assertRaisesMessage(ValueError, msg):
                text.unescape_string_literal(item)

    def test_get_valid_filename(self):
        filename = "^&'@{}[],$=!-#()%+~_123.txt"
        self.assertEqual(text.get_valid_filename(filename), "-_123.txt")
        self.assertEqual(text.get_valid_filename(lazystr(filename)), "-_123.txt")
        msg = "Could not derive file name from '???'"
        with self.assertRaisesMessage(SuspiciousFileOperation, msg):
            text.get_valid_filename("???")
        # After sanitizing this would yield '..'.
        msg = "Could not derive file name from '$.$.$'"
        with self.assertRaisesMessage(SuspiciousFileOperation, msg):
            text.get_valid_filename("$.$.$")

    def test_compress_sequence(self):
        data = [{"key": i} for i in range(10)]
        seq = list(json.JSONEncoder().iterencode(data))
        seq = [s.encode() for s in seq]
        actual_length = len(b"".join(seq))
        out = text.compress_sequence(seq)
        compressed_length = len(b"".join(out))
        self.assertLess(compressed_length, actual_length)

    def test_format_lazy(self):
        self.assertEqual("django/test", format_lazy("{}/{}", "django", lazystr("test")))
        self.assertEqual("django/test", format_lazy("{0}/{1}", *("django", "test")))
        self.assertEqual(
            "django/test", format_lazy("{a}/{b}", **{"a": "django", "b": "test"})
        )
        self.assertEqual(
            "django/test", format_lazy("{a[0]}/{a[1]}", a=("django", "test"))
        )

        t = {}
        s = format_lazy("{0[a]}-{p[a]}", t, p=t)
        t["a"] = lazystr("django")
        self.assertEqual("django-django", s)
        t["a"] = "update"
        self.assertEqual("update-update", s)

        # The format string can be lazy. (string comes from contrib.admin)
        s = format_lazy(
            gettext_lazy("Added {name} “{object}”."),
            name="article",
            object="My first try",
        )
        with override("fr"):
            self.assertEqual("Ajout de article «\xa0My first try\xa0».", s)