Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
129 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |