[Python-checkins] CVS: python/dist/src/Lib/test test_descr.py,1.10,1.11
Guido van Rossum
gvanrossum@users.sourceforge.net
2001年8月15日 16:58:02 -0700
Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv27447
Modified Files:
test_descr.py
Log Message:
Add tests for overridable operators that have default interpretations
(__hash__ etc.), in static and dynamic classes, overridden and
default.
Index: test_descr.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descr.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** test_descr.py 2001年08月15日 17:51:17 1.10
--- test_descr.py 2001年08月15日 23:57:59 1.11
***************
*** 865,868 ****
--- 865,994 ----
verify(d2.goo() == 2)
+ def specials():
+ # Test operators like __hash__ for which a built-in default exists
+ if verbose: print "testing special operators..."
+ # Test the default behavior for static classes
+ class C(object):
+ def __getitem__(self, i):
+ if 0 <= i < 10: return i
+ raise IndexError
+ c1 = C()
+ c2 = C()
+ verify(not not c1)
+ verify(hash(c1) == id(c1))
+ verify(cmp(c1, c2) == cmp(id(c1), id(c2)))
+ verify(c1 == c1)
+ verify(c1 != c2)
+ verify(not c1 != c1)
+ verify(not c1 == c2)
+ verify(str(c1) == '<C object at 0x%x>' % id(c1))
+ verify(repr(c1) == '<C object at 0x%x>' % id(c1))
+ verify(-1 not in c1)
+ for i in range(10):
+ verify(i in c1)
+ verify(10 not in c1)
+ # Test the default behavior for dynamic classes
+ class D(object):
+ __dynamic__ = 1
+ def __getitem__(self, i):
+ if 0 <= i < 10: return i
+ raise IndexError
+ d1 = D()
+ d2 = D()
+ verify(not not d1)
+ verify(hash(d1) == id(d1))
+ verify(cmp(d1, d2) == cmp(id(d1), id(d2)))
+ verify(d1 == d1)
+ verify(d1 != d2)
+ verify(not d1 != d1)
+ verify(not d1 == d2)
+ verify(str(d1) == '<D object at 0x%x>' % id(d1))
+ verify(repr(d1) == '<D object at 0x%x>' % id(d1))
+ verify(-1 not in d1)
+ for i in range(10):
+ verify(i in d1)
+ verify(10 not in d1)
+ # Test overridden behavior for static classes
+ class Proxy(object):
+ def __init__(self, x):
+ self.x = x
+ def __nonzero__(self):
+ return not not self.x
+ def __hash__(self):
+ return hash(self.x)
+ def __eq__(self, other):
+ return self.x == other
+ def __ne__(self, other):
+ return self.x != other
+ def __cmp__(self, other):
+ return cmp(self.x, other.x)
+ def __str__(self):
+ return "Proxy:%s" % self.x
+ def __repr__(self):
+ return "Proxy(%r)" % self.x
+ def __contains__(self, value):
+ return value in self.x
+ p0 = Proxy(0)
+ p1 = Proxy(1)
+ p_1 = Proxy(-1)
+ verify(not p0)
+ verify(not not p1)
+ verify(hash(p0) == hash(0))
+ verify(p0 == p0)
+ verify(p0 != p1)
+ verify(not p0 != p0)
+ verify(not p0 == p1)
+ verify(cmp(p0, p1) == -1)
+ verify(cmp(p0, p0) == 0)
+ verify(cmp(p0, p_1) == 1)
+ verify(str(p0) == "Proxy:0")
+ verify(repr(p0) == "Proxy(0)")
+ p10 = Proxy(range(10))
+ verify(-1 not in p10)
+ for i in range(10):
+ verify(i in p10)
+ verify(10 not in p10)
+ # Test overridden behavior for dynamic classes
+ class DProxy(object):
+ __dynamic__ = 1
+ def __init__(self, x):
+ self.x = x
+ def __nonzero__(self):
+ return not not self.x
+ def __hash__(self):
+ return hash(self.x)
+ def __eq__(self, other):
+ return self.x == other
+ def __ne__(self, other):
+ return self.x != other
+ def __cmp__(self, other):
+ return cmp(self.x, other.x)
+ def __str__(self):
+ return "DProxy:%s" % self.x
+ def __repr__(self):
+ return "DProxy(%r)" % self.x
+ def __contains__(self, value):
+ return value in self.x
+ p0 = DProxy(0)
+ p1 = DProxy(1)
+ p_1 = DProxy(-1)
+ verify(not p0)
+ verify(not not p1)
+ verify(hash(p0) == hash(0))
+ verify(p0 == p0)
+ verify(p0 != p1)
+ verify(not p0 != p0)
+ verify(not p0 == p1)
+ verify(cmp(p0, p1) == -1)
+ verify(cmp(p0, p0) == 0)
+ verify(cmp(p0, p_1) == 1)
+ verify(str(p0) == "DProxy:0")
+ verify(repr(p0) == "DProxy(0)")
+ p10 = DProxy(range(10))
+ verify(-1 not in p10)
+ for i in range(10):
+ verify(i in p10)
+ verify(10 not in p10)
+
def all():
lists()
***************
*** 892,895 ****
--- 1018,1022 ----
overloading()
methods()
+ specials()
all()