summaryrefslogtreecommitdiff
path: root/tests/template_tests
diff options
context:
space:
mode:
authorMatti Pohjanvirta <matti.pohjanvirta@iki.fi>2025-04-20 18:22:51 +0300
committernessita <124304+nessita@users.noreply.github.com>2025-04-23 16:14:03 -0300
commit1e9db35836d42a3c72f3d1015c2f302eb6fee046 (patch)
tree30700b697b08ee8e9058b0bde76dfdd22959c7a2 /tests/template_tests
parent18fa74fc88bdbdea0a370f7ab0f348c2a2339092 (diff)
Fixed #36341 -- Preserved whitespaces in wordwrap template filter.
Regression in 55d89e25f4115c5674cdd9b9bcba2bb2bb6d820b. This work improves the django.utils.text.wrap() function to ensure that empty lines and lines with whitespace only are kept instead of being dropped. Thanks Matti Pohjanvirta for the report and fix. Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Diffstat (limited to 'tests/template_tests')
-rw-r--r--tests/template_tests/filter_tests/test_wordwrap.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/template_tests/filter_tests/test_wordwrap.py b/tests/template_tests/filter_tests/test_wordwrap.py
index 4afa1dd234..1692332e1e 100644
--- a/tests/template_tests/filter_tests/test_wordwrap.py
+++ b/tests/template_tests/filter_tests/test_wordwrap.py
@@ -89,3 +89,44 @@ class FunctionTests(SimpleTestCase):
"I'm afraid",
wordwrap(long_text, 10),
)
+
+ def test_wrap_preserve_newlines(self):
+ cases = [
+ (
+ "this is a long paragraph of text that really needs to be wrapped\n\n"
+ "that is followed by another paragraph separated by an empty line\n",
+ "this is a long paragraph of\ntext that really needs to be\nwrapped\n\n"
+ "that is followed by another\nparagraph separated by an\nempty line\n",
+ 30,
+ ),
+ ("\n\n\n", "\n\n\n", 5),
+ ("\n\n\n\n\n\n", "\n\n\n\n\n\n", 5),
+ ]
+ for text, expected, width in cases:
+ with self.subTest(text=text):
+ self.assertEqual(wordwrap(text, width), expected)
+
+ def test_wrap_preserve_whitespace(self):
+ width = 5
+ width_spaces = " " * width
+ cases = [
+ (
+ f"first line\n{width_spaces}\nsecond line",
+ f"first\nline\n{width_spaces}\nsecond\nline",
+ ),
+ (
+ "first line\n \t\t\t \nsecond line",
+ "first\nline\n \t\t\t \nsecond\nline",
+ ),
+ (
+ f"first line\n{width_spaces}\nsecond line\n\nthird{width_spaces}\n",
+ f"first\nline\n{width_spaces}\nsecond\nline\n\nthird\n",
+ ),
+ (
+ f"first line\n{width_spaces}{width_spaces}\nsecond line",
+ f"first\nline\n{width_spaces}{width_spaces}\nsecond\nline",
+ ),
+ ]
+ for text, expected in cases:
+ with self.subTest(text=text):
+ self.assertEqual(wordwrap(text, width), expected)