/usr/share/pyshared/genshi/tests/input.py is in python-genshi 0.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2006-2009 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://genshi.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://genshi.edgewall.org/log/.
import doctest
import sys
import unittest
from genshi.core import Attrs, Stream
from genshi.input import XMLParser, HTMLParser, ParseError
from genshi.compat import StringIO, BytesIO
class XMLParserTestCase(unittest.TestCase):
def test_text_node_pos_single_line(self):
text = '<elem>foo bar</elem>'
events = list(XMLParser(StringIO(text)))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual('foo bar', data)
self.assertEqual((None, 1, 6), pos)
def test_text_node_pos_multi_line(self):
text = '''<elem>foo
bar</elem>'''
events = list(XMLParser(StringIO(text)))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual('foo\nbar', data)
self.assertEqual((None, 1, -1), pos)
def test_element_attribute_order(self):
text = '<elem title="baz" id="foo" class="bar" />'
events = list(XMLParser(StringIO(text)))
kind, data, pos = events[0]
self.assertEqual(Stream.START, kind)
tag, attrib = data
self.assertEqual('elem', tag)
self.assertEqual(('title', 'baz'), attrib[0])
self.assertEqual(('id', 'foo'), attrib[1])
self.assertEqual(('class', 'bar'), attrib[2])
def test_unicode_input(self):
text = u'<div>\u2013</div>'
events = list(XMLParser(StringIO(text)))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual(u'\u2013', data)
def test_latin1_encoded(self):
text = u'<div>\xf6</div>'.encode('iso-8859-1')
events = list(XMLParser(BytesIO(text), encoding='iso-8859-1'))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual(u'\xf6', data)
def test_latin1_encoded_xmldecl(self):
text = u"""<?xml version="1.0" encoding="iso-8859-1" ?>
<div>\xf6</div>
""".encode('iso-8859-1')
events = list(XMLParser(BytesIO(text)))
kind, data, pos = events[2]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual(u'\xf6', data)
def test_html_entity_with_dtd(self):
text = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html> </html>
"""
events = list(XMLParser(StringIO(text)))
kind, data, pos = events[2]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual(u'\xa0', data)
def test_html_entity_without_dtd(self):
text = '<html> </html>'
events = list(XMLParser(StringIO(text)))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual(u'\xa0', data)
def test_html_entity_in_attribute(self):
text = '<p title=" "/>'
events = list(XMLParser(StringIO(text)))
kind, data, pos = events[0]
self.assertEqual(Stream.START, kind)
self.assertEqual(u'\xa0', data[1].get('title'))
kind, data, pos = events[1]
self.assertEqual(Stream.END, kind)
def test_undefined_entity_with_dtd(self):
text = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>&junk;</html>
"""
events = XMLParser(StringIO(text))
self.assertRaises(ParseError, list, events)
def test_undefined_entity_without_dtd(self):
text = '<html>&junk;</html>'
events = XMLParser(StringIO(text))
self.assertRaises(ParseError, list, events)
class HTMLParserTestCase(unittest.TestCase):
def test_text_node_pos_single_line(self):
text = u'<elem>foo bar</elem>'
events = list(HTMLParser(StringIO(text)))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual('foo bar', data)
self.assertEqual((None, 1, 6), pos)
def test_text_node_pos_multi_line(self):
text = u'''<elem>foo
bar</elem>'''
events = list(HTMLParser(StringIO(text)))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual('foo\nbar', data)
self.assertEqual((None, 1, 6), pos)
def test_input_encoding_text(self):
text = u'<div>\xf6</div>'.encode('iso-8859-1')
events = list(HTMLParser(BytesIO(text), encoding='iso-8859-1'))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual(u'\xf6', data)
def test_input_encoding_attribute(self):
text = u'<div title="\xf6"></div>'.encode('iso-8859-1')
events = list(HTMLParser(BytesIO(text), encoding='iso-8859-1'))
kind, (tag, attrib), pos = events[0]
self.assertEqual(Stream.START, kind)
self.assertEqual(u'\xf6', attrib.get('title'))
def test_unicode_input(self):
text = u'<div>\u2013</div>'
events = list(HTMLParser(StringIO(text)))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual(u'\u2013', data)
def test_html_entity_in_attribute(self):
text = u'<p title=" "></p>'
events = list(HTMLParser(StringIO(text)))
kind, data, pos = events[0]
self.assertEqual(Stream.START, kind)
self.assertEqual(u'\xa0', data[1].get('title'))
kind, data, pos = events[1]
self.assertEqual(Stream.END, kind)
def test_html_entity_in_text(self):
text = u'<p> </p>'
events = list(HTMLParser(StringIO(text)))
kind, data, pos = events[1]
self.assertEqual(Stream.TEXT, kind)
self.assertEqual(u'\xa0', data)
def test_processing_instruction(self):
text = u'<?php echo "Foobar" ?>'
events = list(HTMLParser(StringIO(text)))
kind, (target, data), pos = events[0]
self.assertEqual(Stream.PI, kind)
self.assertEqual('php', target)
self.assertEqual('echo "Foobar"', data)
def test_processing_instruction_no_data_1(self):
text = u'<?foo ?>'
events = list(HTMLParser(StringIO(text)))
kind, (target, data), pos = events[0]
self.assertEqual(Stream.PI, kind)
self.assertEqual('foo', target)
self.assertEqual('', data)
def test_processing_instruction_no_data_2(self):
text = u'<?experiment>...<?/experiment>'
events = list(HTMLParser(StringIO(text)))
kind, (target, data), pos = events[0]
self.assertEqual(Stream.PI, kind)
self.assertEqual('experiment', target)
self.assertEqual('', data)
kind, (target, data), pos = events[2]
self.assertEqual('/experiment', target)
self.assertEqual('', data)
def test_xmldecl(self):
text = '<?xml version="1.0" ?><root />'
events = list(XMLParser(StringIO(text)))
kind, (version, encoding, standalone), pos = events[0]
self.assertEqual(Stream.XML_DECL, kind)
self.assertEqual('1.0', version)
self.assertEqual(None, encoding)
self.assertEqual(-1, standalone)
def test_xmldecl_encoding(self):
text = '<?xml version="1.0" encoding="utf-8" ?><root />'
events = list(XMLParser(StringIO(text)))
kind, (version, encoding, standalone), pos = events[0]
self.assertEqual(Stream.XML_DECL, kind)
self.assertEqual('1.0', version)
self.assertEqual('utf-8', encoding)
self.assertEqual(-1, standalone)
def test_xmldecl_standalone(self):
text = '<?xml version="1.0" standalone="yes" ?><root />'
events = list(XMLParser(StringIO(text)))
kind, (version, encoding, standalone), pos = events[0]
self.assertEqual(Stream.XML_DECL, kind)
self.assertEqual('1.0', version)
self.assertEqual(None, encoding)
self.assertEqual(1, standalone)
def test_processing_instruction_trailing_qmark(self):
text = u'<?php echo "Foobar" ??>'
events = list(HTMLParser(StringIO(text)))
kind, (target, data), pos = events[0]
self.assertEqual(Stream.PI, kind)
self.assertEqual('php', target)
self.assertEqual('echo "Foobar" ?', data)
def test_out_of_order_tags1(self):
text = u'<span><b>Foobar</span></b>'
events = list(HTMLParser(StringIO(text)))
self.assertEqual(5, len(events))
self.assertEqual((Stream.START, ('span', ())), events[0][:2])
self.assertEqual((Stream.START, ('b', ())), events[1][:2])
self.assertEqual((Stream.TEXT, 'Foobar'), events[2][:2])
self.assertEqual((Stream.END, 'b'), events[3][:2])
self.assertEqual((Stream.END, 'span'), events[4][:2])
def test_out_of_order_tags2(self):
text = u'<span class="baz"><b><i>Foobar</span></b></i>'.encode('utf-8')
events = list(HTMLParser(BytesIO(text), encoding='utf-8'))
self.assertEqual(7, len(events))
self.assertEqual((Stream.START, ('span', Attrs([('class', 'baz')]))),
events[0][:2])
self.assertEqual((Stream.START, ('b', ())), events[1][:2])
self.assertEqual((Stream.START, ('i', ())), events[2][:2])
self.assertEqual((Stream.TEXT, 'Foobar'), events[3][:2])
self.assertEqual((Stream.END, 'i'), events[4][:2])
self.assertEqual((Stream.END, 'b'), events[5][:2])
self.assertEqual((Stream.END, 'span'), events[6][:2])
def test_out_of_order_tags3(self):
text = u'<span><b>Foobar</i>'.encode('utf-8')
events = list(HTMLParser(BytesIO(text), encoding='utf-8'))
self.assertEqual(5, len(events))
self.assertEqual((Stream.START, ('span', ())), events[0][:2])
self.assertEqual((Stream.START, ('b', ())), events[1][:2])
self.assertEqual((Stream.TEXT, 'Foobar'), events[2][:2])
self.assertEqual((Stream.END, 'b'), events[3][:2])
self.assertEqual((Stream.END, 'span'), events[4][:2])
def test_hex_charref(self):
text = u'<span>'</span>'
events = list(HTMLParser(StringIO(text)))
self.assertEqual(3, len(events))
self.assertEqual((Stream.START, ('span', ())), events[0][:2])
self.assertEqual((Stream.TEXT, "'"), events[1][:2])
self.assertEqual((Stream.END, 'span'), events[2][:2])
def test_multibyte_character_on_chunk_boundary(self):
text = u'a' * ((4 * 1024) - 1) + u'\xe6'
events = list(HTMLParser(BytesIO(text.encode('utf-8')),
encoding='utf-8'))
self.assertEqual(1, len(events))
self.assertEqual((Stream.TEXT, text), events[0][:2])
def suite():
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(XMLParser.__module__))
suite.addTest(unittest.makeSuite(XMLParserTestCase, 'test'))
suite.addTest(unittest.makeSuite(HTMLParserTestCase, 'test'))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')
|