ยปCore Development>Code coverage>Lib/test/test_xml_etree_c.py

Python code coverage for Lib/test/test_xml_etree_c.py

#countcontent
1n/a# xml.etree test for cElementTree
2n/aimport struct
3n/afrom test import support
4n/afrom test.support import import_fresh_module
5n/aimport types
6n/aimport unittest
7n/a
8n/acET = import_fresh_module('xml.etree.ElementTree',
9n/a fresh=['_elementtree'])
10n/acET_alias = import_fresh_module('xml.etree.cElementTree',
11n/a fresh=['_elementtree', 'xml.etree'])
12n/a
13n/a
14n/a@unittest.skipUnless(cET, 'requires _elementtree')
15n/aclass MiscTests(unittest.TestCase):
16n/a # Issue #8651.
17n/a @support.bigmemtest(size=support._2G + 100, memuse=1, dry_run=False)
18n/a def test_length_overflow(self, size):
19n/a data = b'x' * size
20n/a parser = cET.XMLParser()
21n/a try:
22n/a self.assertRaises(OverflowError, parser.feed, data)
23n/a finally:
24n/a data = None
25n/a
26n/a def test_del_attribute(self):
27n/a element = cET.Element('tag')
28n/a
29n/a element.tag = 'TAG'
30n/a with self.assertRaises(AttributeError):
31n/a del element.tag
32n/a self.assertEqual(element.tag, 'TAG')
33n/a
34n/a with self.assertRaises(AttributeError):
35n/a del element.text
36n/a self.assertIsNone(element.text)
37n/a element.text = 'TEXT'
38n/a with self.assertRaises(AttributeError):
39n/a del element.text
40n/a self.assertEqual(element.text, 'TEXT')
41n/a
42n/a with self.assertRaises(AttributeError):
43n/a del element.tail
44n/a self.assertIsNone(element.tail)
45n/a element.tail = 'TAIL'
46n/a with self.assertRaises(AttributeError):
47n/a del element.tail
48n/a self.assertEqual(element.tail, 'TAIL')
49n/a
50n/a with self.assertRaises(AttributeError):
51n/a del element.attrib
52n/a self.assertEqual(element.attrib, {})
53n/a element.attrib = {'A': 'B', 'C': 'D'}
54n/a with self.assertRaises(AttributeError):
55n/a del element.attrib
56n/a self.assertEqual(element.attrib, {'A': 'B', 'C': 'D'})
57n/a
58n/a def test_trashcan(self):
59n/a # If this test fails, it will most likely die via segfault.
60n/a e = root = cET.Element('root')
61n/a for i in range(200000):
62n/a e = cET.SubElement(e, 'x')
63n/a del e
64n/a del root
65n/a support.gc_collect()
66n/a
67n/a
68n/a@unittest.skipUnless(cET, 'requires _elementtree')
69n/aclass TestAliasWorking(unittest.TestCase):
70n/a # Test that the cET alias module is alive
71n/a def test_alias_working(self):
72n/a e = cET_alias.Element('foo')
73n/a self.assertEqual(e.tag, 'foo')
74n/a
75n/a
76n/a@unittest.skipUnless(cET, 'requires _elementtree')
77n/a@support.cpython_only
78n/aclass TestAcceleratorImported(unittest.TestCase):
79n/a # Test that the C accelerator was imported, as expected
80n/a def test_correct_import_cET(self):
81n/a # SubElement is a function so it retains _elementtree as its module.
82n/a self.assertEqual(cET.SubElement.__module__, '_elementtree')
83n/a
84n/a def test_correct_import_cET_alias(self):
85n/a self.assertEqual(cET_alias.SubElement.__module__, '_elementtree')
86n/a
87n/a def test_parser_comes_from_C(self):
88n/a # The type of methods defined in Python code is types.FunctionType,
89n/a # while the type of methods defined inside _elementtree is
90n/a # <class 'wrapper_descriptor'>
91n/a self.assertNotIsInstance(cET.Element.__init__, types.FunctionType)
92n/a
93n/a
94n/a@unittest.skipUnless(cET, 'requires _elementtree')
95n/a@support.cpython_only
96n/aclass SizeofTest(unittest.TestCase):
97n/a def setUp(self):
98n/a self.elementsize = support.calcobjsize('5P')
99n/a # extra
100n/a self.extra = struct.calcsize('PnnP4P')
101n/a
102n/a check_sizeof = support.check_sizeof
103n/a
104n/a def test_element(self):
105n/a e = cET.Element('a')
106n/a self.check_sizeof(e, self.elementsize)
107n/a
108n/a def test_element_with_attrib(self):
109n/a e = cET.Element('a', href='about:')
110n/a self.check_sizeof(e, self.elementsize + self.extra)
111n/a
112n/a def test_element_with_children(self):
113n/a e = cET.Element('a')
114n/a for i in range(5):
115n/a cET.SubElement(e, 'span')
116n/a # should have space for 8 children now
117n/a self.check_sizeof(e, self.elementsize + self.extra +
118n/a struct.calcsize('8P'))
119n/a
120n/adef test_main():
121n/a from test import test_xml_etree
122n/a
123n/a # Run the tests specific to the C implementation
124n/a support.run_unittest(
125n/a MiscTests,
126n/a TestAliasWorking,
127n/a TestAcceleratorImported,
128n/a SizeofTest,
129n/a )
130n/a
131n/a # Run the same test suite as the Python module
132n/a test_xml_etree.test_main(module=cET)
133n/a
134n/a
135n/aif __name__ == '__main__':
136n/a test_main()