OLD | NEW |
1 # Copyright (c) 2010 Google Inc. All rights reserved. | 1 # Copyright (c) 2010 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 14 matching lines...) Expand all Loading... |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 |
29 import unittest | 29 import unittest |
30 | 30 |
31 from webkitpy.common.memoized import memoized | 31 from webkitpy.common.memoized import memoized |
32 | 32 |
33 | 33 |
34 class _TestObject(object): | 34 class _TestObject(object): |
| 35 |
35 def __init__(self): | 36 def __init__(self): |
36 self.callCount = 0 | 37 self.callCount = 0 |
37 | 38 |
38 @memoized | 39 @memoized |
39 def memoized_add(self, argument): | 40 def memoized_add(self, argument): |
40 """testing docstring""" | 41 """testing docstring""" |
41 self.callCount += 1 | 42 self.callCount += 1 |
42 if argument is None: | 43 if argument is None: |
43 return None # Avoid the TypeError from None + 1 | 44 return None # Avoid the TypeError from None + 1 |
44 return argument + 1 | 45 return argument + 1 |
45 | 46 |
46 | 47 |
47 class MemoizedTest(unittest.TestCase): | 48 class MemoizedTest(unittest.TestCase): |
| 49 |
48 def test_caching(self): | 50 def test_caching(self): |
49 test = _TestObject() | 51 test = _TestObject() |
50 test.callCount = 0 | 52 test.callCount = 0 |
51 self.assertEqual(test.memoized_add(1), 2) | 53 self.assertEqual(test.memoized_add(1), 2) |
52 self.assertEqual(test.callCount, 1) | 54 self.assertEqual(test.callCount, 1) |
53 self.assertEqual(test.memoized_add(1), 2) | 55 self.assertEqual(test.memoized_add(1), 2) |
54 self.assertEqual(test.callCount, 1) | 56 self.assertEqual(test.callCount, 1) |
55 | 57 |
56 # Validate that callCount is working as expected. | 58 # Validate that callCount is working as expected. |
57 self.assertEqual(test.memoized_add(2), 3) | 59 self.assertEqual(test.memoized_add(2), 3) |
58 self.assertEqual(test.callCount, 2) | 60 self.assertEqual(test.callCount, 2) |
59 | 61 |
60 def test_tearoff(self): | 62 def test_tearoff(self): |
61 test = _TestObject() | 63 test = _TestObject() |
62 # Make sure that get()/tear-offs work: | 64 # Make sure that get()/tear-offs work: |
63 tearoff = test.memoized_add | 65 tearoff = test.memoized_add |
64 self.assertEqual(tearoff(4), 5) | 66 self.assertEqual(tearoff(4), 5) |
65 self.assertEqual(test.callCount, 1) | 67 self.assertEqual(test.callCount, 1) |
OLD | NEW |