1
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
2
4
from django.template import Template, TemplateEncodingError, Context
3
5
from django.utils.safestring import SafeData
6
from django.utils import six
4
7
from django.utils.unittest import TestCase
7
10
class UnicodeTests(TestCase):
8
11
def test_template(self):
9
12
# Templates can be created from unicode strings.
10
t1 = Template(u'ŠĐĆŽćžšđ {{ var }}')
13
t1 = Template('ŠĐĆŽćžšđ {{ var }}')
11
14
# Templates can also be created from bytestrings. These are assumed to
12
15
# be encoded using UTF-8.
13
s = '\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91 {{ var }}'
16
s = b'\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91 {{ var }}'
16
19
self.assertRaises(TemplateEncodingError, Template, s)
18
21
# Contexts can be constructed from unicode or UTF-8 bytestrings.
19
c1 = Context({"var": "foo"})
20
c2 = Context({u"var": "foo"})
21
c3 = Context({"var": u"Đđ"})
22
c4 = Context({u"var": "\xc4\x90\xc4\x91"})
22
c1 = Context({b"var": b"foo"})
23
c2 = Context({"var": b"foo"})
24
c3 = Context({b"var": "Đđ"})
25
c4 = Context({"var": b"\xc4\x90\xc4\x91"})
24
27
# Since both templates and all four contexts represent the same thing,
25
28
# they all render the same (and are returned as unicode objects and
26
29
# "safe" objects as well, for auto-escaping purposes).
27
30
self.assertEqual(t1.render(c3), t2.render(c3))
28
self.assertIsInstance(t1.render(c3), unicode)
31
self.assertIsInstance(t1.render(c3), six.text_type)
29
32
self.assertIsInstance(t1.render(c3), SafeData)