Permalink
Browse files

Fixed #23768 -- Rewrote template tests as unit tests.

  • Loading branch information...
prestontimmons authored and timgraham committed Nov 12, 2014
1 parent 4a4ad27 commit b872134bfc14f6322bd1e4b0a08bf5bfd2c43a52
Showing with 4,855 additions and 1,369 deletions.
  1. +1 −0 AUTHORS
  2. +1 −13 tests/template_tests/filters.py
  3. 0 tests/template_tests/syntax_tests/__init__.py
  4. +121 −0 tests/template_tests/syntax_tests/test_autoescape.py
  5. +316 −0 tests/template_tests/syntax_tests/test_basic.py
  6. +18 −0 tests/template_tests/syntax_tests/test_builtins.py
  7. +118 −0 tests/template_tests/syntax_tests/test_cache.py
  8. +92 −0 tests/template_tests/syntax_tests/test_comment.py
  9. +165 −0 tests/template_tests/syntax_tests/test_cycle.py
  10. +61 −0 tests/template_tests/syntax_tests/test_exceptions.py
  11. +399 −0 tests/template_tests/syntax_tests/test_extends.py
  12. +236 −0 tests/template_tests/syntax_tests/test_filter_syntax.py
  13. +48 −0 tests/template_tests/syntax_tests/test_filter_tag.py
  14. +90 −0 tests/template_tests/syntax_tests/test_firstof.py
  15. +179 −0 tests/template_tests/syntax_tests/test_for.py
  16. +399 −0 tests/template_tests/syntax_tests/test_i18n.py
  17. +524 −0 tests/template_tests/syntax_tests/test_if.py
  18. +154 −0 tests/template_tests/syntax_tests/test_if_changed.py
  19. +217 −0 tests/template_tests/syntax_tests/test_if_equal.py
  20. +204 −0 tests/template_tests/syntax_tests/test_include.py
  21. +62 −0 tests/template_tests/syntax_tests/test_invalid_string.py
  22. +76 −0 tests/template_tests/syntax_tests/test_list_index.py
  23. +72 −0 tests/template_tests/syntax_tests/test_load.py
  24. +11 −0 tests/template_tests/syntax_tests/test_lorem.py
  25. +21 −0 tests/template_tests/syntax_tests/test_multiline.py
  26. +54 −0 tests/template_tests/syntax_tests/test_named_endblock.py
  27. +61 −0 tests/template_tests/syntax_tests/test_now.py
  28. +41 −0 tests/template_tests/syntax_tests/test_numpy.py
  29. +103 −0 tests/template_tests/syntax_tests/test_regroup.py
  30. +29 −0 tests/template_tests/syntax_tests/test_setup.py
  31. +23 −0 tests/template_tests/syntax_tests/test_simple_tag.py
  32. +38 −0 tests/template_tests/syntax_tests/test_spaceless.py
  33. +88 −0 tests/template_tests/syntax_tests/test_ssi.py
  34. +51 −0 tests/template_tests/syntax_tests/test_static.py
  35. +68 −0 tests/template_tests/syntax_tests/test_template_tag.py
  36. +267 −0 tests/template_tests/syntax_tests/test_url.py
  37. +39 −0 tests/template_tests/syntax_tests/test_verbatim.py
  38. +146 −0 tests/template_tests/syntax_tests/test_width_ratio.py
  39. +53 −0 tests/template_tests/syntax_tests/test_with.py
  40. +205 −0 tests/template_tests/syntax_tests/utils.py
  41. +4 −1,356 tests/template_tests/tests.py
@@ -554,6 +554,7 @@ answer newbie questions, and generally made Django that much better:
polpak@yahoo.com
pradeep.gowda@gmail.com
Preston Holmes <preston@ptone.com>
Preston Timmons <prestontimmons@gmail.com>
Rachel Willmer <http://www.willmer.com/kb/>
Radek Švarz <http://www.svarz.cz/translate/>
Rajesh Dhawan <rajesh.dhawan@gmail.com>
@@ -11,24 +11,12 @@
from datetime import date, datetime, time, timedelta
from django.test.utils import str_prefix, TZ_SUPPORT
from django.utils.encoding import python_2_unicode_compatible
from django.utils.safestring import mark_safe
from django.utils import timezone
# These two classes are used to test auto-escaping of __unicode__ output.
from .syntax_tests.utils import SafeClass, UnsafeClass
@python_2_unicode_compatible
class UnsafeClass:
def __str__(self):
return 'you & me'
@python_2_unicode_compatible
class SafeClass:
def __str__(self):
return mark_safe('you &gt; me')
# RESULT SYNTAX --
# 'template_name': ('template contents', 'context dict',
# 'expected string output' or Exception class)
@@ -0,0 +1,121 @@
from django.template.base import TemplateSyntaxError
from django.test import TestCase
from django.utils.safestring import mark_safe
from .utils import render, setup, SafeClass, UnsafeClass
class AutoescapeTagTests(TestCase):
@setup({'autoescape-tag01': '{% autoescape off %}hello{% endautoescape %}'})
def test_autoescape_tag01(self):
output = render('autoescape-tag01')
self.assertEqual(output, 'hello')
@setup({'autoescape-tag02': '{% autoescape off %}{{ first }}{% endautoescape %}'})
def test_autoescape_tag02(self):
output = render('autoescape-tag02', {'first': '<b>hello</b>'})
self.assertEqual(output, '<b>hello</b>')
@setup({'autoescape-tag03': '{% autoescape on %}{{ first }}{% endautoescape %}'})
def test_autoescape_tag03(self):
output = render('autoescape-tag03', {'first': '<b>hello</b>'})
self.assertEqual(output, '&lt;b&gt;hello&lt;/b&gt;')
# Autoescape disabling and enabling nest in a predictable way.
@setup({'autoescape-tag04': '{% autoescape off %}'
'{{ first }} {% autoescape on %}{{ first }}{% endautoescape %}{% endautoescape %}'})
def test_autoescape_tag04(self):
output = render('autoescape-tag04', {'first': '<a>'})
self.assertEqual(output, '<a> &lt;a&gt;')
@setup({'autoescape-tag05': '{% autoescape on %}{{ first }}{% endautoescape %}'})
def test_autoescape_tag05(self):
output = render('autoescape-tag05', {'first': '<b>first</b>'})
self.assertEqual(output, '&lt;b&gt;first&lt;/b&gt;')
# Strings (ASCII or unicode) already marked as "safe" are not
# auto-escaped
@setup({'autoescape-tag06': '{{ first }}'})
def test_autoescape_tag06(self):
output = render('autoescape-tag06', {'first': mark_safe('<b>first</b>')})
self.assertEqual(output, '<b>first</b>')
@setup({'autoescape-tag07': '{% autoescape on %}{{ first }}{% endautoescape %}'})
def test_autoescape_tag07(self):
output = render('autoescape-tag07', {'first': mark_safe('<b>Apple</b>')})
self.assertEqual(output, '<b>Apple</b>')
@setup({'autoescape-tag08': r'{% autoescape on %}'
r'{{ var|default_if_none:" endquote\" hah" }}{% endautoescape %}'})
def test_autoescape_tag08(self):
"""
Literal string arguments to filters, if used in the result, are safe.
"""
output = render('autoescape-tag08', {"var": None})
self.assertEqual(output, ' endquote" hah')
# Objects which return safe strings as their __str__ method
# won't get double-escaped.
@setup({'autoescape-tag09': r'{{ unsafe }}'})
def test_autoescape_tag09(self):
output = render('autoescape-tag09', {'unsafe': UnsafeClass()})
self.assertEqual(output, 'you &amp; me')
@setup({'autoescape-tag10': r'{{ safe }}'})
def test_autoescape_tag10(self):
output = render('autoescape-tag10', {'safe': SafeClass()})
self.assertEqual(output, 'you &gt; me')
@setup({'autoescape-filtertag01': '{{ first }}{% filter safe %}{{ first }} x<y{% endfilter %}'})
def test_autoescape_filtertag01(self):
"""
The "safe" and "escape" filters cannot work due to internal
implementation details (fortunately, the (no)autoescape block
tags can be used in those cases)
"""
with self.assertRaises(TemplateSyntaxError):
render('autoescape-filtertag01', {'first': '<a>'})
@setup({'autoescape-ifequal01': '{% ifequal var "this & that" %}yes{% endifequal %}'})
def test_autoescape_ifequal01(self):
"""
ifequal compares unescaped vales.
"""
output = render('autoescape-ifequal01', {'var': 'this & that'})
self.assertEqual(output, 'yes')
# Arguments to filters are 'safe' and manipulate their input unescaped.
@setup({'autoescape-filters01': '{{ var|cut:"&" }}'})
def test_autoescape_filters01(self):
output = render('autoescape-filters01', {'var': 'this & that'})
self.assertEqual(output, 'this that')
@setup({'autoescape-filters02': '{{ var|join:" & " }}'})
def test_autoescape_filters02(self):
output = render('autoescape-filters02', {'var': ('Tom', 'Dick', 'Harry')})
self.assertEqual(output, 'Tom & Dick & Harry')
@setup({'autoescape-literals01': '{{ "this & that" }}'})
def test_autoescape_literals01(self):
"""
Literal strings are safe.
"""
output = render('autoescape-literals01')
self.assertEqual(output, 'this & that')
@setup({'autoescape-stringiterations01': '{% for l in var %}{{ l }},{% endfor %}'})
def test_autoescape_stringiterations01(self):
"""
Iterating over strings outputs safe characters.
"""
output = render('autoescape-stringiterations01', {'var': 'K&R'})
self.assertEqual(output, 'K,&amp;,R,')
@setup({'autoescape-lookup01': '{{ var.key }}'})
def test_autoescape_lookup01(self):
"""
Escape requirement survives lookup.
"""
output = render('autoescape-lookup01', {'var': {'key': 'this & that'}})
self.assertEqual(output, 'this &amp; that')
Oops, something went wrong.

0 comments on commit b872134

Please sign in to comment.