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

Python code coverage for Lib/ctypes/test/test_anon.py

#countcontent
1n/aimport unittest
2n/afrom ctypes import *
3n/a
4n/aclass AnonTest(unittest.TestCase):
5n/a
6n/a def test_anon(self):
7n/a class ANON(Union):
8n/a _fields_ = [("a", c_int),
9n/a ("b", c_int)]
10n/a
11n/a class Y(Structure):
12n/a _fields_ = [("x", c_int),
13n/a ("_", ANON),
14n/a ("y", c_int)]
15n/a _anonymous_ = ["_"]
16n/a
17n/a self.assertEqual(Y.a.offset, sizeof(c_int))
18n/a self.assertEqual(Y.b.offset, sizeof(c_int))
19n/a
20n/a self.assertEqual(ANON.a.offset, 0)
21n/a self.assertEqual(ANON.b.offset, 0)
22n/a
23n/a def test_anon_nonseq(self):
24n/a # TypeError: _anonymous_ must be a sequence
25n/a self.assertRaises(TypeError,
26n/a lambda: type(Structure)("Name",
27n/a (Structure,),
28n/a {"_fields_": [], "_anonymous_": 42}))
29n/a
30n/a def test_anon_nonmember(self):
31n/a # AttributeError: type object 'Name' has no attribute 'x'
32n/a self.assertRaises(AttributeError,
33n/a lambda: type(Structure)("Name",
34n/a (Structure,),
35n/a {"_fields_": [],
36n/a "_anonymous_": ["x"]}))
37n/a
38n/a def test_nested(self):
39n/a class ANON_S(Structure):
40n/a _fields_ = [("a", c_int)]
41n/a
42n/a class ANON_U(Union):
43n/a _fields_ = [("_", ANON_S),
44n/a ("b", c_int)]
45n/a _anonymous_ = ["_"]
46n/a
47n/a class Y(Structure):
48n/a _fields_ = [("x", c_int),
49n/a ("_", ANON_U),
50n/a ("y", c_int)]
51n/a _anonymous_ = ["_"]
52n/a
53n/a self.assertEqual(Y.x.offset, 0)
54n/a self.assertEqual(Y.a.offset, sizeof(c_int))
55n/a self.assertEqual(Y.b.offset, sizeof(c_int))
56n/a self.assertEqual(Y._.offset, sizeof(c_int))
57n/a self.assertEqual(Y.y.offset, sizeof(c_int) * 2)
58n/a
59n/aif __name__ == "__main__":
60n/a unittest.main()