test_unicode.py 1.3 KB

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