' % meld_ns)
self.assertRaises(ValueError, self._parse_html, repeated)
class UtilTests(unittest.TestCase):
def test_insert_xhtml_doctype(self):
from supervisor.templating import insert_doctype
orig = ''
actual = insert_doctype(orig)
expected = ''
self.assertEqual(actual, expected)
def test_insert_doctype_after_xmldecl(self):
from supervisor.templating import insert_doctype
orig = ''
actual = insert_doctype(orig)
expected = ''
self.assertEqual(actual, expected)
def test_insert_meld_ns_decl(self):
from supervisor.templating import insert_meld_ns_decl
orig = ''
actual = insert_meld_ns_decl(orig)
expected = ''
self.assertEqual(actual, expected)
def test_prefeed_preserves_existing_meld_ns(self):
from supervisor.templating import prefeed
orig = ''
actual = prefeed(orig)
expected = ''
self.assertEqual(actual, expected)
def test_prefeed_preserves_existing_doctype(self):
from supervisor.templating import prefeed
orig = ''
actual = prefeed(orig)
self.assertEqual(actual, orig)
class WriterTests(unittest.TestCase):
def _parse(self, xml):
from supervisor.templating import parse_xmlstring
root = parse_xmlstring(xml)
return root
def _parse_html(self, xml):
from supervisor.templating import parse_htmlstring
root = parse_htmlstring(xml)
return root
def _write(self, fn, **kw):
try:
from io import BytesIO
except: # python 2.5
from StringIO import StringIO as BytesIO
out = BytesIO()
fn(out, **kw)
out.seek(0)
actual = out.read()
return actual
def _write_xml(self, node, **kw):
return self._write(node.write_xml, **kw)
def _write_html(self, node, **kw):
return self._write(node.write_html, **kw)
def _write_xhtml(self, node, **kw):
return self._write(node.write_xhtml, **kw)
def assertNormalizedXMLEqual(self, a, b):
from supervisor.compat import as_string
a = normalize_xml(as_string(a, encoding='latin1'))
b = normalize_xml(as_string(b, encoding='latin1'))
self.assertEqual(a, b)
def assertNormalizedHTMLEqual(self, a, b):
from supervisor.compat import as_string
a = normalize_xml(as_string(a, encoding='latin1'))
b = normalize_xml(as_string(b, encoding='latin1'))
self.assertEqual(a, b)
def test_write_simple_xml(self):
root = self._parse(_SIMPLE_XML)
actual = self._write_xml(root)
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
for el, data in root.findmeld('item').repeat(((1,2),)):
el.findmeld('name').text = str(data[0])
el.findmeld('description').text = str(data[1])
actual = self._write_xml(root)
expected = """12"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_xhtml(root)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_as_html(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_html(root)
expected = """
Hello!
"""
self.assertNormalizedHTMLEqual(actual, expected)
def test_write_complex_xhtml_as_html(self):
root = self._parse(_COMPLEX_XHTML)
actual = self._write_html(root)
expected = """
This will be escaped in html output: &
Name
Description
"""
self.assertNormalizedHTMLEqual(actual, expected)
def test_write_complex_xhtml_as_xhtml(self):
# I'm not entirely sure if the cdata "script" quoting in this
# test is entirely correct for XHTML. Ryan Tomayko suggests
# that escaped entities are handled properly in script tags by
# XML-aware browsers at
# http://sourceforge.net/mailarchive/message.php?msg_id=10835582
# but I haven't tested it at all. ZPT does not seem to do
# this; it outputs unescaped data.
root = self._parse(_COMPLEX_XHTML)
actual = self._write_xhtml(root)
expected = """
This will be escaped in html output: &
Name
Description
"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_emptytags_html(self):
from supervisor.compat import as_string
root = self._parse(_EMPTYTAGS_HTML)
actual = self._write_html(root)
expected = """
"""
self.assertEqual(as_string(actual, encoding='latin1'), expected)
def test_write_booleanattrs_xhtml_as_html(self):
root = self._parse(_BOOLEANATTRS_XHTML)
actual = self._write_html(root)
expected = """
"""
self.assertNormalizedHTMLEqual(actual, expected)
def test_write_simple_xhtml_pipeline(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_xhtml(root, pipeline=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_pipeline(self):
root = self._parse(_SIMPLE_XML)
actual = self._write_xml(root, pipeline=True)
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_override_encoding(self):
root = self._parse(_SIMPLE_XML)
actual = self._write_xml(root, encoding="latin-1")
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_as_fragment(self):
root = self._parse(_SIMPLE_XML)
actual = self._write_xml(root, fragment=True)
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_with_doctype(self):
root = self._parse(_SIMPLE_XML)
from supervisor.templating import doctype
actual = self._write_xml(root, doctype=doctype.xhtml)
expected = """
NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_doctype_nodeclaration(self):
root = self._parse(_SIMPLE_XML)
from supervisor.templating import doctype
actual = self._write_xml(root, declaration=False,
doctype=doctype.xhtml)
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xml_fragment_kills_doctype_and_declaration(self):
root = self._parse(_SIMPLE_XML)
from supervisor.templating import doctype
actual = self._write_xml(root, declaration=True,
doctype=doctype.xhtml, fragment=True)
expected = """NameDescription"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_override_encoding(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_xhtml(root, encoding="latin-1", declaration=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_as_fragment(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_xhtml(root, fragment=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_with_doctype(self):
root = self._parse(_SIMPLE_XHTML)
from supervisor.templating import doctype
actual = self._write_xhtml(root, doctype=doctype.xhtml)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_doctype_nodeclaration(self):
root = self._parse(_SIMPLE_XHTML)
from supervisor.templating import doctype
actual = self._write_xhtml(root, declaration=False,
doctype=doctype.xhtml)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_fragment_kills_doctype_and_declaration(self):
root = self._parse(_SIMPLE_XHTML)
from supervisor.templating import doctype
actual = self._write_xhtml(root, declaration=True,
doctype=doctype.xhtml, fragment=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_as_html_fragment(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_html(root, fragment=True)
expected = """Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_with_doctype_as_html(self):
root = self._parse(_SIMPLE_XHTML)
actual = self._write_html(root)
expected = """
Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_write_simple_xhtml_as_html_new_doctype(self):
root = self._parse(_SIMPLE_XHTML)
from supervisor.templating import doctype
actual = self._write_html(root, doctype=doctype.html_strict)
expected = """
Hello!"""
self.assertNormalizedXMLEqual(actual, expected)
def test_unknown_entity(self):
# exception thrown may vary by python or expat version
from xml.parsers import expat
self.assertRaises((expat.error, SyntaxError), self._parse,
'&fleeb;')
def test_content_nostructure(self):
root = self._parse(_SIMPLE_XML)
D = root.findmeld('description')
D.content('description &&', structure=False)
actual = self._write_xml(root)
expected = """
Namedescription &<foo>&<bar>"""
self.assertNormalizedXMLEqual(actual, expected)
def test_content_structure(self):
root = self._parse(_SIMPLE_XML)
D = root.findmeld('description')
D.content('description &', structure=True)
actual = self._write_xml(root)
expected = """
Namedescription &"""
self.assertNormalizedXMLEqual(actual, expected)
def test_replace_nostructure(self):
root = self._parse(_SIMPLE_XML)
D = root.findmeld('description')
D.replace('description &&', structure=False)
actual = self._write_xml(root)
expected = """
Name
description &<foo>&<bar>
"""
self.assertNormalizedXMLEqual(actual, expected)
def test_replace_structure(self):
root = self._parse(_SIMPLE_XML)
D = root.findmeld('description')
D.replace('description &', structure=True)
actual = self._write_xml(root)
expected = """
Name
description &"""
self.assertNormalizedXMLEqual(actual, expected)
def test_escape_cdata(self):
from supervisor.compat import as_bytes
from supervisor.templating import _escape_cdata
a = ('< > <& &' && &foo "" '
'http://www.example.com?foo=bar&bang=baz {')
self.assertEqual(
as_bytes('< > <& &' && &foo "" '
'http://www.example.com?foo=bar&bang=baz {',
encoding='latin1'),
_escape_cdata(a))
def test_escape_cdata_unicodeerror(self):
from supervisor.templating import _escape_cdata
from supervisor.compat import as_bytes
from supervisor.compat import as_string
a = as_string(as_bytes('\x80', encoding='latin1'), encoding='latin1')
self.assertEqual(as_bytes('', encoding='latin1'),
_escape_cdata(a, 'ascii'))
def test_escape_attrib(self):
from supervisor.templating import _escape_attrib
from supervisor.compat import as_bytes
a = ('< > <& &' && &foo "" '
'http://www.example.com?foo=bar&bang=baz {')
self.assertEqual(
as_bytes('< > <& &' '
'&& &foo "" '
'http://www.example.com?foo=bar&bang=baz {',
encoding='latin1'),
_escape_attrib(a, None))
def test_escape_attrib_unicodeerror(self):
from supervisor.templating import _escape_attrib
from supervisor.compat import as_bytes
from supervisor.compat import as_string
a = as_string(as_bytes('\x80', encoding='latin1'), encoding='latin1')
self.assertEqual(as_bytes('', encoding='latin1'),
_escape_attrib(a, 'ascii'))
def normalize_html(s):
s = re.sub(r"[ \t]+", " ", s)
s = re.sub(r"/>", ">", s)
return s
def normalize_xml(s):
s = re.sub(r"\s+", " ", s)
s = re.sub(r"(?s)\s+<", "<", s)
s = re.sub(r"(?s)>\s+", ">", s)
return s
def test_suite():
return unittest.findTestCases(sys.modules[__name__])
def main():
unittest.main(defaultTest='test_suite')
if __name__ == '__main__':
main()