Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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> |
Daniel Veillard | 5f91b37 | 2002-08-12 12:13:01 +0000 | [diff] [blame] | 27 | <foo>hello</foo> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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> |
Daniel Veillard | 5f91b37 | 2002-08-12 12:13:01 +0000 | [diff] [blame] | 35 | <foo>hello</foo> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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> |
Daniel Veillard | 5f91b37 | 2002-08-12 12:13:01 +0000 | [diff] [blame] | 55 | <foo>hello</foo> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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> |
Daniel Veillard | 5f91b37 | 2002-08-12 12:13:01 +0000 | [diff] [blame] | 61 | <foo>hello</foo> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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() |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 72 | if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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") |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 78 | if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 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> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 80 | """: |
| 81 | print "error serializing HTML document 2" |
| 82 | sys.exit(1) |
| 83 | str = doc.serialize(format=1) |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 84 | if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 85 | <html> |
| 86 | <head> |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 87 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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) |
Daniel Veillard | 6cbd6c0 | 2003-12-04 12:31:49 +0000 | [diff] [blame] | 96 | if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 97 | <html> |
| 98 | <head> |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 99 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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") |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 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>""": |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 119 | print "error serializing HTML root 2" |
| 120 | sys.exit(1) |
| 121 | str = root.serialize(format=1) |
| 122 | if str != """<html> |
| 123 | <head> |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 124 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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> |
Daniel Veillard | c6d4a93 | 2002-09-12 15:00:57 +0000 | [diff] [blame] | 134 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
Daniel Veillard | 4e0e297 | 2002-03-06 21:39:42 +0000 | [diff] [blame] | 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() |