test_userlist.py:
Added new test for new __contains__ method. Extensive editing to get rid of asserts.
This commit is contained in:
parent
7b393fc037
commit
f5fa0fc640
|
@ -1,6 +1,13 @@
|
|||
# Check every path through every method of UserList
|
||||
|
||||
from UserList import UserList
|
||||
from test_support import TestFailed
|
||||
|
||||
# Use check instead of assert so -O doesn't render the
|
||||
# test useless.
|
||||
def check(predicate, msg):
|
||||
if not predicate:
|
||||
raise TestFailed(msg + " failed")
|
||||
|
||||
l0 = []
|
||||
l1 = [0]
|
||||
|
@ -31,9 +38,9 @@ vv = UserList("this is also a sequence")
|
|||
|
||||
# Test __repr__
|
||||
|
||||
assert str(u0) == str(l0)
|
||||
assert repr(u1) == repr(l1)
|
||||
assert `u2` == `l2`
|
||||
check(str(u0) == str(l0), "str(u0) == str(l0)")
|
||||
check(repr(u1) == repr(l1), "repr(u1) == repr(l1)")
|
||||
check(`u2` == `l2`, "`u2` == `l2`")
|
||||
|
||||
# Test __cmp__ and __len__
|
||||
|
||||
|
@ -46,12 +53,13 @@ def mycmp(a, b):
|
|||
all = [l0, l1, l2, u, u0, u1, u2, uu, uu0, uu1, uu2]
|
||||
for a in all:
|
||||
for b in all:
|
||||
assert mycmp(a, b) == mycmp(len(a), len(b))
|
||||
check(mycmp(a, b) == mycmp(len(a), len(b)),
|
||||
"mycmp(a, b) == mycmp(len(a), len(b))")
|
||||
|
||||
# Test __getitem__
|
||||
|
||||
for i in range(len(u2)):
|
||||
assert u2[i] == i
|
||||
check(u2[i] == i, "u2[i] == i")
|
||||
|
||||
# Test __setitem__
|
||||
|
||||
|
@ -62,7 +70,7 @@ try:
|
|||
except IndexError:
|
||||
pass
|
||||
else:
|
||||
assert 0, "uu2[2] shouldn't be assignable"
|
||||
raise TestFailed("uu2[2] shouldn't be assignable")
|
||||
|
||||
# Test __delitem__
|
||||
|
||||
|
@ -73,115 +81,120 @@ try:
|
|||
except IndexError:
|
||||
pass
|
||||
else:
|
||||
assert 0, "uu2[0] shouldn't be deletable"
|
||||
raise TestFailed("uu2[0] shouldn't be deletable")
|
||||
|
||||
# Test __getslice__
|
||||
|
||||
for i in range(-3, 4):
|
||||
assert u2[:i] == l2[:i]
|
||||
assert u2[i:] == l2[i:]
|
||||
check(u2[:i] == l2[:i], "u2[:i] == l2[:i]")
|
||||
check(u2[i:] == l2[i:], "u2[i:] == l2[i:]")
|
||||
for j in range(-3, 4):
|
||||
assert u2[i:j] == l2[i:j]
|
||||
check(u2[i:j] == l2[i:j], "u2[i:j] == l2[i:j]")
|
||||
|
||||
# Test __setslice__
|
||||
|
||||
for i in range(-3, 4):
|
||||
u2[:i] = l2[:i]
|
||||
assert u2 == l2
|
||||
check(u2 == l2, "u2 == l2")
|
||||
u2[i:] = l2[i:]
|
||||
assert u2 == l2
|
||||
check(u2 == l2, "u2 == l2")
|
||||
for j in range(-3, 4):
|
||||
u2[i:j] = l2[i:j]
|
||||
assert u2 == l2
|
||||
check(u2 == l2, "u2 == l2")
|
||||
|
||||
uu2 = u2[:]
|
||||
uu2[:0] = [-2, -1]
|
||||
assert uu2 == [-2, -1, 0, 1]
|
||||
check(uu2 == [-2, -1, 0, 1], "uu2 == [-2, -1, 0, 1]")
|
||||
uu2[0:] = []
|
||||
assert uu2 == []
|
||||
check(uu2 == [], "uu2 == []")
|
||||
|
||||
# Test __contains__
|
||||
for i in u2:
|
||||
check(i in u2, "i in u2")
|
||||
for i in min(u2)-1, max(u2)+1:
|
||||
check(i not in u2, "i not in u2")
|
||||
|
||||
# Test __delslice__
|
||||
|
||||
uu2 = u2[:]
|
||||
del uu2[1:2]
|
||||
del uu2[0:1]
|
||||
assert uu2 == []
|
||||
check(uu2 == [], "uu2 == []")
|
||||
|
||||
uu2 = u2[:]
|
||||
del uu2[1:]
|
||||
del uu2[:1]
|
||||
assert uu2 == []
|
||||
check(uu2 == [], "uu2 == []")
|
||||
|
||||
# Test __add__, __radd__, __mul__ and __rmul__
|
||||
|
||||
assert u1 + [] == [] + u1 == u1
|
||||
assert u1 + [1] == u2
|
||||
assert [-1] + u1 == [-1, 0]
|
||||
assert u2 == u2*1 == 1*u2
|
||||
assert u2+u2 == u2*2 == 2*u2
|
||||
assert u2+u2+u2 == u2*3 == 3*u2
|
||||
check(u1 + [] == [] + u1 == u1, "u1 + [] == [] + u1 == u1")
|
||||
check(u1 + [1] == u2, "u1 + [1] == u2")
|
||||
check([-1] + u1 == [-1, 0], "[-1] + u1 == [-1, 0]")
|
||||
check(u2 == u2*1 == 1*u2, "u2 == u2*1 == 1*u2")
|
||||
check(u2+u2 == u2*2 == 2*u2, "u2+u2 == u2*2 == 2*u2")
|
||||
check(u2+u2+u2 == u2*3 == 3*u2, "u2+u2+u2 == u2*3 == 3*u2")
|
||||
|
||||
# Test append
|
||||
|
||||
u = u1[:]
|
||||
u.append(1)
|
||||
assert u == u2
|
||||
check(u == u2, "u == u2")
|
||||
|
||||
# Test insert
|
||||
|
||||
u = u2[:]
|
||||
u.insert(0, -1)
|
||||
assert u == [-1, 0, 1]
|
||||
check(u == [-1, 0, 1], "u == [-1, 0, 1]")
|
||||
|
||||
# Test pop
|
||||
|
||||
u = [-1] + u2
|
||||
u.pop()
|
||||
assert u == [-1, 0]
|
||||
check(u == [-1, 0], "u == [-1, 0]")
|
||||
u.pop(0)
|
||||
assert u == [0]
|
||||
check(u == [0], "u == [0]")
|
||||
|
||||
# Test remove
|
||||
|
||||
u = u2[:]
|
||||
u.remove(1)
|
||||
assert u == u1
|
||||
check(u == u1, "u == u1")
|
||||
|
||||
# Test count
|
||||
u = u2*3
|
||||
assert u.count(0) == 3
|
||||
assert u.count(1) == 3
|
||||
assert u.count(2) == 0
|
||||
check(u.count(0) == 3, "u.count(0) == 3")
|
||||
check(u.count(1) == 3, "u.count(1) == 3")
|
||||
check(u.count(2) == 0, "u.count(2) == 0")
|
||||
|
||||
|
||||
# Test index
|
||||
|
||||
assert u2.index(0) == 0
|
||||
assert u2.index(1) == 1
|
||||
check(u2.index(0) == 0, "u2.index(0) == 0")
|
||||
check(u2.index(1) == 1, "u2.index(1) == 1")
|
||||
try:
|
||||
u2.index(2)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
assert 0, "expected ValueError"
|
||||
raise TestFailed("expected ValueError")
|
||||
|
||||
# Test reverse
|
||||
|
||||
u = u2[:]
|
||||
u.reverse()
|
||||
assert u == [1, 0]
|
||||
check(u == [1, 0], "u == [1, 0]")
|
||||
u.reverse()
|
||||
assert u == u2
|
||||
check(u == u2, "u == u2")
|
||||
|
||||
# Test sort
|
||||
|
||||
u = UserList([1, 0])
|
||||
u.sort()
|
||||
assert u == u2
|
||||
check(u == u2, "u == u2")
|
||||
|
||||
# Test extend
|
||||
|
||||
u = u1[:]
|
||||
u.extend(u2)
|
||||
assert u == u1 + u2
|
||||
|
||||
check(u == u1 + u2, "u == u1 + u2")
|
||||
|
|
Loading…
Reference in New Issue