mirror of
https://github.com/django/django.git
synced 2025-02-08 16:35:17 +00:00
Refs #30686 -- Improved test coverage of Truncator.
This commit is contained in:
parent
c650c1412d
commit
48a4693951
@ -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")
|
||||
|
@ -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(
|
||||
@ -141,6 +221,10 @@ class TestUtilsText(SimpleTestCase):
|
||||
'<p id="par"><strong><em>The quick brown fox…</em></strong></p>',
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user