text.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. import warnings
  3. from django.test import SimpleTestCase
  4. from django.utils import text
  5. class TestUtilsText(SimpleTestCase):
  6. # In Django 1.6 truncate_words() and truncate_html_words() will be removed
  7. # so these tests will need to be adapted accordingly
  8. def setUp(self):
  9. self.save_warnings_state()
  10. warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.utils.text')
  11. def tearDown(self):
  12. self.restore_warnings_state()
  13. def test_truncate_chars(self):
  14. truncator = text.Truncator(
  15. u'The quick brown fox jumped over the lazy dog.'
  16. )
  17. self.assertEqual(u'The quick brown fox jumped over the lazy dog.',
  18. truncator.chars(100)),
  19. self.assertEqual(u'The quick brown fox ...',
  20. truncator.chars(23)),
  21. self.assertEqual(u'The quick brown fo.....',
  22. truncator.chars(23, '.....')),
  23. # Ensure that we normalize our unicode data first
  24. nfc = text.Truncator(u'o\xfco\xfco\xfco\xfc')
  25. nfd = text.Truncator(u'ou\u0308ou\u0308ou\u0308ou\u0308')
  26. self.assertEqual(u'oüoüoüoü', nfc.chars(8))
  27. self.assertEqual(u'oüoüoüoü', nfd.chars(8))
  28. self.assertEqual(u'oü...', nfc.chars(5))
  29. self.assertEqual(u'oü...', nfd.chars(5))
  30. # Ensure the final length is calculated correctly when there are
  31. # combining characters with no precomposed form, and that combining
  32. # characters are not split up.
  33. truncator = text.Truncator(u'-B\u030AB\u030A----8')
  34. self.assertEqual(u'-B\u030A...', truncator.chars(5))
  35. self.assertEqual(u'-B\u030AB\u030A-...', truncator.chars(7))
  36. self.assertEqual(u'-B\u030AB\u030A----8', truncator.chars(8))
  37. # Ensure the length of the end text is correctly calculated when it
  38. # contains combining characters with no precomposed form.
  39. truncator = text.Truncator(u'-----')
  40. self.assertEqual(u'---B\u030A', truncator.chars(4, u'B\u030A'))
  41. self.assertEqual(u'-----', truncator.chars(5, u'B\u030A'))
  42. # Make a best effort to shorten to the desired length, but requesting
  43. # a length shorter than the ellipsis shouldn't break
  44. self.assertEqual(u'...', text.Truncator(u'asdf').chars(1))
  45. def test_truncate_words(self):
  46. truncator = text.Truncator(u'The quick brown fox jumped over the lazy '
  47. 'dog.')
  48. self.assertEqual(u'The quick brown fox jumped over the lazy dog.',
  49. truncator.words(10))
  50. self.assertEqual(u'The quick brown fox...', truncator.words(4))
  51. self.assertEqual(u'The quick brown fox[snip]',
  52. truncator.words(4, '[snip]'))
  53. def test_truncate_html_words(self):
  54. truncator = text.Truncator('<p><strong><em>The quick brown fox jumped '
  55. 'over the lazy dog.</em></strong></p>')
  56. self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the '
  57. 'lazy dog.</em></strong></p>', truncator.words(10, html=True))
  58. self.assertEqual(u'<p><strong><em>The quick brown fox...</em>'
  59. '</strong></p>', truncator.words(4, html=True))
  60. self.assertEqual(u'<p><strong><em>The quick brown fox....</em>'
  61. '</strong></p>', truncator.words(4, '....', html=True))
  62. self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong>'
  63. '</p>', truncator.words(4, '', html=True))
  64. # Test with new line inside tag
  65. truncator = text.Truncator('<p>The quick <a href="xyz.html"\n'
  66. 'id="mylink">brown fox</a> jumped over the lazy dog.</p>')
  67. self.assertEqual(u'<p>The quick <a href="xyz.html"\n'
  68. 'id="mylink">brown...</a></p>', truncator.words(3, '...', html=True))
  69. def test_old_truncate_words(self):
  70. self.assertEqual(u'The quick brown fox jumped over the lazy dog.',
  71. text.truncate_words(u'The quick brown fox jumped over the lazy dog.', 10))
  72. self.assertEqual(u'The quick brown fox ...',
  73. text.truncate_words('The quick brown fox jumped over the lazy dog.', 4))
  74. self.assertEqual(u'The quick brown fox ....',
  75. text.truncate_words('The quick brown fox jumped over the lazy dog.', 4, '....'))
  76. def test_old_truncate_html_words(self):
  77. self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
  78. text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10))
  79. self.assertEqual(u'<p><strong><em>The quick brown fox ...</em></strong></p>',
  80. text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4))
  81. self.assertEqual(u'<p><strong><em>The quick brown fox ....</em></strong></p>',
  82. text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....'))
  83. self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong></p>',
  84. text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, None))
  85. def test_wrap(self):
  86. digits = '1234 67 9'
  87. self.assertEqual(text.wrap(digits, 100), u'1234 67 9')
  88. self.assertEqual(text.wrap(digits, 9), u'1234 67 9')
  89. self.assertEqual(text.wrap(digits, 8), u'1234 67\n9')
  90. self.assertEqual(text.wrap('short\na long line', 7),
  91. u'short\na long\nline')
  92. self.assertEqual(text.wrap('do-not-break-long-words please? ok', 8),
  93. u'do-not-break-long-words\nplease?\nok')
  94. long_word = 'l%sng' % ('o' * 20)
  95. self.assertEqual(text.wrap(long_word, 20), long_word)
  96. self.assertEqual(text.wrap('a %s word' % long_word, 10),
  97. u'a\n%s\nword' % long_word)