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