summaryrefslogtreecommitdiff
path: root/tests/utils_tests/test_text.py
diff options
context:
space:
mode:
authorMattias Loverot <mattias@stubin.se>2016-08-24 18:18:17 +0200
committerClaude Paroz <claude@2xlibre.net>2016-08-24 18:18:17 +0200
commit9aaeec337e217109208672d8fe47eeb49ca492b5 (patch)
tree4b815546bfd3b8a5844464c3cea9d397b792c4c2 /tests/utils_tests/test_text.py
parentcf2cd4053fe3eed50ab2b7269ed72d25712c970a (diff)
Fixed #26866 -- Added format_lazy function
Added format_lazy function to django.utils.text module. Useful when dealing with relative complex lazy string concatenations (e.g. in urls.py when translating urls in regular expressions).
Diffstat (limited to 'tests/utils_tests/test_text.py')
-rw-r--r--tests/utils_tests/test_text.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py
index df4c055503..1ce993bdb2 100644
--- a/tests/utils_tests/test_text.py
+++ b/tests/utils_tests/test_text.py
@@ -6,7 +6,8 @@ import json
from django.test import SimpleTestCase
from django.utils import six, text
from django.utils.functional import lazystr
-from django.utils.translation import override
+from django.utils.text import format_lazy
+from django.utils.translation import override, ugettext_lazy
IS_WIDE_BUILD = (len('\U0001F4A9') == 1)
@@ -225,3 +226,24 @@ class TestUtilsText(SimpleTestCase):
out = text.compress_sequence(seq)
compressed_length = len(b''.join(out))
self.assertTrue(compressed_length < actual_length)
+
+ def test_format_lazy(self):
+ self.assertEqual('django/test', format_lazy('{}/{}', 'django', lazystr('test')))
+ self.assertEqual('django/test', format_lazy('{0}/{1}', *('django', 'test')))
+ self.assertEqual('django/test', format_lazy('{a}/{b}', **{'a': 'django', 'b': 'test'}))
+ self.assertEqual('django/test', format_lazy('{a[0]}/{a[1]}', a=('django', 'test')))
+
+ t = {}
+ s = format_lazy('{0[a]}-{p[a]}', t, p=t)
+ t['a'] = lazystr('django')
+ self.assertEqual('django-django', s)
+ t['a'] = 'update'
+ self.assertEqual('update-update', s)
+
+ # The format string can be lazy. (string comes from contrib.admin)
+ s = format_lazy(
+ ugettext_lazy("Added {name} \"{object}\"."),
+ name='article', object='My first try',
+ )
+ with override('fr'):
+ self.assertEqual('article «\xa0My first try\xa0» ajouté.', s)