def testAttributes(self): class IZoo(Interface): zoo = Attribute(int) class I(Interface): foo = Attribute(int) bar = Attribute(str) foobar = Attribute(int, None) a_zoo = Attribute(IZoo) class Zoo(object): ImplementsInterface(IZoo) # NOTE: This class 'C' doesn't REALLY implements 'I', although it says so. The problem is # that there's a flaw with attributes *not being checked*. # In fact: Attributes should not be in the (Abstract) properties COULD be in # the interface, but they SHOULD NOT be type-checked (because it involves a # getter call, and this affects runtime behaviour). # This should be reviewed later. class C(object): ImplementsInterface(I) c1 = C() c1.foo = 10 c1.bar = 'hello' c1.foobar = 20 a_zoo = Zoo() a_zoo.zoo = 99 c1.a_zoo = a_zoo c2 = C() assert IsImplementation(C, I) == True assert IsImplementation(c1, I) == True assert IsImplementation(c2, I) == True # NOTE: Testing private methods here # If we do a deprecated "full check", then its behaviour is a little bit more correct. from ben10.interface._interface import _IsImplementationFullChecking assert not _IsImplementationFullChecking(C, I) == True # only works with instances assert _IsImplementationFullChecking(c1, I) == True # OK, has attributes assert not _IsImplementationFullChecking(c2, I) == True # not all the attributes necessary # must not be true if including an object that doesn't implement IZoo interface expected for # a_zoo attribute c1.a_zoo = 'wrong' assert not _IsImplementationFullChecking(c1, I) == True # failed, invalid attr type c1.a_zoo = a_zoo # test if we can set foobar to None c1.foobar = None assert IsImplementation(c1, I) == True # OK c1.foobar = 'hello' assert not _IsImplementationFullChecking(c1, I) == True # failed, invalid attr type
def testClassImplementMethod(self): ''' Tests replacing a method that implements an interface with a class. The class must be derived from "Method" in order to be accepted as a valid implementation. ''' class My(object): ImplementsInterface(_InterfM1) def m1(self): '' class MyRightMethod(Method): def __call__(self): '' class MyWrongMethod(object): def __call__(self): '' # NOTE: It doesn't matter runtime modifications in the instance, what is really being tested # is the *class* of the object (My) is what is really being tested. m = My() m.m1 = MyWrongMethod() assert IsImplementation(m, _InterfM1) == True m.m1 = MyRightMethod() assert IsImplementation(m, _InterfM1) == True # NOTE: Testing behaviour of private methods here. from ben10.interface._interface import _IsImplementationFullChecking m = My() m.m1 = MyWrongMethod() r = _IsImplementationFullChecking(m, _InterfM1) assert r == False m.m1 = MyRightMethod() r = _IsImplementationFullChecking(m, _InterfM1) assert r == True del m.m1 assert IsImplementation(m, _InterfM1) == True
def testAttributes(self): class IZoo(Interface): zoo = Attribute(int) class I(Interface): foo = Attribute(int) bar = Attribute(str) foobar = Attribute(int, None) a_zoo = Attribute(IZoo) class Zoo(object): ImplementsInterface(IZoo) # NOTE: This class 'C' doesn't REALLY implements 'I', although it says so. The problem is # that there's a flaw with attributes *not being checked*. # In fact: Attributes should not be in the (Abstract) properties COULD be in # the interface, but they SHOULD NOT be type-checked (because it involves a # getter call, and this affects runtime behaviour). # This should be reviewed later. class C(object): ImplementsInterface(I) c1 = C() c1.foo = 10 c1.bar = 'hello' c1.foobar = 20 a_zoo = Zoo() a_zoo.zoo = 99 c1.a_zoo = a_zoo c2 = C() assert IsImplementation(C, I) == True assert IsImplementation(c1, I) == True assert IsImplementation(c2, I) == True # NOTE: Testing private methods here # If we do a deprecated "full check", then its behaviour is a little bit more correct. from ben10.interface._interface import _IsImplementationFullChecking assert not _IsImplementationFullChecking( C, I) == True # only works with instances assert _IsImplementationFullChecking(c1, I) == True # OK, has attributes assert not _IsImplementationFullChecking( c2, I) == True # not all the attributes necessary # must not be true if including an object that doesn't implement IZoo interface expected for # a_zoo attribute c1.a_zoo = 'wrong' assert not _IsImplementationFullChecking( c1, I) == True # failed, invalid attr type c1.a_zoo = a_zoo # test if we can set foobar to None c1.foobar = None assert IsImplementation(c1, I) == True # OK c1.foobar = 'hello' assert not _IsImplementationFullChecking( c1, I) == True # failed, invalid attr type