Improve representation of injections

This commit is contained in:
Roman Mogilatov 2015-12-07 14:31:51 +02:00
parent cadcf73362
commit 5bb20a9c2d
2 changed files with 116 additions and 6 deletions

View File

@ -20,6 +20,7 @@ else: # pragma: no cover
_OBJECT_INIT = None _OBJECT_INIT = None
@six.python_2_unicode_compatible
class Injection(object): class Injection(object):
"""Base injection class. """Base injection class.
@ -27,7 +28,7 @@ class Injection(object):
""" """
__IS_INJECTION__ = True __IS_INJECTION__ = True
__slots__ = ('injectable', 'is_provider') __slots__ = ('injectable', 'injectable_is_provider')
def __init__(self, injectable): def __init__(self, injectable):
"""Initializer. """Initializer.
@ -43,7 +44,7 @@ class Injection(object):
:type: object | :py:class:`dependency_injector.providers.Provider` :type: object | :py:class:`dependency_injector.providers.Provider`
""" """
self.is_provider = is_provider(injectable) self.injectable_is_provider = is_provider(injectable)
"""Flag that is set to ``True`` if injectable value is provider. """Flag that is set to ``True`` if injectable value is provider.
:type: bool :type: bool
@ -61,11 +62,37 @@ class Injection(object):
:rtype: object :rtype: object
""" """
if self.is_provider: if self.injectable_is_provider:
return self.injectable() return self.injectable()
return self.injectable return self.injectable
def __str__(self, raw=False):
"""Return string representation of provider.
:param raw: Flag for returning of raw representation string
:type raw: bool
:rtype: str
"""
if self.injectable_is_provider:
injectable_representation = self.injectable.__repr__(True)
else:
injectable_representation = repr(self.injectable)
representation = '{injection}({injectable})'.format(
injection=self.__class__.__name__,
injectable=injectable_representation)
return '<{0}>'.format(representation) if not raw else representation
__repr__ = __str__
class Arg(Injection):
"""Positional argument injection."""
__IS_ARG_INJECTION__ = True
@six.python_2_unicode_compatible
class _NamedInjection(Injection): class _NamedInjection(Injection):
"""Base class of named injections.""" """Base class of named injections."""
@ -76,11 +103,25 @@ class _NamedInjection(Injection):
self.name = name self.name = name
super(_NamedInjection, self).__init__(injectable) super(_NamedInjection, self).__init__(injectable)
def __str__(self, raw=False):
"""Return string representation of provider.
class Arg(Injection): :param raw: Flag for returning of raw representation string
"""Positional argument injection.""" :type raw: bool
__IS_ARG_INJECTION__ = True :rtype: str
"""
if self.injectable_is_provider:
injectable_representation = self.injectable.__repr__(True)
else:
injectable_representation = repr(self.injectable)
representation = '{injection}({name}, {injectable})'.format(
injection=self.__class__.__name__,
name=repr(self.name),
injectable=injectable_representation)
return '<{0}>'.format(representation) if not raw else representation
__repr__ = __str__
class KwArg(_NamedInjection): class KwArg(_NamedInjection):

View File

@ -1,5 +1,6 @@
"""Dependency injector injections unittests.""" """Dependency injector injections unittests."""
import six
import unittest2 as unittest import unittest2 as unittest
from dependency_injector import injections from dependency_injector import injections
@ -37,6 +38,18 @@ class InjectionTests(unittest.TestCase):
self.assertIsInstance(injection.value, TestCatalog.Bundle) self.assertIsInstance(injection.value, TestCatalog.Bundle)
def test_repr_with_scalar_value(self):
"""Test Injection representation with scalar value."""
injection = injections.Injection(123)
self.assertEqual(repr(injection), '<Injection(123)>')
def test_repr_with_provider(self):
"""Test Injection representation with provider."""
injection = injections.Injection(providers.Factory(object))
self.assertEqual(repr(injection),
'<Injection(Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class ArgTests(unittest.TestCase): class ArgTests(unittest.TestCase):
"""Positional arg injection test cases.""" """Positional arg injection test cases."""
@ -46,6 +59,18 @@ class ArgTests(unittest.TestCase):
injection = injections.Arg('some_value') injection = injections.Arg('some_value')
self.assertEqual(injection.injectable, 'some_value') self.assertEqual(injection.injectable, 'some_value')
def test_repr_with_scalar_value(self):
"""Test Arg representation with scalar value."""
injection = injections.Arg(123)
self.assertEqual(repr(injection), '<Arg(123)>')
def test_repr_with_provider(self):
"""Test Arg representation with provider."""
injection = injections.Arg(providers.Factory(object))
self.assertEqual(repr(injection),
'<Arg(Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class KwArgTests(unittest.TestCase): class KwArgTests(unittest.TestCase):
"""Keyword arg injection test cases.""" """Keyword arg injection test cases."""
@ -56,6 +81,20 @@ class KwArgTests(unittest.TestCase):
self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.name, 'some_arg_name')
self.assertEqual(injection.injectable, 'some_value') self.assertEqual(injection.injectable, 'some_value')
def test_repr_with_scalar_value(self):
"""Test KwArg representation with scalar value."""
injection = injections.KwArg('some_arg_name', 123)
self.assertEqual(repr(injection), '<KwArg(\'some_arg_name\', 123)>')
def test_repr_with_provider(self):
"""Test KwArg representation with provider."""
injection = injections.KwArg('some_arg_name',
providers.Factory(object))
self.assertEqual(repr(injection),
'<KwArg(\'some_arg_name\', '
'Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class AttributeTests(unittest.TestCase): class AttributeTests(unittest.TestCase):
"""Attribute injection test cases.""" """Attribute injection test cases."""
@ -66,6 +105,21 @@ class AttributeTests(unittest.TestCase):
self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.name, 'some_arg_name')
self.assertEqual(injection.injectable, 'some_value') self.assertEqual(injection.injectable, 'some_value')
def test_repr_with_scalar_value(self):
"""Test Attribute representation with scalar value."""
injection = injections.Attribute('some_arg_name', 123)
self.assertEqual(repr(injection),
'<Attribute(\'some_arg_name\', 123)>')
def test_repr_with_provider(self):
"""Test Attribute representation with provider."""
injection = injections.Attribute('some_arg_name',
providers.Factory(object))
self.assertEqual(repr(injection),
'<Attribute(\'some_arg_name\', '
'Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class MethodTests(unittest.TestCase): class MethodTests(unittest.TestCase):
"""Method injection test cases.""" """Method injection test cases."""
@ -76,6 +130,21 @@ class MethodTests(unittest.TestCase):
self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.name, 'some_arg_name')
self.assertEqual(injection.injectable, 'some_value') self.assertEqual(injection.injectable, 'some_value')
def test_repr_with_scalar_value(self):
"""Test Method representation with scalar value."""
injection = injections.Method('some_arg_name', 123)
self.assertEqual(repr(injection),
'<Method(\'some_arg_name\', 123)>')
def test_repr_with_provider(self):
"""Test Method representation with provider."""
injection = injections.Method('some_arg_name',
providers.Factory(object))
self.assertEqual(repr(injection),
'<Method(\'some_arg_name\', '
'Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class InjectTests(unittest.TestCase): class InjectTests(unittest.TestCase):
"""Inject decorator test cases.""" """Inject decorator test cases."""