mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-01-11 00:31:56 -06:00
119: Added ProxyTarget.implemented_by() classmethod; added corresponding unit tests
This commit is contained in:
parent
9ac8a8b499
commit
8640523632
@ -135,6 +135,18 @@ class ProxyTarget(ReadOnly):
|
|||||||
"must be str, frozenset, or have frozenset '__public__' attribute"
|
"must be str, frozenset, or have frozenset '__public__' attribute"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def implemented_by(cls, arg):
|
||||||
|
if inspect.isclass(arg):
|
||||||
|
subclass = arg
|
||||||
|
else:
|
||||||
|
subclass = arg.__class__
|
||||||
|
assert issubclass(subclass, cls), 'must be subclass of %r' % cls
|
||||||
|
for name in cls.__public__:
|
||||||
|
if not hasattr(subclass, name):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class Proxy(ReadOnly):
|
class Proxy(ReadOnly):
|
||||||
"""
|
"""
|
||||||
|
@ -183,6 +183,49 @@ class test_ProxyTarget(ClassChecker):
|
|||||||
assert ex.implements(any_object)
|
assert ex.implements(any_object)
|
||||||
assert ex.implements(any_object())
|
assert ex.implements(any_object())
|
||||||
|
|
||||||
|
def test_implemented_by(self):
|
||||||
|
"""
|
||||||
|
Tests the `implemented_by` classmethod.
|
||||||
|
"""
|
||||||
|
class base(self.cls):
|
||||||
|
__public__ = frozenset((
|
||||||
|
'attr0',
|
||||||
|
'attr1',
|
||||||
|
'attr2',
|
||||||
|
))
|
||||||
|
|
||||||
|
class okay(base):
|
||||||
|
def attr0(self):
|
||||||
|
pass
|
||||||
|
def __get_attr1(self):
|
||||||
|
assert False # Make sure property isn't accesed on instance
|
||||||
|
attr1 = property(__get_attr1)
|
||||||
|
attr2 = 'hello world'
|
||||||
|
another_attr = 'whatever'
|
||||||
|
|
||||||
|
class fail(base):
|
||||||
|
def __init__(self):
|
||||||
|
# Check that class, not instance is inspected:
|
||||||
|
self.attr2 = 'hello world'
|
||||||
|
def attr0(self):
|
||||||
|
pass
|
||||||
|
def __get_attr1(self):
|
||||||
|
assert False # Make sure property isn't accesed on instance
|
||||||
|
attr1 = property(__get_attr1)
|
||||||
|
another_attr = 'whatever'
|
||||||
|
|
||||||
|
# Test that AssertionError is raised trying to pass something not
|
||||||
|
# subclass nor instance of base:
|
||||||
|
raises(AssertionError, base.implemented_by, object)
|
||||||
|
|
||||||
|
# Test on subclass with needed attributes:
|
||||||
|
assert base.implemented_by(okay) is True
|
||||||
|
assert base.implemented_by(okay()) is True
|
||||||
|
|
||||||
|
# Test on subclass *without* needed attributes:
|
||||||
|
assert base.implemented_by(fail) is False
|
||||||
|
assert base.implemented_by(fail()) is False
|
||||||
|
|
||||||
|
|
||||||
class test_Proxy(ClassChecker):
|
class test_Proxy(ClassChecker):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user