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