1 #!/usr/bin/env python
\r
2 from __future__ import with_statement
\r
3 #-------------------------------------------------------------------------------
\r
5 from xml.etree.ElementTree import fromstring
\r
6 #-------------------------------------------------------------------------------
\r
7 from xmlbuilder import XMLBuilder
\r
8 #-------------------------------------------------------------------------------
\r
9 def xmlStructureEqual(xml1,xml2):
\r
10 tree1 = fromstring(xml1)
\r
11 tree2 = fromstring(xml2)
\r
12 return _xmlStructureEqual(tree1,tree2)
\r
13 #-------------------------------------------------------------------------------
\r
14 def _xmlStructureEqual(tree1,tree2):
\r
15 if tree1.tag != tree2.tag:
\r
17 attr1 = list(tree1.attrib.items())
\r
19 attr2 = list(tree2.attrib.items())
\r
23 return tree1.getchildren() == tree2.getchildren()
\r
24 #-------------------------------------------------------------------------------
\r
31 <el val="1">xyz</el>
\r
32 <el val="2">abc</el>
\r
36 <sup-el val="23">test </sup-el>
\r
40 #-------------------------------------------------------------------------------
\r
41 class TestXMLBuilder(unittest.TestCase):
\r
42 def testShift(self):
\r
43 xml = (XMLBuilder() << ('root',))
\r
44 self.assertEqual(str(xml),"<root />")
\r
47 xml << ('root',"some text")
\r
48 self.assertEqual(str(xml),"<root>some text</root>")
\r
51 xml << ('root',{'x':1,'y':'2'})
\r
52 self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'>some text</root>"))
\r
55 xml << ('root',{'x':1,'y':'2'})
\r
56 self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'></root>"))
\r
59 xml << ('root',{'x':1,'y':'2'})
\r
60 self.assert_(not xmlStructureEqual(str(xml),"<root x='2' y='2'></root>"))
\r
64 xml << ('root',"gonduras.ua",{'x':1,'y':'2'})
\r
65 self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'>gonduras.ua</root>"))
\r
68 xml << ('root',"gonduras.ua",{'x':1,'y':'2'})
\r
69 self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'>gonduras.com</root>"))
\r
70 #---------------------------------------------------------------------------
\r
73 with xml.root(lenght = 12):
\r
75 self.assertEqual(str(xml),'<root lenght="12" />')
\r
79 xml << "text1" << "text2" << ('some_node',)
\r
80 self.assertEqual(str(xml),"<root>text1text2<some_node /></root>")
\r
81 #---------------------------------------------------------------------------
\r
82 def testFormat(self):
\r
83 x = XMLBuilder('utf-8',format = True)
\r
86 with x.array(len = 10):
\r
89 with x.el('xyz',val = 1):
\r
91 x << ("el","abc",{'val':2}) << ('el',dict(val=3))
\r
92 x << ('el',dict(val=4)) << ('el',dict(val='5'))
\r
93 with x('sup-el',val = 23):
\r
95 self.assertEqual(str(x),result1)
\r
96 #-------------------------------------------------------------------------------
\r
97 if __name__ == '__main__':
\r
99 #-------------------------------------------------------------------------------
\r