diff options
| author | David Smith <smithdc@gmail.com> | 2023-01-03 08:17:56 +0000 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-02-06 16:35:08 +0100 |
| commit | 48a469395191e87d3b84ad35bae2c8b53d91ed61 (patch) | |
| tree | 1576109fa56b07c1c54cc40f1998bdb942b7ac32 /tests/utils_tests/test_text.py | |
| parent | c650c1412d1933e339cc93f9b6745c3eedb1c25b (diff) | |
Refs #30686 -- Improved test coverage of Truncator.
Diffstat (limited to 'tests/utils_tests/test_text.py')
| -rw-r--r-- | tests/utils_tests/test_text.py | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py index 77e637ae6c..a7f25c7936 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( + '<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( + "…", + 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 @@ -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( + '<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 …", 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ías! ¿Cómo está?</i>" + ) + self.assertEqual( + "<i>Buenos días! ¿Cómo…</i>", + truncator.chars(40, html=True), + ) + truncator = text.Truncator("<p>I <3 python, what about you?</p>") + self.assertEqual("<p>I <3 python,…</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( @@ -142,6 +222,10 @@ class TestUtilsText(SimpleTestCase): 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), ) @@ -150,6 +234,14 @@ class TestUtilsText(SimpleTestCase): 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 \t 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 ' @@ -159,6 +251,10 @@ class TestUtilsText(SimpleTestCase): '<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( @@ -183,6 +279,9 @@ class TestUtilsText(SimpleTestCase): truncator = text.Truncator("<p>I <3 python, what about you?</p>") self.assertEqual("<p>I <3 python,…</p>", truncator.words(3, html=True)) + truncator = text.Truncator("foo</p>") + self.assertEqual("foo</p>", 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 |
