summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_xml_etree.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_xml_etree.py')
-rw-r--r--Lib/test/test_xml_etree.py117
1 files changed, 117 insertions, 0 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index b2492cd..db06ace 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -788,6 +788,123 @@ class ElementTreeTest(unittest.TestCase):
elem = ET.fromstring("<html><body>text</body></html>")
self.assertEqual(ET.tostring(elem), b'<html><body>text</body></html>')
+ def test_indent(self):
+ elem = ET.XML("<root></root>")
+ ET.indent(elem)
+ self.assertEqual(ET.tostring(elem), b'<root />')
+
+ elem = ET.XML("<html><body>text</body></html>")
+ ET.indent(elem)
+ self.assertEqual(ET.tostring(elem), b'<html>\n <body>text</body>\n</html>')
+
+ elem = ET.XML("<html> <body>text</body> </html>")
+ ET.indent(elem)
+ self.assertEqual(ET.tostring(elem), b'<html>\n <body>text</body>\n</html>')
+
+ elem = ET.XML("<html><body>text</body>tail</html>")
+ ET.indent(elem)
+ self.assertEqual(ET.tostring(elem), b'<html>\n <body>text</body>tail</html>')
+
+ elem = ET.XML("<html><body><p>par</p>\n<p>text</p>\t<p><br/></p></body></html>")
+ ET.indent(elem)
+ self.assertEqual(
+ ET.tostring(elem),
+ b'<html>\n'
+ b' <body>\n'
+ b' <p>par</p>\n'
+ b' <p>text</p>\n'
+ b' <p>\n'
+ b' <br />\n'
+ b' </p>\n'
+ b' </body>\n'
+ b'</html>'
+ )
+
+ elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>")
+ ET.indent(elem)
+ self.assertEqual(
+ ET.tostring(elem),
+ b'<html>\n'
+ b' <body>\n'
+ b' <p>pre<br />post</p>\n'
+ b' <p>text</p>\n'
+ b' </body>\n'
+ b'</html>'
+ )
+
+ def test_indent_space(self):
+ elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>")
+ ET.indent(elem, space='\t')
+ self.assertEqual(
+ ET.tostring(elem),
+ b'<html>\n'
+ b'\t<body>\n'
+ b'\t\t<p>pre<br />post</p>\n'
+ b'\t\t<p>text</p>\n'
+ b'\t</body>\n'
+ b'</html>'
+ )
+
+ elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>")
+ ET.indent(elem, space='')
+ self.assertEqual(
+ ET.tostring(elem),
+ b'<html>\n'
+ b'<body>\n'
+ b'<p>pre<br />post</p>\n'
+ b'<p>text</p>\n'
+ b'</body>\n'
+ b'</html>'
+ )
+
+ def test_indent_space_caching(self):
+ elem = ET.XML("<html><body><p>par</p><p>text</p><p><br/></p><p /></body></html>")
+ ET.indent(elem)
+ self.assertEqual(
+ {el.tail for el in elem.iter()},
+ {None, "\n", "\n ", "\n "}
+ )
+ self.assertEqual(
+ {el.text for el in elem.iter()},
+ {None, "\n ", "\n ", "\n ", "par", "text"}
+ )
+ self.assertEqual(
+ len({el.tail for el in elem.iter()}),
+ len({id(el.tail) for el in elem.iter()}),
+ )
+
+ def test_indent_level(self):
+ elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>")
+ with self.assertRaises(ValueError):
+ ET.indent(elem, level=-1)
+ self.assertEqual(
+ ET.tostring(elem),
+ b"<html><body><p>pre<br />post</p><p>text</p></body></html>"
+ )
+
+ ET.indent(elem, level=2)
+ self.assertEqual(
+ ET.tostring(elem),
+ b'<html>\n'
+ b' <body>\n'
+ b' <p>pre<br />post</p>\n'
+ b' <p>text</p>\n'
+ b' </body>\n'
+ b' </html>'
+ )
+
+ elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>")
+ ET.indent(elem, level=1, space=' ')
+ self.assertEqual(
+ ET.tostring(elem),
+ b'<html>\n'
+ b' <body>\n'
+ b' <p>pre<br />post</p>\n'
+ b' <p>text</p>\n'
+ b' </body>\n'
+ b' </html>'
+ )
+
def test_tostring_default_namespace(self):
elem = ET.XML('<body xmlns="http://effbot.org/ns"><tag/></body>')
self.assertEqual(