diff options
| author | Jannis Leidel <jannis@leidel.info> | 2011-07-14 13:47:10 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2011-07-14 13:47:10 +0000 |
| commit | 3b774583711e39dae7a5cfde314288f8019f59c6 (patch) | |
| tree | f14bf8b086ad3b4d46b6cd4e1ebbfb1938836737 /tests | |
| parent | 12b7c2a702bea68e53c438fa2c7a4a01d890695d (diff) | |
Fixed #5025 -- Add a "truncatechars" template filter. Many thanks to Chris Beaven.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16542 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/utils/text.py | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/tests/regressiontests/utils/text.py b/tests/regressiontests/utils/text.py index f565d87b1d..d4aa53fba9 100644 --- a/tests/regressiontests/utils/text.py +++ b/tests/regressiontests/utils/text.py @@ -1,10 +1,69 @@ +# -*- coding: utf-8 -*- import unittest from django.utils import text class TestUtilsText(unittest.TestCase): + def test_truncate_chars(self): + truncator = text.Truncator( + u'The quick brown fox jumped over the lazy dog.' + ) + self.assertEqual(u'The quick brown fox jumped over the lazy dog.', + truncator.chars(100)), + self.assertEqual(u'The quick brown fox ...', + truncator.chars(23)), + self.assertEqual(u'The quick brown fo.....', + truncator.chars(23, '.....')), + + # Ensure that we normalize our unicode data first + nfc = text.Truncator(u'o\xfco\xfco\xfco\xfc') + nfd = text.Truncator(u'ou\u0308ou\u0308ou\u0308ou\u0308') + self.assertEqual(u'oüoüoüoü', nfc.chars(8)) + self.assertEqual(u'oüoüoüoü', nfd.chars(8)) + self.assertEqual(u'oü...', nfc.chars(5)) + self.assertEqual(u'oü...', nfd.chars(5)) + + # Ensure the final length is calculated correctly when there are + # combining characters with no precomposed form, and that combining + # characters are not split up. + truncator = text.Truncator(u'-B\u030AB\u030A----8') + self.assertEqual(u'-B\u030A...', truncator.chars(5)) + self.assertEqual(u'-B\u030AB\u030A-...', truncator.chars(7)) + self.assertEqual(u'-B\u030AB\u030A----8', truncator.chars(8)) + + # Ensure the length of the end text is correctly calculated when it + # contains combining characters with no precomposed form. + truncator = text.Truncator(u'-----') + self.assertEqual(u'---B\u030A', truncator.chars(4, u'B\u030A')) + self.assertEqual(u'-----', truncator.chars(5, u'B\u030A')) + + # Make a best effort to shorten to the desired length, but requesting + # a length shorter than the ellipsis shouldn't break + self.assertEqual(u'...', text.Truncator(u'asdf').chars(1)) + def test_truncate_words(self): + truncator = text.Truncator(u'The quick brown fox jumped over the lazy ' + 'dog.') + self.assertEqual(u'The quick brown fox jumped over the lazy dog.', + truncator.words(10)) + self.assertEqual(u'The quick brown fox...', truncator.words(4)) + self.assertEqual(u'The quick brown fox[snip]', + truncator.words(4, '[snip]')) + + def test_truncate_html_words(self): + truncator = text.Truncator('<p><strong><em>The quick brown fox jumped ' + 'over the lazy dog.</em></strong></p>') + self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the ' + 'lazy dog.</em></strong></p>', truncator.words(10, html=True)) + self.assertEqual(u'<p><strong><em>The quick brown fox...</em>' + '</strong></p>', truncator.words(4, html=True)) + self.assertEqual(u'<p><strong><em>The quick brown fox....</em>' + '</strong></p>', truncator.words(4, '....', html=True)) + self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong>' + '</p>', truncator.words(4, '', html=True)) + + def test_old_truncate_words(self): self.assertEqual(u'The quick brown fox jumped over the lazy dog.', text.truncate_words(u'The quick brown fox jumped over the lazy dog.', 10)) self.assertEqual(u'The quick brown fox ...', @@ -12,7 +71,7 @@ class TestUtilsText(unittest.TestCase): self.assertEqual(u'The quick brown fox ....', text.truncate_words('The quick brown fox jumped over the lazy dog.', 4, '....')) - def test_truncate_html_words(self): + def test_old_truncate_html_words(self): self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10)) self.assertEqual(u'<p><strong><em>The quick brown fox ...</em></strong></p>', |
