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.assertTrue(isinstance(el, etree.ElementBase))
62 self.assertTrue(hasattr(el, 'bluff'))
63 self.assertFalse(hasattr(el[0], 'maeh'))
64 self.assertFalse(hasattr(el[0], 'bluff'))
65 self.assertEqual(el.bluff(), 'bluff')
66 del el
67
68 self.Namespace('ns11').update(maeh_dict)
69 el = tree.getroot()
70 self.assertTrue(hasattr(el, 'bluff'))
71 self.assertTrue(hasattr(el[0], 'maeh'))
72 self.assertEqual(el.bluff(), 'bluff')
73 self.assertEqual(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.assertTrue(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.assertTrue(isinstance(child, etree.ElementBase), child.tag)
106 self.assertFalse(isinstance(el[-1], etree.ElementBase))
107
108 self.assertTrue(hasattr(el[0], 'bluff'))
109 self.assertTrue(hasattr(el[1], 'bluff'))
110 self.assertTrue(hasattr(el[2], 'maeh'))
111 self.assertTrue(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.assertTrue(hasattr(el, 'bluff'))
126
127 child = etree.SubElement(el, "{ns21}maeh")
128 self.assertTrue(hasattr(child, 'maeh'))
129 child = etree.SubElement(el, "{ns20}bluff")
130 self.assertTrue(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.assertTrue(hasattr(el[0], 'maeh'))
136 self.assertTrue(hasattr(el[1], 'bluff'))
137 self.assertFalse(hasattr(el[2], 'bluff'))
138 self.assertFalse(hasattr(el[2], 'maeh'))
139
140 self.assertEqual(el.bluff(), 'bluff')
141 self.assertEqual(el[0].maeh(), 'maeh')
142 self.assertEqual(el[1].bluff(), 'bluff')
143
144 self.Namespace('ns20').clear()
145 self.Namespace('ns21').clear()
146
148 bluff_dict = {None : self.bluff_class}
149 self.Namespace('ns30').update(bluff_dict)
150
151 maeh_dict = {'maeh' : self.maeh_class}
152 self.Namespace(None).update(maeh_dict)
153
154 el = etree.Element("{ns30}bluff")
155 etree.SubElement(el, "maeh")
156 self.assertTrue(hasattr(el, 'bluff'))
157 self.assertTrue(hasattr(el[0], 'maeh'))
158 self.assertEqual(el.bluff(), 'bluff')
159 self.assertEqual(el[0].maeh(), 'maeh')
160
161 self.Namespace(None).clear()
162 self.Namespace('ns30').clear()
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.assertEqual('default_class', el.tag)
180 self.assertEqual('testtext', el.text)
181 self.assertEqual(None, el.tail)
182 self.assertEqual(2, len(el))
183 self.assertEqual(7, len(list(el.iter())))
184
185 self.assertEqual('bluff_class', el[0].tag)
186 self.assertEqual('TaIL', el[0][0].tail)
187 self.assertEqual('TaIL', ''.join(el[0].itertext()))
188 self.assertEqual('{http://a.b/c}HONK',
189 el[0][0].tag)
190 self.assertEqual('maeh_class',
191 el[0][1].tag)
192
193 self.assertEqual('maeh_class', el[1].tag)
194 self.assertEqual('TeXT', el[1].text)
195 self.assertEqual('bluff_class', el[1][0].tag)
196 self.assertEqual('{http://a.b/c}HONK', el[1][1].tag)
197 self.assertEqual('TAiL', el[1][1].tail)
198
199 self.assertEqual('TeXTTAiL',
200 ''.join(el[1].itertext()))
201 self.assertEqual('Tail', el[1].tail)
202 self.assertEqual('TAiL', el[1][1].tail)
203 self.assertEqual('bluff_class', el[1][0].tag)
204 self.assertEqual('{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