1
2
3 """
4 Test cases related to namespace implementation classes and the
5 namespace registry mechanism
6 """
7
8 import unittest, sys, os.path
9
10 this_dir = os.path.dirname(__file__)
11 if this_dir not in sys.path:
12 sys.path.insert(0, this_dir)
13
14 from common_imports import etree, HelperTestCase, _bytes
15 from common_imports import doctest, make_doctest
16
18
27
35
40
51
53 bluff_dict = {'bluff' : self.bluff_class}
54 maeh_dict = {'maeh' : self.maeh_class}
55
56 self.Namespace('ns10').update(bluff_dict)
57
58 tree = self.parse(_bytes('<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>'))
59
60 el = tree.getroot()
61 self.assert_(isinstance(el, etree.ElementBase))
62 self.assert_(hasattr(el, 'bluff'))
63 self.assertFalse(hasattr(el[0], 'maeh'))
64 self.assertFalse(hasattr(el[0], 'bluff'))
65 self.assertEquals(el.bluff(), 'bluff')
66 del el
67
68 self.Namespace('ns11').update(maeh_dict)
69 el = tree.getroot()
70 self.assert_(hasattr(el, 'bluff'))
71 self.assert_(hasattr(el[0], 'maeh'))
72 self.assertEquals(el.bluff(), 'bluff')
73 self.assertEquals(el[0].maeh(), 'maeh')
74 del el
75
76 self.Namespace('ns10').clear()
77
78 tree = self.parse(_bytes('<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>'))
79 el = tree.getroot()
80 self.assertFalse(hasattr(el, 'bluff'))
81 self.assertFalse(hasattr(el, 'maeh'))
82 self.assertFalse(hasattr(el[0], 'bluff'))
83 self.assert_(hasattr(el[0], 'maeh'))
84
85 self.Namespace('ns11').clear()
86
88 bluff_dict = {
89 None : self.bluff_class,
90 'maeh' : self.maeh_class
91 }
92
93 ns = self.Namespace("uri:nsDefClass")
94 ns.update(bluff_dict)
95
96 tree = self.parse(_bytes('''
97 <test xmlns="bla" xmlns:ns1="uri:nsDefClass" xmlns:ns2="uri:nsDefClass">
98 <ns2:el1/><ns1:el2/><ns1:maeh/><ns2:maeh/><maeh/>
99 </test>
100 '''))
101
102 el = tree.getroot()
103 self.assertFalse(isinstance(el, etree.ElementBase))
104 for child in el[:-1]:
105 self.assert_(isinstance(child, etree.ElementBase), child.tag)
106 self.assertFalse(isinstance(el[-1], etree.ElementBase))
107
108 self.assert_(hasattr(el[0], 'bluff'))
109 self.assert_(hasattr(el[1], 'bluff'))
110 self.assert_(hasattr(el[2], 'maeh'))
111 self.assert_(hasattr(el[3], 'maeh'))
112 self.assertFalse(hasattr(el[4], 'maeh'))
113 del el
114
115 ns.clear()
116
118 bluff_dict = {'bluff' : self.bluff_class}
119 self.Namespace('ns20').update(bluff_dict)
120
121 maeh_dict = {'maeh' : self.maeh_class}
122 self.Namespace('ns21').update(maeh_dict)
123
124 el = etree.Element("{ns20}bluff")
125 self.assert_(hasattr(el, 'bluff'))
126
127 child = etree.SubElement(el, "{ns21}maeh")
128 self.assert_(hasattr(child, 'maeh'))
129 child = etree.SubElement(el, "{ns20}bluff")
130 self.assert_(hasattr(child, 'bluff'))
131 child = etree.SubElement(el, "{ns21}bluff")
132 self.assertFalse(hasattr(child, 'bluff'))
133 self.assertFalse(hasattr(child, 'maeh'))
134
135 self.assert_(hasattr(el[0], 'maeh'))
136 self.assert_(hasattr(el[1], 'bluff'))
137 self.assertFalse(hasattr(el[2], 'bluff'))
138 self.assertFalse(hasattr(el[2], 'maeh'))
139
140 self.assertEquals(el.bluff(), 'bluff')
141 self.assertEquals(el[0].maeh(), 'maeh')
142 self.assertEquals(el[1].bluff(), 'bluff')
143
144 self.Namespace('ns20').clear()
145 self.Namespace('ns21').clear()
146
163
171
172 el = default(
173 "test",
174 "text",
175 bluff(honk, "TaIL", maeh),
176 maeh("TeXT", bluff, honk(), "TAiL"),
177 "Tail")
178
179 self.assertEquals('default_class', el.tag)
180 self.assertEquals('testtext', el.text)
181 self.assertEquals(None, el.tail)
182 self.assertEquals(2, len(el))
183 self.assertEquals(7, len(list(el.iter())))
184
185 self.assertEquals('bluff_class', el[0].tag)
186 self.assertEquals('TaIL', el[0][0].tail)
187 self.assertEquals('TaIL', ''.join(el[0].itertext()))
188 self.assertEquals('{http://a.b/c}HONK',
189 el[0][0].tag)
190 self.assertEquals('maeh_class',
191 el[0][1].tag)
192
193 self.assertEquals('maeh_class', el[1].tag)
194 self.assertEquals('TeXT', el[1].text)
195 self.assertEquals('bluff_class', el[1][0].tag)
196 self.assertEquals('{http://a.b/c}HONK', el[1][1].tag)
197 self.assertEquals('TAiL', el[1][1].tail)
198
199 self.assertEquals('TeXTTAiL',
200 ''.join(el[1].itertext()))
201 self.assertEquals('Tail', el[1].tail)
202 self.assertEquals('TAiL', el[1][1].tail)
203 self.assertEquals('bluff_class', el[1][0].tag)
204 self.assertEquals('{http://a.b/c}HONK', el[1][1].tag)
205
206
213
214 if __name__ == '__main__':
215 print('to test use test.py %s' % __file__)
216