diff options
| author | Iacopo Spalletti <i.spalletti@nephila.it> | 2015-11-07 14:30:20 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-12-12 14:46:48 -0500 |
| commit | d693074d431c50e4801dd6bf52525ce1436358f0 (patch) | |
| tree | ad452646aad45bf9241478f3fc17803d05320cfc /tests/utils_tests/test_text.py | |
| parent | 93fc23b2d542105f5d129dff2dd2c8895e9abd5d (diff) | |
Fixed #20223 -- Added keep_lazy() as a replacement for allow_lazy().
Thanks to bmispelon and uruz for the initial patch.
Diffstat (limited to 'tests/utils_tests/test_text.py')
| -rw-r--r-- | tests/utils_tests/test_text.py | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py index e6f5c0f378..5d97844800 100644 --- a/tests/utils_tests/test_text.py +++ b/tests/utils_tests/test_text.py @@ -5,12 +5,9 @@ import json from django.test import SimpleTestCase from django.utils import six, text -from django.utils.encoding import force_text -from django.utils.functional import lazy +from django.utils.functional import lazystr from django.utils.translation import override -lazystr = lazy(force_text, six.text_type) - IS_WIDE_BUILD = (len('\U0001F4A9') == 1) @@ -93,6 +90,8 @@ class TestUtilsText(SimpleTestCase): # Make a best effort to shorten to the desired length, but requesting # a length shorter than the ellipsis shouldn't break self.assertEqual('...', text.Truncator('asdf').chars(1)) + # Ensure that lazy strings are handled correctly + self.assertEqual(text.Truncator(lazystr('The quick brown fox')).chars(12), 'The quick...') def test_truncate_words(self): truncator = text.Truncator('The quick brown fox jumped over the lazy ' @@ -102,6 +101,9 @@ class TestUtilsText(SimpleTestCase): self.assertEqual('The quick brown fox...', truncator.words(4)) self.assertEqual('The quick brown fox[snip]', truncator.words(4, '[snip]')) + # Ensure that lazy strings are handled correctly + truncator = text.Truncator(lazystr('The quick brown fox jumped over the lazy dog.')) + self.assertEqual('The quick brown fox...', truncator.words(4)) def test_truncate_html_words(self): truncator = text.Truncator('<p id="par"><strong><em>The quick brown fox' @@ -156,6 +158,7 @@ class TestUtilsText(SimpleTestCase): self.assertEqual(text.wrap(long_word, 20), long_word) self.assertEqual(text.wrap('a %s word' % long_word, 10), 'a\n%s\nword' % long_word) + self.assertEqual(text.wrap(lazystr(digits), 100), '1234 67 9') def test_normalize_newlines(self): self.assertEqual(text.normalize_newlines("abc\ndef\rghi\r\n"), @@ -163,6 +166,7 @@ class TestUtilsText(SimpleTestCase): self.assertEqual(text.normalize_newlines("\n\r\r\n\r"), "\n\n\n\n") self.assertEqual(text.normalize_newlines("abcdefghi"), "abcdefghi") self.assertEqual(text.normalize_newlines(""), "") + self.assertEqual(text.normalize_newlines(lazystr("abc\ndef\rghi\r\n")), "abc\ndef\nghi\n") def test_normalize_newlines_bytes(self): """normalize_newlines should be able to handle bytes too""" @@ -170,6 +174,12 @@ class TestUtilsText(SimpleTestCase): self.assertEqual(normalized, "abc\ndef\nghi\n") self.assertIsInstance(normalized, six.text_type) + def test_phone2numeric(self): + numeric = text.phone2numeric('0800 flowers') + self.assertEqual(numeric, '0800 3569377') + lazy_numeric = lazystr(text.phone2numeric('0800 flowers')) + self.assertEqual(lazy_numeric, '0800 3569377') + def test_slugify(self): items = ( # given - expected - unicode? @@ -195,10 +205,23 @@ class TestUtilsText(SimpleTestCase): ] for value, output in items: self.assertEqual(text.unescape_entities(value), output) + self.assertEqual(text.unescape_entities(lazystr(value)), output) + + def test_unescape_string_literal(self): + items = [ + ('"abc"', 'abc'), + ("'abc'", 'abc'), + ('"a \"bc\""', 'a "bc"'), + ("'\'ab\' c'", "'ab' c"), + ] + for value, output in items: + self.assertEqual(text.unescape_string_literal(value), output) + self.assertEqual(text.unescape_string_literal(lazystr(value)), output) def test_get_valid_filename(self): filename = "^&'@{}[],$=!-#()%+~_123.txt" self.assertEqual(text.get_valid_filename(filename), "-_123.txt") + self.assertEqual(text.get_valid_filename(lazystr(filename)), "-_123.txt") def test_compress_sequence(self): data = [{'key': i} for i in range(10)] |
