Author: Alex
Date: 2010-11-03 23:47:05 -0500 (Wed, 03 Nov 2010)
New Revision: 14448

Modified:
   django/trunk/tests/regressiontests/templates/context.py
   django/trunk/tests/regressiontests/templates/custom.py
   django/trunk/tests/regressiontests/templates/parser.py
   django/trunk/tests/regressiontests/templates/tests.py
   django/trunk/tests/regressiontests/templates/unicode.py
Log:
Converted templates doctests into unittests.  We have always been at war with 
doctests.

Modified: django/trunk/tests/regressiontests/templates/context.py
===================================================================
--- django/trunk/tests/regressiontests/templates/context.py     2010-11-04 
01:29:37 UTC (rev 14447)
+++ django/trunk/tests/regressiontests/templates/context.py     2010-11-04 
04:47:05 UTC (rev 14448)
@@ -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)

Modified: django/trunk/tests/regressiontests/templates/custom.py
===================================================================
--- django/trunk/tests/regressiontests/templates/custom.py      2010-11-04 
01:29:37 UTC (rev 14447)
+++ django/trunk/tests/regressiontests/templates/custom.py      2010-11-04 
04:47:05 UTC (rev 14448)
@@ -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"
+        )

Modified: django/trunk/tests/regressiontests/templates/parser.py
===================================================================
--- django/trunk/tests/regressiontests/templates/parser.py      2010-11-04 
01:29:37 UTC (rev 14447)
+++ django/trunk/tests/regressiontests/templates/parser.py      2010-11-04 
04:47:05 UTC (rev 14448)
@@ -1,121 +1,83 @@
 """
 Testing some internals of the template processing. These are *not* examples to 
be copied in user code.
 """
+from django.template import (TokenParser, FilterExpression, Parser, Variable,
+    TemplateSyntaxError)
+from django.utils.unittest import TestCase
 
-token_parsing=r"""
-Tests for TokenParser behavior in the face of quoted strings with spaces.
 
->>> from django.template import TokenParser
+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())
 
-Test case 1: {% tag thevar|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 thevar|filter sometag")
->>> p.tagname
-'tag'
->>> p.value()
-'thevar|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 2: {% 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)
 
-Test case 3: {% tag 'a value'|filter sometag %}
+        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")
 
->>> p = TokenParser("tag 'a value'|filter sometag")
->>> p.tagname
-'tag'
->>> p.value()
-"'a value'|filter"
->>> p.more()
-True
->>> p.tag()
-'sometag'
->>> p.more()
-False
-"""
+        fe = FilterExpression(ur'"Some \"Good\" News"', p)
+        self.assertEqual(fe.filters, [])
+        self.assertEqual(fe.var, u'Some "Good" News')
 
-filter_parsing = r"""
->>> from django.template import FilterExpression, Parser
+        # Filtered variables should reject access of attributes beginning with
+        # underscores.
+        self.assertRaises(TemplateSyntaxError,
+            FilterExpression, "article._hidden|upper", p
+        )
 
->>> c = {'article': {'section': u'News'}}
->>> p = Parser("")
->>> def fe_test(s): return FilterExpression(s, p).resolve(c)
+    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")
 
->>> 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"
+        # 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")
 
->>> fe = FilterExpression(ur'"Some \"Good\" News"', p)
->>> fe.filters
-[]
->>> fe.var
-u'Some "Good" 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"
+        )
 
-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'
-"""
+        # Variables should reject access of attributes beginning with
+        # underscores.
+        self.assertRaises(TemplateSyntaxError,
+            Variable, "article._hidden"
+        )

Modified: django/trunk/tests/regressiontests/templates/tests.py
===================================================================
--- django/trunk/tests/regressiontests/templates/tests.py       2010-11-04 
01:29:37 UTC (rev 14447)
+++ django/trunk/tests/regressiontests/templates/tests.py       2010-11-04 
04:47:05 UTC (rev 14448)
@@ -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 #
 #################################

Modified: django/trunk/tests/regressiontests/templates/unicode.py
===================================================================
--- django/trunk/tests/regressiontests/templates/unicode.py     2010-11-04 
01:29:37 UTC (rev 14447)
+++ django/trunk/tests/regressiontests/templates/unicode.py     2010-11-04 
04:47:05 UTC (rev 14448)
@@ -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)

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to