MCPcopy
hub / github.com/django/django / test_truncate_chars

Method test_truncate_chars

tests/utils_tests/test_text.py:60–96  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

58 self.assertEqual(list(text.smart_split(test)), expected)
59
60 def test_truncate_chars(self):
61 truncator = text.Truncator("The quick brown fox jumped over the lazy dog.")
62 self.assertEqual(
63 "The quick brown fox jumped over the lazy dog.", truncator.chars(100)
64 ),
65 self.assertEqual("The quick brown fox …", truncator.chars(21))
66 self.assertEqual("The quick brown fo.....", truncator.chars(23, "....."))
67 self.assertEqual(".....", truncator.chars(4, "....."))
68
69 nfc = text.Truncator("o\xfco\xfco\xfco\xfc")
70 nfd = text.Truncator("ou\u0308ou\u0308ou\u0308ou\u0308")
71 self.assertEqual("oüoüoüoü", nfc.chars(8))
72 self.assertEqual("oüoüoüoü", nfd.chars(8))
73 self.assertEqual("oü…", nfc.chars(3))
74 self.assertEqual("oü…", nfd.chars(3))
75
76 # Ensure the final length is calculated correctly when there are
77 # combining characters with no precomposed form, and that combining
78 # characters are not split up.
79 truncator = text.Truncator("-B\u030aB\u030a----8")
80 self.assertEqual("-B\u030a…", truncator.chars(3))
81 self.assertEqual("-B\u030aB\u030a-…", truncator.chars(5))
82 self.assertEqual("-B\u030aB\u030a----8", truncator.chars(8))
83
84 # Ensure the length of the end text is correctly calculated when it
85 # contains combining characters with no precomposed form.
86 truncator = text.Truncator("-----")
87 self.assertEqual("---B\u030a", truncator.chars(4, "B\u030a"))
88 self.assertEqual("-----", truncator.chars(5, "B\u030a"))
89
90 # Make a best effort to shorten to the desired length, but requesting
91 # a length shorter than the ellipsis shouldn't break
92 self.assertEqual("...", text.Truncator("asdf").chars(1, truncate="..."))
93 # lazy strings are handled correctly
94 self.assertEqual(
95 text.Truncator(lazystr("The quick brown fox")).chars(10), "The quick…"
96 )
97
98 def test_truncate_chars_html(self):
99 truncator = text.Truncator(

Callers

nothing calls this directly

Calls 2

charsMethod · 0.95
lazystrFunction · 0.90

Tested by

no test coverage detected