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