From 0659391baf60902168f738f520f75ed20d835aa1 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 4 Nov 2010 04:47:05 +0000 Subject: [PATCH] Converted templates doctests into unittests. We have always been at war with doctests. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14448 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/templates/context.py | 32 ++-- tests/regressiontests/templates/custom.py | 16 +- tests/regressiontests/templates/parser.py | 170 ++++++++------------- tests/regressiontests/templates/tests.py | 18 +-- tests/regressiontests/templates/unicode.py | 56 +++---- 5 files changed, 115 insertions(+), 177 deletions(-) diff --git a/tests/regressiontests/templates/context.py b/tests/regressiontests/templates/context.py index 7886c8328b..05c1dd57b9 100644 --- a/tests/regressiontests/templates/context.py +++ b/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) diff --git a/tests/regressiontests/templates/custom.py b/tests/regressiontests/templates/custom.py index e83b7dc736..bd1e441e7f 100644 --- a/tests/regressiontests/templates/custom.py +++ b/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" + ) diff --git a/tests/regressiontests/templates/parser.py b/tests/regressiontests/templates/parser.py index 4db54556ed..1609c67e22 100644 --- a/tests/regressiontests/templates/parser.py +++ b/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 +from django.template import (TokenParser, FilterExpression, Parser, Variable, + TemplateSyntaxError) +from django.utils.unittest import TestCase -Test case 1: {% tag thevar|filter sometag %} +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") ->>> p.tagname -'tag' ->>> p.value() -'thevar|filter' ->>> p.more() -True ->>> p.tag() -'sometag' ->>> p.more() -False + 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()) -Test case 2: {% tag "a value"|filter sometag %} + 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') ->>> p.tagname -'tag' ->>> p.value() -'"a value"|filter' ->>> p.more() -True ->>> p.tag() -'sometag' ->>> p.more() -False + 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()) -Test case 3: {% tag 'a value'|filter sometag %} + def test_filter_parsing(self): + c = {"article": {"section": u"News"}} + p = Parser("") ->>> p = TokenParser("tag 'a value'|filter sometag") ->>> p.tagname -'tag' ->>> p.value() -"'a value'|filter" ->>> p.more() -True ->>> p.tag() -'sometag' ->>> p.more() -False -""" + def fe_test(s, val): + self.assertEqual(FilterExpression(s, p).resolve(c), val) -filter_parsing = r""" ->>> from django.template import FilterExpression, Parser + 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") ->>> c = {'article': {'section': u'News'}} ->>> p = Parser("") ->>> def fe_test(s): return FilterExpression(s, p).resolve(c) + fe = FilterExpression(ur'"Some \"Good\" News"', p) + self.assertEqual(fe.filters, []) + self.assertEqual(fe.var, u'Some "Good" News') ->>> 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" + # Filtered variables should reject access of attributes beginning with + # underscores. + self.assertRaises(TemplateSyntaxError, + FilterExpression, "article._hidden|upper", p + ) ->>> fe = FilterExpression(ur'"Some \"Good\" News"', p) ->>> fe.filters -[] ->>> fe.var -u'Some "Good" News' + 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") -Filtered variables should reject access of attributes beginning with underscores. + # 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") ->>> FilterExpression('article._hidden|upper', p) -Traceback (most recent call last): -... -TemplateSyntaxError: Variables and attributes may not begin with underscores: 'article._hidden' -""" + # 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" + ) -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' -""" + # Variables should reject access of attributes beginning with + # underscores. + self.assertRaises(TemplateSyntaxError, + Variable, "article._hidden" + ) diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 2da6d48de7..29171ebf08 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -21,10 +21,10 @@ from django.utils.translation import activate, deactivate, ugettext as _ 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 @@ except ImportError: 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 # ################################# diff --git a/tests/regressiontests/templates/unicode.py b/tests/regressiontests/templates/unicode.py index e5f308d202..c8d7309ad7 100644 --- a/tests/regressiontests/templates/unicode.py +++ b/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)