/usr/lib/python2.7/dist-packages/chameleon/tests/test_parser.py is in python-chameleon 2.16-4.
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 | from __future__ import with_statement
import sys
from unittest import TestCase
from ..namespaces import XML_NS
from ..namespaces import XMLNS_NS
from ..namespaces import PY_NS
class ParserTest(TestCase):
def test_sample_files(self):
import os
import traceback
path = os.path.join(os.path.dirname(__file__), "inputs")
for filename in os.listdir(path):
if not filename.endswith('.html'):
continue
with open(os.path.join(path, filename), 'rb') as f:
source = f.read()
from ..utils import read_encoded
try:
want = read_encoded(source)
except UnicodeDecodeError:
exc = sys.exc_info()[1]
self.fail("%s - %s" % (exc, filename))
from ..tokenize import iter_xml
from ..parser import ElementParser
try:
tokens = iter_xml(want)
parser = ElementParser(tokens, {
'xmlns': XMLNS_NS,
'xml': XML_NS,
'py': PY_NS,
})
elements = tuple(parser)
except:
self.fail(traceback.format_exc())
output = []
def render(kind, args):
if kind == 'element':
# start tag
tag, end, children = args
output.append("%(prefix)s%(name)s" % tag)
for attr in tag['attrs']:
output.append(
"%(space)s%(name)s%(eq)s%(quote)s%(value)s%(quote)s" % \
attr
)
output.append("%(suffix)s" % tag)
# children
for item in children:
render(*item)
# end tag
output.append(
"%(prefix)s%(name)s%(space)s%(suffix)s" % end
)
elif kind == 'text':
text = args[0]
output.append(text)
elif kind == 'start_tag':
node = args[0]
output.append(
"%(prefix)s%(name)s%(space)s%(suffix)s" % node
)
else:
raise RuntimeError("Not implemented: %s." % kind)
for kind, args in elements:
render(kind, args)
got = "".join(output)
from doctest import OutputChecker
checker = OutputChecker()
if checker.check_output(want, got, 0) is False:
from doctest import Example
example = Example(f.name, want)
diff = checker.output_difference(
example, got, 0)
self.fail("(%s) - \n%s" % (f.name, diff))
|