OLD | NEW |
(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) |
OLD | NEW |