Permalink
Browse files

add let().

  • Loading branch information...
1 parent b2622b0 commit a9192989c3c9eb062ce03fae1e634e358785de3c @sumeet committed Apr 23, 2012
Showing with 60 additions and 1 deletion.
  1. +28 −0 test/test_case_test.py
  2. +2 −1 testify/__init__.py
  3. +30 −0 testify/test_case.py
View
@@ -1,8 +1,11 @@
+import itertools
+
from testify import TestCase
from testify import assert_equal
from testify import class_setup
from testify import class_setup_teardown
from testify import class_teardown
+from testify import let
from testify import run
from testify import setup
from testify import setup_teardown
@@ -245,5 +248,30 @@ def test_method_1(self):
def test_method_2(self):
pass
+class LetTest(TestCase):
+
+ @let
+ def counter(self):
+ return itertools.count(0)
+
+ def test_first_call_is_not_cached(self):
+ assert_equal(self.counter.next(), 0)
+
+ def test_subsequent_calls_are_cached(self):
+ assert_equal(self.counter.next(), 0)
+ assert_equal(self.counter.next(), 1)
+
+class LetWithLambdaTest(TestCase):
+
+ counter = let(lambda self: itertools.count(0))
+
+ def test_first_call_is_not_cached(self):
+ assert_equal(self.counter.next(), 0)
+
+ def test_subsequent_calls_are_cached(self):
+ assert_equal(self.counter.next(), 0)
+ assert_equal(self.counter.next(), 1)
+
+
if __name__ == '__main__':
run()
View
@@ -42,7 +42,8 @@
class_teardown,
setup_teardown,
class_setup_teardown,
- suite)
+ suite,
+ let)
from utils import turtle
View
@@ -470,3 +470,33 @@ def fixture_method(func):
setup_teardown = __fixture_decorator_factory('setup_teardown')
class_setup_teardown = __fixture_decorator_factory('class_setup_teardown')
+
+class let(object):
+ """Decorator that creates a lazy-evaluated helper property. The value is
+ cached across multiple calls in the same test, but not across multiple
+ tests.
+ """
+
+ class _unsaved(object): pass
+
+ def __init__(self, func):
+ self._func = func
+ self._result = self._unsaved
+
+ def __get__(self, test_case, cls):
+ if test_case is None:
+ return self
+ if self._result is self._unsaved:
+ self._save_result(self._func(test_case))
+ self._register_reset_after_test_completion(test_case)
+ return self._result
+
+ def _save_result(self, result):
+ self._result = result
+
+ def _register_reset_after_test_completion(self, test_case):
+ test_case.register_callback(TestCase.EVENT_ON_COMPLETE_TEST_METHOD,
+ lambda _: self._reset_value())
+
+ def _reset_value(self):
+ self._result = self._unsaved

0 comments on commit a919298

Please sign in to comment.