1 | #!/usr/bin/python -u
|
---|
2 | import sys
|
---|
3 | import libxml2
|
---|
4 |
|
---|
5 | # Memory debug specific
|
---|
6 | libxml2.debugMemory(1)
|
---|
7 |
|
---|
8 | #
|
---|
9 | # Testing XML document serialization
|
---|
10 | #
|
---|
11 | doc = libxml2.parseDoc("""<root><foo>hello</foo></root>""")
|
---|
12 | str = doc.serialize()
|
---|
13 | if str != """<?xml version="1.0"?>
|
---|
14 | <root><foo>hello</foo></root>
|
---|
15 | """:
|
---|
16 | print("error serializing XML document 1")
|
---|
17 | sys.exit(1)
|
---|
18 | str = doc.serialize("iso-8859-1")
|
---|
19 | if str != """<?xml version="1.0" encoding="iso-8859-1"?>
|
---|
20 | <root><foo>hello</foo></root>
|
---|
21 | """:
|
---|
22 | print("error serializing XML document 2")
|
---|
23 | sys.exit(1)
|
---|
24 | str = doc.serialize(format=1)
|
---|
25 | if str != """<?xml version="1.0"?>
|
---|
26 | <root>
|
---|
27 | <foo>hello</foo>
|
---|
28 | </root>
|
---|
29 | """:
|
---|
30 | print("error serializing XML document 3")
|
---|
31 | sys.exit(1)
|
---|
32 | str = doc.serialize("iso-8859-1", 1)
|
---|
33 | if str != """<?xml version="1.0" encoding="iso-8859-1"?>
|
---|
34 | <root>
|
---|
35 | <foo>hello</foo>
|
---|
36 | </root>
|
---|
37 | """:
|
---|
38 | print("error serializing XML document 4")
|
---|
39 | sys.exit(1)
|
---|
40 |
|
---|
41 | #
|
---|
42 | # Test serializing a subnode
|
---|
43 | #
|
---|
44 | root = doc.getRootElement()
|
---|
45 | str = root.serialize()
|
---|
46 | if str != """<root><foo>hello</foo></root>""":
|
---|
47 | print("error serializing XML root 1")
|
---|
48 | sys.exit(1)
|
---|
49 | str = root.serialize("iso-8859-1")
|
---|
50 | if str != """<root><foo>hello</foo></root>""":
|
---|
51 | print("error serializing XML root 2")
|
---|
52 | sys.exit(1)
|
---|
53 | str = root.serialize(format=1)
|
---|
54 | if str != """<root>
|
---|
55 | <foo>hello</foo>
|
---|
56 | </root>""":
|
---|
57 | print("error serializing XML root 3")
|
---|
58 | sys.exit(1)
|
---|
59 | str = root.serialize("iso-8859-1", 1)
|
---|
60 | if str != """<root>
|
---|
61 | <foo>hello</foo>
|
---|
62 | </root>""":
|
---|
63 | print("error serializing XML root 4")
|
---|
64 | sys.exit(1)
|
---|
65 | doc.freeDoc()
|
---|
66 |
|
---|
67 | #
|
---|
68 | # Testing HTML document serialization
|
---|
69 | #
|
---|
70 | doc = libxml2.htmlParseDoc("""<html><head><title>Hello</title><body><p>hello</body></html>""", None)
|
---|
71 | str = doc.serialize()
|
---|
72 | if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
---|
73 | <html><head><title>Hello</title></head><body><p>hello</p></body></html>
|
---|
74 | """:
|
---|
75 | print("error serializing HTML document 1")
|
---|
76 | sys.exit(1)
|
---|
77 | str = doc.serialize("ISO-8859-1")
|
---|
78 | if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
---|
79 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Hello</title></head><body><p>hello</p></body></html>
|
---|
80 | """:
|
---|
81 | print("error serializing HTML document 2")
|
---|
82 | sys.exit(1)
|
---|
83 | str = doc.serialize(format=1)
|
---|
84 | if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
---|
85 | <html>
|
---|
86 | <head>
|
---|
87 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
88 | <title>Hello</title>
|
---|
89 | </head>
|
---|
90 | <body><p>hello</p></body>
|
---|
91 | </html>
|
---|
92 | """:
|
---|
93 | print("error serializing HTML document 3")
|
---|
94 | sys.exit(1)
|
---|
95 | str = doc.serialize("iso-8859-1", 1)
|
---|
96 | if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
---|
97 | <html>
|
---|
98 | <head>
|
---|
99 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
100 | <title>Hello</title>
|
---|
101 | </head>
|
---|
102 | <body><p>hello</p></body>
|
---|
103 | </html>
|
---|
104 | """:
|
---|
105 | print("error serializing HTML document 4")
|
---|
106 | sys.exit(1)
|
---|
107 |
|
---|
108 | #
|
---|
109 | # Test serializing a subnode
|
---|
110 | #
|
---|
111 | doc.htmlSetMetaEncoding(None)
|
---|
112 | root = doc.getRootElement()
|
---|
113 | str = root.serialize()
|
---|
114 | if str != """<html><head><title>Hello</title></head><body><p>hello</p></body></html>""":
|
---|
115 | print("error serializing HTML root 1")
|
---|
116 | sys.exit(1)
|
---|
117 | str = root.serialize("ISO-8859-1")
|
---|
118 | if str != """<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Hello</title></head><body><p>hello</p></body></html>""":
|
---|
119 | print("error serializing HTML root 2")
|
---|
120 | sys.exit(1)
|
---|
121 | str = root.serialize(format=1)
|
---|
122 | if str != """<html>
|
---|
123 | <head>
|
---|
124 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
125 | <title>Hello</title>
|
---|
126 | </head>
|
---|
127 | <body><p>hello</p></body>
|
---|
128 | </html>""":
|
---|
129 | print("error serializing HTML root 3")
|
---|
130 | sys.exit(1)
|
---|
131 | str = root.serialize("iso-8859-1", 1)
|
---|
132 | if str != """<html>
|
---|
133 | <head>
|
---|
134 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
135 | <title>Hello</title>
|
---|
136 | </head>
|
---|
137 | <body><p>hello</p></body>
|
---|
138 | </html>""":
|
---|
139 | print("error serializing HTML root 4")
|
---|
140 | sys.exit(1)
|
---|
141 |
|
---|
142 | doc.freeDoc()
|
---|
143 |
|
---|
144 | # Memory debug specific
|
---|
145 | libxml2.cleanupParser()
|
---|
146 | if libxml2.debugMemory(1) == 0:
|
---|
147 | print("OK")
|
---|
148 | else:
|
---|
149 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|
150 | libxml2.dumpMemory()
|
---|