##// END OF EJS Templates
test-ctxmanager: fix Python 2.6 compatibility problem
Bryan O'Sullivan -
r27786:4a7dc29b default
parent child Browse files
Show More
@@ -1,77 +1,79 b''
1 from __future__ import absolute_import
1 from __future__ import absolute_import
2
2
3 import silenttestrunner
3 import silenttestrunner
4 import unittest
4 import unittest
5
5
6 from mercurial.util import ctxmanager
6 from mercurial.util import ctxmanager
7
7
8 class contextmanager(object):
8 class contextmanager(object):
9 def __init__(self, name, trace):
9 def __init__(self, name, trace):
10 self.name = name
10 self.name = name
11 self.entered = False
11 self.entered = False
12 self.exited = False
12 self.exited = False
13 self.trace = trace
13 self.trace = trace
14
14
15 def __enter__(self):
15 def __enter__(self):
16 self.entered = True
16 self.entered = True
17 self.trace(('enter', self.name))
17 self.trace(('enter', self.name))
18 return self
18 return self
19
19
20 def __exit__(self, exc_type, exc_val, exc_tb):
20 def __exit__(self, exc_type, exc_val, exc_tb):
21 self.exited = exc_type, exc_val, exc_tb
21 self.exited = exc_type, exc_val, exc_tb
22 self.trace(('exit', self.name))
22 self.trace(('exit', self.name))
23
23
24 def __repr__(self):
24 def __repr__(self):
25 return '<ctx %r>' % self.name
25 return '<ctx %r>' % self.name
26
26
27 class ctxerror(Exception):
27 class ctxerror(Exception):
28 pass
28 pass
29
29
30 class raise_on_enter(contextmanager):
30 class raise_on_enter(contextmanager):
31 def __enter__(self):
31 def __enter__(self):
32 self.trace(('raise', self.name))
32 self.trace(('raise', self.name))
33 raise ctxerror(self.name)
33 raise ctxerror(self.name)
34
34
35 class raise_on_exit(contextmanager):
35 class raise_on_exit(contextmanager):
36 def __exit__(self, exc_type, exc_val, exc_tb):
36 def __exit__(self, exc_type, exc_val, exc_tb):
37 self.trace(('raise', self.name))
37 self.trace(('raise', self.name))
38 raise ctxerror(self.name)
38 raise ctxerror(self.name)
39
39
40 def ctxmgr(name, trace):
40 def ctxmgr(name, trace):
41 return lambda: contextmanager(name, trace)
41 return lambda: contextmanager(name, trace)
42
42
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.enter()
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')])
54
54
55 def test_raise_on_enter(self):
55 def test_raise_on_enter(self):
56 trace = []
56 trace = []
57 addtrace = trace.append
57 addtrace = trace.append
58 with self.assertRaises(ctxerror):
58 def go():
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.enter()
61 c.enter()
62 addtrace('unreachable')
62 addtrace('unreachable')
63 self.assertRaises(ctxerror, go)
63 self.assertEqual(trace, [('enter', 'a'), ('raise', 'b'), ('exit', 'a')])
64 self.assertEqual(trace, [('enter', 'a'), ('raise', 'b'), ('exit', 'a')])
64
65
65 def test_raise_on_exit(self):
66 def test_raise_on_exit(self):
66 trace = []
67 trace = []
67 addtrace = trace.append
68 addtrace = trace.append
68 with self.assertRaises(ctxerror):
69 def go():
69 with ctxmanager(ctxmgr('a', addtrace),
70 with ctxmanager(ctxmgr('a', addtrace),
70 lambda: raise_on_exit('b', addtrace)) as c:
71 lambda: raise_on_exit('b', addtrace)) as c:
71 c.enter()
72 c.enter()
72 addtrace('running')
73 addtrace('running')
74 self.assertRaises(ctxerror, go)
73 self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'), 'running',
75 self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'), 'running',
74 ('raise', 'b'), ('exit', 'a')])
76 ('raise', 'b'), ('exit', 'a')])
75
77
76 if __name__ == '__main__':
78 if __name__ == '__main__':
77 silenttestrunner.main(__name__)
79 silenttestrunner.main(__name__)
General Comments 0
You need to be logged in to leave comments. Login now