@@ -788,6 +788,123 @@ def test_writestring(self):
788788 elem = ET .fromstring ("<html><body>text</body></html>" )
789789 self .assertEqual (ET .tostring (elem ), b'<html><body>text</body></html>' )
790790
791+ def test_indent (self ):
792+ elem = ET .XML ("<root></root>" )
793+ ET .indent (elem )
794+ self .assertEqual (ET .tostring (elem ), b'<root />' )
795+
796+ elem = ET .XML ("<html><body>text</body></html>" )
797+ ET .indent (elem )
798+ self .assertEqual (ET .tostring (elem ), b'<html>\n <body>text</body>\n </html>' )
799+
800+ elem = ET .XML ("<html> <body>text</body> </html>" )
801+ ET .indent (elem )
802+ self .assertEqual (ET .tostring (elem ), b'<html>\n <body>text</body>\n </html>' )
803+
804+ elem = ET .XML ("<html><body>text</body>tail</html>" )
805+ ET .indent (elem )
806+ self .assertEqual (ET .tostring (elem ), b'<html>\n <body>text</body>tail</html>' )
807+
808+ elem = ET .XML ("<html><body><p>par</p>\n <p>text</p>\t <p><br/></p></body></html>" )
809+ ET .indent (elem )
810+ self .assertEqual (
811+ ET .tostring (elem ),
812+ b'<html>\n '
813+ b' <body>\n '
814+ b' <p>par</p>\n '
815+ b' <p>text</p>\n '
816+ b' <p>\n '
817+ b' <br />\n '
818+ b' </p>\n '
819+ b' </body>\n '
820+ b'</html>'
821+ )
822+
823+ elem = ET .XML ("<html><body><p>pre<br/>post</p><p>text</p></body></html>" )
824+ ET .indent (elem )
825+ self .assertEqual (
826+ ET .tostring (elem ),
827+ b'<html>\n '
828+ b' <body>\n '
829+ b' <p>pre<br />post</p>\n '
830+ b' <p>text</p>\n '
831+ b' </body>\n '
832+ b'</html>'
833+ )
834+
835+ def test_indent_space (self ):
836+ elem = ET .XML ("<html><body><p>pre<br/>post</p><p>text</p></body></html>" )
837+ ET .indent (elem , space = '\t ' )
838+ self .assertEqual (
839+ ET .tostring (elem ),
840+ b'<html>\n '
841+ b'\t <body>\n '
842+ b'\t \t <p>pre<br />post</p>\n '
843+ b'\t \t <p>text</p>\n '
844+ b'\t </body>\n '
845+ b'</html>'
846+ )
847+
848+ elem = ET .XML ("<html><body><p>pre<br/>post</p><p>text</p></body></html>" )
849+ ET .indent (elem , space = '' )
850+ self .assertEqual (
851+ ET .tostring (elem ),
852+ b'<html>\n '
853+ b'<body>\n '
854+ b'<p>pre<br />post</p>\n '
855+ b'<p>text</p>\n '
856+ b'</body>\n '
857+ b'</html>'
858+ )
859+
860+ def test_indent_space_caching (self ):
861+ elem = ET .XML ("<html><body><p>par</p><p>text</p><p><br/></p><p /></body></html>" )
862+ ET .indent (elem )
863+ self .assertEqual (
864+ {el .tail for el in elem .iter ()},
865+ {None , "\n " , "\n " , "\n " }
866+ )
867+ self .assertEqual (
868+ {el .text for el in elem .iter ()},
869+ {None , "\n " , "\n " , "\n " , "par" , "text" }
870+ )
871+ self .assertEqual (
872+ len ({el .tail for el in elem .iter ()}),
873+ len ({id (el .tail ) for el in elem .iter ()}),
874+ )
875+
876+ def test_indent_level (self ):
877+ elem = ET .XML ("<html><body><p>pre<br/>post</p><p>text</p></body></html>" )
878+ with self .assertRaises (ValueError ):
879+ ET .indent (elem , level = - 1 )
880+ self .assertEqual (
881+ ET .tostring (elem ),
882+ b"<html><body><p>pre<br />post</p><p>text</p></body></html>"
883+ )
884+
885+ ET .indent (elem , level = 2 )
886+ self .assertEqual (
887+ ET .tostring (elem ),
888+ b'<html>\n '
889+ b' <body>\n '
890+ b' <p>pre<br />post</p>\n '
891+ b' <p>text</p>\n '
892+ b' </body>\n '
893+ b' </html>'
894+ )
895+
896+ elem = ET .XML ("<html><body><p>pre<br/>post</p><p>text</p></body></html>" )
897+ ET .indent (elem , level = 1 , space = ' ' )
898+ self .assertEqual (
899+ ET .tostring (elem ),
900+ b'<html>\n '
901+ b' <body>\n '
902+ b' <p>pre<br />post</p>\n '
903+ b' <p>text</p>\n '
904+ b' </body>\n '
905+ b' </html>'
906+ )
907+
791908 def test_tostring_default_namespace (self ):
792909 elem = ET .XML ('<body xmlns="http://effbot.org/ns"><tag/></body>' )
793910 self .assertEqual (
0 commit comments