@@ -480,7 +480,15 @@ def testGetAttributeNS(self):
480480 self .assertEqual (child2 .getAttributeNS ("http://www.python.org" , "missing" ),
481481 '' )
482482
483- def testGetAttributeNode (self ): pass
483+ def testGetAttributeNode (self ):
484+ dom = parseString ("<doc a='1'/>" )
485+ elem = dom .documentElement
486+ attr = elem .getAttributeNode ("a" )
487+ self .assertEqual (attr .name , "a" )
488+ self .assertEqual (attr .value , "1" )
489+ self .assertIs (attr .ownerElement , elem )
490+ self .assertIsNone (elem .getAttributeNode ("b" ))
491+ dom .unlink ()
484492
485493 def testGetElementsByTagNameNS (self ):
486494 d = """<foo xmlns:minidom='http://pyxml.sf.net/minidom'>
@@ -669,9 +677,34 @@ def testTextRepr(self):
669677 self .assertEqual (str (el ), repr (el ))
670678 self .assertEqual ('<DOM Text node "\' foo\' ">' , str (el ))
671679
672- def testWriteText (self ): pass
680+ def testWriteText (self ):
681+ dom = parseString ("<doc><a>text</a><b><&></b></doc>" )
682+ elem = dom .documentElement
683+ writer = io .StringIO ()
684+ elem .writexml (writer )
685+ self .assertEqual (writer .getvalue (),
686+ "<doc><a>text</a><b><&></b></doc>" )
687+ writer = io .StringIO ()
688+ elem .writexml (writer , indent = " " , addindent = " " , newl = "\n " )
689+ self .assertEqual (writer .getvalue (),
690+ " <doc>\n "
691+ " <a>text</a>\n "
692+ " <b><&></b>\n "
693+ " </doc>\n " )
694+ dom .unlink ()
673695
674- def testDocumentElement (self ): pass
696+ def testDocumentElement (self ):
697+ dom = parseString ("<!-- comment --><doc/><?pi data?>" )
698+ elem = dom .documentElement
699+ self .assertEqual (elem .tagName , "doc" )
700+ self .assertIs (elem , dom .childNodes [1 ])
701+ dom .unlink ()
702+
703+ dom = Document ()
704+ self .assertIsNone (dom .documentElement )
705+ elem = dom .appendChild (dom .createElement ("doc" ))
706+ self .assertIs (dom .documentElement , elem )
707+ dom .unlink ()
675708
676709 def testTooManyDocumentElements (self ):
677710 doc = parseString ("<doc/>" )
@@ -681,25 +714,126 @@ def testTooManyDocumentElements(self):
681714 elem .unlink ()
682715 doc .unlink ()
683716
684- def testCreateElementNS (self ): pass
717+ def testCreateElementNS (self ):
718+ dom = Document ()
719+ elem = dom .createElementNS ("http://xml.python.org/ns" , "p:elem" )
720+ self .assertEqual (elem .nodeType , Node .ELEMENT_NODE )
721+ self .assertEqual (elem .tagName , "p:elem" )
722+ self .assertEqual (elem .nodeName , "p:elem" )
723+ self .assertEqual (elem .namespaceURI , "http://xml.python.org/ns" )
724+ self .assertEqual (elem .prefix , "p" )
725+ self .assertEqual (elem .localName , "elem" )
726+ self .assertIs (elem .ownerDocument , dom )
727+ self .assertIsNone (elem .parentNode )
728+
729+ elem = dom .createElementNS ("http://xml.python.org/ns" , "elem" )
730+ self .assertEqual (elem .tagName , "elem" )
731+ self .assertIsNone (elem .prefix )
732+ self .assertEqual (elem .localName , "elem" )
733+ dom .unlink ()
685734
686- def testCreateAttributeNS (self ): pass
735+ def testCreateAttributeNS (self ):
736+ dom = Document ()
737+ attr = dom .createAttributeNS ("http://xml.python.org/ns" , "p:attr" )
738+ self .assertEqual (attr .nodeType , Node .ATTRIBUTE_NODE )
739+ self .assertEqual (attr .name , "p:attr" )
740+ self .assertEqual (attr .nodeName , "p:attr" )
741+ self .assertEqual (attr .namespaceURI , "http://xml.python.org/ns" )
742+ self .assertEqual (attr .prefix , "p" )
743+ self .assertEqual (attr .localName , "attr" )
744+ self .assertEqual (attr .value , "" )
745+ self .assertIs (attr .ownerDocument , dom )
746+ self .assertIsNone (attr .ownerElement )
747+
748+ elem = dom .appendChild (dom .createElement ("doc" ))
749+ elem .setAttributeNode (attr )
750+ self .assertIs (attr .ownerElement , elem )
751+ self .assertIs (elem .getAttributeNodeNS ("http://xml.python.org/ns" ,
752+ "attr" ), attr )
753+ dom .unlink ()
687754
688- def testParse (self ): pass
755+ def testParse (self ):
756+ # parsing from a file object is tested in testParseFromBinaryFile
757+ # and testParseFromTextFile
758+ dom = parse (tstfile )
759+ self .assertEqual (dom .nodeType , Node .DOCUMENT_NODE )
760+ self .assertEqual (dom .documentElement .tagName , "HTML" )
761+ dom .unlink ()
689762
690- def testParseString ( self ): pass
763+ self . assertRaises ( ExpatError , parseString , "<doc>" )
691764
692- def testComment (self ): pass
765+ def testParseString (self ):
766+ dom = parseString ("<doc>text</doc>" )
767+ self .assertEqual (dom .nodeType , Node .DOCUMENT_NODE )
768+ self .assertEqual (dom .documentElement .tagName , "doc" )
769+ self .assertEqual (dom .documentElement .firstChild .data , "text" )
770+ dom .unlink ()
771+
772+ dom = parseString (b"<?xml version='1.0' encoding='utf-8'?>"
773+ b"<doc>\xc3 \xa9 </doc>" )
774+ self .assertEqual (dom .documentElement .firstChild .data , "\xe9 " )
775+ dom .unlink ()
776+
777+ def testComment (self ):
778+ dom = Document ()
779+ comment = dom .createComment ("comment" )
780+ self .assertEqual (comment .nodeType , Node .COMMENT_NODE )
781+ self .assertEqual (comment .nodeName , "#comment" )
782+ self .assertEqual (comment .data , "comment" )
783+ self .assertEqual (comment .nodeValue , "comment" )
784+ self .assertIsNone (comment .attributes )
785+ dom .appendChild (comment )
786+ self .assertEqual (dom .toxml (),
787+ '<?xml version="1.0" ?><!--comment-->' )
788+ dom .unlink ()
693789
694- def testAttrListItem (self ): pass
790+ dom = parseString ("<doc><!--comment--></doc>" )
791+ comment = dom .documentElement .firstChild
792+ self .assertEqual (comment .nodeType , Node .COMMENT_NODE )
793+ self .assertEqual (comment .data , "comment" )
794+ dom .unlink ()
695795
696- def testAttrListItems (self ): pass
796+ def testAttrListItem (self ):
797+ dom = parseString ("<doc a='1' b='2'/>" )
798+ attrs = dom .documentElement .attributes
799+ self .assertEqual (attrs .item (0 ).name , "a" )
800+ self .assertEqual (attrs .item (1 ).name , "b" )
801+ self .assertIsNone (attrs .item (2 ))
802+ dom .unlink ()
697803
698- def testAttrListItemNS (self ): pass
804+ def testAttrListItems (self ):
805+ dom = parseString ("<doc a='1' b='2'/>" )
806+ attrs = dom .documentElement .attributes
807+ self .assertEqual (attrs .items (), [("a" , "1" ), ("b" , "2" )])
808+ dom .unlink ()
699809
700- def testAttrListKeys (self ): pass
810+ def testAttrListItemNS (self ):
811+ dom = parseString ("<doc xmlns:p='http://xml.python.org/ns' "
812+ "p:a='1' b='2'/>" )
813+ attrs = dom .documentElement .attributes
814+ self .assertEqual (attrs .itemsNS (), [
815+ ((xml .dom .XMLNS_NAMESPACE , "p" ), "http://xml.python.org/ns" ),
816+ (("http://xml.python.org/ns" , "a" ), "1" ),
817+ ((None , "b" ), "2" ),
818+ ])
819+ dom .unlink ()
701820
702- def testAttrListKeysNS (self ): pass
821+ def testAttrListKeys (self ):
822+ dom = parseString ("<doc a='1' b='2'/>" )
823+ attrs = dom .documentElement .attributes
824+ self .assertEqual (list (attrs .keys ()), ["a" , "b" ])
825+ dom .unlink ()
826+
827+ def testAttrListKeysNS (self ):
828+ dom = parseString ("<doc xmlns:p='http://xml.python.org/ns' "
829+ "p:a='1' b='2'/>" )
830+ attrs = dom .documentElement .attributes
831+ self .assertEqual (list (attrs .keysNS ()), [
832+ (xml .dom .XMLNS_NAMESPACE , "p" ),
833+ ("http://xml.python.org/ns" , "a" ),
834+ (None , "b" ),
835+ ])
836+ dom .unlink ()
703837
704838 def testRemoveNamedItem (self ):
705839 doc = parseString ("<doc a=''/>" )
@@ -720,29 +854,162 @@ def testRemoveNamedItemNS(self):
720854 self .assertRaises (xml .dom .NotFoundErr , attrs .removeNamedItemNS ,
721855 "http://xml.python.org/" , "b" )
722856
723- def testAttrListValues (self ): pass
857+ def testAttrListValues (self ):
858+ dom = parseString ("<doc a='1' b='2'/>" )
859+ attrs = dom .documentElement .attributes
860+ self .assertEqual ([attr .name for attr in attrs .values ()], ["a" , "b" ])
861+ self .assertEqual ([attr .value for attr in attrs .values ()], ["1" , "2" ])
862+ dom .unlink ()
863+
864+ def testAttrListLength (self ):
865+ dom = parseString ("<doc a='1' b='2'/>" )
866+ attrs = dom .documentElement .attributes
867+ self .assertEqual (attrs .length , 2 )
868+ self .assertEqual (len (attrs ), 2 )
869+ dom .unlink ()
870+
871+ dom = parseString ("<doc/>" )
872+ self .assertEqual (dom .documentElement .attributes .length , 0 )
873+ dom .unlink ()
874+
875+ def testAttrList__getitem__ (self ):
876+ dom = parseString ("<doc xmlns:p='http://xml.python.org/ns' "
877+ "p:a='1' b='2'/>" )
878+ attrs = dom .documentElement .attributes
879+ self .assertEqual (attrs ["b" ].value , "2" )
880+ self .assertEqual (attrs [("http://xml.python.org/ns" , "a" )].value , "1" )
881+ self .assertRaises (KeyError , attrs .__getitem__ , "missing" )
882+ self .assertRaises (KeyError , attrs .__getitem__ , (None , "missing" ))
883+ dom .unlink ()
724884
725- def testAttrListLength (self ): pass
885+ def testAttrList__setitem__ (self ):
886+ dom = parseString ("<doc a='1'/>" )
887+ elem = dom .documentElement
888+ attrs = elem .attributes
889+ attrs ["a" ] = "2"
890+ self .assertEqual (elem .getAttribute ("a" ), "2" )
891+ attrs ["b" ] = "3"
892+ self .assertEqual (elem .getAttribute ("b" ), "3" )
893+ self .assertEqual (attrs .length , 2 )
894+
895+ attr = dom .createAttribute ("c" )
896+ attr .value = "4"
897+ attrs ["c" ] = attr
898+ self .assertIs (elem .getAttributeNode ("c" ), attr )
899+ self .assertEqual (elem .getAttribute ("c" ), "4" )
900+ dom .unlink ()
726901
727- def testAttrList__getitem__ (self ): pass
902+ def testSetAttrValueandNodeValue (self ):
903+ dom = parseString ("<doc a='1'/>" )
904+ attr = dom .documentElement .getAttributeNode ("a" )
905+ self .assertEqual (attr .value , "1" )
906+ self .assertEqual (attr .nodeValue , "1" )
907+ attr .value = "2"
908+ self .assertEqual (attr .nodeValue , "2" )
909+ attr .nodeValue = "3"
910+ self .assertEqual (attr .value , "3" )
911+ self .assertEqual (dom .documentElement .getAttribute ("a" ), "3" )
912+ dom .unlink ()
728913
729- def testAttrList__setitem__ (self ): pass
914+ def testParseElement (self ):
915+ dom = parseString ("<doc><child/><child/></doc>" )
916+ elem = dom .documentElement
917+ self .assertEqual (elem .nodeType , Node .ELEMENT_NODE )
918+ self .assertEqual (elem .tagName , "doc" )
919+ self .assertIsNone (elem .namespaceURI )
920+ self .assertIs (elem .parentNode , dom )
921+ self .assertIs (elem .ownerDocument , dom )
922+ self .assertEqual ([child .tagName for child in elem .childNodes ],
923+ ["child" , "child" ])
924+ dom .unlink ()
730925
731- def testSetAttrValueandNodeValue (self ): pass
926+ def testParseAttributes (self ):
927+ dom = parseString ("<doc a='1' b='&'/>" )
928+ elem = dom .documentElement
929+ self .assertEqual (elem .getAttribute ("a" ), "1" )
930+ self .assertEqual (elem .getAttribute ("b" ), "&" )
931+ self .assertEqual (elem .getAttribute ("missing" ), "" )
932+ self .assertTrue (elem .hasAttribute ("a" ))
933+ self .assertFalse (elem .hasAttribute ("missing" ))
934+ attr = elem .getAttributeNode ("a" )
935+ self .assertTrue (attr .specified )
936+ self .assertIsNone (attr .namespaceURI )
937+ dom .unlink ()
732938
733- def testParseElement (self ): pass
939+ def testParseElementNamespaces (self ):
940+ dom = parseString ("<p:doc xmlns:p='http://xml.python.org/ns'"
941+ " xmlns='http://xml.python.org/default'>"
942+ "<child/></p:doc>" )
943+ elem = dom .documentElement
944+ self .assertEqual (elem .tagName , "p:doc" )
945+ self .assertEqual (elem .namespaceURI , "http://xml.python.org/ns" )
946+ self .assertEqual (elem .prefix , "p" )
947+ self .assertEqual (elem .localName , "doc" )
948+ child = elem .getElementsByTagName ("child" )[0 ]
949+ self .assertEqual (child .namespaceURI , "http://xml.python.org/default" )
950+ self .assertIsNone (child .prefix )
951+ self .assertEqual (child .localName , "child" )
952+ dom .unlink ()
734953
735- def testParseAttributes (self ): pass
954+ def testParseAttributeNamespaces (self ):
955+ dom = parseString ("<doc xmlns:p='http://xml.python.org/ns'"
956+ " p:a='1' b='2'/>" )
957+ elem = dom .documentElement
958+ self .assertEqual (elem .getAttributeNS ("http://xml.python.org/ns" , "a" ),
959+ "1" )
960+ self .assertEqual (elem .getAttributeNS (None , "b" ), "2" )
961+ attr = elem .getAttributeNodeNS ("http://xml.python.org/ns" , "a" )
962+ self .assertEqual (attr .name , "p:a" )
963+ self .assertEqual (attr .prefix , "p" )
964+ self .assertEqual (attr .localName , "a" )
965+ declaration = elem .getAttributeNode ("xmlns:p" )
966+ self .assertEqual (declaration .namespaceURI , xml .dom .XMLNS_NAMESPACE )
967+ self .assertEqual (declaration .value , "http://xml.python.org/ns" )
968+ dom .unlink ()
736969
737- def testParseElementNamespaces (self ): pass
970+ def testParseProcessingInstructions (self ):
971+ # the content of a processing instruction is tested
972+ # in testProcessingInstruction
973+ dom = parseString ("<?before data?><doc/><?after?>" )
974+ pi = dom .childNodes [0 ]
975+ self .assertEqual (pi .nodeType , Node .PROCESSING_INSTRUCTION_NODE )
976+ self .assertEqual (pi .target , "before" )
977+ self .assertEqual (pi .data , "data" )
978+ self .assertIs (pi .parentNode , dom )
979+ pi = dom .childNodes [2 ]
980+ self .assertEqual (pi .target , "after" )
981+ self .assertEqual (pi .data , "" )
982+ dom .unlink ()
738983
739- def testParseAttributeNamespaces (self ): pass
984+ def testChildNodes (self ):
985+ dom = parseString ("<doc>text<child/><!--comment--></doc>" )
986+ children = dom .documentElement .childNodes
987+ self .assertEqual (len (children ), 3 )
988+ self .assertEqual ([child .nodeType for child in children ],
989+ [Node .TEXT_NODE , Node .ELEMENT_NODE , Node .COMMENT_NODE ])
990+ for child in children :
991+ self .assertIs (child .parentNode , dom .documentElement )
992+ dom .unlink ()
740993
741- def testParseProcessingInstructions (self ): pass
994+ dom = parseString ("<doc/>" )
995+ self .assertEqual (len (dom .documentElement .childNodes ), 0 )
996+ dom .unlink ()
742997
743- def testChildNodes (self ): pass
998+ def testFirstChild (self ):
999+ dom = parseString ("<doc><a/><b/></doc>" )
1000+ elem = dom .documentElement
1001+ self .assertEqual (elem .firstChild .tagName , "a" )
1002+ self .assertEqual (elem .lastChild .tagName , "b" )
1003+ self .assertIs (elem .firstChild , elem .childNodes [0 ])
1004+ self .assertIs (elem .lastChild , elem .childNodes [- 1 ])
1005+ self .assertIsNone (elem .firstChild .previousSibling )
1006+ self .assertIs (elem .firstChild .nextSibling , elem .lastChild )
1007+ dom .unlink ()
7441008
745- def testFirstChild (self ): pass
1009+ dom = parseString ("<doc/>" )
1010+ self .assertIsNone (dom .documentElement .firstChild )
1011+ self .assertIsNone (dom .documentElement .lastChild )
1012+ dom .unlink ()
7461013
7471014 def testHasChildNodes (self ):
7481015 dom = parseString ("<doc><foo/></doc>" )
0 commit comments