781
|
1 # -*- coding: utf-8 -*-
|
|
2 #
|
|
3 # Copyright (C) 2006-2008 Edgewall Software
|
|
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
|
|
8 # are also available at http://genshi.edgewall.org/wiki/License.
|
|
9 #
|
|
10 # This software consists of voluntary contributions made by many
|
|
11 # individuals. For the exact contribution history, see the revision
|
|
12 # history and logs, available at http://genshi.edgewall.org/log/.
|
|
13
|
|
14 import doctest
|
|
15 import unittest
|
|
16 import sys
|
|
17
|
|
18 from genshi.core import Attrs, Stream, QName
|
|
19 from genshi.input import HTML, XML
|
|
20 from genshi.output import DocType, XMLSerializer, XHTMLSerializer, \
|
|
21 HTMLSerializer, EmptyTagFilter
|
|
22 from genshi.optimization import Optimizer, OptimizedFragment, OPTIMIZED_FRAGMENT
|
|
23
|
|
24 from genshi.template.base import BadDirectiveError, TemplateSyntaxError
|
|
25 from genshi.template.markup import MarkupTemplate
|
|
26
|
|
27
|
|
28 def _unopt(code):
|
|
29 return code.replace(' py:optimize=""', '')
|
|
30
|
|
31 class OptimizedTemplatesTestCase(unittest.TestCase):
|
|
32
|
|
33 def _test_doc(self, doc, serializer='xml'):
|
|
34 unopt = MarkupTemplate(XML(_unopt(doc)), serializer=serializer)
|
|
35 optimizer = Optimizer(10)
|
|
36 opt = MarkupTemplate(XML(doc), serializer=serializer,
|
|
37 optimizer=optimizer)
|
|
38 result = unopt.generate().render()
|
|
39 #non-cached one
|
|
40 self.assertEqual(opt.generate().render(), result)
|
|
41 #cached one
|
|
42 self.assertEqual(opt.generate().render(), result)
|
|
43 def test_double_match(self):
|
|
44 code = """\
|
|
45 <root xmlns:py="http://genshi.edgewall.org/">
|
|
46 <py:match path="tag/test">
|
|
47 <other>
|
|
48 ${select('.')}
|
|
49 </other>
|
|
50 <foo py:optimize="">
|
|
51 Some text that could <b>be</b> optimized.
|
|
52 </foo>
|
|
53 </py:match>
|
|
54 <py:match path="tag/other/test">
|
|
55 <other>
|
|
56 ${select('.')}
|
|
57 </other>
|
|
58 </py:match>
|
|
59 <tag>
|
|
60 <test py:optimize="">
|
|
61 Foo bar <i>bar</i>
|
|
62 </test>
|
|
63 </tag>
|
|
64 </root>
|
|
65 """
|
|
66 self._test_doc(code)
|
|
67
|
|
68
|
|
69
|
|
70 def suite():
|
|
71 suite = unittest.TestSuite()
|
|
72 suite.addTest(unittest.makeSuite(OptimizedTemplatesTestCase, 'test'))
|
|
73 return suite
|
|
74
|
|
75 if __name__ == '__main__':
|
|
76 unittest.main(defaultTest='suite')
|