Update representation of injections

This commit is contained in:
Roman Mogilatov 2015-12-09 19:28:52 +02:00
parent 5bb20a9c2d
commit 9ef7a961f6
2 changed files with 61 additions and 88 deletions

View File

@ -66,22 +66,16 @@ class Injection(object):
return self.injectable() return self.injectable()
return self.injectable return self.injectable
def __str__(self, raw=False): def __str__(self):
"""Return string representation of provider. """Return string representation of provider.
:param raw: Flag for returning of raw representation string
:type raw: bool
:rtype: str :rtype: str
""" """
if self.injectable_is_provider: return '<{injection}({injectable}) at {address}>'.format(
injectable_representation = self.injectable.__repr__(True) injection='.'.join((self.__class__.__module__,
else: self.__class__.__name__)),
injectable_representation = repr(self.injectable) injectable=repr(self.injectable),
representation = '{injection}({injectable})'.format( address=hex(id(self)))
injection=self.__class__.__name__,
injectable=injectable_representation)
return '<{0}>'.format(representation) if not raw else representation
__repr__ = __str__ __repr__ = __str__
@ -103,23 +97,17 @@ class _NamedInjection(Injection):
self.name = name self.name = name
super(_NamedInjection, self).__init__(injectable) super(_NamedInjection, self).__init__(injectable)
def __str__(self, raw=False): def __str__(self):
"""Return string representation of provider. """Return string representation of provider.
:param raw: Flag for returning of raw representation string
:type raw: bool
:rtype: str :rtype: str
""" """
if self.injectable_is_provider: return '<{injection}({name}, {injectable}) at {address}>'.format(
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), name=repr(self.name),
injectable=injectable_representation) injection='.'.join((self.__class__.__module__,
return '<{0}>'.format(representation) if not raw else representation self.__class__.__name__)),
injectable=repr(self.injectable),
address=hex(id(self)))
__repr__ = __str__ __repr__ = __str__

View File

@ -1,6 +1,5 @@
"""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
@ -38,17 +37,15 @@ class InjectionTests(unittest.TestCase):
self.assertIsInstance(injection.value, TestCatalog.Bundle) self.assertIsInstance(injection.value, TestCatalog.Bundle)
def test_repr_with_scalar_value(self): def test_repr(self):
"""Test Injection representation with scalar value.""" """Test Injection representation."""
injection = injections.Injection(123) provider = providers.Factory(object)
self.assertEqual(repr(injection), '<Injection(123)>') injection = injections.Injection(provider)
self.assertEqual(
def test_repr_with_provider(self): repr(injection),
"""Test Injection representation with provider.""" '<dependency_injector.injections.Injection({0}) at {1}>'.format(
injection = injections.Injection(providers.Factory(object)) repr(provider),
self.assertEqual(repr(injection), hex(id(injection))))
'<Injection(Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class ArgTests(unittest.TestCase): class ArgTests(unittest.TestCase):
@ -59,17 +56,15 @@ 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): def test_repr(self):
"""Test Arg representation with scalar value.""" """Test Arg representation."""
injection = injections.Arg(123) provider = providers.Factory(object)
self.assertEqual(repr(injection), '<Arg(123)>') injection = injections.Arg(provider)
self.assertEqual(
def test_repr_with_provider(self): repr(injection),
"""Test Arg representation with provider.""" '<dependency_injector.injections.Arg({0}) at {1}>'.format(
injection = injections.Arg(providers.Factory(object)) repr(provider),
self.assertEqual(repr(injection), hex(id(injection))))
'<Arg(Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class KwArgTests(unittest.TestCase): class KwArgTests(unittest.TestCase):
@ -81,19 +76,16 @@ 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): def test_repr(self):
"""Test KwArg representation with scalar value.""" """Test KwArg representation."""
injection = injections.KwArg('some_arg_name', 123) provider = providers.Factory(object)
self.assertEqual(repr(injection), '<KwArg(\'some_arg_name\', 123)>') injection = injections.KwArg('name', provider)
self.assertEqual(
def test_repr_with_provider(self): repr(injection),
"""Test KwArg representation with provider.""" '<dependency_injector.injections.KwArg({0}, {1}) at {2}>'.format(
injection = injections.KwArg('some_arg_name', repr('name'),
providers.Factory(object)) repr(provider),
self.assertEqual(repr(injection), hex(id(injection))))
'<KwArg(\'some_arg_name\', '
'Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class AttributeTests(unittest.TestCase): class AttributeTests(unittest.TestCase):
@ -105,20 +97,17 @@ 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): def test_repr(self):
"""Test Attribute representation with scalar value.""" """Test Attribute representation."""
injection = injections.Attribute('some_arg_name', 123) provider = providers.Factory(object)
self.assertEqual(repr(injection), injection = injections.Attribute('name', provider)
'<Attribute(\'some_arg_name\', 123)>') self.assertEqual(
repr(injection),
def test_repr_with_provider(self): '<dependency_injector.injections.Attribute({0}, {1}) '
"""Test Attribute representation with provider.""" 'at {2}>'.format(
injection = injections.Attribute('some_arg_name', repr('name'),
providers.Factory(object)) repr(provider),
self.assertEqual(repr(injection), hex(id(injection))))
'<Attribute(\'some_arg_name\', '
'Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class MethodTests(unittest.TestCase): class MethodTests(unittest.TestCase):
@ -130,20 +119,16 @@ 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): def test_repr(self):
"""Test Method representation with scalar value.""" """Test Method representation."""
injection = injections.Method('some_arg_name', 123) provider = providers.Factory(object)
self.assertEqual(repr(injection), injection = injections.Method('name', provider)
'<Method(\'some_arg_name\', 123)>') self.assertEqual(
repr(injection),
def test_repr_with_provider(self): '<dependency_injector.injections.Method({0}, {1}) at {2}>'.format(
"""Test Method representation with provider.""" repr('name'),
injection = injections.Method('some_arg_name', repr(provider),
providers.Factory(object)) hex(id(injection))))
self.assertEqual(repr(injection),
'<Method(\'some_arg_name\', '
'Factory({0}.object))>'.format(
six.moves.builtins.__name__))
class InjectTests(unittest.TestCase): class InjectTests(unittest.TestCase):