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