|
1 | 1 | import json
|
2 | 2 | import sys
|
| 3 | +from unittest.mock import patch |
3 | 4 |
|
4 | 5 | from django.core.exceptions import SuspiciousFileOperation
|
5 | 6 | from django.test import SimpleTestCase
|
@@ -94,11 +95,17 @@ def test_truncate_chars(self):
|
94 | 95 | text.Truncator(lazystr("The quick brown fox")).chars(10), "The quick…"
|
95 | 96 | )
|
96 | 97 |
|
97 |
| - def test_truncate_chars_html(self): |
| 98 | + @patch("django.utils.text.Truncator.MAX_LENGTH_HTML", 10_000) |
| 99 | + def test_truncate_chars_html_size_limit(self): |
| 100 | + max_len = text.Truncator.MAX_LENGTH_HTML |
| 101 | + bigger_len = text.Truncator.MAX_LENGTH_HTML + 1 |
| 102 | + valid_html = "<p>Joel is a slug</p>" # 14 chars |
98 | 103 | perf_test_values = [
|
99 |
| - (("</a" + "\t" * 50000) + "//>", None), |
100 |
| - ("&" * 50000, "&" * 9 + "…"), |
| 104 | + ("</a" + "\t" * (max_len - 6) + "//>", None), |
| 105 | + ("</p" + "\t" * bigger_len + "//>", "</p" + "\t" * 6 + "…"), |
| 106 | + ("&" * bigger_len, "&" * 9 + "…"), |
101 | 107 | ("_X<<<<<<<<<<<>", None),
|
| 108 | + (valid_html * bigger_len, "<p>Joel is a…</p>"), # 10 chars |
102 | 109 | ]
|
103 | 110 | for value, expected in perf_test_values:
|
104 | 111 | with self.subTest(value=value):
|
@@ -176,15 +183,25 @@ def test_truncate_html_words(self):
|
176 | 183 | truncator = text.Truncator("<p>I <3 python, what about you?</p>")
|
177 | 184 | self.assertEqual("<p>I <3 python,…</p>", truncator.words(3, html=True))
|
178 | 185 |
|
| 186 | + @patch("django.utils.text.Truncator.MAX_LENGTH_HTML", 10_000) |
| 187 | + def test_truncate_words_html_size_limit(self): |
| 188 | + max_len = text.Truncator.MAX_LENGTH_HTML |
| 189 | + bigger_len = text.Truncator.MAX_LENGTH_HTML + 1 |
| 190 | + valid_html = "<p>Joel is a slug</p>" # 4 words |
179 | 191 | perf_test_values = [
|
180 |
| - ("</a" + "\t" * 50000) + "//>", |
181 |
| - "&" * 50000, |
182 |
| - "_X<<<<<<<<<<<>", |
| 192 | + ("</a" + "\t" * (max_len - 6) + "//>", None), |
| 193 | + ("</p" + "\t" * bigger_len + "//>", "</p" + "\t" * (max_len - 3) + "…"), |
| 194 | + ("&" * max_len, None), # no change |
| 195 | + ("&" * bigger_len, "&" * max_len + "…"), |
| 196 | + ("_X<<<<<<<<<<<>", None), |
| 197 | + (valid_html * bigger_len, valid_html * 12 + "<p>Joel is…</p>"), # 50 words |
183 | 198 | ]
|
184 |
| - for value in perf_test_values: |
| 199 | + for value, expected in perf_test_values: |
185 | 200 | with self.subTest(value=value):
|
186 | 201 | truncator = text.Truncator(value)
|
187 |
| - self.assertEqual(value, truncator.words(50, html=True)) |
| 202 | + self.assertEqual( |
| 203 | + expected if expected else value, truncator.words(50, html=True) |
| 204 | + ) |
188 | 205 |
|
189 | 206 | def test_wrap(self):
|
190 | 207 | digits = "1234 67 9"
|
|
0 commit comments