#!/usr/bin/env python

import os
import tempfile
import unittest
from genshi.template import TemplateLoader


foo_data = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:py="http://genshi.edgewall.org/"
       xmlns:xi="http://www.w3.org/2001/XInclude">

     <py:match path="div[@id='banner']">
         No banner here!
     </py:match>

     <xi:include href="bar.html" />
</html>"""

bar_data = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:py="http://genshi.edgewall.org/"
       xmlns:xi="http://www.w3.org/2001/XInclude" py:strip="">

  <py:match path="body" once="true" buffer="false">
    <body>
        <div id="banner">
            This is the original text.
        </div>
    </body>
  </py:match>

  <body>
  </body>
</html>"""



class TestPyMatchDoesNotMatch(unittest.TestCase):
    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        file(os.path.join(self.tempdir, 'foo.html'), 'w').write(foo_data)
        file(os.path.join(self.tempdir, 'bar.html'), 'w').write(bar_data)

    def tearDown(self):
        self.removeall(self.tempdir)

# ----------------------------------------------------------------------------
# copied from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/193736        
    def removeall(self, path):
        if not os.path.isdir(path):
            return
        files = os.listdir(path)
        for x in files:
            fullpath = os.path.join(path, x)
            if os.path.isfile(fullpath):
                os.remove(fullpath)
            elif os.path.isdir(fullpath):
                removeall(fullpath)
                os.rmdir(fullpath)
# ----------------------------------------------------------------------------

    def test_no_match(self):
        loader = TemplateLoader(self.tempdir)
        template = loader.load('foo.html')
        output = template.generate().render('xhtml', doctype='xhtml')
        # Generated XML will be invalid but this was filed as bug #243 so I
        # don't care for now.
        self.assertFalse('This is the original text.' in output)
        self.assertTrue('No banner here!' in output)



