Mercurial > hg
comparison tests/test-ctxmanager.py @ 32279:68c43a416585
tests: use context manager form of assertRaises
Support for using unittest.TestCase.assertRaises as a context
manager was added in Python 2.7. This form is more readable,
especially for complex tests.
While I was here, I also restored the use of assertRaisesRegexp,
which was removed in c6921568cd20 for Python 2.6 compatibility.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 13 May 2017 11:52:44 -0700 |
parents | 441491aba8c3 |
children |
comparison
equal
deleted
inserted
replaced
32278:7c3ef55dedbe | 32279:68c43a416585 |
---|---|
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 def go(): | 58 with self.assertRaises(ctxerror): |
59 with util.ctxmanager(ctxmgr('a', addtrace), | 59 with util.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) | |
64 self.assertEqual(trace, [('enter', 'a'), ('raise', 'b'), ('exit', 'a')]) | 63 self.assertEqual(trace, [('enter', 'a'), ('raise', 'b'), ('exit', 'a')]) |
65 | 64 |
66 def test_raise_on_exit(self): | 65 def test_raise_on_exit(self): |
67 trace = [] | 66 trace = [] |
68 addtrace = trace.append | 67 addtrace = trace.append |
69 def go(): | 68 with self.assertRaises(ctxerror): |
70 with util.ctxmanager(ctxmgr('a', addtrace), | 69 with util.ctxmanager(ctxmgr('a', addtrace), |
71 lambda: raise_on_exit('b', addtrace)) as c: | 70 lambda: raise_on_exit('b', addtrace)) as c: |
72 c.enter() | 71 c.enter() |
73 addtrace('running') | 72 addtrace('running') |
74 self.assertRaises(ctxerror, go) | |
75 self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'), 'running', | 73 self.assertEqual(trace, [('enter', 'a'), ('enter', 'b'), 'running', |
76 ('raise', 'b'), ('exit', 'a')]) | 74 ('raise', 'b'), ('exit', 'a')]) |
77 | 75 |
78 if __name__ == '__main__': | 76 if __name__ == '__main__': |
79 silenttestrunner.main(__name__) | 77 silenttestrunner.main(__name__) |