2011-07-14 15:47:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-07 18:08:47 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-05-03 15:27:01 +02:00
|
|
|
import warnings
|
2010-09-27 17:15:04 +02:00
|
|
|
|
2012-05-03 15:27:01 +02:00
|
|
|
from django.test import SimpleTestCase
|
2010-09-27 17:15:04 +02:00
|
|
|
from django.utils import text
|
|
|
|
|
2012-05-03 15:27:01 +02:00
|
|
|
class TestUtilsText(SimpleTestCase):
|
|
|
|
|
|
|
|
# In Django 1.6 truncate_words() and truncate_html_words() will be removed
|
|
|
|
# so these tests will need to be adapted accordingly
|
2011-07-14 15:47:10 +02:00
|
|
|
def test_truncate_chars(self):
|
|
|
|
truncator = text.Truncator(
|
2012-06-07 18:08:47 +02:00
|
|
|
'The quick brown fox jumped over the lazy dog.'
|
2011-07-14 15:47:10 +02:00
|
|
|
)
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('The quick brown fox jumped over the lazy dog.',
|
2011-07-14 15:47:10 +02:00
|
|
|
truncator.chars(100)),
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('The quick brown fox ...',
|
2011-07-14 15:47:10 +02:00
|
|
|
truncator.chars(23)),
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('The quick brown fo.....',
|
2011-07-14 15:47:10 +02:00
|
|
|
truncator.chars(23, '.....')),
|
|
|
|
|
|
|
|
# Ensure that we normalize our unicode data first
|
2012-06-07 18:08:47 +02:00
|
|
|
nfc = text.Truncator('o\xfco\xfco\xfco\xfc')
|
|
|
|
nfd = text.Truncator('ou\u0308ou\u0308ou\u0308ou\u0308')
|
|
|
|
self.assertEqual('oüoüoüoü', nfc.chars(8))
|
|
|
|
self.assertEqual('oüoüoüoü', nfd.chars(8))
|
|
|
|
self.assertEqual('oü...', nfc.chars(5))
|
|
|
|
self.assertEqual('oü...', nfd.chars(5))
|
2011-07-14 15:47:10 +02:00
|
|
|
|
|
|
|
# Ensure the final length is calculated correctly when there are
|
|
|
|
# combining characters with no precomposed form, and that combining
|
|
|
|
# characters are not split up.
|
2012-06-07 18:08:47 +02:00
|
|
|
truncator = text.Truncator('-B\u030AB\u030A----8')
|
|
|
|
self.assertEqual('-B\u030A...', truncator.chars(5))
|
|
|
|
self.assertEqual('-B\u030AB\u030A-...', truncator.chars(7))
|
|
|
|
self.assertEqual('-B\u030AB\u030A----8', truncator.chars(8))
|
2011-07-14 15:47:10 +02:00
|
|
|
|
|
|
|
# Ensure the length of the end text is correctly calculated when it
|
|
|
|
# contains combining characters with no precomposed form.
|
2012-06-07 18:08:47 +02:00
|
|
|
truncator = text.Truncator('-----')
|
|
|
|
self.assertEqual('---B\u030A', truncator.chars(4, 'B\u030A'))
|
|
|
|
self.assertEqual('-----', truncator.chars(5, 'B\u030A'))
|
2011-07-14 15:47:10 +02:00
|
|
|
|
|
|
|
# Make a best effort to shorten to the desired length, but requesting
|
|
|
|
# a length shorter than the ellipsis shouldn't break
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('...', text.Truncator('asdf').chars(1))
|
2011-07-14 15:47:10 +02:00
|
|
|
|
2010-09-27 17:15:04 +02:00
|
|
|
def test_truncate_words(self):
|
2012-06-07 18:08:47 +02:00
|
|
|
truncator = text.Truncator('The quick brown fox jumped over the lazy '
|
2011-07-14 15:47:10 +02:00
|
|
|
'dog.')
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('The quick brown fox jumped over the lazy dog.',
|
2011-07-14 15:47:10 +02:00
|
|
|
truncator.words(10))
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('The quick brown fox...', truncator.words(4))
|
|
|
|
self.assertEqual('The quick brown fox[snip]',
|
2011-07-14 15:47:10 +02:00
|
|
|
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>')
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('<p><strong><em>The quick brown fox jumped over the '
|
2011-07-14 15:47:10 +02:00
|
|
|
'lazy dog.</em></strong></p>', truncator.words(10, html=True))
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('<p><strong><em>The quick brown fox...</em>'
|
2011-07-14 15:47:10 +02:00
|
|
|
'</strong></p>', truncator.words(4, html=True))
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('<p><strong><em>The quick brown fox....</em>'
|
2011-07-14 15:47:10 +02:00
|
|
|
'</strong></p>', truncator.words(4, '....', html=True))
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('<p><strong><em>The quick brown fox</em></strong>'
|
2011-07-14 15:47:10 +02:00
|
|
|
'</p>', truncator.words(4, '', html=True))
|
2012-01-02 19:47:18 +01:00
|
|
|
# Test with new line inside tag
|
|
|
|
truncator = text.Truncator('<p>The quick <a href="xyz.html"\n'
|
|
|
|
'id="mylink">brown fox</a> jumped over the lazy dog.</p>')
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('<p>The quick <a href="xyz.html"\n'
|
2012-01-02 19:47:18 +01:00
|
|
|
'id="mylink">brown...</a></p>', truncator.words(3, '...', html=True))
|
2011-07-14 15:47:10 +02:00
|
|
|
|
|
|
|
def test_old_truncate_words(self):
|
2012-05-03 18:19:18 +02:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2012-05-03 21:23:50 +02:00
|
|
|
warnings.simplefilter("always")
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('The quick brown fox jumped over the lazy dog.',
|
|
|
|
text.truncate_words('The quick brown fox jumped over the lazy dog.', 10))
|
|
|
|
self.assertEqual('The quick brown fox ...',
|
2012-05-03 18:19:18 +02:00
|
|
|
text.truncate_words('The quick brown fox jumped over the lazy dog.', 4))
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('The quick brown fox ....',
|
2012-05-03 18:19:18 +02:00
|
|
|
text.truncate_words('The quick brown fox jumped over the lazy dog.', 4, '....'))
|
2012-05-03 20:18:05 +02:00
|
|
|
self.assertGreater(len(w), 0)
|
2010-11-30 22:21:37 +01:00
|
|
|
|
2011-07-14 15:47:10 +02:00
|
|
|
def test_old_truncate_html_words(self):
|
2012-05-03 18:19:18 +02:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
2012-05-03 21:23:50 +02:00
|
|
|
warnings.simplefilter("always")
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
|
2012-05-03 18:19:18 +02:00
|
|
|
text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10))
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('<p><strong><em>The quick brown fox ...</em></strong></p>',
|
2012-05-03 18:19:18 +02:00
|
|
|
text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4))
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('<p><strong><em>The quick brown fox ....</em></strong></p>',
|
2012-05-03 18:19:18 +02:00
|
|
|
text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....'))
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual('<p><strong><em>The quick brown fox</em></strong></p>',
|
2012-05-03 18:19:18 +02:00
|
|
|
text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, None))
|
2012-05-03 20:18:05 +02:00
|
|
|
self.assertGreater(len(w), 0)
|
2010-11-30 22:21:37 +01:00
|
|
|
|
|
|
|
def test_wrap(self):
|
|
|
|
digits = '1234 67 9'
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual(text.wrap(digits, 100), '1234 67 9')
|
|
|
|
self.assertEqual(text.wrap(digits, 9), '1234 67 9')
|
|
|
|
self.assertEqual(text.wrap(digits, 8), '1234 67\n9')
|
2010-11-30 22:21:37 +01:00
|
|
|
|
|
|
|
self.assertEqual(text.wrap('short\na long line', 7),
|
2012-06-07 18:08:47 +02:00
|
|
|
'short\na long\nline')
|
2010-11-30 22:21:37 +01:00
|
|
|
|
|
|
|
self.assertEqual(text.wrap('do-not-break-long-words please? ok', 8),
|
2012-06-07 18:08:47 +02:00
|
|
|
'do-not-break-long-words\nplease?\nok')
|
2010-11-30 22:21:37 +01:00
|
|
|
|
|
|
|
long_word = 'l%sng' % ('o' * 20)
|
|
|
|
self.assertEqual(text.wrap(long_word, 20), long_word)
|
|
|
|
self.assertEqual(text.wrap('a %s word' % long_word, 10),
|
2012-06-07 18:08:47 +02:00
|
|
|
'a\n%s\nword' % long_word)
|