root/trunk/test/w3c/testXMLNode.py

Revision 72, 4.4 kB (checked in by sholloway, 6 years ago)

--

Line 
1 #!/usr/bin/env python ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 ##~ Copyright (C) 2002-2004  TechGame Networks, LLC.
3 ##~
4 ##~ This library is free software; you can redistribute it and/or
5 ##~ modify it under the terms of the BSD style License as found in the
6 ##~ LICENSE file included with this distribution.
7 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8
9 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 #~ Imports
11 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12
13 import unittest
14
15 from TG.common import path
16 from TG.w3c import xmlNode
17
18 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 #~ Definitions
20 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 class TestCommonPython(unittest.TestCase):
23
24     def testNoNamespace(self):
25         testxml = """<?xml version='1.0'?>
26             <nodeA attr1='A.One' attr2="A.Two">
27                 <nodeB/>
28                 <nodeC attr1='C.One' attr2='C.Two'>
29                     <nodeD>
30                         Node D's text
31                     </nodeD>
32                 </nodeC>
33             </nodeA>
34         """
35         node = xmlNode.Producer().parse(testxml)
36         self.assertEqual(node.node, 'nodeA')
37         self.assertEqual(node.namespace, None)
38         self.assertEqual(node.attrs, {'attr2': 'A.Two', 'attr1': 'A.One'})
39         self.assertEqual(len(node), 5)
40         self.assertEqual(len(node.listNodes()), 2)
41         self.assertEqual([subnode.node for subnode in node.iterNodes()], ['nodeB', 'nodeC'])
42         self.assertEqual([subnode.node for subnode in node['nodeC',][0].iterNodes()], ['nodeD'])
43    
44     def testNamespace(self):
45         testxml = """<?xml version='1.0'?>
46             <nodeA xmlns='A Test Namespace' attr1='A.One' attr2="A.Two">
47                 <nodeB/>
48                 <nodeC attr1='C.One' attr2='C.Two'>
49                     <nodeD>
50                         Node D's text
51                     </nodeD>
52                 </nodeC>
53             </nodeA>
54         """
55         node = xmlNode.Producer().parse(testxml)
56         self.assertEqual(node.node, 'nodeA')
57         self.assertEqual(node.namespace, 'A Test Namespace')
58         self.assertEqual(node.attrs, {'attr2': 'A.Two', 'attr1': 'A.One'})
59         self.assertEqual(len(node), 5)
60         self.assertEqual(len(node.listNodes()), 2)
61         self.assertEqual([subnode.node for subnode in node.iterNodes()], ['nodeB', 'nodeC'])
62         self.assertEqual([subnode.node for subnode in node['nodeC',][0].iterNodes()], ['nodeD'])
63
64     def testBuilding(self):
65         root = xmlNode.XMLNode('root', 'MyRootNamespace')
66         root += 'Some cdata'
67         root += ('subelem',)
68         root += 'More data'
69         root += ('element',)
70         root += 'anotherelem', 'SubNamespace'
71         anotherelem = root[-1]
72         anotherelem.attrs['myattr'] = 'a value'
73         anotherelem += 'some text'
74
75         result = \
76 '''<root xmlns="MyRootNamespace">
77     Some cdata
78     <subelem/>
79     More data
80     <element/>
81     <anotherelem xmlns="SubNamespace" myattr="a value">
82         some text
83     </anotherelem>
84 </root>'''
85         self.assertEqual(result, root.toXML(True))
86
87         result = '''<root xmlns="MyRootNamespace">Some cdata<subelem/>More data<element/><anotherelem xmlns="SubNamespace" myattr="a value">some text</anotherelem></root>'''
88         self.assertEqual(result, root.toXML())
89         self.assertEqual(root.toXML(False), root.toXML())
90        
91
92     def testJabberXMLFile(self):
93         rootPath = path.path(__file__).parent.joinpath('media/JabberTest.xml')
94         root = xmlNode.Producer().parseFile(rootPath.open())
95
96         for data in root.iterData():
97             self.assert_(data.strip() in ['SENT:', 'RECV:', ''])
98
99         for node in root.iterNodes():
100             self.assert_(node.node in ['iq', 'presence', 'message'], '%r\'s name was not iq, message, or presenece'%node)
101             self.assertEqual(node.namespace, 'jabber:client', '%r\'s namespace was not \'jabber:client\''%node)
102        
103         self.assertEqual(len(root.listNodes('iq')), 10)
104         self.assertEqual(len(root.listNodes('message')), 2)
105         self.assertEqual(len(root.listNodes('presence')), 3)
106         self.assertEqual(len(root.listNodes('query')), 0)
107    
108 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109 #~ Unittest Main
110 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111
112 if __name__=='__main__':
113     unittest.main()
114
115
Note: See TracBrowser for help on using the browser.