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