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