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