From 48a469395191e87d3b84ad35bae2c8b53d91ed61 Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 3 Jan 2023 08:17:56 +0000 Subject: [PATCH] Refs #30686 -- Improved test coverage of Truncator. --- .../filter_tests/test_truncatechars.py | 5 + tests/utils_tests/test_text.py | 99 +++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/tests/template_tests/filter_tests/test_truncatechars.py b/tests/template_tests/filter_tests/test_truncatechars.py index a444125cf89..351b32f9de1 100644 --- a/tests/template_tests/filter_tests/test_truncatechars.py +++ b/tests/template_tests/filter_tests/test_truncatechars.py @@ -22,3 +22,8 @@ class TruncatecharsTests(SimpleTestCase): "truncatechars03", {"a": "Testing, testing"} ) self.assertEqual(output, "Testing, testing") + + @setup({"truncatechars04": "{{ a|truncatechars:3 }}"}) + def test_truncatechars04(self): + output = self.engine.render_to_string("truncatechars04", {"a": "abc"}) + self.assertEqual(output, "abc") diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py index 77e637ae6c8..a7f25c7936a 100644 --- a/tests/utils_tests/test_text.py +++ b/tests/utils_tests/test_text.py @@ -95,6 +95,45 @@ class TestUtilsText(SimpleTestCase): text.Truncator(lazystr("The quick brown fox")).chars(10), "The quick…" ) + def test_truncate_chars_html(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(80, html=True), + ) + self.assertEqual( + '

The quick brown fox jumped over the lazy dog.' + "

", + truncator.chars(46, html=True), + ) + self.assertEqual( + '

The quick brown fox jumped over the lazy dog.' + "

", + truncator.chars(45, html=True), + ) + self.assertEqual( + '

The quick…

', + truncator.chars(10, html=True), + ) + self.assertEqual( + "…", + truncator.chars(1, html=True), + ) + self.assertEqual( + '

The qu....

', + truncator.chars(10, "....", html=True), + ) + self.assertEqual( + '

The quick

', + truncator.chars(10, "", html=True), + ) + truncator = text.Truncator("foo

") + self.assertEqual("foo

", 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 @@ -114,6 +153,47 @@ class TestUtilsText(SimpleTestCase): expected if expected else value, truncator.chars(10, html=True) ) + def test_truncate_chars_html_with_newline_inside_tag(self): + truncator = text.Truncator( + '

The quick brown fox jumped over ' + "the lazy dog.

" + ) + self.assertEqual( + '

The quick brow…

', + truncator.chars(15, html=True), + ) + self.assertEqual( + "

Th…

", + truncator.chars(3, html=True), + ) + + def test_truncate_chars_html_with_void_elements(self): + truncator = text.Truncator( + "
The
quick brown fox jumped over the lazy dog." + ) + self.assertEqual("
The
quick brown…", truncator.chars(16, html=True)) + truncator = text.Truncator( + "
The
quick brown fox jumped over the lazy dog." + ) + self.assertEqual( + "
The
quick brown…", truncator.chars(16, html=True) + ) + self.assertEqual("
The
q…", truncator.chars(6, html=True)) + self.assertEqual("
The …", truncator.chars(5, html=True)) + self.assertEqual("
The…", truncator.chars(4, html=True)) + self.assertEqual("
Th…", truncator.chars(3, html=True)) + + def test_truncate_chars_html_with_html_entities(self): + truncator = text.Truncator( + "Buenos días! ¿Cómo está?" + ) + self.assertEqual( + "Buenos días! ¿Cómo…", + truncator.chars(40, html=True), + ) + truncator = text.Truncator("

I <3 python, what about you?

") + self.assertEqual("

I <3 python,…

", truncator.chars(16, html=True)) + def test_truncate_words(self): truncator = text.Truncator("The quick brown fox jumped over the lazy dog.") self.assertEqual( @@ -141,6 +221,10 @@ class TestUtilsText(SimpleTestCase): '

The quick brown fox…

', truncator.words(4, html=True), ) + self.assertEqual( + "", + truncator.words(0, html=True), + ) self.assertEqual( '

The quick brown fox....

', truncator.words(4, "....", html=True), @@ -150,6 +234,14 @@ class TestUtilsText(SimpleTestCase): truncator.words(4, "", html=True), ) + truncator = text.Truncator( + "

The quick \t brown fox jumped over the lazy dog.

" + ) + self.assertEqual( + "

The quick \t brown fox…

", + truncator.words(4, html=True), + ) + # Test with new line inside tag truncator = text.Truncator( '

The quick brown fox jumped over ' @@ -159,6 +251,10 @@ class TestUtilsText(SimpleTestCase): '

The quick brown…

', truncator.words(3, html=True), ) + self.assertEqual( + "

The…

", + truncator.words(1, html=True), + ) # Test self-closing tags truncator = text.Truncator( @@ -183,6 +279,9 @@ class TestUtilsText(SimpleTestCase): truncator = text.Truncator("

I <3 python, what about you?

") self.assertEqual("

I <3 python,…

", truncator.words(3, html=True)) + truncator = text.Truncator("foo

") + self.assertEqual("foo

", truncator.words(3, html=True)) + @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