Permalink
Browse files

__call__ is added to Slacker in order to support Slacker(Foo)(...) sy…

…ntax
  • Loading branch information...
1 parent 06c1d57 commit 851cd1c26a4cb99422d1f5fe1653de5429b9bf4a @kmike committed Apr 29, 2011
Showing with 22 additions and 18 deletions.
  1. +3 −0 slacker/postpone.py
  2. +19 −18 slacker/tests.py
View
@@ -111,6 +111,9 @@ def __init__(self, obj, worker=None):
def __getattr__(self, item):
return getattr(Postponed(self._obj, self._worker), item)
+ def __call__(self, *args, **kwargs):
+ return Postponed(self._obj, self._worker).__call__(*args, **kwargs)
+
def proceed_pickled(pickled_postponed_obj):
"""
View
@@ -1,6 +1,7 @@
+import pickle
from django.utils import unittest
-from slacker.postpone import Postponed
+from slacker.postpone import Postponed, Slacker
class Foo(object):
@@ -23,48 +24,48 @@ def name(self):
self.name_accessed = True
return self._name
+FooSlacker = Slacker(Foo)
class PostponeTest(unittest.TestCase):
def assertRestored(self, chain, value):
self.assertEqual(chain._proceed(), value)
def setUp(self):
- self.foo = Foo('foo')
-
- def _foo(self):
- # PostponeProxy objects shouldn't be reused
- return Postponed(self.foo)
+ self.foo = FooSlacker('foo')
def test_method_basic(self):
- self.assertRestored(self._foo().get_name(), 'foo')
+ self.assertRestored(self.foo.get_name(), 'foo')
def test_method_args(self):
- chain = self._foo().xy(1, 2)
+ chain = self.foo.xy(1, 2)
self.assertRestored(chain, '1-2')
def test_method_kwargs(self):
- self.assertRestored(self._foo().xy(y=2, x=1), '1-2')
+ self.assertRestored(self.foo.xy(y=2, x=1), '1-2')
def test_attributes(self):
- self.assertRestored(self._foo().name, 'foo')
+ self.assertRestored(self.foo.name, 'foo')
def test_slicing(self):
- self.assertRestored(self._foo().name[0], 'f')
+ self.assertRestored(self.foo.name[0], 'f')
def test_chaining(self):
- chain = self._foo().create_bar().bar.create_bar().bar.name[1:-1]
+ chain = self.foo.create_bar().bar.create_bar().bar.name[1:-1]
self.assertRestored(chain, 'oo bar ba')
def test_no_execution(self):
- self._foo().create_bar()
- self.assertFalse(hasattr(self.foo, 'bar'))
+ real_foo = Foo('foo')
+ foo = Postponed(real_foo)
+
+ foo.create_bar()
+ self.assertFalse(hasattr(real_foo, 'bar'))
- self._foo().name
- self.assertFalse(self.foo.name_accessed)
+ foo.name
+ self.assertFalse(real_foo.name_accessed)
- self.foo.name
- self.assertTrue(self.foo.name_accessed)
+ real_foo.name
+ self.assertTrue(real_foo.name_accessed)
def test_top_level_callables(self):
chain = Postponed(Foo)('bar')

0 comments on commit 851cd1c

Please sign in to comment.