PageRenderTime 243ms CodeModel.GetById 120ms app.highlight 10ms RepoModel.GetById 111ms app.codeStats 0ms

/tests/regressiontests/templates/parser.py

https://code.google.com/p/mango-py/
Python | 83 lines | 72 code | 6 blank | 5 comment | 0 complexity | 672d47061bf0a01c7d4838030227642b MD5 | raw file
 1"""
 2Testing some internals of the template processing. These are *not* examples to be copied in user code.
 3"""
 4from django.template import (TokenParser, FilterExpression, Parser, Variable,
 5    TemplateSyntaxError)
 6from django.utils.unittest import TestCase
 7
 8
 9class 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        )