Mercurial > genshi > mirror
view markup/tests/output.py @ 187:3932175304db stable-0.2.x 0.2.0
Prepare [milestone:0.2] release.
author | cmlenz |
---|---|
date | Tue, 22 Aug 2006 15:28:35 +0000 |
parents | ba7556e3a835 |
children | 0141f45c18e1 |
line wrap: on
line source
# -*- coding: utf-8 -*- # # Copyright (C) 2006 Edgewall Software # All rights reserved. # # This software is licensed as described in the file COPYING, which # you should have received as part of this distribution. The terms # are also available at http://markup.edgewall.org/wiki/License. # # This software consists of voluntary contributions made by many # individuals. For the exact contribution history, see the revision # history and logs, available at http://markup.edgewall.org/log/. import doctest import unittest import sys from markup.core import Stream from markup.input import HTML, XML from markup.output import DocType, XMLSerializer, XHTMLSerializer, \ HTMLSerializer class XMLSerializerTestCase(unittest.TestCase): def test_doctype_in_stream(self): stream = Stream([(Stream.DOCTYPE, DocType.HTML_STRICT, ('?', -1, -1))]) output = stream.render(XMLSerializer) self.assertEqual('<!DOCTYPE html PUBLIC ' '"-//W3C//DTD HTML 4.01//EN" ' '"http://www.w3.org/TR/html4/strict.dtd">\n', output) def test_doctype_in_stream_no_sysid(self): stream = Stream([(Stream.DOCTYPE, ('html', '-//W3C//DTD HTML 4.01//EN', None), ('?', -1, -1))]) output = stream.render(XMLSerializer) self.assertEqual('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">\n', output) def test_doctype_in_stream_no_pubid(self): stream = Stream([(Stream.DOCTYPE, ('html', None, 'http://www.w3.org/TR/html4/strict.dtd'), ('?', -1, -1))]) output = stream.render(XMLSerializer) self.assertEqual('<!DOCTYPE html SYSTEM ' '"http://www.w3.org/TR/html4/strict.dtd">\n', output) def test_doctype_in_stream_no_pubid_or_sysid(self): stream = Stream([(Stream.DOCTYPE, ('html', None, None), ('?', -1, -1))]) output = stream.render(XMLSerializer) self.assertEqual('<!DOCTYPE html>\n', output) def test_serializer_doctype(self): stream = Stream([]) output = stream.render(XMLSerializer, doctype=DocType.HTML_STRICT) self.assertEqual('<!DOCTYPE html PUBLIC ' '"-//W3C//DTD HTML 4.01//EN" ' '"http://www.w3.org/TR/html4/strict.dtd">\n', output) def test_doctype_one_and_only(self): stream = Stream([(Stream.DOCTYPE, ('html', None, None), ('?', -1, -1))]) output = stream.render(XMLSerializer, doctype=DocType.HTML_STRICT) self.assertEqual('<!DOCTYPE html PUBLIC ' '"-//W3C//DTD HTML 4.01//EN" ' '"http://www.w3.org/TR/html4/strict.dtd">\n', output) def test_comment(self): stream = Stream([(Stream.COMMENT, 'foo bar', ('?', -1, -1))]) output = stream.render(XMLSerializer) self.assertEqual('<!--foo bar-->', output) def test_processing_instruction(self): stream = Stream([(Stream.PI, ('python', 'x = 2'), ('?', -1, -1))]) output = stream.render(XMLSerializer) self.assertEqual('<?python x = 2?>', output) class XHTMLSerializerTestCase(unittest.TestCase): def test_textarea_whitespace(self): content = '\nHey there. \n\n I am indented.\n' stream = XML('<textarea name="foo">%s</textarea>' % content) output = stream.render(XHTMLSerializer) self.assertEqual('<textarea name="foo">%s</textarea>' % content, output) def test_xml_space(self): text = '<foo xml:space="preserve"> Do not mess \n\n with me </foo>' output = XML(text).render(XHTMLSerializer) self.assertEqual(text, output) def test_empty_script(self): text = """<html xmlns="http://www.w3.org/1999/xhtml"> <script src="foo.js" /> </html>""" output = XML(text).render(XHTMLSerializer) self.assertEqual("""<html xmlns="http://www.w3.org/1999/xhtml"> <script src="foo.js"></script> </html>""", output) def test_script_escaping(self): text = """<script>/*<![CDATA[*/ if (1 < 2) { alert("Doh"); } /*]]>*/</script>""" output = XML(text).render(XHTMLSerializer) self.assertEqual(text, output) def test_style_escaping(self): text = """<style>/*<![CDATA[*/ html > body { display: none; } /*]]>*/</style>""" output = XML(text).render(XHTMLSerializer) self.assertEqual(text, output) def test_embedded_svg(self): text = """<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg"> <body> <button> <svg:svg width="600px" height="400px"> <svg:polygon id="triangle" points="50,50 50,300 300,300" /> </svg:svg> </button> </body> </html>""" output = XML(text).render(XHTMLSerializer) self.assertEqual(text, output) def test_xhtml_namespace_prefix(self): text = """<html:div xmlns:html="http://www.w3.org/1999/xhtml"> <html:strong>Hello</html:strong> </html:div>""" output = XML(text).render(XHTMLSerializer) self.assertEqual(text, output) class HTMLSerializerTestCase(unittest.TestCase): def test_xml_space(self): text = '<foo xml:space="preserve"> Do not mess \n\n with me </foo>' output = XML(text).render(HTMLSerializer) self.assertEqual('<foo> Do not mess \n\n with me </foo>', output) def test_empty_script(self): text = '<script src="foo.js" />' output = XML(text).render(XHTMLSerializer) self.assertEqual('<script src="foo.js"></script>', output) def test_script_escaping(self): text = '<script>if (1 < 2) { alert("Doh"); }</script>' output = XML(text).render(HTMLSerializer) self.assertEqual('<script>if (1 < 2) { alert("Doh"); }</script>', output) def test_style_escaping(self): text = '<style>html > body { display: none; }</style>' output = XML(text).render(HTMLSerializer) self.assertEqual('<style>html > body { display: none; }</style>', output) def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(XMLSerializerTestCase, 'test')) suite.addTest(unittest.makeSuite(XHTMLSerializerTestCase, 'test')) suite.addTest(unittest.makeSuite(HTMLSerializerTestCase, 'test')) suite.addTest(doctest.DocTestSuite(XMLSerializer.__module__)) return suite if __name__ == '__main__': unittest.main(defaultTest='suite')