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
def __str__(self, raw=False):
def __str__(self):
"""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
return '<{injection}({injectable}) at {address}>'.format(
injection='.'.join((self.__class__.__module__,
self.__class__.__name__)),
injectable=repr(self.injectable),
address=hex(id(self)))
__repr__ = __str__
@ -103,23 +97,17 @@ class _NamedInjection(Injection):
self.name = name
super(_NamedInjection, self).__init__(injectable)
def __str__(self, raw=False):
def __str__(self):
"""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}({name}, {injectable})'.format(
injection=self.__class__.__name__,
return '<{injection}({name}, {injectable}) at {address}>'.format(
name=repr(self.name),
injectable=injectable_representation)
return '<{0}>'.format(representation) if not raw else representation
injection='.'.join((self.__class__.__module__,
self.__class__.__name__)),
injectable=repr(self.injectable),
address=hex(id(self)))
__repr__ = __str__

View File

@ -1,6 +1,5 @@
"""Dependency injector injections unittests."""
import six
import unittest2 as unittest
from dependency_injector import injections
@ -38,17 +37,15 @@ class InjectionTests(unittest.TestCase):
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__))
def test_repr(self):
"""Test Injection representation."""
provider = providers.Factory(object)
injection = injections.Injection(provider)
self.assertEqual(
repr(injection),
'<dependency_injector.injections.Injection({0}) at {1}>'.format(
repr(provider),
hex(id(injection))))
class ArgTests(unittest.TestCase):
@ -59,17 +56,15 @@ class ArgTests(unittest.TestCase):
injection = injections.Arg('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__))
def test_repr(self):
"""Test Arg representation."""
provider = providers.Factory(object)
injection = injections.Arg(provider)
self.assertEqual(
repr(injection),
'<dependency_injector.injections.Arg({0}) at {1}>'.format(
repr(provider),
hex(id(injection))))
class KwArgTests(unittest.TestCase):
@ -81,19 +76,16 @@ class KwArgTests(unittest.TestCase):
self.assertEqual(injection.name, 'some_arg_name')
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__))
def test_repr(self):
"""Test KwArg representation."""
provider = providers.Factory(object)
injection = injections.KwArg('name', provider)
self.assertEqual(
repr(injection),
'<dependency_injector.injections.KwArg({0}, {1}) at {2}>'.format(
repr('name'),
repr(provider),
hex(id(injection))))
class AttributeTests(unittest.TestCase):
@ -105,20 +97,17 @@ class AttributeTests(unittest.TestCase):
self.assertEqual(injection.name, 'some_arg_name')
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__))
def test_repr(self):
"""Test Attribute representation."""
provider = providers.Factory(object)
injection = injections.Attribute('name', provider)
self.assertEqual(
repr(injection),
'<dependency_injector.injections.Attribute({0}, {1}) '
'at {2}>'.format(
repr('name'),
repr(provider),
hex(id(injection))))
class MethodTests(unittest.TestCase):
@ -130,20 +119,16 @@ class MethodTests(unittest.TestCase):
self.assertEqual(injection.name, 'some_arg_name')
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__))
def test_repr(self):
"""Test Method representation."""
provider = providers.Factory(object)
injection = injections.Method('name', provider)
self.assertEqual(
repr(injection),
'<dependency_injector.injections.Method({0}, {1}) at {2}>'.format(
repr('name'),
repr(provider),
hex(id(injection))))
class InjectTests(unittest.TestCase):