Mercurial > genshi > genshi-test
annotate markup/tests/template.py @ 29:4b6cee37ce62
* Minor simplification of template directives: they no longer get passed the template instance and the position, as no directive was actually using
that information.
* More docstrings.
author | cmlenz |
---|---|
date | Wed, 28 Jun 2006 10:40:39 +0000 |
parents | b8456279c444 |
children | 57d607f25484 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
27 | 3 # Copyright (C) 2006 Christopher Lenz |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
27 | 8 # are also available at http://markup.cmlenz.net/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
27 | 12 # history and logs, available at http://markup.cmlenz.net/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 import sys | |
17 | |
18 from markup.core import Stream | |
19 from markup.template import BadDirectiveError, Context, Template, \ | |
20 TemplateSyntaxError | |
21 | |
22 | |
23 class TemplateTestCase(unittest.TestCase): | |
24 | |
25 def test_interpolate_string(self): | |
26 parts = list(Template._interpolate('bla')) | |
27 self.assertEqual(1, len(parts)) | |
28 self.assertEqual(Stream.TEXT, parts[0][0]) | |
29 self.assertEqual('bla', parts[0][1]) | |
30 | |
31 def test_interpolate_simple(self): | |
32 parts = list(Template._interpolate('${bla}')) | |
33 self.assertEqual(1, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
34 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 35 self.assertEqual('bla', parts[0][1].source) |
36 | |
37 def test_interpolate_escaped(self): | |
38 parts = list(Template._interpolate('$${bla}')) | |
39 self.assertEqual(1, len(parts)) | |
40 self.assertEqual(Stream.TEXT, parts[0][0]) | |
41 self.assertEqual('${bla}', parts[0][1]) | |
42 | |
43 def test_interpolate_short(self): | |
44 parts = list(Template._interpolate('$bla')) | |
45 self.assertEqual(1, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
46 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 47 self.assertEqual('bla', parts[0][1].source) |
48 | |
49 def test_interpolate_mixed1(self): | |
50 parts = list(Template._interpolate('$foo bar $baz')) | |
51 self.assertEqual(3, len(parts)) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
52 self.assertEqual(Template.EXPR, parts[0][0]) |
1 | 53 self.assertEqual('foo', parts[0][1].source) |
54 self.assertEqual(Stream.TEXT, parts[1][0]) | |
55 self.assertEqual(' bar ', parts[1][1]) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
56 self.assertEqual(Template.EXPR, parts[2][0]) |
1 | 57 self.assertEqual('baz', parts[2][1].source) |
58 | |
59 def test_interpolate_mixed2(self): | |
60 parts = list(Template._interpolate('foo $bar baz')) | |
61 self.assertEqual(3, len(parts)) | |
62 self.assertEqual(Stream.TEXT, parts[0][0]) | |
63 self.assertEqual('foo ', parts[0][1]) | |
10
c5890ef863ba
Moved the template-specific stream event kinds into the template module.
cmlenz
parents:
1
diff
changeset
|
64 self.assertEqual(Template.EXPR, parts[1][0]) |
1 | 65 self.assertEqual('bar', parts[1][1].source) |
66 self.assertEqual(Stream.TEXT, parts[2][0]) | |
67 self.assertEqual(' baz', parts[2][1]) | |
68 | |
69 def test_bad_directive_error(self): | |
70 xml = '<p xmlns:py="http://purl.org/kid/ns#" py:do="nothing" />' | |
71 try: | |
21
eca77129518a
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
10
diff
changeset
|
72 tmpl = Template(xml, filename='test.html') |
1 | 73 except BadDirectiveError, e: |
74 self.assertEqual('test.html', e.filename) | |
75 if sys.version_info[:2] >= (2, 4): | |
76 self.assertEqual(1, e.lineno) | |
77 | |
78 def test_directive_value_syntax_error(self): | |
79 xml = '<p xmlns:py="http://purl.org/kid/ns#" py:if="bar\'" />' | |
21
eca77129518a
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
10
diff
changeset
|
80 tmpl = Template(xml, filename='test.html') |
1 | 81 try: |
82 list(tmpl.generate(Context())) | |
83 self.fail('Expected SyntaxError') | |
84 except TemplateSyntaxError, e: | |
85 self.assertEqual('test.html', e.filename) | |
86 if sys.version_info[:2] >= (2, 4): | |
87 self.assertEqual(1, e.lineno) | |
88 # We don't really care about the offset here, do we? | |
89 | |
90 def test_expression_syntax_error(self): | |
91 xml = '<p>\n Foo <em>${bar"}</em>\n</p>' | |
92 tmpl = Template(xml, filename='test.html') | |
93 ctxt = Context(bar='baz') | |
94 try: | |
95 list(tmpl.generate(ctxt)) | |
96 self.fail('Expected SyntaxError') | |
97 except TemplateSyntaxError, e: | |
98 self.assertEqual('test.html', e.filename) | |
99 if sys.version_info[:2] >= (2, 4): | |
100 self.assertEqual(2, e.lineno) | |
101 self.assertEqual(10, e.offset) | |
102 | |
103 | |
104 def suite(): | |
105 suite = unittest.TestSuite() | |
106 suite.addTest(doctest.DocTestSuite(Template.__module__)) | |
107 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) | |
108 return suite | |
109 | |
110 if __name__ == '__main__': | |
111 unittest.main(defaultTest='suite') |