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