tests/test-ctxmanager.py
changeset 27785 ba427b51f1d8
parent 27703 4e27c0a70574
child 27786 4a7dc29bfad8
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__':