1 | #!/usr/bin/env python3
|
---|
2 | import sys
|
---|
3 | import setup_test
|
---|
4 | import libxml2
|
---|
5 |
|
---|
6 | # Memory debug specific
|
---|
7 | libxml2.debugMemory(1)
|
---|
8 |
|
---|
9 | log = ""
|
---|
10 |
|
---|
11 | class callback:
|
---|
12 | def startDocument(self):
|
---|
13 | global log
|
---|
14 | log = log + "startDocument:"
|
---|
15 |
|
---|
16 | def endDocument(self):
|
---|
17 | global log
|
---|
18 | log = log + "endDocument:"
|
---|
19 |
|
---|
20 | def startElement(self, tag, attrs):
|
---|
21 | global log
|
---|
22 | log = log + "startElement %s %s:" % (tag, attrs)
|
---|
23 |
|
---|
24 | def endElement(self, tag):
|
---|
25 | global log
|
---|
26 | log = log + "endElement %s:" % (tag)
|
---|
27 |
|
---|
28 | def characters(self, data):
|
---|
29 | global log
|
---|
30 | log = log + "characters: %s:" % (data)
|
---|
31 |
|
---|
32 | def warning(self, msg):
|
---|
33 | global log
|
---|
34 | log = log + "warning: %s:" % (msg)
|
---|
35 |
|
---|
36 | def error(self, msg):
|
---|
37 | global log
|
---|
38 | log = log + "error: %s:" % (msg)
|
---|
39 |
|
---|
40 | def fatalError(self, msg):
|
---|
41 | global log
|
---|
42 | log = log + "fatalError: %s:" % (msg)
|
---|
43 |
|
---|
44 | handler = callback()
|
---|
45 |
|
---|
46 | log=""
|
---|
47 | chunk="""<foo><bar2/>"""
|
---|
48 | ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
|
---|
49 | ctxt.parseChunk(chunk, len(chunk), 0)
|
---|
50 | ctxt=None
|
---|
51 |
|
---|
52 | reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:"
|
---|
53 | if log != reference:
|
---|
54 | print("Error got: %s" % log)
|
---|
55 | print("Expected: %s" % reference)
|
---|
56 | sys.exit(1)
|
---|
57 |
|
---|
58 | log=""
|
---|
59 | chunk="""<foo><bar2></bar2>"""
|
---|
60 | ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
|
---|
61 | ctxt.parseChunk(chunk, len(chunk), 0)
|
---|
62 | ctxt=None
|
---|
63 |
|
---|
64 | reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:"
|
---|
65 | if log != reference:
|
---|
66 | print("Error got: %s" % log)
|
---|
67 | print("Expected: %s" % reference)
|
---|
68 | sys.exit(1)
|
---|
69 |
|
---|
70 | log=""
|
---|
71 | chunk="""<foo><bar2>"""
|
---|
72 | ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
|
---|
73 | ctxt.parseChunk(chunk, len(chunk), 0)
|
---|
74 | ctxt=None
|
---|
75 |
|
---|
76 | reference = "startDocument:startElement foo None:startElement bar2 None:"
|
---|
77 | if log != reference:
|
---|
78 | print("Error got: %s" % log)
|
---|
79 | print("Expected: %s" % reference)
|
---|
80 | sys.exit(1)
|
---|
81 |
|
---|
82 | log=""
|
---|
83 | chunk="""<foo><bar2 a="1" b='2' />"""
|
---|
84 | ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
|
---|
85 | ctxt.parseChunk(chunk, len(chunk), 0)
|
---|
86 | ctxt=None
|
---|
87 |
|
---|
88 | reference1 = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
|
---|
89 | reference2 = "startDocument:startElement foo None:startElement bar2 {'b': '2', 'a': '1'}:endElement bar2:"
|
---|
90 | if log not in (reference1, reference2):
|
---|
91 | print("Error got: %s" % log)
|
---|
92 | print("Expected: %s" % reference)
|
---|
93 | sys.exit(1)
|
---|
94 |
|
---|
95 | log=""
|
---|
96 | chunk="""<foo><bar2 a="1" b='2' >"""
|
---|
97 | ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
|
---|
98 | ctxt.parseChunk(chunk, len(chunk), 0)
|
---|
99 | ctxt=None
|
---|
100 |
|
---|
101 | reference1 = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:"
|
---|
102 | reference2 = "startDocument:startElement foo None:startElement bar2 {'b': '2', 'a': '1'}:"
|
---|
103 | if log not in (reference1, reference2):
|
---|
104 | print("Error got: %s" % log)
|
---|
105 | print("Expected: %s" % reference)
|
---|
106 | sys.exit(1)
|
---|
107 |
|
---|
108 | log=""
|
---|
109 | chunk="""<foo><bar2 a="1" b='2' ></bar2>"""
|
---|
110 | ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
|
---|
111 | ctxt.parseChunk(chunk, len(chunk), 0)
|
---|
112 | ctxt=None
|
---|
113 |
|
---|
114 | reference1 = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
|
---|
115 | reference2 = "startDocument:startElement foo None:startElement bar2 {'b': '2', 'a': '1'}:endElement bar2:"
|
---|
116 | if log not in (reference1, reference2):
|
---|
117 | print("Error got: %s" % log)
|
---|
118 | print("Expected: %s" % reference)
|
---|
119 | sys.exit(1)
|
---|
120 |
|
---|
121 | log=""
|
---|
122 | chunk="""<foo><bar2 a="b='1' />"""
|
---|
123 | ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
|
---|
124 | ctxt.parseChunk(chunk, len(chunk), 0)
|
---|
125 | ctxt=None
|
---|
126 |
|
---|
127 | reference = "startDocument:startElement foo None:"
|
---|
128 | if log != reference:
|
---|
129 | print("Error got: %s" % log)
|
---|
130 | print("Expected: %s" % reference)
|
---|
131 | sys.exit(1)
|
---|
132 |
|
---|
133 | # Memory debug specific
|
---|
134 | libxml2.cleanupParser()
|
---|
135 | if libxml2.debugMemory(1) == 0:
|
---|
136 | print("OK")
|
---|
137 | else:
|
---|
138 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|