summaryrefslogtreecommitdiff
path: root/tests/utils_tests
diff options
context:
space:
mode:
authorIacopo Spalletti <i.spalletti@nephila.it>2015-11-07 14:30:20 +0100
committerTim Graham <timograham@gmail.com>2015-12-12 14:46:48 -0500
commitd693074d431c50e4801dd6bf52525ce1436358f0 (patch)
treead452646aad45bf9241478f3fc17803d05320cfc /tests/utils_tests
parent93fc23b2d542105f5d129dff2dd2c8895e9abd5d (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')
-rw-r--r--tests/utils_tests/test_html.py7
-rw-r--r--tests/utils_tests/test_safestring.py5
-rw-r--r--tests/utils_tests/test_text.py31
3 files changed, 36 insertions, 7 deletions
diff --git a/tests/utils_tests/test_html.py b/tests/utils_tests/test_html.py
index ed026f0a28..20de85148e 100644
--- a/tests/utils_tests/test_html.py
+++ b/tests/utils_tests/test_html.py
@@ -8,6 +8,7 @@ from django.test import SimpleTestCase
from django.utils import html, safestring, six
from django.utils._os import upath
from django.utils.encoding import force_text
+from django.utils.functional import lazystr
class TestUtilsHtml(SimpleTestCase):
@@ -35,6 +36,7 @@ class TestUtilsHtml(SimpleTestCase):
for value, output in items:
for pattern in patterns:
self.check_output(f, pattern % value, pattern % output)
+ self.check_output(f, lazystr(pattern % value), pattern % output)
# Check repeated values.
self.check_output(f, value * 2, output * 2)
# Verify it doesn't double replace &.
@@ -61,6 +63,7 @@ class TestUtilsHtml(SimpleTestCase):
)
for value, output in items:
self.check_output(f, value, output)
+ self.check_output(f, lazystr(value), output)
def test_strip_tags(self):
f = html.strip_tags
@@ -86,6 +89,7 @@ class TestUtilsHtml(SimpleTestCase):
)
for value, output in items:
self.check_output(f, value, output)
+ self.check_output(f, lazystr(value), output)
# Some convoluted syntax for which parsing may differ between python versions
output = html.strip_tags('<sc<!-- -->ript>test<<!-- -->/script>')
@@ -113,6 +117,7 @@ class TestUtilsHtml(SimpleTestCase):
items = (' <adf>', '<adf> ', ' </adf> ', ' <f> x</f>')
for value in items:
self.check_output(f, value)
+ self.check_output(f, lazystr(value))
# Strings that have spaces to strip.
items = (
('<d> </d>', '<d></d>'),
@@ -121,6 +126,7 @@ class TestUtilsHtml(SimpleTestCase):
)
for value, output in items:
self.check_output(f, value, output)
+ self.check_output(f, lazystr(value), output)
def test_escapejs(self):
f = html.escapejs
@@ -139,6 +145,7 @@ class TestUtilsHtml(SimpleTestCase):
)
for value, output in items:
self.check_output(f, value, output)
+ self.check_output(f, lazystr(value), output)
def test_smart_urlquote(self):
quote = html.smart_urlquote
diff --git a/tests/utils_tests/test_safestring.py b/tests/utils_tests/test_safestring.py
index acd36053d6..7cc92a1370 100644
--- a/tests/utils_tests/test_safestring.py
+++ b/tests/utils_tests/test_safestring.py
@@ -3,13 +3,12 @@ from __future__ import unicode_literals
from django.template import Context, Template
from django.test import SimpleTestCase
from django.utils import html, six, text
-from django.utils.encoding import force_bytes, force_text
-from django.utils.functional import lazy
+from django.utils.encoding import force_bytes
+from django.utils.functional import lazy, lazystr
from django.utils.safestring import (
EscapeData, SafeData, mark_for_escaping, mark_safe,
)
-lazystr = lazy(force_text, six.text_type)
lazybytes = lazy(force_bytes, bytes)
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)]