File: test_sax_xmlproc.py

package info (click to toggle)
qm 1.1.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,628 kB
  • ctags: 10,249
  • sloc: python: 41,482; ansic: 20,611; xml: 12,837; sh: 485; makefile: 226
file content (106 lines) | stat: -rw-r--r-- 2,629 bytes parent folder | download | duplicates (5)
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
from test.test_support import verbose, TestFailed, findfile
from xml.sax.sax2exts import XMLValParserFactory
from xml.sax import InputSource, SAXException, ContentHandler
from StringIO import StringIO
import sys

#make_parser, ContentHandler, \
#                    SAXException, SAXReaderNotAvailable, SAXParseException
#  try:
#      make_parser()
#  except SAXReaderNotAvailable:
#      # don't try to test this module if we cannot create a parser
#      raise ImportError("no XML parsers available")
#  from xml.sax.saxutils import XMLGenerator, escape, XMLFilterBase
#  from xml.sax.expatreader import create_parser
#  from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
#  from cStringIO import StringIO

# ===== Utilities

tests = 0
fails = 0

def confirm(outcome, name):
    global tests, fails

    tests = tests + 1
    if outcome:
        print "Passed", name
    else:
        print "Failed", name
        fails = fails + 1

def gen_inputs():
    f = open("xmlval_illformed.dtd","w")
    f.write("""<!ELEMENT configuration EMPTY>\n""")

if __name__=='__main__' and len(sys.argv)>1 and sys.argv[1] == 'generate':
    gen_inputs()
    raise SystemExit

doc1 = """<?xml version="1.0"?>
<!DOCTYPE configuration SYSTEM "NONEXISTENT.dtd">
<configuration/>"""

def test_nonexistent():
    p = XMLValParserFactory.make_parser()
    i = InputSource("doc1.xml")
    i.setByteStream(StringIO(doc1))
    try:
        p.parse(i)
    except SAXException,e:
        print "PASS:",e
        return 1
    else:
        return 0

doc2 = """<?xml version="1.0"?>
<!DOCTYPE configuration SYSTEM "xmlval_illformed.dtd">
<"""

def test_illformed():
    p = XMLValParserFactory.make_parser()
    i = InputSource("doc2.xml")
    i.setByteStream(StringIO(doc2))
    try:
        p.parse(i)
    except SAXException,e:
        print "PASS:",e
        return 1
    else:
        return 0

doc3 = """<?xml version="1.0"?>
<!DOCTYPE configuration [
  <!ELEMENT configuration EMPTY>
]>
<configuration>
</configuration>
"""

class H(ContentHandler):
    def __init__(self):
        self.passed = 0

    def ignorableWhitespace(self, data):
        self.passed = 1

def test_ignorable():
    p = XMLValParserFactory.make_parser()
    i = InputSource("doc3.xml")
    i.setByteStream(StringIO(doc3))
    h = H()
    p.setContentHandler(h)
    p.parse(i)
    return h.passed

items = locals().items()
items.sort()
for (name, value) in items:
    if name[ : 5] == "test_":
        confirm(value(), name)

print "%d tests, %d failures" % (tests, fails)
if fails != 0:
    raise TestFailed, "%d of %d tests failed" % (fails, tests)