Debian Codegraph @

python/tests/serialize.py

raw
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 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><title>Hello</title></head>
88 <body><p>hello</p></body>
89 </html>
90 """:
91 print("error serializing HTML document 3")
92 sys.exit(1)
93 str = doc.serialize("iso-8859-1", 1)
94 if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
95 <html>
96 <head>
97 <meta charset="iso-8859-1">
98 <title>Hello</title>
99 </head>
100 <body><p>hello</p></body>
101 </html>
102 """:
103 print("error serializing HTML document 4", str)
104 sys.exit(1)
105
106 #
107 # Test serializing a subnode
108 #
109 doc.htmlSetMetaEncoding(None)
110 root = doc.getRootElement()
111 str = root.serialize()
112 if str != """<html><head><title>Hello</title></head><body><p>hello</p></body></html>""":
113 print("error serializing HTML root 1")
114 sys.exit(1)
115 str = root.serialize("ISO-8859-1")
116 if str != """<html><head><meta charset="ISO-8859-1"><title>Hello</title></head><body><p>hello</p></body></html>""":
117 print("error serializing HTML root 2")
118 sys.exit(1)
119 str = root.serialize(format=1)
120 if str != """<html>
121 <head><title>Hello</title></head>
122 <body><p>hello</p></body>
123 </html>""":
124 print("error serializing HTML root 3")
125 sys.exit(1)
126 str = root.serialize("iso-8859-1", 1)
127 if str != """<html>
128 <head>
129 <meta charset="iso-8859-1">
130 <title>Hello</title>
131 </head>
132 <body><p>hello</p></body>
133 </html>""":
134 print("error serializing HTML root 4")
135 sys.exit(1)
136
137 doc.freeDoc()
138
139 # Memory debug specific
140 libxml2.cleanupParser()
141 if libxml2.debugMemory(1) == 0:
142 print("OK")
143 else:
144 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))