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

Python code coverage for Lib/test/test_dictcomps.py

#countcontent
1n/aimport unittest
2n/a
3n/a# For scope testing.
4n/ag = "Global variable"
5n/a
6n/a
7n/aclass DictComprehensionTest(unittest.TestCase):
8n/a
9n/a def test_basics(self):
10n/a expected = {0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17,
11n/a 8: 18, 9: 19}
12n/a actual = {k: k + 10 for k in range(10)}
13n/a self.assertEqual(actual, expected)
14n/a
15n/a expected = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
16n/a actual = {k: v for k in range(10) for v in range(10) if k == v}
17n/a self.assertEqual(actual, expected)
18n/a
19n/a def test_scope_isolation(self):
20n/a k = "Local Variable"
21n/a
22n/a expected = {0: None, 1: None, 2: None, 3: None, 4: None, 5: None,
23n/a 6: None, 7: None, 8: None, 9: None}
24n/a actual = {k: None for k in range(10)}
25n/a self.assertEqual(actual, expected)
26n/a self.assertEqual(k, "Local Variable")
27n/a
28n/a expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
29n/a 38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6,
30n/a 55: 6, 56: 6, 57: 6, 58: 6, 59: 6, 63: 7, 64: 7, 65: 7,
31n/a 66: 7, 67: 7, 68: 7, 69: 7, 72: 8, 73: 8, 74: 8, 75: 8,
32n/a 76: 8, 77: 8, 78: 8, 79: 8, 81: 9, 82: 9, 83: 9, 84: 9,
33n/a 85: 9, 86: 9, 87: 9, 88: 9, 89: 9}
34n/a actual = {k: v for v in range(10) for k in range(v * 9, v * 10)}
35n/a self.assertEqual(k, "Local Variable")
36n/a self.assertEqual(actual, expected)
37n/a
38n/a def test_scope_isolation_from_global(self):
39n/a expected = {0: None, 1: None, 2: None, 3: None, 4: None, 5: None,
40n/a 6: None, 7: None, 8: None, 9: None}
41n/a actual = {g: None for g in range(10)}
42n/a self.assertEqual(actual, expected)
43n/a self.assertEqual(g, "Global variable")
44n/a
45n/a expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
46n/a 38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6,
47n/a 55: 6, 56: 6, 57: 6, 58: 6, 59: 6, 63: 7, 64: 7, 65: 7,
48n/a 66: 7, 67: 7, 68: 7, 69: 7, 72: 8, 73: 8, 74: 8, 75: 8,
49n/a 76: 8, 77: 8, 78: 8, 79: 8, 81: 9, 82: 9, 83: 9, 84: 9,
50n/a 85: 9, 86: 9, 87: 9, 88: 9, 89: 9}
51n/a actual = {g: v for v in range(10) for g in range(v * 9, v * 10)}
52n/a self.assertEqual(g, "Global variable")
53n/a self.assertEqual(actual, expected)
54n/a
55n/a def test_global_visibility(self):
56n/a expected = {0: 'Global variable', 1: 'Global variable',
57n/a 2: 'Global variable', 3: 'Global variable',
58n/a 4: 'Global variable', 5: 'Global variable',
59n/a 6: 'Global variable', 7: 'Global variable',
60n/a 8: 'Global variable', 9: 'Global variable'}
61n/a actual = {k: g for k in range(10)}
62n/a self.assertEqual(actual, expected)
63n/a
64n/a def test_local_visibility(self):
65n/a v = "Local variable"
66n/a expected = {0: 'Local variable', 1: 'Local variable',
67n/a 2: 'Local variable', 3: 'Local variable',
68n/a 4: 'Local variable', 5: 'Local variable',
69n/a 6: 'Local variable', 7: 'Local variable',
70n/a 8: 'Local variable', 9: 'Local variable'}
71n/a actual = {k: v for k in range(10)}
72n/a self.assertEqual(actual, expected)
73n/a self.assertEqual(v, "Local variable")
74n/a
75n/a def test_illegal_assignment(self):
76n/a with self.assertRaisesRegex(SyntaxError, "can't assign"):
77n/a compile("{x: y for y, x in ((1, 2), (3, 4))} = 5", "<test>",
78n/a "exec")
79n/a
80n/a with self.assertRaisesRegex(SyntaxError, "can't assign"):
81n/a compile("{x: y for y, x in ((1, 2), (3, 4))} += 5", "<test>",
82n/a "exec")
83n/a
84n/a
85n/aif __name__ == "__main__":
86n/a unittest.main()