From 6ee37ada3241ed263d8d1c2901b030d964cbd161 Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 3 Jan 2023 20:48:06 +0000 Subject: Fixed #30686 -- Used Python HTMLParser in utils.text.Truncator. --- tests/utils_tests/test_text.py | 48 +++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 22 deletions(-) (limited to 'tests/utils_tests') diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py index 6004712bf2..b38d8238c5 100644 --- a/tests/utils_tests/test_text.py +++ b/tests/utils_tests/test_text.py @@ -111,7 +111,7 @@ class TestUtilsText(SimpleTestCase): truncator.chars(46, html=True), ) self.assertEqual( - '

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

The quick brown fox jumped over the lazy dog…' "

", truncator.chars(45, html=True), ) @@ -120,7 +120,7 @@ class TestUtilsText(SimpleTestCase): truncator.chars(10, html=True), ) self.assertEqual( - "…", + '

', truncator.chars(1, html=True), ) self.assertEqual("", truncator.chars(0, html=True)) @@ -142,18 +142,16 @@ class TestUtilsText(SimpleTestCase): bigger_len = text.Truncator.MAX_LENGTH_HTML + 1 valid_html = "

Joel is a slug

" # 14 chars perf_test_values = [ - ("", None), - ("", "", None), + ("", ""), + ("", "

"), + ("&" * bigger_len, ""), + ("_X<<<<<<<<<<<>", "_X<<<<<<<…"), (valid_html * bigger_len, "

Joel is a…

"), # 10 chars ] for value, expected in perf_test_values: with self.subTest(value=value): truncator = text.Truncator(value) - self.assertEqual( - expected if expected else value, truncator.chars(10, html=True) - ) + self.assertEqual(expected, truncator.chars(10, html=True)) def test_truncate_chars_html_with_newline_inside_tag(self): truncator = text.Truncator( @@ -181,7 +179,7 @@ class TestUtilsText(SimpleTestCase): "
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(5, html=True)) self.assertEqual("
The…", truncator.chars(4, html=True)) self.assertEqual("
Th…", truncator.chars(3, html=True)) @@ -190,11 +188,19 @@ class TestUtilsText(SimpleTestCase): "Buenos días! ¿Cómo está?" ) self.assertEqual( - "Buenos días! ¿Cómo…", + "Buenos días! ¿Cómo está?", truncator.chars(40, html=True), ) + self.assertEqual( + "Buenos días…", + truncator.chars(12, html=True), + ) + self.assertEqual( + "Buenos días! ¿Cómo está…", + truncator.chars(24, html=True), + ) truncator = text.Truncator("

I <3 python, what about you?

") - self.assertEqual("

I <3 python,…

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

I <3 python, wh…

", truncator.chars(16, html=True)) def test_truncate_words(self): truncator = text.Truncator("The quick brown fox jumped over the lazy dog.") @@ -242,7 +248,7 @@ class TestUtilsText(SimpleTestCase): "

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

" ) self.assertEqual( - "

The quick \t brown fox…

", + "

The quick brown fox…

", truncator.words(4, html=True), ) @@ -277,7 +283,7 @@ class TestUtilsText(SimpleTestCase): "Buenos días! ¿Cómo está?" ) self.assertEqual( - "Buenos días! ¿Cómo…", + "Buenos días! ¿Cómo…", truncator.words(3, html=True), ) truncator = text.Truncator("

I <3 python, what about you?

") @@ -292,19 +298,17 @@ class TestUtilsText(SimpleTestCase): bigger_len = text.Truncator.MAX_LENGTH_HTML + 1 valid_html = "

Joel is a slug

" # 4 words perf_test_values = [ - ("", None), - ("", "", None), + ("", ""), + ("", "

"), + ("&" * max_len, ""), + ("&" * bigger_len, ""), + ("_X<<<<<<<<<<<>", "_X<<<<<<<<<<<>"), (valid_html * bigger_len, valid_html * 12 + "

Joel is…

"), # 50 words ] for value, expected in perf_test_values: with self.subTest(value=value): truncator = text.Truncator(value) - self.assertEqual( - expected if expected else value, truncator.words(50, html=True) - ) + self.assertEqual(expected, truncator.words(50, html=True)) def test_wrap(self): digits = "1234 67 9" -- cgit v1.3