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

Side by Side Diff: build/android/pylib/utils/decorators_test.py

Issue 2664873002: Add logdog_helper script. (Closed)
Patch Set: Update one use of AddLink to SetLink I missed Created 3 years, 10 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
« no previous file with comments | « build/android/pylib/utils/decorators.py ('k') | build/android/pylib/utils/logdog_helper.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright 2017 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Unit tests for decorators.py."""
7
8 import unittest
9
10 from pylib.utils import decorators
11
12
13 class NoRaiseExceptionDecoratorTest(unittest.TestCase):
14
15 def testFunctionDoesNotRaiseException(self):
16 """Tests that the |NoRaiseException| decorator catches exception."""
17
18 @decorators.NoRaiseException()
19 def raiseException():
20 raise Exception()
21
22 try:
23 raiseException()
24 except Exception: # pylint: disable=broad-except
25 self.fail('Exception was not caught by |NoRaiseException| decorator')
26
27 def testFunctionReturnsCorrectValues(self):
28 """Tests that the |NoRaiseException| decorator returns correct values."""
29
30 @decorators.NoRaiseException(default_return_value=111)
31 def raiseException():
32 raise Exception()
33
34 @decorators.NoRaiseException(default_return_value=111)
35 def doesNotRaiseException():
36 return 999
37
38 self.assertEquals(raiseException(), 111)
39 self.assertEquals(doesNotRaiseException(), 999)
40
41
42 class MemoizeDecoratorTest(unittest.TestCase):
43
44 def testFunctionExceptionNotMemoized(self):
45 """Tests that |Memoize| decorator does not cache exception results."""
46
47 class ExceptionType1(Exception):
48 pass
49
50 class ExceptionType2(Exception):
51 pass
52
53 @decorators.Memoize
54 def raiseExceptions():
55 if raiseExceptions.count == 0:
56 raiseExceptions.count += 1
57 raise ExceptionType1()
58
59 if raiseExceptions.count == 1:
60 raise ExceptionType2()
61 raiseExceptions.count = 0
62
63 with self.assertRaises(ExceptionType1):
64 raiseExceptions()
65 with self.assertRaises(ExceptionType2):
66 raiseExceptions()
67
68 def testFunctionResultMemoized(self):
69 """Tests that |Memoize| decorator caches results."""
70
71 @decorators.Memoize
72 def memoized():
73 memoized.count += 1
74 return memoized.count
75 memoized.count = 0
76
77 def notMemoized():
78 notMemoized.count += 1
79 return notMemoized.count
80 notMemoized.count = 0
81
82 self.assertEquals(memoized(), 1)
83 self.assertEquals(memoized(), 1)
84 self.assertEquals(memoized(), 1)
85
86 self.assertEquals(notMemoized(), 1)
87 self.assertEquals(notMemoized(), 2)
88 self.assertEquals(notMemoized(), 3)
89
90 def testFunctionMemoizedBasedOnArgs(self):
91 """Tests that |Memoize| caches results based on args and kwargs."""
92
93 @decorators.Memoize
94 def returnValueBasedOnArgsKwargs(a, k=0):
95 return a + k
96
97 self.assertEquals(returnValueBasedOnArgsKwargs(1, 1), 2)
98 self.assertEquals(returnValueBasedOnArgsKwargs(1, 2), 3)
99 self.assertEquals(returnValueBasedOnArgsKwargs(2, 1), 3)
100 self.assertEquals(returnValueBasedOnArgsKwargs(3, 3), 6)
101
102
103 if __name__ == '__main__':
104 unittest.main(verbosity=2)
OLDNEW
« no previous file with comments | « build/android/pylib/utils/decorators.py ('k') | build/android/pylib/utils/logdog_helper.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698