Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Converted templates doctests into unittests. We have always been at w…

…ar with doctests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14448 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0659391baf60902168f738f520f75ed20d835aa1 1 parent cabc21c
@alex alex authored
View
32 tests/regressiontests/templates/context.py
@@ -1,22 +1,16 @@
# coding: utf-8
+from django.template import Context
+from django.utils.unittest import TestCase
-context_tests = r"""
->>> from django.template import Context
->>> c = Context({'a': 1, 'b': 'xyzzy'})
->>> c['a']
-1
->>> c.push()
-{}
->>> c['a'] = 2
->>> c['a']
-2
->>> c.get('a')
-2
->>> c.pop()
-{'a': 2}
->>> c['a']
-1
->>> c.get('foo', 42)
-42
-"""
+class ContextTests(TestCase):
+ def test_context(self):
+ c = Context({"a": 1, "b": "xyzzy"})
+ self.assertEqual(c["a"], 1)
+ self.assertEqual(c.push(), {})
+ c["a"] = 2
+ self.assertEqual(c["a"], 2)
+ self.assertEqual(c.get("a"), 2)
+ self.assertEqual(c.pop(), {"a": 2})
+ self.assertEqual(c["a"], 1)
+ self.assertEqual(c.get("foo", 42), 42)
View
16 tests/regressiontests/templates/custom.py
@@ -1,11 +1,11 @@
-from django import test
from django import template
+from django.utils.unittest import TestCase
-custom_filters = """
->>> t = template.Template("{% load custom %}{{ string|trim:5 }}")
->>> ctxt = template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})
->>> t.render(ctxt)
-u"abcde"
-"""
-
+class CustomTests(TestCase):
+ def test_filter(self):
+ t = template.Template("{% load custom %}{{ string|trim:5 }}")
+ self.assertEqual(
+ t.render(template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})),
+ u"abcde"
+ )
View
198 tests/regressiontests/templates/parser.py
@@ -1,121 +1,83 @@
"""
Testing some internals of the template processing. These are *not* examples to be copied in user code.
"""
-
-token_parsing=r"""
-Tests for TokenParser behavior in the face of quoted strings with spaces.
-
->>> from django.template import TokenParser
-
-
-Test case 1: {% tag thevar|filter sometag %}
-
->>> p = TokenParser("tag thevar|filter sometag")
->>> p.tagname
-'tag'
->>> p.value()
-'thevar|filter'
->>> p.more()
-True
->>> p.tag()
-'sometag'
->>> p.more()
-False
-
-Test case 2: {% tag "a value"|filter sometag %}
-
->>> p = TokenParser('tag "a value"|filter sometag')
->>> p.tagname
-'tag'
->>> p.value()
-'"a value"|filter'
->>> p.more()
-True
->>> p.tag()
-'sometag'
->>> p.more()
-False
-
-Test case 3: {% tag 'a value'|filter sometag %}
-
->>> p = TokenParser("tag 'a value'|filter sometag")
->>> p.tagname
-'tag'
->>> p.value()
-"'a value'|filter"
->>> p.more()
-True
->>> p.tag()
-'sometag'
->>> p.more()
-False
-"""
-
-filter_parsing = r"""
->>> from django.template import FilterExpression, Parser
-
->>> c = {'article': {'section': u'News'}}
->>> p = Parser("")
->>> def fe_test(s): return FilterExpression(s, p).resolve(c)
-
->>> fe_test('article.section')
-u'News'
->>> fe_test('article.section|upper')
-u'NEWS'
->>> fe_test(u'"News"')
-u'News'
->>> fe_test(u"'News'")
-u'News'
->>> fe_test(ur'"Some \"Good\" News"')
-u'Some "Good" News'
->>> fe_test(ur"'Some \'Bad\' News'")
-u"Some 'Bad' News"
-
->>> fe = FilterExpression(ur'"Some \"Good\" News"', p)
->>> fe.filters
-[]
->>> fe.var
-u'Some "Good" News'
-
-Filtered variables should reject access of attributes beginning with underscores.
-
->>> FilterExpression('article._hidden|upper', p)
-Traceback (most recent call last):
-...
-TemplateSyntaxError: Variables and attributes may not begin with underscores: 'article._hidden'
-"""
-
-variable_parsing = r"""
->>> from django.template import Variable
-
->>> c = {'article': {'section': u'News'}}
->>> Variable('article.section').resolve(c)
-u'News'
->>> Variable(u'"News"').resolve(c)
-u'News'
->>> Variable(u"'News'").resolve(c)
-u'News'
-
-Translated strings are handled correctly.
-
->>> Variable('_(article.section)').resolve(c)
-u'News'
->>> Variable('_("Good News")').resolve(c)
-u'Good News'
->>> Variable("_('Better News')").resolve(c)
-u'Better News'
-
-Escaped quotes work correctly as well.
-
->>> Variable(ur'"Some \"Good\" News"').resolve(c)
-u'Some "Good" News'
->>> Variable(ur"'Some \'Better\' News'").resolve(c)
-u"Some 'Better' News"
-
-Variables should reject access of attributes beginning with underscores.
-
->>> Variable('article._hidden')
-Traceback (most recent call last):
-...
-TemplateSyntaxError: Variables and attributes may not begin with underscores: 'article._hidden'
-"""
+from django.template import (TokenParser, FilterExpression, Parser, Variable,
+ TemplateSyntaxError)
+from django.utils.unittest import TestCase
+
+
+class ParserTests(TestCase):
+ def test_token_parsing(self):
+ # Tests for TokenParser behavior in the face of quoted strings with
+ # spaces.
+
+ p = TokenParser("tag thevar|filter sometag")
+ self.assertEqual(p.tagname, "tag")
+ self.assertEqual(p.value(), "thevar|filter")
+ self.assertTrue(p.more())
+ self.assertEqual(p.tag(), "sometag")
+ self.assertFalse(p.more())
+
+ p = TokenParser('tag "a value"|filter sometag')
+ self.assertEqual(p.tagname, "tag")
+ self.assertEqual(p.value(), '"a value"|filter')
+ self.assertTrue(p.more())
+ self.assertEqual(p.tag(), "sometag")
+ self.assertFalse(p.more())
+
+ p = TokenParser("tag 'a value'|filter sometag")
+ self.assertEqual(p.tagname, "tag")
+ self.assertEqual(p.value(), "'a value'|filter")
+ self.assertTrue(p.more())
+ self.assertEqual(p.tag(), "sometag")
+ self.assertFalse(p.more())
+
+ def test_filter_parsing(self):
+ c = {"article": {"section": u"News"}}
+ p = Parser("")
+
+ def fe_test(s, val):
+ self.assertEqual(FilterExpression(s, p).resolve(c), val)
+
+ fe_test("article.section", u"News")
+ fe_test("article.section|upper", u"NEWS")
+ fe_test(u'"News"', u"News")
+ fe_test(u"'News'", u"News")
+ fe_test(ur'"Some \"Good\" News"', u'Some "Good" News')
+ fe_test(ur'"Some \"Good\" News"', u'Some "Good" News')
+ fe_test(ur"'Some \'Bad\' News'", u"Some 'Bad' News")
+
+ fe = FilterExpression(ur'"Some \"Good\" News"', p)
+ self.assertEqual(fe.filters, [])
+ self.assertEqual(fe.var, u'Some "Good" News')
+
+ # Filtered variables should reject access of attributes beginning with
+ # underscores.
+ self.assertRaises(TemplateSyntaxError,
+ FilterExpression, "article._hidden|upper", p
+ )
+
+ def test_variable_parsing(self):
+ c = {"article": {"section": u"News"}}
+ self.assertEqual(Variable("article.section").resolve(c), "News")
+ self.assertEqual(Variable(u'"News"').resolve(c), "News")
+ self.assertEqual(Variable(u"'News'").resolve(c), "News")
+
+ # Translated strings are handled correctly.
+ self.assertEqual(Variable("_(article.section)").resolve(c), "News")
+ self.assertEqual(Variable('_("Good News")').resolve(c), "Good News")
+ self.assertEqual(Variable("_('Better News')").resolve(c), "Better News")
+
+ # Escaped quotes work correctly as well.
+ self.assertEqual(
+ Variable(ur'"Some \"Good\" News"').resolve(c), 'Some "Good" News'
+ )
+ self.assertEqual(
+ Variable(ur"'Some \'Better\' News'").resolve(c), "Some 'Better' News"
+ )
+
+ # Variables should reject access of attributes beginning with
+ # underscores.
+ self.assertRaises(TemplateSyntaxError,
+ Variable, "article._hidden"
+ )
View
18 tests/regressiontests/templates/tests.py
@@ -21,10 +21,10 @@
from django.utils.safestring import mark_safe
from django.utils.tzinfo import LocalTimezone
-from context import context_tests
-from custom import custom_filters
-from parser import token_parsing, filter_parsing, variable_parsing
-from unicode import unicode_tests
+from context import ContextTests
+from custom import CustomTests
+from parser import ParserTests
+from unicode import UnicodeTests
from nodelist import NodelistTest
from smartif import *
@@ -35,16 +35,6 @@
import filters
-# Some other tests we would like to run
-__test__ = {
- 'unicode': unicode_tests,
- 'context': context_tests,
- 'token_parsing': token_parsing,
- 'filter_parsing': filter_parsing,
- 'variable_parsing': variable_parsing,
- 'custom_filters': custom_filters,
-}
-
#################################
# Custom template tag for tests #
#################################
View
56 tests/regressiontests/templates/unicode.py
@@ -1,37 +1,29 @@
# -*- coding: utf-8 -*-
+from django.template import Template, TemplateEncodingError, Context
+from django.utils.safestring import SafeData
+from django.utils.unittest import TestCase
-unicode_tests = ur"""
-Templates can be created from unicode strings.
->>> from django.template import *
->>> from django.utils.safestring import SafeData
->>> t1 = Template(u'ŠĐĆŽćžšđ {{ var }}')
-Templates can also be created from bytestrings. These are assumed by encoded
-using UTF-8.
+class UnicodeTests(TestCase):
+ def test_template(self):
+ # Templates can be created from unicode strings.
+ t1 = Template(u'ŠĐĆŽćžšđ {{ var }}')
+ # Templates can also be created from bytestrings. These are assumed to
+ # be encoded using UTF-8.
+ s = '\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91 {{ var }}'
+ t2 = Template(s)
+ s = '\x80\xc5\xc0'
+ self.assertRaises(TemplateEncodingError, Template, s)
->>> s = '\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91 {{ var }}'
->>> t2 = Template(s)
->>> s = '\x80\xc5\xc0'
->>> Template(s)
-Traceback (most recent call last):
- ...
-TemplateEncodingError: Templates can only be constructed from unicode or UTF-8 strings.
+ # Contexts can be constructed from unicode or UTF-8 bytestrings.
+ c1 = Context({"var": "foo"})
+ c2 = Context({u"var": "foo"})
+ c3 = Context({"var": u"Đđ"})
+ c4 = Context({u"var": "\xc4\x90\xc4\x91"})
-Contexts can be constructed from unicode or UTF-8 bytestrings.
-
->>> c1 = Context({'var': 'foo'})
->>> c2 = Context({u'var': 'foo'})
->>> c3 = Context({'var': u'Đđ'})
->>> c4 = Context({u'var': '\xc4\x90\xc4\x91'})
-
-Since both templates and all four contexts represent the same thing, they all
-render the same (and are returned as unicode objects and "safe" objects as
-well, for auto-escaping purposes).
-
->>> t1.render(c3) == t2.render(c3)
-True
->>> isinstance(t1.render(c3), unicode)
-True
->>> isinstance(t1.render(c3), SafeData)
-True
-"""
+ # Since both templates and all four contexts represent the same thing,
+ # they all render the same (and are returned as unicode objects and
+ # "safe" objects as well, for auto-escaping purposes).
+ self.assertEqual(t1.render(c3), t2.render(c3))
+ self.assertIsInstance(t1.render(c3), unicode)
+ self.assertIsInstance(t1.render(c3), SafeData)
Please sign in to comment.
Something went wrong with that request. Please try again.