~ubuntu-branches/ubuntu/utopic/python-django/utopic

« back to all changes in this revision

Viewing changes to tests/regressiontests/templates/unicode.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- coding: utf-8 -*-
 
2
from __future__ import unicode_literals
 
3
 
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
5
8
 
6
9
 
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 }}'
14
17
        t2 = Template(s)
15
 
        s = '\x80\xc5\xc0'
 
18
        s = b'\x80\xc5\xc0'
16
19
        self.assertRaises(TemplateEncodingError, Template, s)
17
20
 
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"})
23
26
 
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)