93: Added Proxy.implements() method; addeded corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-08-09 01:06:42 +00:00
parent 45201e31c1
commit cc5b017494
2 changed files with 113 additions and 81 deletions

View File

@ -102,7 +102,7 @@ class Proxy(ReadOnly):
Allows access to only certain attributes on its target object (a Allows access to only certain attributes on its target object (a
ProxyTarget). ProxyTarget).
Think of a proxy as an argreement that "I will have at most these Think of a proxy as an agreement that "I will have at most these
attributes". This is different from (although similar to) an interface, attributes". This is different from (although similar to) an interface,
which can be thought of as an agreement that "I will have at least these which can be thought of as an agreement that "I will have at least these
attributes". attributes".
@ -136,9 +136,13 @@ class Proxy(ReadOnly):
check_identifier(self.name) check_identifier(self.name)
self.__lock__() self.__lock__()
def implements(self, arg):
return self.__base.implements(arg)
def __iter__(self): def __iter__(self):
""" """
Iterates though the attribute names this proxy is allowing access to. Iterates (in ascending order) though the attribute names this proxy is
allowing access to.
""" """
for name in sorted(self.__public__): for name in sorted(self.__public__):
yield name yield name
@ -176,6 +180,7 @@ class Proxy(ReadOnly):
) )
class Plugin(object): class Plugin(object):
""" """
Base class for all plugins. Base class for all plugins.

View File

@ -28,7 +28,7 @@ from ipalib import plugable, errors
def test_valid_identifier(): def test_valid_identifier():
""" """
Test the plugable.valid_identifier function. Test the `valid_identifier` function.
""" """
f = plugable.check_identifier f = plugable.check_identifier
okay = [ okay = [
@ -172,39 +172,16 @@ class test_ProxyTarget(ClassChecker):
assert ex.implements(any_object()) assert ex.implements(any_object())
def test_Plugin(): class test_Proxy(ClassChecker):
cls = plugable.Plugin """
assert type(cls.name) is property Test the `Proxy` class.
"""
_cls = plugable.Proxy
api = 'the api instance' def test_class(self):
p = plugable.Plugin() assert self.cls.__bases__ == (plugable.ReadOnly,)
assert read_only(p, 'name') == 'Plugin'
assert repr(p) == '%s.Plugin()' % plugable.__name__
assert read_only(p, 'api') is None
raises(AssertionError, p.finalize, None)
p.finalize(api)
assert read_only(p, 'api') is api
raises(AssertionError, p.finalize, api)
class some_plugin(plugable.Plugin):
pass
p = some_plugin()
assert read_only(p, 'name') == 'some_plugin'
assert repr(p) == '%s.some_plugin()' % __name__
assert read_only(p, 'api') is None
raises(AssertionError, p.finalize, None)
p.finalize(api)
assert read_only(p, 'api') is api
raises(AssertionError, p.finalize, api)
def test_Proxy():
cls = plugable.Proxy
assert issubclass(cls, plugable.ReadOnly)
def test_proxy(self):
# Setup: # Setup:
class base(object): class base(object):
__public__ = frozenset(( __public__ = frozenset((
@ -233,14 +210,14 @@ def test_Proxy():
attr_name = 'add' attr_name = 'add'
# Test that TypeError is raised when base is not a class: # Test that TypeError is raised when base is not a class:
raises(TypeError, cls, base(), None) raises(TypeError, self.cls, base(), None)
# Test that ValueError is raised when target is not instance of base: # Test that ValueError is raised when target is not instance of base:
raises(ValueError, cls, base, object()) raises(ValueError, self.cls, base, object())
# Test with correct arguments: # Test with correct arguments:
i = plugin() i = plugin()
p = cls(base, i) p = self.cls(base, i)
assert read_only(p, 'name') == 'user_add' assert read_only(p, 'name') == 'user_add'
assert list(p) == sorted(base.__public__) assert list(p) == sorted(base.__public__)
@ -262,19 +239,69 @@ def test_Proxy():
# Test name_attr='name' kw arg # Test name_attr='name' kw arg
i = plugin() i = plugin()
p = cls(base, i, 'attr_name') p = self.cls(base, i, 'attr_name')
assert read_only(p, 'name') == 'add' assert read_only(p, 'name') == 'add'
# Test _clone(): # Test _clone():
i = plugin() i = plugin()
p = cls(base, i) p = self.cls(base, i)
assert read_only(p, 'name') == 'user_add' assert read_only(p, 'name') == 'user_add'
c = p._clone('attr_name') c = p._clone('attr_name')
assert isinstance(c, cls) assert isinstance(c, self.cls)
assert read_only(c, 'name') == 'add' assert read_only(c, 'name') == 'add'
assert c is not p assert c is not p
assert c('whoever') == p('whoever') assert c('whoever') == p('whoever')
def test_implements(self):
"""
Test the `implements` method.
"""
class base(object):
__public__ = frozenset()
name = 'base'
@classmethod
def implements(cls, arg):
return arg + 7
class sub(base):
@classmethod
def implements(cls, arg):
"""
Defined to make sure base.implements() is called, not
target.implements()
"""
return arg
o = sub()
p = self.cls(base, o)
assert p.implements(3) == 10
def test_Plugin():
cls = plugable.Plugin
assert type(cls.name) is property
api = 'the api instance'
p = plugable.Plugin()
assert read_only(p, 'name') == 'Plugin'
assert repr(p) == '%s.Plugin()' % plugable.__name__
assert read_only(p, 'api') is None
raises(AssertionError, p.finalize, None)
p.finalize(api)
assert read_only(p, 'api') is api
raises(AssertionError, p.finalize, api)
class some_plugin(plugable.Plugin):
pass
p = some_plugin()
assert read_only(p, 'name') == 'some_plugin'
assert repr(p) == '%s.some_plugin()' % __name__
assert read_only(p, 'api') is None
raises(AssertionError, p.finalize, None)
p.finalize(api)
assert read_only(p, 'api') is api
raises(AssertionError, p.finalize, api)
def test_NameSpace(): def test_NameSpace():
cls = plugable.NameSpace cls = plugable.NameSpace