1
2
3 """
4 Test cases related to namespace implementation classes and the
5 namespace registry mechanism
6 """
7
8 import unittest
9
10 from common_imports import etree, HelperTestCase, doctest
11
13
22
29
33
44
46 bluff_dict = {u'bluff' : self.bluff_class}
47 maeh_dict = {u'maeh' : self.maeh_class}
48
49 etree.Namespace(u'ns10').update(bluff_dict)
50
51 tree = self.parse(u'<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>')
52
53 el = tree.getroot()
54 self.assert_(isinstance(el, etree.ElementBase))
55 self.assert_(hasattr(el, 'bluff'))
56 self.assertFalse(hasattr(el[0], 'maeh'))
57 self.assertFalse(hasattr(el[0], 'bluff'))
58 self.assertEquals(el.bluff(), u'bluff')
59 del el
60
61 etree.Namespace(u'ns11').update(maeh_dict)
62 el = tree.getroot()
63 self.assert_(hasattr(el, 'bluff'))
64 self.assert_(hasattr(el[0], 'maeh'))
65 self.assertEquals(el.bluff(), u'bluff')
66 self.assertEquals(el[0].maeh(), u'maeh')
67 del el
68
69 etree.Namespace(u'ns10').clear()
70
71 tree = self.parse(u'<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>')
72 el = tree.getroot()
73 self.assertFalse(hasattr(el, 'bluff'))
74 self.assertFalse(hasattr(el, 'maeh'))
75 self.assertFalse(hasattr(el[0], 'bluff'))
76 self.assert_(hasattr(el[0], 'maeh'))
77
78 etree.Namespace(u'ns11').clear()
79
81 bluff_dict = {
82 None : self.bluff_class,
83 'maeh' : self.maeh_class
84 }
85
86 ns = etree.Namespace("uri:nsDefClass")
87 ns.update(bluff_dict)
88
89 tree = self.parse(u'''
90 <test xmlns="bla" xmlns:ns1="uri:nsDefClass" xmlns:ns2="uri:nsDefClass">
91 <ns2:el1/><ns1:el2/><ns1:maeh/><ns2:maeh/><maeh/>
92 </test>
93 ''')
94
95 el = tree.getroot()
96 self.assertFalse(isinstance(el, etree.ElementBase))
97 for child in el[:-1]:
98 self.assert_(isinstance(child, etree.ElementBase), child.tag)
99 self.assertFalse(isinstance(el[-1], etree.ElementBase))
100
101 self.assert_(hasattr(el[0], 'bluff'))
102 self.assert_(hasattr(el[1], 'bluff'))
103 self.assert_(hasattr(el[2], 'maeh'))
104 self.assert_(hasattr(el[3], 'maeh'))
105 self.assertFalse(hasattr(el[4], 'maeh'))
106 del el
107
108 ns.clear()
109
111 bluff_dict = {u'bluff' : self.bluff_class}
112 etree.Namespace(u'ns20').update(bluff_dict)
113
114 maeh_dict = {u'maeh' : self.maeh_class}
115 etree.Namespace(u'ns21').update(maeh_dict)
116
117 el = etree.Element("{ns20}bluff")
118 self.assert_(hasattr(el, 'bluff'))
119
120 child = etree.SubElement(el, "{ns21}maeh")
121 self.assert_(hasattr(child, 'maeh'))
122 child = etree.SubElement(el, "{ns20}bluff")
123 self.assert_(hasattr(child, 'bluff'))
124 child = etree.SubElement(el, "{ns21}bluff")
125 self.assertFalse(hasattr(child, 'bluff'))
126 self.assertFalse(hasattr(child, 'maeh'))
127
128 self.assert_(hasattr(el[0], 'maeh'))
129 self.assert_(hasattr(el[1], 'bluff'))
130 self.assertFalse(hasattr(el[2], 'bluff'))
131 self.assertFalse(hasattr(el[2], 'maeh'))
132
133 self.assertEquals(el.bluff(), u'bluff')
134 self.assertEquals(el[0].maeh(), u'maeh')
135 self.assertEquals(el[1].bluff(), u'bluff')
136
137 etree.Namespace(u'ns20').clear()
138 etree.Namespace(u'ns21').clear()
139
156
158 suite = unittest.TestSuite()
159 suite.addTests([unittest.makeSuite(ETreeNamespaceClassesTestCase)])
160 optionflags = doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS
161 suite.addTests(
162 [doctest.DocFileSuite('../../../doc/element_classes.txt',
163 optionflags=optionflags)],
164 )
165 return suite
166
167 if __name__ == '__main__':
168 unittest.main()
169