Edgewall Software

Ticket #244: test_no_match.py

File test_no_match.py, 2.3 KB (added by felix.schwarz@…, 4 years ago)
Line 
1#!/usr/bin/env python
2
3import os
4import tempfile
5import unittest
6from genshi.template import TemplateLoader
7
8
9foo_data = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
10"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
11<html xmlns="http://www.w3.org/1999/xhtml"
12       xmlns:py="http://genshi.edgewall.org/"
13       xmlns:xi="http://www.w3.org/2001/XInclude">
14
15     <py:match path="div[@id='banner']">
16         No banner here!
17     </py:match>
18
19     <xi:include href="bar.html" />
20</html>"""
21
22bar_data = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
23"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
24<html xmlns="http://www.w3.org/1999/xhtml"
25       xmlns:py="http://genshi.edgewall.org/"
26       xmlns:xi="http://www.w3.org/2001/XInclude" py:strip="">
27
28  <py:match path="body" once="true" buffer="false">
29    <body>
30        <div id="banner">
31            This is the original text.
32        </div>
33    </body>
34  </py:match>
35
36  <body>
37  </body>
38</html>"""
39
40
41
42class TestPyMatchDoesNotMatch(unittest.TestCase):
43    def setUp(self):
44        self.tempdir = tempfile.mkdtemp()
45        file(os.path.join(self.tempdir, 'foo.html'), 'w').write(foo_data)
46        file(os.path.join(self.tempdir, 'bar.html'), 'w').write(bar_data)
47
48    def tearDown(self):
49        self.removeall(self.tempdir)
50
51# ----------------------------------------------------------------------------
52# copied from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/193736       
53    def removeall(self, path):
54        if not os.path.isdir(path):
55            return
56        files = os.listdir(path)
57        for x in files:
58            fullpath = os.path.join(path, x)
59            if os.path.isfile(fullpath):
60                os.remove(fullpath)
61            elif os.path.isdir(fullpath):
62                removeall(fullpath)
63                os.rmdir(fullpath)
64# ----------------------------------------------------------------------------
65
66    def test_no_match(self):
67        loader = TemplateLoader(self.tempdir)
68        template = loader.load('foo.html')
69        output = template.generate().render('xhtml', doctype='xhtml')
70        # Generated XML will be invalid but this was filed as bug #243 so I
71        # don't care for now.
72        self.assertFalse('This is the original text.' in output)
73        self.assertTrue('No banner here!' in output)
74
75