[PEAK] Type and class adaptation
Radek Kanovsky
rk at dat.cz
Tue Nov 23 02:48:18 EST 2004
On Mon, Nov 22, 2004 at 08:42:28PM -0500, Phillip J. Eby wrote:
> >------------------------ implementation ---------------------------
>
> May I include the implementation in PEAK/PyProtocols?
Of course, I would be glad. More comprehensive unit test is attached.
RadekK
from unittest import *
from peak.core import *
from dispatch.strategy import Signature, Argument
from wm4.utils.dispatching import IsSubclass
import types, new
class myint (int) : pass
class yourint (int) : pass
class SomeClass (object) : pass
class MyClass (SomeClass) : pass
class YourClass (SomeClass) : pass
class Classic : pass
class MyClassic (Classic) : pass
class YourClassic (Classic) : pass
def mkGM (*class_names) :
[dispatch.generic()]
def gm (arg) :
pass
for cn in class_names :
gm.addMethod(
Signature([(Argument(0), IsSubclass(eval(cn)))]),
new.instancemethod(lambda cn, arg: cn, cn, cn)
)
return gm
NAM = dispatch.NoApplicableMethods
AMB = dispatch.AmbiguousMethod
class TestIsSubclass (TestCase) :
def test10_Empty (self) :
gm = mkGM()
self.assertRaises(NAM, gm, object)
self.assertRaises(NAM, gm, type)
self.assertRaises(NAM, gm, int)
self.assertRaises(NAM, gm, myint)
self.assertRaises(NAM, gm, yourint)
self.assertRaises(NAM, gm, 44)
def test11_IsSubclassOfObject (self) :
gm = mkGM('SomeClass')
self.assertRaises(NAM, gm, object)
self.assertRaises(NAM, gm, int)
self.assertRaises(NAM, gm, types.ClassType)
self.assertRaises(NAM, gm, types.NoneType)
self.assertEqual(gm(SomeClass), 'SomeClass')
self.assertEqual(gm(MyClass), 'SomeClass')
self.assertEqual(gm(YourClass), 'SomeClass')
gm = mkGM('SomeClass', 'MyClass')
self.assertEqual(gm(SomeClass), 'SomeClass')
self.assertEqual(gm(MyClass), 'MyClass')
self.assertEqual(gm(YourClass), 'SomeClass')
def test12_IsSubclassOfBuiltinTypes (self) :
gm = mkGM('int')
self.assertRaises(NAM, gm, object)
self.assertEqual(gm(int), 'int')
self.assertEqual(gm(myint), 'int')
self.assertEqual(gm(yourint), 'int')
self.assertRaises(NAM, gm, None)
self.assertRaises(NAM, gm, 1)
self.assertRaises(NAM, gm, myint(1))
gm = mkGM('int', 'myint')
self.assertRaises(NAM, gm, object)
self.assertEqual(gm(int), 'int')
self.assertEqual(gm(myint), 'myint')
self.assertEqual(gm(yourint), 'int')
self.assertRaises(NAM, gm, None)
self.assertRaises(NAM, gm, 1)
self.assertRaises(NAM, gm, myint(1))
def test13_IsSubclassOfOldClass (self) :
gm = mkGM('Classic')
self.assertRaises(NAM, gm, object)
self.assertRaises(NAM, gm, int)
self.assertRaises(NAM, gm, types.ClassType)
self.assertRaises(NAM, gm, types.NoneType)
self.assertRaises(NAM, gm, SomeClass)
self.assertEqual(gm(Classic), 'Classic')
self.assertEqual(gm(MyClassic), 'Classic')
self.assertEqual(gm(YourClassic), 'Classic')
gm = mkGM('Classic', 'MyClassic')
self.assertEqual(gm(Classic), 'Classic')
self.assertEqual(gm(MyClassic), 'MyClassic')
self.assertEqual(gm(YourClassic), 'Classic')
def test14_CombinedIsSubclass (self) :
gm = mkGM('int', 'SomeClass', 'Classic')
self.assertRaises(NAM, gm, object)
self.assertRaises(NAM, gm, types.ClassType)
self.assertRaises(NAM, gm, types.NoneType)
self.assertEqual(gm(SomeClass), 'SomeClass')
self.assertEqual(gm(MyClass), 'SomeClass')
self.assertEqual(gm(YourClass), 'SomeClass')
self.assertEqual(gm(int), 'int')
self.assertEqual(gm(myint), 'int')
self.assertEqual(gm(yourint), 'int')
self.assertEqual(gm(Classic), 'Classic')
self.assertEqual(gm(MyClassic), 'Classic')
self.assertEqual(gm(YourClassic), 'Classic')
gm = mkGM(
'int', 'myint', 'SomeClass', 'MyClass', 'Classic', 'MyClassic'
)
self.assertRaises(NAM, gm, object)
self.assertRaises(NAM, gm, types.ClassType)
self.assertRaises(NAM, gm, types.NoneType)
self.assertEqual(gm(SomeClass), 'SomeClass')
self.assertEqual(gm(MyClass), 'MyClass')
self.assertEqual(gm(YourClass), 'SomeClass')
self.assertEqual(gm(int), 'int')
self.assertEqual(gm(myint), 'myint')
self.assertEqual(gm(yourint), 'int')
self.assertEqual(gm(Classic), 'Classic')
self.assertEqual(gm(MyClassic), 'MyClassic')
self.assertEqual(gm(YourClassic), 'Classic')
def test15_IsSubclassAndIsInstance (self) :
gm = mkGM('int', 'myint')
[gm.when('arg in int')]
def gm_int (arg) : return 'int %d' % arg
[gm.when('arg in myint')]
def gm_myint (arg) : return 'myint %d' % arg
self.assertRaises(NAM, gm, object)
self.assertRaises(NAM, gm, types.ClassType)
self.assertRaises(NAM, gm, types.NoneType)
self.assertRaises(NAM, gm, None)
self.assertRaises(NAM, gm, SomeClass)
self.assertRaises(NAM, gm, Classic)
self.assertEqual(gm(int), 'int')
self.assertEqual(gm(myint), 'myint')
self.assertEqual(gm(yourint), 'int')
self.assertEqual(gm(4), 'int 4')
self.assertEqual(gm(myint(5)), 'myint 5')
self.assertEqual(gm(yourint(6)), 'int 6')
def test20_AmbiguousMethods (self) :
[dispatch.generic()]
def gm (arg) : pass
[gm.when('arg in IsSubclass(object)')]
def gm_obj (arg) : return 'obj'
[gm.when('arg in type')]
def gm_type (arg) : return 'type'
self.assertRaises(AMB, gm, object)
self.assertRaises(AMB, gm, int)
self.assertRaises(AMB, gm, SomeClass)
self.assertRaises(NAM, gm, Classic)
def test21_AmbiguousMethods (self) :
[dispatch.generic()]
def gm (arg) : pass
[gm.when('arg in IsSubclass(object)')]
def gm_obj1 (arg) : return 'obj1'
[gm.when('arg in IsSubclass(object)')]
def gm_obj2 (arg) : return 'obj2'
self.assertRaises(AMB, gm, object)
self.assertRaises(AMB, gm, int)
self.assertRaises(AMB, gm, SomeClass)
self.assertRaises(NAM, gm, Classic)
def test30_MultipleArguments (self) :
[dispatch.generic()]
def gm (a,b) : pass
[gm.when('a in IsSubclass(int) and b in IsSubclass(SomeClass)')]
def gm1 (a, b) : return 'int SomeClass'
[gm.when('a in IsSubclass(Classic)')]
def gm2 (a, b) : return 'Classic'
[gm.when('a in IsSubclass(MyClassic)')]
def gm3 (a, b) : return 'MyClassic'
self.assertEqual(gm(int, SomeClass), 'int SomeClass')
self.assertEqual(gm(myint, SomeClass), 'int SomeClass')
self.assertEqual(gm(int, MyClass), 'int SomeClass')
self.assertEqual(gm(myint, MyClass), 'int SomeClass')
self.assertEqual(gm(Classic, MyClass), 'Classic')
self.assertEqual(gm(MyClassic, int), 'MyClassic')
self.assertEqual(gm(YourClassic, None), 'Classic')
self.assertRaises(NAM, gm, 1, None)
self.assertRaises(NAM, gm, 1, Classic)
self.assertRaises(NAM, gm, object, int)
self.assertRaises(NAM, gm, int, None)
self.assertRaises(NAM, gm, int, 4)
def suite() :
return TestSuite((
makeSuite(TestIsSubclass),
))
if __name__ == '__main__':
main( defaultTest='suite' )
More information about the PEAK
mailing list