~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/template_tests/test_unicode.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

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