/usr/lib/python2.7/dist-packages/nevow/test/test_flatsax.py is in python-nevow 0.14.2-1.
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 | # Copyright (c) 2004 Divmod.
# See LICENSE for details.
from nevow.testutil import TestCase
from nevow.flat.flatsax import parse, parseString
from nevow.flat import flatten
from nevow.stan import Tag, slot
def norm(s):
return ' '.join(s.split())
class Basic(TestCase):
"""
Tests for Nevow documents which are loaded via L{xml.sax} from XHTML
templates.
"""
def _tagChildren(self, tag):
"""
Return a list of the children of C{tag} which are themselves L{Tag}s.
"""
return [x for x in tag.children if isinstance(x, Tag)]
def test_tagLocation(self):
"""
L{Tag} instances returned by L{parse} have a C{filename} attribute
which gives the name of the file from which they were parsed, a
C{lineNumber} attribute giving the line number on which the tag was
seen in that file, and a C{columnNumber} attribute giving the column
number at which the tag was seen in that file.
"""
fName = self.mktemp()
fObj = file(fName, 'w')
fObj.write(
'<html>\n'
' <head>\n'
' <title>\n'
' Hello, world.\n'
' </title>\n'
' </head>\n'
' <body>\n'
' Hi.\n'
' </body>\n'
'</html>\n')
fObj.close()
[html] = parse(file(fName))
[head, body] = self._tagChildren(html)
[title] = self._tagChildren(head)
self.assertEqual(html.filename, fName)
self.assertEqual(html.lineNumber, 1)
self.assertEqual(html.columnNumber, 0)
self.assertEqual(head.filename, fName)
self.assertEqual(head.lineNumber, 2)
self.assertEqual(head.columnNumber, 2)
self.assertEqual(title.filename, fName)
self.assertEqual(title.lineNumber, 3)
self.assertEqual(title.columnNumber, 4)
self.assertEqual(body.filename, fName)
self.assertEqual(body.lineNumber, 7)
self.assertEqual(body.columnNumber, 2)
def test_attrLocation(self):
"""
I{attr} L{Tag} instances returned by L{parse} have a C{filename}
attribute which gives the name of the file from which they were parsed,
a C{lineNumber} attribute giving the line number on which the tag was
seen in that file, and a C{columnNumber} attribute giving the column
number at which the tag was seen in that file.
"""
fName = self.mktemp()
fObj = file(fName, 'w')
fObj.write(
'<html xmlns:nevow="http://nevow.com/ns/nevow/0.1">\n'
' <nevow:attr name="foo" />\n'
'</html>\n')
fObj.close()
[html] = parse(file(fName))
attr = html.attributes['foo']
self.assertEqual(attr.filename, fName)
self.assertEqual(attr.lineNumber, 2)
self.assertEqual(attr.columnNumber, 4)
def test_slotLocation(self):
"""
L{slot} instances returned by L{parse} have the same C{filename},
C{lineNumber}, and C{columnNumber} attributes as L{Tag} instances do.
"""
fName = self.mktemp()
fObj = file(fName, 'w')
fObj.write(
'<html xmlns:nevow="http://nevow.com/ns/nevow/0.1">\n'
' <nevow:slot name="foo" />\n'
'</html>')
fObj.close()
[html] = parse(file(fName))
[foo] = [x for x in html.children if isinstance(x, slot)]
self.assertEqual(foo.filename, fName)
self.assertEqual(foo.lineNumber, 2)
self.assertEqual(foo.columnNumber, 4)
def test_parseString(self):
xml = '''<html></html>'''
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_attrs(self):
xml = '''<p class="foo"></p>'''
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_xmlns(self):
xml = '''<html xmlns="http://www.w3.org/1999/xhtml"></html>'''
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_processingInstruction(self):
xml = '''<html></html>'''
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_doctype(self):
xml = (
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n'
'<html></html>')
self.failUnlessEqual(norm(xml), norm(flatten(parseString(xml))))
def test_entities(self):
xml = """<p>&</p>"""
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_cdata(self):
xml = '<script type="text/javascript"><![CDATA[<abc]]></script>'
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_comment(self):
xml = '<!-- comment &£ --><html></html>'
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_commentWhereSpacingMatters(self):
"""
Explicitly test that spacing in comments is maintained.
"""
xml = """<head>
<!--[if IE]>
<style>
div.logo {
margin-left: 10px;
}
</style>
<![endif]-->
</head>"""
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_unicodeComment(self):
xml = '<!-- \xc2\xa3 --><html></html>'
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_xmlAttr(self):
xml = '<html xml:lang="en"></html>'
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_badNamespace(self):
xml = '<html foo:bar="wee"><abc:p>xyz</abc:p></html>'
self.failUnlessEqual(xml, flatten(parseString(xml)))
test_badNamespace.skip = (
'the standard 2.3 sax parser likes all namespaces to be defined '
'so this test fails. it does pass with python-xml')
def test_switchns(self):
xml = (
'<html xmlns="http://www.w3.org/1999/xhtml">'
'<p>in default namespace</p>'
'<foo:div xmlns:foo="http://www.w3.org/1999/xhtml">'
'<foo:p>in foo namespace</foo:p></foo:div></html>')
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_otherns(self):
xml = (
'<html xmlns="http://www.w3.org/1999/xhtml" '
'xmlns:xf="http://www.w3.org/2002/xforms"><p>'
'in default namespace</p><xf:input><xf:label>'
'in another namespace</xf:label></xf:input></html>')
self.failUnlessEqual(xml, flatten(parseString(xml)))
def test_invisiblens(self):
"""
Test that invisible tags do not get output with a namespace.
"""
xml = (
'<p xmlns:n="http://nevow.com/ns/nevow/0.1">'
'<n:invisible>123</n:invisible></p>')
self.failUnlessEqual('<p>123</p>', flatten(parseString(xml)))
|