@@ -482,7 +482,15 @@ def testGetAttributeNS(self):
482482 self .assertEqual (child2 .getAttributeNS ("http://www.python.org" , "missing" ),
483483 '' )
484484
485- def testGetAttributeNode (self ): pass
485+ def testGetAttributeNode (self ):
486+ dom = parseString ("<doc a='1'/>" )
487+ elem = dom .documentElement
488+ attr = elem .getAttributeNode ("a" )
489+ self .assertEqual (attr .name , "a" )
490+ self .assertEqual (attr .value , "1" )
491+ self .assertIs (attr .ownerElement , elem )
492+ self .assertIsNone (elem .getAttributeNode ("b" ))
493+ dom .unlink ()
486494
487495 def testGetElementsByTagNameNS (self ):
488496 d = """<foo xmlns:minidom='http://pyxml.sf.net/minidom'>
@@ -671,9 +679,34 @@ def testTextRepr(self):
671679 self .assertEqual (str (el ), repr (el ))
672680 self .assertEqual ('<DOM Text node "\' foo\' ">' , str (el ))
673681
674- def testWriteText (self ): pass
682+ def testWriteText (self ):
683+ dom = parseString ("<doc><a>text</a><b><&></b></doc>" )
684+ elem = dom .documentElement
685+ writer = io .StringIO ()
686+ elem .writexml (writer )
687+ self .assertEqual (writer .getvalue (),
688+ "<doc><a>text</a><b><&></b></doc>" )
689+ writer = io .StringIO ()
690+ elem .writexml (writer , indent = " " , addindent = " " , newl = "\n " )
691+ self .assertEqual (writer .getvalue (),
692+ " <doc>\n "
693+ " <a>text</a>\n "
694+ " <b><&></b>\n "
695+ " </doc>\n " )
696+ dom .unlink ()
675697
676- def testDocumentElement (self ): pass
698+ def testDocumentElement (self ):
699+ dom = parseString ("<!-- comment --><doc/><?pi data?>" )
700+ elem = dom .documentElement
701+ self .assertEqual (elem .tagName , "doc" )
702+ self .assertIs (elem , dom .childNodes [1 ])
703+ dom .unlink ()
704+
705+ dom = Document ()
706+ self .assertIsNone (dom .documentElement )
707+ elem = dom .appendChild (dom .createElement ("doc" ))
708+ self .assertIs (dom .documentElement , elem )
709+ dom .unlink ()
677710
678711 def testTooManyDocumentElements (self ):
679712 doc = parseString ("<doc/>" )
@@ -683,25 +716,126 @@ def testTooManyDocumentElements(self):
683716 elem .unlink ()
684717 doc .unlink ()
685718
686- def testCreateElementNS (self ): pass
719+ def testCreateElementNS (self ):
720+ dom = Document ()
721+ elem = dom .createElementNS ("http://xml.python.org/ns" , "p:elem" )
722+ self .assertEqual (elem .nodeType , Node .ELEMENT_NODE )
723+ self .assertEqual (elem .tagName , "p:elem" )
724+ self .assertEqual (elem .nodeName , "p:elem" )
725+ self .assertEqual (elem .namespaceURI , "http://xml.python.org/ns" )
726+ self .assertEqual (elem .prefix , "p" )
727+ self .assertEqual (elem .localName , "elem" )
728+ self .assertIs (elem .ownerDocument , dom )
729+ self .assertIsNone (elem .parentNode )
730+
731+ elem = dom .createElementNS ("http://xml.python.org/ns" , "elem" )
732+ self .assertEqual (elem .tagName , "elem" )
733+ self .assertIsNone (elem .prefix )
734+ self .assertEqual (elem .localName , "elem" )
735+ dom .unlink ()
687736
688- def testCreateAttributeNS (self ): pass
737+ def testCreateAttributeNS (self ):
738+ dom = Document ()
739+ attr = dom .createAttributeNS ("http://xml.python.org/ns" , "p:attr" )
740+ self .assertEqual (attr .nodeType , Node .ATTRIBUTE_NODE )
741+ self .assertEqual (attr .name , "p:attr" )
742+ self .assertEqual (attr .nodeName , "p:attr" )
743+ self .assertEqual (attr .namespaceURI , "http://xml.python.org/ns" )
744+ self .assertEqual (attr .prefix , "p" )
745+ self .assertEqual (attr .localName , "attr" )
746+ self .assertEqual (attr .value , "" )
747+ self .assertIs (attr .ownerDocument , dom )
748+ self .assertIsNone (attr .ownerElement )
749+
750+ elem = dom .appendChild (dom .createElement ("doc" ))
751+ elem .setAttributeNode (attr )
752+ self .assertIs (attr .ownerElement , elem )
753+ self .assertIs (elem .getAttributeNodeNS ("http://xml.python.org/ns" ,
754+ "attr" ), attr )
755+ dom .unlink ()
689756
690- def testParse (self ): pass
757+ def testParse (self ):
758+ # parsing from a file object is tested in testParseFromBinaryFile
759+ # and testParseFromTextFile
760+ dom = parse (tstfile )
761+ self .assertEqual (dom .nodeType , Node .DOCUMENT_NODE )
762+ self .assertEqual (dom .documentElement .tagName , "HTML" )
763+ dom .unlink ()
691764
692- def testParseString ( self ): pass
765+ self . assertRaises ( ExpatError , parseString , "<doc>" )
693766
694- def testComment (self ): pass
767+ def testParseString (self ):
768+ dom = parseString ("<doc>text</doc>" )
769+ self .assertEqual (dom .nodeType , Node .DOCUMENT_NODE )
770+ self .assertEqual (dom .documentElement .tagName , "doc" )
771+ self .assertEqual (dom .documentElement .firstChild .data , "text" )
772+ dom .unlink ()
773+
774+ dom = parseString (b"<?xml version='1.0' encoding='utf-8'?>"
775+ b"<doc>\xc3 \xa9 </doc>" )
776+ self .assertEqual (dom .documentElement .firstChild .data , "\xe9 " )
777+ dom .unlink ()
778+
779+ def testComment (self ):
780+ dom = Document ()
781+ comment = dom .createComment ("comment" )
782+ self .assertEqual (comment .nodeType , Node .COMMENT_NODE )
783+ self .assertEqual (comment .nodeName , "#comment" )
784+ self .assertEqual (comment .data , "comment" )
785+ self .assertEqual (comment .nodeValue , "comment" )
786+ self .assertIsNone (comment .attributes )
787+ dom .appendChild (comment )
788+ self .assertEqual (dom .toxml (),
789+ '<?xml version="1.0" ?><!--comment-->' )
790+ dom .unlink ()
695791
696- def testAttrListItem (self ): pass
792+ dom = parseString ("<doc><!--comment--></doc>" )
793+ comment = dom .documentElement .firstChild
794+ self .assertEqual (comment .nodeType , Node .COMMENT_NODE )
795+ self .assertEqual (comment .data , "comment" )
796+ dom .unlink ()
697797
698- def testAttrListItems (self ): pass
798+ def testAttrListItem (self ):
799+ dom = parseString ("<doc a='1' b='2'/>" )
800+ attrs = dom .documentElement .attributes
801+ self .assertEqual (attrs .item (0 ).name , "a" )
802+ self .assertEqual (attrs .item (1 ).name , "b" )
803+ self .assertIsNone (attrs .item (2 ))
804+ dom .unlink ()
699805
700- def testAttrListItemNS (self ): pass
806+ def testAttrListItems (self ):
807+ dom = parseString ("<doc a='1' b='2'/>" )
808+ attrs = dom .documentElement .attributes
809+ self .assertEqual (attrs .items (), [("a" , "1" ), ("b" , "2" )])
810+ dom .unlink ()
701811
702- def testAttrListKeys (self ): pass
812+ def testAttrListItemNS (self ):
813+ dom = parseString ("<doc xmlns:p='http://xml.python.org/ns' "
814+ "p:a='1' b='2'/>" )
815+ attrs = dom .documentElement .attributes
816+ self .assertEqual (attrs .itemsNS (), [
817+ ((xml .dom .XMLNS_NAMESPACE , "p" ), "http://xml.python.org/ns" ),
818+ (("http://xml.python.org/ns" , "a" ), "1" ),
819+ ((None , "b" ), "2" ),
820+ ])
821+ dom .unlink ()
703822
704- def testAttrListKeysNS (self ): pass
823+ def testAttrListKeys (self ):
824+ dom = parseString ("<doc a='1' b='2'/>" )
825+ attrs = dom .documentElement .attributes
826+ self .assertEqual (list (attrs .keys ()), ["a" , "b" ])
827+ dom .unlink ()
828+
829+ def testAttrListKeysNS (self ):
830+ dom = parseString ("<doc xmlns:p='http://xml.python.org/ns' "
831+ "p:a='1' b='2'/>" )
832+ attrs = dom .documentElement .attributes
833+ self .assertEqual (list (attrs .keysNS ()), [
834+ (xml .dom .XMLNS_NAMESPACE , "p" ),
835+ ("http://xml.python.org/ns" , "a" ),
836+ (None , "b" ),
837+ ])
838+ dom .unlink ()
705839
706840 def testRemoveNamedItem (self ):
707841 doc = parseString ("<doc a=''/>" )
@@ -722,29 +856,162 @@ def testRemoveNamedItemNS(self):
722856 self .assertRaises (xml .dom .NotFoundErr , attrs .removeNamedItemNS ,
723857 "http://xml.python.org/" , "b" )
724858
725- def testAttrListValues (self ): pass
859+ def testAttrListValues (self ):
860+ dom = parseString ("<doc a='1' b='2'/>" )
861+ attrs = dom .documentElement .attributes
862+ self .assertEqual ([attr .name for attr in attrs .values ()], ["a" , "b" ])
863+ self .assertEqual ([attr .value for attr in attrs .values ()], ["1" , "2" ])
864+ dom .unlink ()
865+
866+ def testAttrListLength (self ):
867+ dom = parseString ("<doc a='1' b='2'/>" )
868+ attrs = dom .documentElement .attributes
869+ self .assertEqual (attrs .length , 2 )
870+ self .assertEqual (len (attrs ), 2 )
871+ dom .unlink ()
872+
873+ dom = parseString ("<doc/>" )
874+ self .assertEqual (dom .documentElement .attributes .length , 0 )
875+ dom .unlink ()
876+
877+ def testAttrList__getitem__ (self ):
878+ dom = parseString ("<doc xmlns:p='http://xml.python.org/ns' "
879+ "p:a='1' b='2'/>" )
880+ attrs = dom .documentElement .attributes
881+ self .assertEqual (attrs ["b" ].value , "2" )
882+ self .assertEqual (attrs [("http://xml.python.org/ns" , "a" )].value , "1" )
883+ self .assertRaises (KeyError , attrs .__getitem__ , "missing" )
884+ self .assertRaises (KeyError , attrs .__getitem__ , (None , "missing" ))
885+ dom .unlink ()
726886
727- def testAttrListLength (self ): pass
887+ def testAttrList__setitem__ (self ):
888+ dom = parseString ("<doc a='1'/>" )
889+ elem = dom .documentElement
890+ attrs = elem .attributes
891+ attrs ["a" ] = "2"
892+ self .assertEqual (elem .getAttribute ("a" ), "2" )
893+ attrs ["b" ] = "3"
894+ self .assertEqual (elem .getAttribute ("b" ), "3" )
895+ self .assertEqual (attrs .length , 2 )
896+
897+ attr = dom .createAttribute ("c" )
898+ attr .value = "4"
899+ attrs ["c" ] = attr
900+ self .assertIs (elem .getAttributeNode ("c" ), attr )
901+ self .assertEqual (elem .getAttribute ("c" ), "4" )
902+ dom .unlink ()
728903
729- def testAttrList__getitem__ (self ): pass
904+ def testSetAttrValueandNodeValue (self ):
905+ dom = parseString ("<doc a='1'/>" )
906+ attr = dom .documentElement .getAttributeNode ("a" )
907+ self .assertEqual (attr .value , "1" )
908+ self .assertEqual (attr .nodeValue , "1" )
909+ attr .value = "2"
910+ self .assertEqual (attr .nodeValue , "2" )
911+ attr .nodeValue = "3"
912+ self .assertEqual (attr .value , "3" )
913+ self .assertEqual (dom .documentElement .getAttribute ("a" ), "3" )
914+ dom .unlink ()
730915
731- def testAttrList__setitem__ (self ): pass
916+ def testParseElement (self ):
917+ dom = parseString ("<doc><child/><child/></doc>" )
918+ elem = dom .documentElement
919+ self .assertEqual (elem .nodeType , Node .ELEMENT_NODE )
920+ self .assertEqual (elem .tagName , "doc" )
921+ self .assertIsNone (elem .namespaceURI )
922+ self .assertIs (elem .parentNode , dom )
923+ self .assertIs (elem .ownerDocument , dom )
924+ self .assertEqual ([child .tagName for child in elem .childNodes ],
925+ ["child" , "child" ])
926+ dom .unlink ()
732927
733- def testSetAttrValueandNodeValue (self ): pass
928+ def testParseAttributes (self ):
929+ dom = parseString ("<doc a='1' b='&'/>" )
930+ elem = dom .documentElement
931+ self .assertEqual (elem .getAttribute ("a" ), "1" )
932+ self .assertEqual (elem .getAttribute ("b" ), "&" )
933+ self .assertEqual (elem .getAttribute ("missing" ), "" )
934+ self .assertTrue (elem .hasAttribute ("a" ))
935+ self .assertFalse (elem .hasAttribute ("missing" ))
936+ attr = elem .getAttributeNode ("a" )
937+ self .assertTrue (attr .specified )
938+ self .assertIsNone (attr .namespaceURI )
939+ dom .unlink ()
734940
735- def testParseElement (self ): pass
941+ def testParseElementNamespaces (self ):
942+ dom = parseString ("<p:doc xmlns:p='http://xml.python.org/ns'"
943+ " xmlns='http://xml.python.org/default'>"
944+ "<child/></p:doc>" )
945+ elem = dom .documentElement
946+ self .assertEqual (elem .tagName , "p:doc" )
947+ self .assertEqual (elem .namespaceURI , "http://xml.python.org/ns" )
948+ self .assertEqual (elem .prefix , "p" )
949+ self .assertEqual (elem .localName , "doc" )
950+ child = elem .getElementsByTagName ("child" )[0 ]
951+ self .assertEqual (child .namespaceURI , "http://xml.python.org/default" )
952+ self .assertIsNone (child .prefix )
953+ self .assertEqual (child .localName , "child" )
954+ dom .unlink ()
736955
737- def testParseAttributes (self ): pass
956+ def testParseAttributeNamespaces (self ):
957+ dom = parseString ("<doc xmlns:p='http://xml.python.org/ns'"
958+ " p:a='1' b='2'/>" )
959+ elem = dom .documentElement
960+ self .assertEqual (elem .getAttributeNS ("http://xml.python.org/ns" , "a" ),
961+ "1" )
962+ self .assertEqual (elem .getAttributeNS (None , "b" ), "2" )
963+ attr = elem .getAttributeNodeNS ("http://xml.python.org/ns" , "a" )
964+ self .assertEqual (attr .name , "p:a" )
965+ self .assertEqual (attr .prefix , "p" )
966+ self .assertEqual (attr .localName , "a" )
967+ declaration = elem .getAttributeNode ("xmlns:p" )
968+ self .assertEqual (declaration .namespaceURI , xml .dom .XMLNS_NAMESPACE )
969+ self .assertEqual (declaration .value , "http://xml.python.org/ns" )
970+ dom .unlink ()
738971
739- def testParseElementNamespaces (self ): pass
972+ def testParseProcessingInstructions (self ):
973+ # the content of a processing instruction is tested
974+ # in testProcessingInstruction
975+ dom = parseString ("<?before data?><doc/><?after?>" )
976+ pi = dom .childNodes [0 ]
977+ self .assertEqual (pi .nodeType , Node .PROCESSING_INSTRUCTION_NODE )
978+ self .assertEqual (pi .target , "before" )
979+ self .assertEqual (pi .data , "data" )
980+ self .assertIs (pi .parentNode , dom )
981+ pi = dom .childNodes [2 ]
982+ self .assertEqual (pi .target , "after" )
983+ self .assertEqual (pi .data , "" )
984+ dom .unlink ()
740985
741- def testParseAttributeNamespaces (self ): pass
986+ def testChildNodes (self ):
987+ dom = parseString ("<doc>text<child/><!--comment--></doc>" )
988+ children = dom .documentElement .childNodes
989+ self .assertEqual (len (children ), 3 )
990+ self .assertEqual ([child .nodeType for child in children ],
991+ [Node .TEXT_NODE , Node .ELEMENT_NODE , Node .COMMENT_NODE ])
992+ for child in children :
993+ self .assertIs (child .parentNode , dom .documentElement )
994+ dom .unlink ()
742995
743- def testParseProcessingInstructions (self ): pass
996+ dom = parseString ("<doc/>" )
997+ self .assertEqual (len (dom .documentElement .childNodes ), 0 )
998+ dom .unlink ()
744999
745- def testChildNodes (self ): pass
1000+ def testFirstChild (self ):
1001+ dom = parseString ("<doc><a/><b/></doc>" )
1002+ elem = dom .documentElement
1003+ self .assertEqual (elem .firstChild .tagName , "a" )
1004+ self .assertEqual (elem .lastChild .tagName , "b" )
1005+ self .assertIs (elem .firstChild , elem .childNodes [0 ])
1006+ self .assertIs (elem .lastChild , elem .childNodes [- 1 ])
1007+ self .assertIsNone (elem .firstChild .previousSibling )
1008+ self .assertIs (elem .firstChild .nextSibling , elem .lastChild )
1009+ dom .unlink ()
7461010
747- def testFirstChild (self ): pass
1011+ dom = parseString ("<doc/>" )
1012+ self .assertIsNone (dom .documentElement .firstChild )
1013+ self .assertIsNone (dom .documentElement .lastChild )
1014+ dom .unlink ()
7481015
7491016 def testHasChildNodes (self ):
7501017 dom = parseString ("<doc><foo/></doc>" )
0 commit comments