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

Unified 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: Stronger flush meta logic, moar test. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « recipe_engine/unittests/stream_test.py ('k') | recipe_engine/util.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: recipe_engine/unittests/util_test.py
diff --git a/recipe_engine/unittests/util_test.py b/recipe_engine/unittests/util_test.py
new file mode 100755
index 0000000000000000000000000000000000000000..37932d65fbf4bb7f4ed19bd87f356b970b7184a7
--- /dev/null
+++ b/recipe_engine/unittests/util_test.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python
+# Copyright 2015 The LUCI Authors. All rights reserved.
+# Use of this source code is governed under the Apache License, Version 2.0
+# that can be found in the LICENSE file.
+
+import unittest
+
+import test_env
+
+from recipe_engine import util
+
+
+class TestMultiException(unittest.TestCase):
+
+ def testNoExceptionsRaisesNothing(self):
+ mb = util.MultiException.Builder()
+ with mb.catch():
+ pass
+ mb.raise_if_any()
+
+ mexc = util.MultiException()
+ self.assertEqual(str(mexc), 'MultiException(No exceptions)')
+
+ def testExceptionsRaised(self):
+ fail_exc = Exception('fail!')
+ mb = util.MultiException.Builder()
+ with mb.catch():
+ raise fail_exc
+
+ mexc = mb.get()
+ self.assertEqual(len(mexc), 1)
+ self.assertIs(mexc[0], fail_exc)
+ self.assertEqual(str(mexc), 'MultiException(fail!)')
+
+ def testMultipleExceptions(self):
+ mb = util.MultiException.Builder()
+ with mb.catch():
+ raise KeyError('One')
+ with mb.catch():
+ raise ValueError('Two')
+
+ mexc = mb.get()
+ self.assertIsNotNone(mexc)
+ self.assertEqual(len(mexc), 2)
+
+ exceptions = list(mexc)
+ self.assertIsInstance(exceptions[0], KeyError)
+ self.assertIsInstance(exceptions[1], ValueError)
+ self.assertEqual(str(mexc), "MultiException('One', and 1 more...)")
+
+ def testTargetedException(self):
+ mb = util.MultiException().Builder()
+ def not_caught():
+ with mb.catch(ValueError):
+ raise KeyError('One')
+ self.assertRaises(KeyError, not_caught)
+ self.assertIsNone(mb.get())
+
+
+class TestMapDeferExceptions(unittest.TestCase):
+
+ def testNoExceptionsDoesNothing(self):
+ v = []
+ util.map_defer_exceptions(lambda e: v.append(e), [1, 2, 3])
+ self.assertEqual(v, [1, 2, 3])
+
+ def testCatchesExceptions(self):
+ v = []
+ def fn(e):
+ if e == 0:
+ raise ValueError('Zero')
+ v.append(e)
+
+ mexc = None
+ try:
+ util.map_defer_exceptions(fn, [0, 1, 0, 0, 2, 0, 3, 0])
+ except util.MultiException as e:
+ mexc = e
+
+ self.assertEqual(v, [1, 2, 3])
+ self.assertIsNotNone(mexc)
+ self.assertEqual(len(mexc), 5)
+
+ def testCatchesSpecificExceptions(self):
+ def fn(e):
+ raise ValueError('Zero')
+ self.assertRaises(ValueError, util.map_defer_exceptions, fn, [1], KeyError)
+
+
+if __name__ == '__main__':
+ unittest.main()
« no previous file with comments | « recipe_engine/unittests/stream_test.py ('k') | recipe_engine/util.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698