33: Finished unit tests for plugable.Proxy

This commit is contained in:
Jason Gerard DeRose 2008-08-01 01:47:49 +00:00
parent a131ebf724
commit f53dec2600
2 changed files with 78 additions and 14 deletions

View File

@ -27,10 +27,19 @@ import errors
def to_cli(name): def to_cli(name):
assert isinstance(name, basestring) """
Takes a Python identifier and transforms it into form suitable for the
Command Line Interface.
"""
assert isinstance(name, str)
return name.replace('__', '.').replace('_', '-') return name.replace('__', '.').replace('_', '-')
def from_cli(cli_name): def from_cli(cli_name):
"""
Takes a string from the Command Line Interface and transforms it into a
Python identifier.
"""
assert isinstance(cli_name, basestring) assert isinstance(cli_name, basestring)
return cli_name.replace('-', '_').replace('.', '__') return cli_name.replace('-', '_').replace('.', '__')
@ -69,7 +78,6 @@ class Proxy(object):
__slots__ = ( __slots__ = (
'__obj', '__obj',
'name', 'name',
'cli_name',
) )
def __init__(self, obj, proxy_name=None): def __init__(self, obj, proxy_name=None):
@ -77,11 +85,10 @@ class Proxy(object):
Proxy attributes on `obj`. Proxy attributes on `obj`.
""" """
if proxy_name is None: if proxy_name is None:
proxy_name = obj.name proxy_name = obj.__class__.__name__
assert isinstance(proxy_name, str) assert isinstance(proxy_name, str)
object.__setattr__(self, '_Proxy__obj', obj) object.__setattr__(self, '_Proxy__obj', obj)
object.__setattr__(self, 'name', proxy_name) object.__setattr__(self, 'name', proxy_name)
object.__setattr__(self, 'cli_name', to_cli(proxy_name))
for name in self.__slots__: for name in self.__slots__:
object.__setattr__(self, name, getattr(obj, name)) object.__setattr__(self, name, getattr(obj, name))
@ -107,7 +114,7 @@ class Proxy(object):
return '%s(%r)' % (self.__class__.__name__, self.__obj) return '%s(%r)' % (self.__class__.__name__, self.__obj)
def __str__(self): def __str__(self):
return self.cli_name return to_cli(self.name)
class Registrar(object): class Registrar(object):

View File

@ -55,20 +55,77 @@ def test_Plugin():
def test_Proxy(): def test_Proxy():
class CommandProxy(plugable.Proxy): class CommandProxy(plugable.Proxy):
__slots__ = ( __slots__ = (
'get_label', 'validate',
'__call__', '__call__',
) )
class Command(plugable.Plugin): class do_something(object):
def get_label(self): def __repr__(self):
return 'Add User' return '<my repr>'
def __call__(self, *argv, **kw):
return (argv, kw)
i = Command() def __call__(self, arg):
p = CommandProxy(i, 'hello') return arg + 1
def validate(self, arg):
return arg + 2
def not_public(self, arg):
return arg + 3
# Test basic Proxy functionality
i = do_something()
p = CommandProxy(i)
assert '__dict__' not in dir(p) assert '__dict__' not in dir(p)
#assert repr(p) == 'CommandProxy(%s.Command())' % __name__ assert p.name == 'do_something'
assert str(p) == 'do-something'
assert repr(p) == 'CommandProxy(<my repr>)'
assert p(1) == 2
assert p.validate(1) == 3
# Test that proxy_name can be overriden:
i = do_something()
p = CommandProxy(i, proxy_name='user__add')
assert '__dict__' not in dir(p)
assert p.name == 'user__add'
assert str(p) == 'user.add'
assert repr(p) == 'CommandProxy(<my repr>)'
assert p(1) == 2
assert p.validate(1) == 3
# Test that attributes not listed in __slots__ are not present:
name = 'not_public'
i = do_something()
p = CommandProxy(i)
assert getattr(i, name)(1) == 4
raised = False
try:
getattr(p, name)
except AttributeError:
raised = True
assert raised
# Test that attributes are read-only:
name = 'validate'
i = do_something()
p = CommandProxy(i)
assert getattr(p, name)(1) == 3
raised = False
try:
# Test __setattr__()
setattr(p, name, 'new_object')
except AttributeError:
raised = True
assert raised
raised = False
try:
# Test __delattr__()
delattr(p, name)
except AttributeError:
raised = True
assert raised
def test_Registrar(): def test_Registrar():