Add check for callable in providers.Callable.__init__

This commit is contained in:
Roman Mogilatov 2015-03-16 11:52:53 +02:00
parent b1f6963fbe
commit 1736a6e008
2 changed files with 19 additions and 4 deletions

View File

@ -256,11 +256,13 @@ class Callable(Provider):
with some predefined dependency injections. with some predefined dependency injections.
""" """
__slots__ = ('calls', 'injections') __slots__ = ('callback', 'injections')
def __init__(self, calls, *injections): def __init__(self, callback, *injections):
"""Initializer.""" """Initializer."""
self.calls = calls if not callable(callback):
raise Error('Callable expected, got {}'.format(str(callback)))
self.callback = callback
self.injections = tuple((injection self.injections = tuple((injection
for injection in injections for injection in injections
if is_injection(injection))) if is_injection(injection)))
@ -275,7 +277,7 @@ class Callable(Provider):
for injection in self.injections)) for injection in self.injections))
injections.update(kwargs) injections.update(kwargs)
return self.calls(*args, **injections) return self.callback(*args, **injections)
class Config(Provider): class Config(Provider):

View File

@ -474,6 +474,10 @@ class CallableTests(unittest.TestCase):
Injection('arg2', 'a2'), Injection('arg2', 'a2'),
Injection('arg3', 'a3')) Injection('arg3', 'a3'))
def test_init_with_not_callable(self):
"""Test creation of provider with not callable."""
self.assertRaises(Error, Callable, 123)
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(Callable(map))) self.assertTrue(is_provider(Callable(map)))
@ -505,3 +509,12 @@ class CallableTests(unittest.TestCase):
self.assertEqual(result1, (3, 2, 1)) self.assertEqual(result1, (3, 2, 1))
self.assertEqual(result2, (3, 2, 1)) self.assertEqual(result2, (3, 2, 1))
class ConfigTests(unittest.TestCase):
"""Config test cases."""
def setUp(self):
"""Set test cases environment up."""
self.provider = Config()