deeper pass on xmlrpclib vs xmlrpc.client as well as configparser
[sfa.git] / xmlbuilder-0.9 / xmlbuilder / tests / __init__.py
1 #!/usr/bin/env python
2 from __future__ import with_statement
3 #-------------------------------------------------------------------------------
4 import unittest
5 from xml.etree.ElementTree import fromstring
6 #-------------------------------------------------------------------------------
7 from xmlbuilder import XMLBuilder
8 #-------------------------------------------------------------------------------
9 def xmlStructureEqual(xml1,xml2):
10     tree1 = fromstring(xml1)
11     tree2 = fromstring(xml2)
12     return _xmlStructureEqual(tree1,tree2)
13 #-------------------------------------------------------------------------------
14 def _xmlStructureEqual(tree1,tree2):
15     if tree1.tag != tree2.tag:
16         return False
17     attr1 = list(tree1.attrib.items())
18     attr1.sort()
19     attr2 = list(tree2.attrib.items())
20     attr2.sort()
21     if attr1 != attr2:
22         return False
23     return tree1.getchildren() == tree2.getchildren()
24 #-------------------------------------------------------------------------------
25 result1 = \
26 """
27 <root>
28     <array />
29     <array len="10">
30         <el val="0" />
31         <el val="1">xyz</el>
32         <el val="2">abc</el>
33         <el val="3" />
34         <el val="4" />
35         <el val="5" />
36         <sup-el val="23">test  </sup-el>
37     </array>
38 </root>
39 """.strip()
40 #-------------------------------------------------------------------------------
41 class TestXMLBuilder(unittest.TestCase):
42     def testShift(self):
43         xml = (XMLBuilder() << ('root',))
44         self.assertEqual(str(xml),"<root />")
45         
46         xml = XMLBuilder()
47         xml << ('root',"some text")
48         self.assertEqual(str(xml),"<root>some text</root>")
49         
50         xml = XMLBuilder()
51         xml << ('root',{'x':1,'y':'2'})
52         self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'>some text</root>"))
53         
54         xml = XMLBuilder()
55         xml << ('root',{'x':1,'y':'2'})
56         self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'></root>"))
57
58         xml = XMLBuilder()
59         xml << ('root',{'x':1,'y':'2'})
60         self.assert_(not xmlStructureEqual(str(xml),"<root x='2' y='2'></root>"))
61
62         
63         xml = XMLBuilder()
64         xml << ('root',"gonduras.ua",{'x':1,'y':'2'})
65         self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'>gonduras.ua</root>"))
66         
67         xml = XMLBuilder()
68         xml << ('root',"gonduras.ua",{'x':1,'y':'2'})
69         self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'>gonduras.com</root>"))
70     #---------------------------------------------------------------------------
71     def testWith(self):
72         xml = XMLBuilder()
73         with xml.root(lenght = 12):
74             pass
75         self.assertEqual(str(xml),'<root lenght="12" />')
76         
77         xml = XMLBuilder()
78         with xml.root():
79             xml << "text1" << "text2" << ('some_node',)
80         self.assertEqual(str(xml),"<root>text1text2<some_node /></root>")
81     #---------------------------------------------------------------------------
82     def testFormat(self):
83         x = XMLBuilder('utf-8',format = True)
84         with x.root():
85             x << ('array',)
86             with x.array(len = 10):
87                 with x.el(val = 0):
88                     pass
89                 with x.el('xyz',val = 1):
90                     pass
91                 x << ("el","abc",{'val':2}) << ('el',dict(val=3))
92                 x << ('el',dict(val=4)) << ('el',dict(val='5'))
93                 with x('sup-el',val = 23):
94                     x << "test  "
95         self.assertEqual(str(x),result1)
96 #-------------------------------------------------------------------------------
97 if __name__ == '__main__':
98     unittest.main()
99 #-------------------------------------------------------------------------------