home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_MINIDOM.PY < prev    next >
Encoding:
Python Source  |  2000-10-13  |  10.9 KB  |  414 lines

  1. # test for xml.dom.minidom
  2.  
  3. from xml.dom.minidom import parse, Node, Document, parseString
  4. import xml.parsers.expat
  5.  
  6. import os.path
  7. import sys
  8. import traceback
  9. from test_support import verbose
  10.  
  11. if __name__ == "__main__":
  12.     base = sys.argv[0]
  13. else:
  14.     base = __file__
  15. tstfile = os.path.join(os.path.dirname(base), "test.xml")
  16. del base
  17.  
  18. def confirm(test, testname = "Test"):
  19.     if test: 
  20.         print "Passed " + testname
  21.     else: 
  22.         print "Failed " + testname
  23.         raise Exception
  24.  
  25. Node._debug = 1
  26.  
  27. def testParseFromFile():
  28.     from StringIO import StringIO
  29.     dom = parse(StringIO(open(tstfile).read()))
  30.     dom.unlink()
  31.     confirm(isinstance(dom,Document))
  32.  
  33. def testGetElementsByTagName():
  34.     dom = parse(tstfile)
  35.     confirm(dom.getElementsByTagName("LI") == \
  36.             dom.documentElement.getElementsByTagName("LI"))
  37.     dom.unlink()
  38.  
  39. def testInsertBefore():
  40.     dom = parse(tstfile)
  41.     docel = dom.documentElement
  42.     #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
  43.     #                        docel.childNodes[1])
  44.                             
  45.     #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
  46.     #                        docel.childNodes[0])
  47.  
  48.     #confirm( docel.childNodes[0].tet == "a")
  49.     #confirm( docel.childNodes[2].tet == "a")
  50.     dom.unlink()
  51.  
  52. def testAppendChild():
  53.     dom = parse(tstfile)
  54.     dom.documentElement.appendChild(dom.createComment(u"Hello"))
  55.     confirm(dom.documentElement.childNodes[-1].nodeName == "#comment")
  56.     confirm(dom.documentElement.childNodes[-1].data == "Hello")
  57.     dom.unlink()
  58.  
  59. def testNonZero():
  60.     dom = parse(tstfile)
  61.     confirm(dom)# should not be zero
  62.     dom.appendChild(dom.createComment("foo"))
  63.     confirm(not dom.childNodes[-1].childNodes)
  64.     dom.unlink()
  65.  
  66. def testUnlink():
  67.     dom = parse(tstfile)
  68.     dom.unlink()
  69.  
  70. def testElement():
  71.     dom = Document()
  72.     dom.appendChild(dom.createElement("abc"))
  73.     confirm(dom.documentElement)
  74.     dom.unlink()
  75.  
  76. def testAAA():
  77.     dom = parseString("<abc/>")
  78.     el = dom.documentElement
  79.     el.setAttribute("spam", "jam2")
  80.     dom.unlink()
  81.  
  82. def testAAB():
  83.     dom = parseString("<abc/>")
  84.     el = dom.documentElement
  85.     el.setAttribute("spam", "jam")
  86.     el.setAttribute("spam", "jam2")
  87.     dom.unlink()
  88.  
  89. def testAddAttr():
  90.     dom = Document()
  91.     child = dom.appendChild(dom.createElement("abc"))
  92.  
  93.     child.setAttribute("def", "ghi")
  94.     confirm(child.getAttribute("def") == "ghi")
  95.     confirm(child.attributes["def"].value == "ghi")
  96.  
  97.     child.setAttribute("jkl", "mno")
  98.     confirm(child.getAttribute("jkl") == "mno")
  99.     confirm(child.attributes["jkl"].value == "mno")
  100.  
  101.     confirm(len(child.attributes) == 2)
  102.  
  103.     child.setAttribute("def", "newval")
  104.     confirm(child.getAttribute("def") == "newval")
  105.     confirm(child.attributes["def"].value == "newval")
  106.  
  107.     confirm(len(child.attributes) == 2)
  108.     dom.unlink()
  109.  
  110. def testDeleteAttr():
  111.     dom = Document()
  112.     child = dom.appendChild(dom.createElement("abc"))
  113.  
  114.     confirm(len(child.attributes) == 0)
  115.     child.setAttribute("def", "ghi")
  116.     confirm(len(child.attributes) == 1)
  117.     del child.attributes["def"]
  118.     confirm(len(child.attributes) == 0)
  119.     dom.unlink()
  120.  
  121. def testRemoveAttr():
  122.     dom = Document()
  123.     child = dom.appendChild(dom.createElement("abc"))
  124.  
  125.     child.setAttribute("def", "ghi")
  126.     confirm(len(child.attributes) == 1)
  127.     child.removeAttribute("def")
  128.     confirm(len(child.attributes) == 0)
  129.  
  130.     dom.unlink()
  131.  
  132. def testRemoveAttrNS():
  133.     dom = Document()
  134.     child = dom.appendChild(
  135.             dom.createElementNS("http://www.python.org", "python:abc"))
  136.     child.setAttributeNS("http://www.w3.org", "xmlns:python", 
  137.                                             "http://www.python.org")
  138.     child.setAttributeNS("http://www.python.org", "python:abcattr", "foo")
  139.     confirm(len(child.attributes) == 2)
  140.     child.removeAttributeNS("http://www.python.org", "abcattr")
  141.     confirm(len(child.attributes) == 1)
  142.  
  143.     dom.unlink()
  144.     
  145. def testRemoveAttributeNode():
  146.     dom = Document()
  147.     child = dom.appendChild(dom.createElement("foo"))
  148.     child.setAttribute("spam", "jam")
  149.     confirm(len(child.attributes) == 1)
  150.     node = child.getAttributeNode("spam")
  151.     child.removeAttributeNode(node)
  152.     confirm(len(child.attributes) == 0)
  153.  
  154.     dom.unlink()
  155.  
  156. def testChangeAttr():
  157.     dom = parseString("<abc/>")
  158.     el = dom.documentElement
  159.     el.setAttribute("spam", "jam")
  160.     confirm(len(el.attributes) == 1)
  161.     el.setAttribute("spam", "bam")
  162.     confirm(len(el.attributes) == 1)
  163.     el.attributes["spam"] = "ham"
  164.     confirm(len(el.attributes) == 1)
  165.     el.setAttribute("spam2", "bam")
  166.     confirm(len(el.attributes) == 2)
  167.     el.attributes[ "spam2"] = "bam2"
  168.     confirm(len(el.attributes) == 2)
  169.     dom.unlink()
  170.  
  171. def testGetAttrList():
  172.     pass
  173.  
  174. def testGetAttrValues(): pass
  175.  
  176. def testGetAttrLength(): pass
  177.  
  178. def testGetAttribute(): pass
  179.  
  180. def testGetAttributeNS(): pass
  181.  
  182. def testGetAttributeNode(): pass
  183.  
  184. def testGetElementsByTagNameNS(): pass
  185.  
  186. def testGetEmptyNodeListFromElementsByTagNameNS(): pass
  187.  
  188. def testElementReprAndStr():
  189.     dom = Document()
  190.     el = dom.appendChild(dom.createElement("abc"))
  191.     string1 = repr(el)
  192.     string2 = str(el)
  193.     confirm(string1 == string2)
  194.     dom.unlink()
  195.  
  196. # commented out until Fredrick's fix is checked in
  197. def _testElementReprAndStrUnicode():
  198.     dom = Document()
  199.     el = dom.appendChild(dom.createElement(u"abc"))
  200.     string1 = repr(el)
  201.     string2 = str(el)
  202.     confirm(string1 == string2)
  203.     dom.unlink()
  204.  
  205. # commented out until Fredrick's fix is checked in
  206. def _testElementReprAndStrUnicodeNS():
  207.     dom = Document()
  208.     el = dom.appendChild(
  209.         dom.createElementNS(u"http://www.slashdot.org", u"slash:abc"))
  210.     string1 = repr(el)
  211.     string2 = str(el)
  212.     confirm(string1 == string2)
  213.     confirm(string1.find("slash:abc") != -1)
  214.     dom.unlink()
  215.     confirm(len(Node.allnodes) == 0)
  216.  
  217. def testAttributeRepr():
  218.     dom = Document()
  219.     el = dom.appendChild(dom.createElement(u"abc"))
  220.     node = el.setAttribute("abc", "def")
  221.     confirm(str(node) == repr(node))
  222.     dom.unlink()
  223.     confirm(len(Node.allnodes) == 0)
  224.  
  225. def testTextNodeRepr(): pass
  226.  
  227. def testWriteXML():
  228.     str = '<a b="c"/>'
  229.     dom = parseString(str)
  230.     domstr = dom.toxml()
  231.     dom.unlink()
  232.     confirm(str == domstr)
  233.     confirm(len(Node.allnodes) == 0)
  234.  
  235. def testProcessingInstruction(): pass
  236.  
  237. def testProcessingInstructionRepr(): pass
  238.  
  239. def testTextRepr(): pass
  240.  
  241. def testWriteText(): pass
  242.  
  243. def testDocumentElement(): pass
  244.  
  245. def testTooManyDocumentElements(): pass
  246.  
  247. def testCreateElementNS(): pass
  248.  
  249. def testCreatAttributeNS(): pass
  250.  
  251. def testParse(): pass
  252.  
  253. def testParseString(): pass
  254.  
  255. def testComment(): pass
  256.  
  257. def testAttrListItem(): pass
  258.  
  259. def testAttrListItems(): pass
  260.  
  261. def testAttrListItemNS(): pass
  262.  
  263. def testAttrListKeys(): pass
  264.  
  265. def testAttrListKeysNS(): pass
  266.  
  267. def testAttrListValues(): pass
  268.  
  269. def testAttrListLength(): pass
  270.  
  271. def testAttrList__getitem__(): pass
  272.  
  273. def testAttrList__setitem__(): pass
  274.  
  275. def testSetAttrValueandNodeValue(): pass
  276.  
  277. def testParseElement(): pass
  278.  
  279. def testParseAttributes(): pass
  280.  
  281. def testParseElementNamespaces(): pass
  282.  
  283. def testParseAttributeNamespaces(): pass
  284.  
  285. def testParseProcessingInstructions(): pass
  286.  
  287. def testChildNodes(): pass
  288.  
  289. def testFirstChild(): pass
  290.  
  291. def testHasChildNodes(): pass
  292.  
  293. def testCloneElementShallow(): pass
  294.  
  295. def testCloneElementShallowCopiesAttributes(): pass
  296.  
  297. def testCloneElementDeep(): pass
  298.  
  299. def testCloneDocumentShallow(): pass
  300.  
  301. def testCloneDocumentDeep(): pass
  302.  
  303. def testCloneAttributeShallow(): pass
  304.  
  305. def testCloneAttributeDeep(): pass
  306.  
  307. def testClonePIShallow(): pass
  308.  
  309. def testClonePIDeep(): pass
  310.  
  311. def testSiblings():
  312.     doc = parseString("<doc><?pi?>text?<elm/></doc>")
  313.     root = doc.documentElement
  314.     (pi, text, elm) = root.childNodes
  315.  
  316.     confirm(pi.nextSibling is text and 
  317.             pi.previousSibling is None and 
  318.             text.nextSibling is elm and 
  319.             text.previousSibling is pi and 
  320.             elm.nextSibling is None and 
  321.             elm.previousSibling is text, "testSiblings")
  322.  
  323.     doc.unlink()
  324.  
  325. def testParents():
  326.     doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")
  327.     root = doc.documentElement
  328.     elm1 = root.childNodes[0]
  329.     (elm2a, elm2b) = elm1.childNodes
  330.     elm3 = elm2b.childNodes[0]
  331.  
  332.     confirm(root.parentNode is doc and
  333.             elm1.parentNode is root and
  334.             elm2a.parentNode is elm1 and
  335.             elm2b.parentNode is elm1 and
  336.             elm3.parentNode is elm2b, "testParents")
  337.  
  338.     doc.unlink()
  339.  
  340. def testSAX2DOM():
  341.     from xml.dom import pulldom
  342.  
  343.     sax2dom = pulldom.SAX2DOM()
  344.     sax2dom.startDocument()
  345.     sax2dom.startElement("doc", {})
  346.     sax2dom.characters("text")
  347.     sax2dom.startElement("subelm", {})
  348.     sax2dom.characters("text")
  349.     sax2dom.endElement("subelm")
  350.     sax2dom.characters("text")    
  351.     sax2dom.endElement("doc")
  352.     sax2dom.endDocument()
  353.  
  354.     doc = sax2dom.document
  355.     root = doc.documentElement
  356.     (text1, elm1, text2) = root.childNodes
  357.     text3 = elm1.childNodes[0]
  358.  
  359.     confirm(text1.previousSibling is None and
  360.             text1.nextSibling is elm1 and
  361.             elm1.previousSibling is text1 and
  362.             elm1.nextSibling is text2 and
  363.             text2.previousSibling is elm1 and
  364.             text2.nextSibling is None and
  365.             text3.previousSibling is None and
  366.             text3.nextSibling is None, "testSAX2DOM - siblings")
  367.  
  368.     confirm(root.parentNode is doc and
  369.             text1.parentNode is root and
  370.             elm1.parentNode is root and
  371.             text2.parentNode is root and
  372.             text3.parentNode is elm1, "testSAX2DOM - parents")
  373.             
  374.     doc.unlink()
  375.  
  376. # --- MAIN PROGRAM
  377.     
  378. names = globals().keys()
  379. names.sort()
  380.  
  381. works = 1
  382.  
  383. for name in names:
  384.     if name.startswith("test"):
  385.         func = globals()[name]
  386.         try:
  387.             func()
  388.             print "Test Succeeded", name
  389.             confirm(len(Node.allnodes) == 0,
  390.                     "assertion: len(Node.allnodes) == 0")
  391.             if len(Node.allnodes):
  392.                 print "Garbage left over:"
  393.                 if verbose:
  394.                     print Node.allnodes.items()[0:10]
  395.                 else:
  396.                     # Don't print specific nodes if repeatable results
  397.                     # are needed
  398.                     print len(Node.allnodes)
  399.             Node.allnodes = {}
  400.         except Exception, e:
  401.             works = 0
  402.             print "Test Failed: ", name
  403.             traceback.print_exception(*sys.exc_info())
  404.             print `e`
  405.             Node.allnodes = {}
  406.  
  407. if works:
  408.     print "All tests succeeded"
  409. else:
  410.     print "\n\n\n\n************ Check for failures!"
  411.  
  412. Node.debug = None # Delete debug output collected in a StringIO object
  413. Node._debug = 0   # And reset debug mode
  414.