2 from __future__ import with_statement
3 #-------------------------------------------------------------------------------
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:
17 attr1 = list(tree1.attrib.items())
19 attr2 = list(tree2.attrib.items())
23 return tree1.getchildren() == tree2.getchildren()
24 #-------------------------------------------------------------------------------
36 <sup-el val="23">test </sup-el>
40 #-------------------------------------------------------------------------------
41 class TestXMLBuilder(unittest.TestCase):
43 xml = (XMLBuilder() << ('root',))
44 self.assertEqual(str(xml),"<root />")
47 xml << ('root',"some text")
48 self.assertEqual(str(xml),"<root>some text</root>")
51 xml << ('root',{'x':1,'y':'2'})
52 self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'>some text</root>"))
55 xml << ('root',{'x':1,'y':'2'})
56 self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'></root>"))
59 xml << ('root',{'x':1,'y':'2'})
60 self.assert_(not xmlStructureEqual(str(xml),"<root x='2' y='2'></root>"))
64 xml << ('root',"gonduras.ua",{'x':1,'y':'2'})
65 self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'>gonduras.ua</root>"))
68 xml << ('root',"gonduras.ua",{'x':1,'y':'2'})
69 self.assert_(xmlStructureEqual(str(xml),"<root x='1' y='2'>gonduras.com</root>"))
70 #---------------------------------------------------------------------------
73 with xml.root(lenght = 12):
75 self.assertEqual(str(xml),'<root lenght="12" />')
79 xml << "text1" << "text2" << ('some_node',)
80 self.assertEqual(str(xml),"<root>text1text2<some_node /></root>")
81 #---------------------------------------------------------------------------
83 x = XMLBuilder('utf-8',format = True)
86 with x.array(len = 10):
89 with x.el('xyz',val = 1):
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):
95 self.assertEqual(str(x),result1)
96 #-------------------------------------------------------------------------------
97 if __name__ == '__main__':
99 #-------------------------------------------------------------------------------