""" $Id: TreeBuilderFunc.py,v 1.2 2000/10/17 02:40:38 kmacleod Exp $ """ import unittest import string import re import Orchard.XML xml_string = \ """ text5 """ xmlns_ns = "http://www.w3.org/2000/xmlns/" orchard = Orchard.XML.load(xml_string) class TreeBuilderFuncTestCase(unittest.TestCase): def checkTree(self): el1 = orchard.root assert ( el1.node_type is Orchard.XML.ElementType and el1.name == 'el1' and el1.namespace_uri == None and not hasattr(el1, 'prefix') and el1.local_name == 'el1' and len(el1.attributes) == 1 and el1.attributes.attr1.name == 'attr1' and el1.attributes.attr1.value == 'text1' and not hasattr(el1.attributes.attr1, 'prefix') and el1.attributes.attr1.namespace_uri == None ) for el2 in el1.contents: if el2.node_type is Orchard.XML.ElementType: break assert ( el2.node_type is Orchard.XML.ElementType and el2.name == 'el2' and el2.namespace_uri == 'uri:ns1' and not hasattr(el2, 'prefix') and el2.local_name == 'el2' and len(el2.attributes) == 1 and el2.attributes.xmlns.name == 'xmlns' and el2.attributes.xmlns.value == 'uri:ns1' and not hasattr(el2.attributes.xmlns, 'Prefix') and el2.attributes.xmlns.namespace_uri == None ) for el3 in el2.contents: if el3.node_type is Orchard.XML.ElementType: break assert ( el3.node_type is Orchard.XML.ElementType and el3.name == 'el3' and el3.namespace_uri == 'uri:ns1' and not hasattr(el3, 'prefix') and el3.local_name == 'el3' and len(el3.attributes) == 1 and el3.attributes[('uri:ns1', 'attr2')].name == 'attr2' and el3.attributes[('uri:ns1', 'attr2')].value == 'text2' and not hasattr(el3.attributes[('uri:ns1', 'attr2')], 'prefix') and el3.attributes[('uri:ns1', 'attr2')].namespace_uri == 'uri:ns1' ) for el4 in el1.contents: if ( el4.node_type is Orchard.XML.ElementType and el4.local_name == 'el4' ): break assert ( el4.node_type is Orchard.XML.ElementType and el4.name == 'el4' and el4.namespace_uri == None and not hasattr(el4, 'prefix') and el4.local_name == 'el4' and len(el4.attributes) == 1 and el4.attributes[(xmlns_ns, 'ns2')].name == 'xmlns:ns2' and el4.attributes[(xmlns_ns, 'ns2')].value == 'uri:ns2' and el4.attributes[(xmlns_ns, 'ns2')].namespace_uri == xmlns_ns and el4.attributes[(xmlns_ns, 'ns2')].prefix == 'xmlns' ) for el5 in el4.contents: if el5.node_type is Orchard.XML.ElementType: break assert ( el5.node_type is Orchard.XML.ElementType and el5.name == 'ns2:el5' and el5.namespace_uri == 'uri:ns2' and el5.prefix == 'ns2' and el5.local_name == 'el5' and len(el5.attributes) == 2 and el5.attributes.attr3.name == 'attr3' and el5.attributes.attr3.value == 'text3' and el5.attributes.attr3.namespace_uri == None and not hasattr(el5.attributes.attr3, 'prefix') and el5.attributes[('uri:ns2', 'attr4')].name == 'ns2:attr4' and el5.attributes[('uri:ns2', 'attr4')].value == 'text4' and el5.attributes[('uri:ns2', 'attr4')].namespace_uri == 'uri:ns2' and el5.attributes[('uri:ns2', 'attr4')].prefix == 'ns2' ) for el6 in el1.contents: if ( el6.node_type is Orchard.XML.ElementType and el6.local_name == 'el6' ): break assert ( el6.node_type is Orchard.XML.ElementType and el6.name == 'el6' and el6.namespace_uri == None and not hasattr(el6, 'prefix') and el6.local_name == 'el6' and len(el6.attributes) == 0 ) chars1 = el1.contents[0] assert ( chars1.node_type is Orchard.XML.CharactersType and re.match(r'^\s*$', chars1.data) != None ) chars2 = el6.contents[0] assert ( chars2.node_type is Orchard.XML.CharactersType and chars2.data == 'text5' ) for pi1 in orchard.contents: if pi1.node_type is Orchard.XML.ProcessingInstructionType: break assert ( pi1.node_type is Orchard.XML.ProcessingInstructionType and pi1.target == 'pi1' and pi1.data == 'data1' ) for pi2 in el1.contents: if pi2.node_type is Orchard.XML.ProcessingInstructionType: break assert ( pi2.node_type is Orchard.XML.ProcessingInstructionType and pi2.target == 'pi2' and pi2.data == 'data2' ) for pi3 in orchard.contents: if ( pi3.node_type is Orchard.XML.ProcessingInstructionType and pi3.target == 'pi3' ): break assert ( pi3.node_type is Orchard.XML.ProcessingInstructionType and pi3.target == 'pi3' and pi3.data == 'data3' ) def suite(): return unittest.makeSuite(TreeBuilderFuncTestCase,'check') if __name__ == "__main__": unittest.TextTestRunner().run(suite())