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

Unified Diff: infra/libs/decorators/test/decorators_test.py

Issue 355153002: Refactor infra git libs and testing. (Closed) Base URL: https://chromium.googlesource.com/infra/infra@fake_testing_support
Patch Set: Change config ref to have a sandard naming scheme Created 6 years, 6 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
Index: infra/libs/decorators/test/decorators_test.py
diff --git a/infra/libs/decorators/test/decorators_test.py b/infra/libs/decorators/test/decorators_test.py
new file mode 100644
index 0000000000000000000000000000000000000000..3afa2b2c76aa69b9402982c376622f844c1480f0
--- /dev/null
+++ b/infra/libs/decorators/test/decorators_test.py
@@ -0,0 +1,60 @@
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import unittest
+
+from infra.libs import decorators
+
+
+class TestCachedProperty(unittest.TestCase):
+ def setUp(self):
+ self.calls = calls = []
+ class Foo(object):
+ def __init__(self, success=True, override=None):
+ self.success = success
+ self.override = override
+
+ @decorators.cached_property
+ def happy(self):
+ calls.append(1)
+ if self.override is not None:
+ self._happy = self.override # pylint: disable=W0201
+ if not self.success:
+ raise Exception('nope')
+ return 'days'
+ self.Foo = Foo
+
+ def testBasic(self):
+ f = self.Foo()
+ self.assertEqual(f.happy, 'days')
+ self.assertEqual(f.happy, 'days')
+ self.assertEqual(sum(self.calls), 1)
+
+ def testBareReturnsSelf(self):
+ self.assertIsInstance(self.Foo.happy, decorators.cached_property)
+
+ def testOverride(self):
+ f = self.Foo(override='cowabunga!')
+ self.assertEqual(f.happy, 'cowabunga!')
+ self.assertEqual(f.happy, 'cowabunga!')
+ self.assertEqual(sum(self.calls), 1)
+
+ def testNoCache(self):
+ f = self.Foo(False)
+ with self.assertRaises(Exception):
+ f.happy # pylint: disable=W0104
+ f.success = True
+ self.assertEqual(f.happy, 'days')
+ self.assertEqual(sum(self.calls), 2)
+
+ def testDel(self):
+ f = self.Foo()
+ self.assertEqual(f.happy, 'days')
+ self.assertEqual(f.happy, 'days')
+ del f.happy
+ self.assertEqual(f.happy, 'days')
+ del f.happy
+ del f.happy
+ self.assertEqual(f.happy, 'days')
+ self.assertEqual(sum(self.calls), 3)

Powered by Google App Engine
This is Rietveld 408576698