Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(410)

Side by Side Diff: recipe_engine/unittests/util_test.py

Issue 2265673002: Add LogDog / annotation protobuf support. (Closed) Base URL: https://github.com/luci/recipes-py@step-formal-struct
Patch Set: Code review comments. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2015 The LUCI Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0
4 # that can be found in the LICENSE file.
5
6 import unittest
7
8 import test_env
9
10 from recipe_engine import util
11
12
13 class TestMultiException(unittest.TestCase):
14
15 def testNoExceptionsRaisesNothing(self):
16 mexc = util.MultiException()
17 with mexc.catch():
18 pass
19 mexc.raise_if_any()
20 self.assertEqual(str(mexc), 'MultiException(No exceptions)')
21
22 def testExceptionsRaised(self):
23 fail_exc = Exception('fail!')
24 mexc = util.MultiException()
25 with mexc.catch():
26 raise fail_exc
27 self.assertRaises(util.MultiException, mexc.raise_if_any)
28 self.assertEqual(len(mexc), 1)
29 self.assertIs(mexc[0], fail_exc)
30 self.assertEqual(str(mexc), 'MultiException(fail!)')
31
32 def testMultipleExceptions(self):
33 mexc = util.MultiException()
34 with mexc.catch():
35 raise KeyError('One')
36 with mexc.catch():
37 raise ValueError('Two')
38 self.assertEqual(len(mexc), 2)
39
40 exceptions = list(mexc)
41 self.assertIsInstance(exceptions[0], KeyError)
42 self.assertIsInstance(exceptions[1], ValueError)
43 self.assertEqual(str(mexc), "MultiException('One', and 1 more...)")
44
45 def testTargetedException(self):
46 mexc = util.MultiException()
47 def not_caught():
48 with mexc.catch(ValueError):
49 raise KeyError('One')
50 self.assertRaises(KeyError, not_caught)
51 self.assertFalse(mexc)
52
53
54 class TestDeferExceptionsFor(unittest.TestCase):
55
56 def testNoExceptionsDoesNothing(self):
57 v = []
58 util.defer_exceptions_for([1, 2, 3], lambda e: v.append(e))
59 self.assertEqual(v, [1, 2, 3])
60
61 def testCatchesExceptions(self):
62 v = []
63 def fn(e):
64 if e == 0:
65 raise ValueError('Zero')
66 v.append(e)
67
68 mexc = None
69 try:
70 util.defer_exceptions_for([0, 1, 0, 0, 2, 0, 3, 0], fn)
71 except util.MultiException as e:
72 mexc = e
73
74 self.assertEqual(v, [1, 2, 3])
75 self.assertIsNotNone(mexc)
76 self.assertEqual(len(mexc), 5)
77
78 def testCatchesSpecificExceptions(self):
79 def fn(e):
80 raise ValueError('Zero')
81 self.assertRaises(ValueError, util.defer_exceptions_for, [1], fn, KeyError)
82
83
84 if __name__ == '__main__':
85 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698