Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 Gaynor authored November 04, 2010
32  tests/regressiontests/templates/context.py
... ...
@@ -1,22 +1,16 @@
1 1
 # coding: utf-8
  2
+from django.template import Context
  3
+from django.utils.unittest import TestCase
2 4
 
3  
-context_tests = r"""
4  
->>> from django.template import Context
5  
->>> c = Context({'a': 1, 'b': 'xyzzy'})
6  
->>> c['a']
7  
-1
8  
->>> c.push()
9  
-{}
10  
->>> c['a'] = 2
11  
->>> c['a']
12  
-2
13  
->>> c.get('a')
14  
-2
15  
->>> c.pop()
16  
-{'a': 2}
17  
->>> c['a']
18  
-1
19  
->>> c.get('foo', 42)
20  
-42
21  
-"""
22 5
 
  6
+class ContextTests(TestCase):
  7
+    def test_context(self):
  8
+        c = Context({"a": 1, "b": "xyzzy"})
  9
+        self.assertEqual(c["a"], 1)
  10
+        self.assertEqual(c.push(), {})
  11
+        c["a"] = 2
  12
+        self.assertEqual(c["a"], 2)
  13
+        self.assertEqual(c.get("a"), 2)
  14
+        self.assertEqual(c.pop(), {"a": 2})
  15
+        self.assertEqual(c["a"], 1)
  16
+        self.assertEqual(c.get("foo", 42), 42)
16  tests/regressiontests/templates/custom.py
... ...
@@ -1,11 +1,11 @@
1  
-from django import test
2 1
 from django import template
  2
+from django.utils.unittest import TestCase
3 3
 
4 4
 
5  
-custom_filters = """
6  
->>> t = template.Template("{% load custom %}{{ string|trim:5 }}")
7  
->>> ctxt = template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})
8  
->>> t.render(ctxt)
9  
-u"abcde"
10  
-"""
11  
-
  5
+class CustomTests(TestCase):
  6
+    def test_filter(self):
  7
+        t = template.Template("{% load custom %}{{ string|trim:5 }}")
  8
+        self.assertEqual(
  9
+            t.render(template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})),
  10
+            u"abcde"
  11
+        )
198  tests/regressiontests/templates/parser.py
... ...
@@ -1,121 +1,83 @@
1 1
 """
2 2
 Testing some internals of the template processing. These are *not* examples to be copied in user code.
3 3
 """
4  
-
5  
-token_parsing=r"""
6  
-Tests for TokenParser behavior in the face of quoted strings with spaces.
7  
-
8  
->>> from django.template import TokenParser
9  
-
10  
-
11  
-Test case 1: {% tag thevar|filter sometag %}
12  
-
13  
->>> p = TokenParser("tag thevar|filter sometag")
14  
->>> p.tagname
15  
-'tag'
16  
->>> p.value()
17  
-'thevar|filter'
18  
->>> p.more()
19  
-True
20  
->>> p.tag()
21  
-'sometag'
22  
->>> p.more()
23  
-False
24  
-
25  
-Test case 2: {% tag "a value"|filter sometag %}
26  
-
27  
->>> p = TokenParser('tag "a value"|filter sometag')
28  
->>> p.tagname
29  
-'tag'
30  
->>> p.value()
31  
-'"a value"|filter'
32  
->>> p.more()
33  
-True
34  
->>> p.tag()
35  
-'sometag'
36  
->>> p.more()
37  
-False
38  
-
39  
-Test case 3: {% tag 'a value'|filter sometag %}
40  
-
41  
->>> p = TokenParser("tag 'a value'|filter sometag")
42  
->>> p.tagname
43  
-'tag'
44  
->>> p.value()
45  
-"'a value'|filter"
46  
->>> p.more()
47  
-True
48  
->>> p.tag()
49  
-'sometag'
50  
->>> p.more()
51  
-False
52  
-"""
53  
-
54  
-filter_parsing = r"""
55  
->>> from django.template import FilterExpression, Parser
56  
-
57  
->>> c = {'article': {'section': u'News'}}
58  
->>> p = Parser("")
59  
->>> def fe_test(s): return FilterExpression(s, p).resolve(c)
60  
-
61  
->>> fe_test('article.section')
62  
-u'News'
63  
->>> fe_test('article.section|upper')
64  
-u'NEWS'
65  
->>> fe_test(u'"News"')
66  
-u'News'
67  
->>> fe_test(u"'News'")
68  
-u'News'
69  
->>> fe_test(ur'"Some \"Good\" News"')
70  
-u'Some "Good" News'
71  
->>> fe_test(ur"'Some \'Bad\' News'")
72  
-u"Some 'Bad' News"
73  
-
74  
->>> fe = FilterExpression(ur'"Some \"Good\" News"', p)
75  
->>> fe.filters
76  
-[]
77  
->>> fe.var
78  
-u'Some "Good" News'
79  
-
80  
-Filtered variables should reject access of attributes beginning with underscores.
81  
-
82  
->>> FilterExpression('article._hidden|upper', p)
83  
-Traceback (most recent call last):
84  
-...
85  
-TemplateSyntaxError: Variables and attributes may not begin with underscores: 'article._hidden'
86  
-"""
87  
-
88  
-variable_parsing = r"""
89  
->>> from django.template import Variable
90  
-
91  
->>> c = {'article': {'section': u'News'}}
92  
->>> Variable('article.section').resolve(c)
93  
-u'News'
94  
->>> Variable(u'"News"').resolve(c)
95  
-u'News'
96  
->>> Variable(u"'News'").resolve(c)
97  
-u'News'
98  
-
99  
-Translated strings are handled correctly.
100  
-
101  
->>> Variable('_(article.section)').resolve(c)
102  
-u'News'
103  
->>> Variable('_("Good News")').resolve(c)
104  
-u'Good News'
105  
->>> Variable("_('Better News')").resolve(c)
106  
-u'Better News'
107  
-
108  
-Escaped quotes work correctly as well.
109  
-
110  
->>> Variable(ur'"Some \"Good\" News"').resolve(c)
111  
-u'Some "Good" News'
112  
->>> Variable(ur"'Some \'Better\' News'").resolve(c)
113  
-u"Some 'Better' News"
114  
-
115  
-Variables should reject access of attributes beginning with underscores.
116  
-
117  
->>> Variable('article._hidden')
118  
-Traceback (most recent call last):
119  
-...
120  
-TemplateSyntaxError: Variables and attributes may not begin with underscores: 'article._hidden'
121  
-"""
  4
+from django.template import (TokenParser, FilterExpression, Parser, Variable,
  5
+    TemplateSyntaxError)
  6
+from django.utils.unittest import TestCase
  7
+
  8
+
  9
+class ParserTests(TestCase):
  10
+    def test_token_parsing(self):
  11
+        # Tests for TokenParser behavior in the face of quoted strings with
  12
+        # spaces.
  13
+
  14
+        p = TokenParser("tag thevar|filter sometag")
  15
+        self.assertEqual(p.tagname, "tag")
  16
+        self.assertEqual(p.value(), "thevar|filter")
  17
+        self.assertTrue(p.more())
  18
+        self.assertEqual(p.tag(), "sometag")
  19
+        self.assertFalse(p.more())
  20
+
  21
+        p = TokenParser('tag "a value"|filter sometag')
  22
+        self.assertEqual(p.tagname, "tag")
  23
+        self.assertEqual(p.value(), '"a value"|filter')
  24
+        self.assertTrue(p.more())
  25
+        self.assertEqual(p.tag(), "sometag")
  26
+        self.assertFalse(p.more())
  27
+
  28
+        p = TokenParser("tag 'a value'|filter sometag")
  29
+        self.assertEqual(p.tagname, "tag")
  30
+        self.assertEqual(p.value(), "'a value'|filter")
  31
+        self.assertTrue(p.more())
  32
+        self.assertEqual(p.tag(), "sometag")
  33
+        self.assertFalse(p.more())
  34
+
  35
+    def test_filter_parsing(self):
  36
+        c = {"article": {"section": u"News"}}
  37
+        p = Parser("")
  38
+
  39
+        def fe_test(s, val):
  40
+            self.assertEqual(FilterExpression(s, p).resolve(c), val)
  41
+
  42
+        fe_test("article.section", u"News")
  43
+        fe_test("article.section|upper", u"NEWS")
  44
+        fe_test(u'"News"', u"News")
  45
+        fe_test(u"'News'", u"News")
  46
+        fe_test(ur'"Some \"Good\" News"', u'Some "Good" News')
  47
+        fe_test(ur'"Some \"Good\" News"', u'Some "Good" News')
  48
+        fe_test(ur"'Some \'Bad\' News'", u"Some 'Bad' News")
  49
+
  50
+        fe = FilterExpression(ur'"Some \"Good\" News"', p)
  51
+        self.assertEqual(fe.filters, [])
  52
+        self.assertEqual(fe.var, u'Some "Good" News')
  53
+
  54
+        # Filtered variables should reject access of attributes beginning with
  55
+        # underscores.
  56
+        self.assertRaises(TemplateSyntaxError,
  57
+            FilterExpression, "article._hidden|upper", p
  58
+        )
  59
+
  60
+    def test_variable_parsing(self):
  61
+        c = {"article": {"section": u"News"}}
  62
+        self.assertEqual(Variable("article.section").resolve(c), "News")
  63
+        self.assertEqual(Variable(u'"News"').resolve(c), "News")
  64
+        self.assertEqual(Variable(u"'News'").resolve(c), "News")
  65
+
  66
+        # Translated strings are handled correctly.
  67
+        self.assertEqual(Variable("_(article.section)").resolve(c), "News")
  68
+        self.assertEqual(Variable('_("Good News")').resolve(c), "Good News")
  69
+        self.assertEqual(Variable("_('Better News')").resolve(c), "Better News")
  70
+
  71
+        # Escaped quotes work correctly as well.
  72
+        self.assertEqual(
  73
+            Variable(ur'"Some \"Good\" News"').resolve(c), 'Some "Good" News'
  74
+        )
  75
+        self.assertEqual(
  76
+            Variable(ur"'Some \'Better\' News'").resolve(c), "Some 'Better' News"
  77
+        )
  78
+
  79
+        # Variables should reject access of attributes beginning with
  80
+        # underscores.
  81
+        self.assertRaises(TemplateSyntaxError,
  82
+            Variable, "article._hidden"
  83
+        )
18  tests/regressiontests/templates/tests.py
@@ -21,10 +21,10 @@
21 21
 from django.utils.safestring import mark_safe
22 22
 from django.utils.tzinfo import LocalTimezone
23 23
 
24  
-from context import context_tests
25  
-from custom import custom_filters
26  
-from parser import token_parsing, filter_parsing, variable_parsing
27  
-from unicode import unicode_tests
  24
+from context import ContextTests
  25
+from custom import CustomTests
  26
+from parser import ParserTests
  27
+from unicode import UnicodeTests
28 28
 from nodelist import NodelistTest
29 29
 from smartif import *
30 30
 
@@ -35,16 +35,6 @@
35 35
 
36 36
 import filters
37 37
 
38  
-# Some other tests we would like to run
39  
-__test__ = {
40  
-    'unicode': unicode_tests,
41  
-    'context': context_tests,
42  
-    'token_parsing': token_parsing,
43  
-    'filter_parsing': filter_parsing,
44  
-    'variable_parsing': variable_parsing,
45  
-    'custom_filters': custom_filters,
46  
-}
47  
-
48 38
 #################################
49 39
 # Custom template tag for tests #
50 40
 #################################
56  tests/regressiontests/templates/unicode.py
... ...
@@ -1,37 +1,29 @@
1 1
 # -*- coding: utf-8 -*-
  2
+from django.template import Template, TemplateEncodingError, Context
  3
+from django.utils.safestring import SafeData
  4
+from django.utils.unittest import TestCase
2 5
 
3  
-unicode_tests = ur"""
4  
-Templates can be created from unicode strings.
5  
->>> from django.template import *
6  
->>> from django.utils.safestring import SafeData
7  
->>> t1 = Template(u'ŠĐĆŽćžšđ {{ var }}')
8 6
 
9  
-Templates can also be created from bytestrings. These are assumed by encoded
10  
-using UTF-8.
  7
+class UnicodeTests(TestCase):
  8
+    def test_template(self):
  9
+        # Templates can be created from unicode strings.
  10
+        t1 = Template(u'ŠĐĆŽćžšđ {{ var }}')
  11
+        # Templates can also be created from bytestrings. These are assumed to
  12
+        # be encoded using UTF-8.
  13
+        s = '\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91 {{ var }}'
  14
+        t2 = Template(s)
  15
+        s = '\x80\xc5\xc0'
  16
+        self.assertRaises(TemplateEncodingError, Template, s)
11 17
 
12  
->>> s = '\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91 {{ var }}'
13  
->>> t2 = Template(s)
14  
->>> s = '\x80\xc5\xc0'
15  
->>> Template(s)
16  
-Traceback (most recent call last):
17  
-    ...
18  
-TemplateEncodingError: Templates can only be constructed from unicode or UTF-8 strings.
  18
+        # Contexts can be constructed from unicode or UTF-8 bytestrings.
  19
+        c1 = Context({"var": "foo"})
  20
+        c2 = Context({u"var": "foo"})
  21
+        c3 = Context({"var": u"Đđ"})
  22
+        c4 = Context({u"var": "\xc4\x90\xc4\x91"})
19 23
 
20  
-Contexts can be constructed from unicode or UTF-8 bytestrings.
21  
-
22  
->>> c1 = Context({'var': 'foo'})
23  
->>> c2 = Context({u'var': 'foo'})
24  
->>> c3 = Context({'var': u'Đđ'})
25  
->>> c4 = Context({u'var': '\xc4\x90\xc4\x91'})
26  
-
27  
-Since both templates and all four contexts represent the same thing, they all
28  
-render the same (and are returned as unicode objects and "safe" objects as
29  
-well, for auto-escaping purposes).
30  
-
31  
->>> t1.render(c3) == t2.render(c3)
32  
-True
33  
->>> isinstance(t1.render(c3), unicode)
34  
-True
35  
->>> isinstance(t1.render(c3), SafeData)
36  
-True
37  
-"""
  24
+        # Since both templates and all four contexts represent the same thing,
  25
+        # they all render the same (and are returned as unicode objects and
  26
+        # "safe" objects as well, for auto-escaping purposes).
  27
+        self.assertEqual(t1.render(c3), t2.render(c3))
  28
+        self.assertIsInstance(t1.render(c3), unicode)
  29
+        self.assertIsInstance(t1.render(c3), SafeData)

0 notes on commit 0659391

Please sign in to comment.
Something went wrong with that request. Please try again.