comparison tests/test-ctxmanager.py @ 27785:ba427b51f1d8

util: rename ctxmanager's __call__ method to enter
author Bryan O'Sullivan <bryano@fb.com>
date Thu, 14 Jan 2016 09:31:01 -0800
parents 4e27c0a70574
children 4a7dc29bfad8
comparison
equal deleted inserted replaced
27784:432242f41d9f 27785:ba427b51f1d8
43 class test_ctxmanager(unittest.TestCase): 43 class test_ctxmanager(unittest.TestCase):
44 def test_basics(self): 44 def test_basics(self):
45 trace = [] 45 trace = []
46 addtrace = trace.append 46 addtrace = trace.append
47 with ctxmanager(ctxmgr('a', addtrace), ctxmgr('b', addtrace)) as c: 47 with ctxmanager(ctxmgr('a', addtrace), ctxmgr('b', addtrace)) as c:
48 a, b = c() 48 a, b = c.enter()
49 c.atexit(addtrace, ('atexit', 'x')) 49 c.atexit(addtrace, ('atexit', 'x'))
50 c.atexit(addtrace, ('atexit', 'y')) 50 c.atexit(addtrace, ('atexit', 'y'))
51 self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'), 51 self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'),
52 ('atexit', 'y'), ('atexit', 'x'), 52 ('atexit', 'y'), ('atexit', 'x'),
53 ('exit', 'b'), ('exit', 'a')]) 53 ('exit', 'b'), ('exit', 'a')])
56 trace = [] 56 trace = []
57 addtrace = trace.append 57 addtrace = trace.append
58 with self.assertRaises(ctxerror): 58 with self.assertRaises(ctxerror):
59 with ctxmanager(ctxmgr('a', addtrace), 59 with ctxmanager(ctxmgr('a', addtrace),
60 lambda: raise_on_enter('b', addtrace)) as c: 60 lambda: raise_on_enter('b', addtrace)) as c:
61 c() 61 c.enter()
62 addtrace('unreachable') 62 addtrace('unreachable')
63 self.assertEqual(trace, [('enter', 'a'), ('raise', 'b'), ('exit', 'a')]) 63 self.assertEqual(trace, [('enter', 'a'), ('raise', 'b'), ('exit', 'a')])
64 64
65 def test_raise_on_exit(self): 65 def test_raise_on_exit(self):
66 trace = [] 66 trace = []
67 addtrace = trace.append 67 addtrace = trace.append
68 with self.assertRaises(ctxerror): 68 with self.assertRaises(ctxerror):
69 with ctxmanager(ctxmgr('a', addtrace), 69 with ctxmanager(ctxmgr('a', addtrace),
70 lambda: raise_on_exit('b', addtrace)) as c: 70 lambda: raise_on_exit('b', addtrace)) as c:
71 c() 71 c.enter()
72 addtrace('running') 72 addtrace('running')
73 self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'), 'running', 73 self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'), 'running',
74 ('raise', 'b'), ('exit', 'a')]) 74 ('raise', 'b'), ('exit', 'a')])
75 75
76 if __name__ == '__main__': 76 if __name__ == '__main__':