Port test_bool.py to PyUnit. From SF patch #662807.

This commit is contained in:
Walter Dörwald 2003-04-25 10:22:01 +00:00
parent 74bfd70e92
commit dbcede5d66
1 changed files with 295 additions and 220 deletions

View File

@ -1,8 +1,12 @@
# Test properties of bool promised by PEP 285 # Test properties of bool promised by PEP 285
from test.test_support import verbose, TestFailed, TESTFN, vereq, have_unicode import unittest
from test import test_support
import os import os
from test.test_support import verbose, TestFailed, TESTFN, vereq, have_unicode
def veris(a, b): def veris(a, b):
if a is not b: if a is not b:
raise TestFailed, "%r is %r" % (a, b) raise TestFailed, "%r is %r" % (a, b)
@ -11,255 +15,326 @@ def verisnot(a, b):
if a is b: if a is b:
raise TestFailed, "%r is %r" % (a, b) raise TestFailed, "%r is %r" % (a, b)
try: class BoolTest(unittest.TestCase):
def assertIs(self, a, b):
self.assert_(a is b)
def assertIsNot(self, a, b):
self.assert_(a is not b)
def test_subclass(self):
try:
class C(bool): class C(bool):
pass pass
except TypeError: except TypeError:
pass pass
else: else:
raise TestFailed, "bool should not be subclassable" self.fail("bool should not be subclassable")
try: self.assertRaises(TypeError, int.__new__, bool, 0)
int.__new__(bool, 0)
except TypeError:
pass
else:
raise TestFailed, "should not be able to create new bool instances"
# checking tp_print slot def test_print(self):
fo = open(TESTFN, "wb") try:
print >> fo, False, True fo = open(test_support.TESTFN, "wb")
fo.close() print >> fo, False, True
fo = open(TESTFN, "rb") fo.close()
vereq(fo.read(), 'False True\n') fo = open(test_support.TESTFN, "rb")
fo.close() self.assertEqual(fo.read(), 'False True\n')
os.remove(TESTFN) finally:
fo.close()
os.remove(test_support.TESTFN)
# checking repr and str def test_repr(self):
vereq(str(False), 'False') self.assertEqual(repr(False), 'False')
vereq(str(True), 'True') self.assertEqual(repr(True), 'True')
vereq(repr(False), 'False') self.assertEqual(eval(repr(False)), False)
vereq(repr(True), 'True') self.assertEqual(eval(repr(True)), True)
vereq(eval(repr(False)), False)
vereq(eval(repr(True)), True)
vereq(int(False), 0) def test_str(self):
verisnot(int(False), False) self.assertEqual(str(False), 'False')
vereq(int(True), 1) self.assertEqual(str(True), 'True')
verisnot(int(True), True)
vereq(+False, 0) def test_int(self):
verisnot(+False, False) self.assertEqual(int(False), 0)
vereq(-False, 0) self.assertIsNot(int(False), False)
verisnot(-False, False) self.assertEqual(int(True), 1)
vereq(abs(False), 0) self.assertIsNot(int(True), True)
verisnot(abs(False), False)
vereq(+True, 1)
verisnot(+True, True)
vereq(-True, -1)
vereq(abs(True), 1)
verisnot(abs(True), True)
vereq(~False, -1)
vereq(~True, -2)
vereq(False+2, 2) def test_math(self):
vereq(True+2, 3) self.assertEqual(+False, 0)
vereq(2+False, 2) self.assertIsNot(+False, False)
vereq(2+True, 3) self.assertEqual(-False, 0)
self.assertIsNot(-False, False)
self.assertEqual(abs(False), 0)
self.assertIsNot(abs(False), False)
self.assertEqual(+True, 1)
self.assertIsNot(+True, True)
self.assertEqual(-True, -1)
self.assertEqual(abs(True), 1)
self.assertIsNot(abs(True), True)
self.assertEqual(~False, -1)
self.assertEqual(~True, -2)
vereq(False+False, 0) self.assertEqual(False+2, 2)
verisnot(False+False, False) self.assertEqual(True+2, 3)
vereq(False+True, 1) self.assertEqual(2+False, 2)
verisnot(False+True, True) self.assertEqual(2+True, 3)
vereq(True+False, 1)
verisnot(True+False, True)
vereq(True+True, 2)
vereq(True-True, 0) self.assertEqual(False+False, 0)
verisnot(True-True, False) self.assertIsNot(False+False, False)
vereq(False-False, 0) self.assertEqual(False+True, 1)
verisnot(False-False, False) self.assertIsNot(False+True, True)
vereq(True-False, 1) self.assertEqual(True+False, 1)
verisnot(True-False, True) self.assertIsNot(True+False, True)
vereq(False-True, -1) self.assertEqual(True+True, 2)
vereq(True*1, 1) self.assertEqual(True-True, 0)
vereq(False*1, 0) self.assertIsNot(True-True, False)
verisnot(False*1, False) self.assertEqual(False-False, 0)
self.assertIsNot(False-False, False)
self.assertEqual(True-False, 1)
self.assertIsNot(True-False, True)
self.assertEqual(False-True, -1)
vereq(True/1, 1) self.assertEqual(True*1, 1)
verisnot(True/1, True) self.assertEqual(False*1, 0)
vereq(False/1, 0) self.assertIsNot(False*1, False)
verisnot(False/1, False)
for b in False, True: self.assertEqual(True/1, 1)
for i in 0, 1, 2: self.assertIsNot(True/1, True)
vereq(b**i, int(b)**i) self.assertEqual(False/1, 0)
verisnot(b**i, bool(int(b)**i)) self.assertIsNot(False/1, False)
for a in False, True:
for b in False, True: for b in False, True:
veris(a&b, bool(int(a)&int(b))) for i in 0, 1, 2:
veris(a|b, bool(int(a)|int(b))) self.assertEqual(b**i, int(b)**i)
veris(a^b, bool(int(a)^int(b))) self.assertIsNot(b**i, bool(int(b)**i))
vereq(a&int(b), int(a)&int(b))
verisnot(a&int(b), bool(int(a)&int(b)))
vereq(a|int(b), int(a)|int(b))
verisnot(a|int(b), bool(int(a)|int(b)))
vereq(a^int(b), int(a)^int(b))
verisnot(a^int(b), bool(int(a)^int(b)))
vereq(int(a)&b, int(a)&int(b))
verisnot(int(a)&b, bool(int(a)&int(b)))
vereq(int(a)|b, int(a)|int(b))
verisnot(int(a)|b, bool(int(a)|int(b)))
vereq(int(a)^b, int(a)^int(b))
verisnot(int(a)^b, bool(int(a)^int(b)))
veris(1==1, True) for a in False, True:
veris(1==0, False) for b in False, True:
# XXX <, <=, >, >=, != self.assertIs(a&b, bool(int(a)&int(b)))
self.assertIs(a|b, bool(int(a)|int(b)))
self.assertIs(a^b, bool(int(a)^int(b)))
self.assertEqual(a&int(b), int(a)&int(b))
self.assertIsNot(a&int(b), bool(int(a)&int(b)))
self.assertEqual(a|int(b), int(a)|int(b))
self.assertIsNot(a|int(b), bool(int(a)|int(b)))
self.assertEqual(a^int(b), int(a)^int(b))
self.assertIsNot(a^int(b), bool(int(a)^int(b)))
self.assertEqual(int(a)&b, int(a)&int(b))
self.assertIsNot(int(a)&b, bool(int(a)&int(b)))
self.assertEqual(int(a)|b, int(a)|int(b))
self.assertIsNot(int(a)|b, bool(int(a)|int(b)))
self.assertEqual(int(a)^b, int(a)^int(b))
self.assertIsNot(int(a)^b, bool(int(a)^int(b)))
x = [1] self.assertIs(1==1, True)
veris(x is x, True) self.assertIs(1==0, False)
veris(x is not x, False) self.assertIs(0<1, True)
self.assertIs(1<0, False)
self.assertIs(0<=0, True)
self.assertIs(1<=0, False)
self.assertIs(1>0, True)
self.assertIs(1>1, False)
self.assertIs(1>=1, True)
self.assertIs(0>=1, False)
self.assertIs(0!=1, True)
self.assertIs(0!=0, False)
veris(1 in x, True) x = [1]
veris(0 in x, False) self.assertIs(x is x, True)
veris(1 not in x, False) self.assertIs(x is not x, False)
veris(0 not in x, True)
veris(not True, False) self.assertIs(1 in x, True)
veris(not False, True) self.assertIs(0 in x, False)
self.assertIs(1 not in x, False)
self.assertIs(0 not in x, True)
veris(bool(10), True) x = {1: 2}
veris(bool(1), True) self.assertIs(x is x, True)
veris(bool(-1), True) self.assertIs(x is not x, False)
veris(bool(0), False)
veris(bool("hello"), True)
veris(bool(""), False)
veris(bool(), False)
veris(hasattr([], "append"), True) self.assertIs(1 in x, True)
veris(hasattr([], "wobble"), False) self.assertIs(0 in x, False)
self.assertIs(1 not in x, False)
self.assertIs(0 not in x, True)
veris(callable(len), True) self.assertIs(not True, False)
veris(callable(1), False) self.assertIs(not False, True)
veris(isinstance(True, bool), True) def test_convert(self):
veris(isinstance(False, bool), True) self.assertRaises(TypeError, bool, 42, 42)
veris(isinstance(True, int), True) self.assertIs(bool(10), True)
veris(isinstance(False, int), True) self.assertIs(bool(1), True)
veris(isinstance(1, bool), False) self.assertIs(bool(-1), True)
veris(isinstance(0, bool), False) self.assertIs(bool(0), False)
self.assertIs(bool("hello"), True)
self.assertIs(bool(""), False)
self.assertIs(bool(), False)
veris(issubclass(bool, int), True) def test_hasattr(self):
veris(issubclass(int, bool), False) self.assertIs(hasattr([], "append"), True)
self.assertIs(hasattr([], "wobble"), False)
veris({}.has_key(1), False) def test_callable(self):
veris({1:1}.has_key(1), True) self.assertIs(callable(len), True)
self.assertIs(callable(1), False)
veris("xyz".endswith("z"), True) def test_isinstance(self):
veris("xyz".endswith("x"), False) self.assertIs(isinstance(True, bool), True)
veris("xyz0123".isalnum(), True) self.assertIs(isinstance(False, bool), True)
veris("@#$%".isalnum(), False) self.assertIs(isinstance(True, int), True)
veris("xyz".isalpha(), True) self.assertIs(isinstance(False, int), True)
veris("@#$%".isalpha(), False) self.assertIs(isinstance(1, bool), False)
veris("0123".isdigit(), True) self.assertIs(isinstance(0, bool), False)
veris("xyz".isdigit(), False)
veris("xyz".islower(), True)
veris("XYZ".islower(), False)
veris(" ".isspace(), True)
veris("XYZ".isspace(), False)
veris("X".istitle(), True)
veris("x".istitle(), False)
veris("XYZ".isupper(), True)
veris("xyz".isupper(), False)
veris("xyz".startswith("x"), True)
veris("xyz".startswith("z"), False)
if have_unicode: def test_issubclass(self):
veris(unicode("xyz", 'ascii').endswith(unicode("z", 'ascii')), True) self.assertIs(issubclass(bool, int), True)
veris(unicode("xyz", 'ascii').endswith(unicode("x", 'ascii')), False) self.assertIs(issubclass(int, bool), False)
veris(unicode("xyz0123", 'ascii').isalnum(), True)
veris(unicode("@#$%", 'ascii').isalnum(), False)
veris(unicode("xyz", 'ascii').isalpha(), True)
veris(unicode("@#$%", 'ascii').isalpha(), False)
veris(unicode("0123", 'ascii').isdecimal(), True)
veris(unicode("xyz", 'ascii').isdecimal(), False)
veris(unicode("0123", 'ascii').isdigit(), True)
veris(unicode("xyz", 'ascii').isdigit(), False)
veris(unicode("xyz", 'ascii').islower(), True)
veris(unicode("XYZ", 'ascii').islower(), False)
veris(unicode("0123", 'ascii').isnumeric(), True)
veris(unicode("xyz", 'ascii').isnumeric(), False)
veris(unicode(" ", 'ascii').isspace(), True)
veris(unicode("XYZ", 'ascii').isspace(), False)
veris(unicode("X", 'ascii').istitle(), True)
veris(unicode("x", 'ascii').istitle(), False)
veris(unicode("XYZ", 'ascii').isupper(), True)
veris(unicode("xyz", 'ascii').isupper(), False)
veris(unicode("xyz", 'ascii').startswith(unicode("x", 'ascii')), True)
veris(unicode("xyz", 'ascii').startswith(unicode("z", 'ascii')), False)
f = file(TESTFN, "w") def test_haskey(self):
veris(f.closed, False) self.assertIs({}.has_key(1), False)
f.close() self.assertIs({1:1}.has_key(1), True)
veris(f.closed, True)
os.remove(TESTFN)
import operator def test_string(self):
veris(operator.truth(0), False) self.assertIs("xyz".endswith("z"), True)
veris(operator.truth(1), True) self.assertIs("xyz".endswith("x"), False)
veris(operator.isCallable(0), False) self.assertIs("xyz0123".isalnum(), True)
veris(operator.isCallable(len), True) self.assertIs("@#$%".isalnum(), False)
veris(operator.isNumberType(None), False) self.assertIs("xyz".isalpha(), True)
veris(operator.isNumberType(0), True) self.assertIs("@#$%".isalpha(), False)
veris(operator.not_(1), False) self.assertIs("0123".isdigit(), True)
veris(operator.not_(0), True) self.assertIs("xyz".isdigit(), False)
veris(operator.isSequenceType(0), False) self.assertIs("xyz".islower(), True)
veris(operator.isSequenceType([]), True) self.assertIs("XYZ".islower(), False)
veris(operator.contains([], 1), False) self.assertIs(" ".isspace(), True)
veris(operator.contains([1], 1), True) self.assertIs("XYZ".isspace(), False)
veris(operator.isMappingType(1), False) self.assertIs("X".istitle(), True)
veris(operator.isMappingType({}), True) self.assertIs("x".istitle(), False)
veris(operator.lt(0, 0), False) self.assertIs("XYZ".isupper(), True)
veris(operator.lt(0, 1), True) self.assertIs("xyz".isupper(), False)
self.assertIs("xyz".startswith("x"), True)
self.assertIs("xyz".startswith("z"), False)
import marshal if have_unicode:
veris(marshal.loads(marshal.dumps(True)), True) self.assertIs(unicode("xyz", 'ascii').endswith(unicode("z", 'ascii')), True)
veris(marshal.loads(marshal.dumps(False)), False) self.assertIs(unicode("xyz", 'ascii').endswith(unicode("x", 'ascii')), False)
self.assertIs(unicode("xyz0123", 'ascii').isalnum(), True)
self.assertIs(unicode("@#$%", 'ascii').isalnum(), False)
self.assertIs(unicode("xyz", 'ascii').isalpha(), True)
self.assertIs(unicode("@#$%", 'ascii').isalpha(), False)
self.assertIs(unicode("0123", 'ascii').isdecimal(), True)
self.assertIs(unicode("xyz", 'ascii').isdecimal(), False)
self.assertIs(unicode("0123", 'ascii').isdigit(), True)
self.assertIs(unicode("xyz", 'ascii').isdigit(), False)
self.assertIs(unicode("xyz", 'ascii').islower(), True)
self.assertIs(unicode("XYZ", 'ascii').islower(), False)
self.assertIs(unicode("0123", 'ascii').isnumeric(), True)
self.assertIs(unicode("xyz", 'ascii').isnumeric(), False)
self.assertIs(unicode(" ", 'ascii').isspace(), True)
self.assertIs(unicode("XYZ", 'ascii').isspace(), False)
self.assertIs(unicode("X", 'ascii').istitle(), True)
self.assertIs(unicode("x", 'ascii').istitle(), False)
self.assertIs(unicode("XYZ", 'ascii').isupper(), True)
self.assertIs(unicode("xyz", 'ascii').isupper(), False)
self.assertIs(unicode("xyz", 'ascii').startswith(unicode("x", 'ascii')), True)
self.assertIs(unicode("xyz", 'ascii').startswith(unicode("z", 'ascii')), False)
import pickle def test_boolean(self):
veris(pickle.loads(pickle.dumps(True)), True) self.assertEqual(True & 1, 1)
veris(pickle.loads(pickle.dumps(False)), False) self.assert_(not isinstance(True & 1, bool))
veris(pickle.loads(pickle.dumps(True, True)), True) self.assertIs(True & True, True)
veris(pickle.loads(pickle.dumps(False, True)), False)
import cPickle self.assertEqual(True | 1, 1)
veris(cPickle.loads(cPickle.dumps(True)), True) self.assert_(not isinstance(True | 1, bool))
veris(cPickle.loads(cPickle.dumps(False)), False) self.assertIs(True | True, True)
veris(cPickle.loads(cPickle.dumps(True, True)), True)
veris(cPickle.loads(cPickle.dumps(False, True)), False)
veris(pickle.loads(cPickle.dumps(True)), True) self.assertEqual(True ^ 1, 0)
veris(pickle.loads(cPickle.dumps(False)), False) self.assert_(not isinstance(True ^ 1, bool))
veris(pickle.loads(cPickle.dumps(True, True)), True) self.assertIs(True ^ True, False)
veris(pickle.loads(cPickle.dumps(False, True)), False)
veris(cPickle.loads(pickle.dumps(True)), True) def test_fileclosed(self):
veris(cPickle.loads(pickle.dumps(False)), False) try:
veris(cPickle.loads(pickle.dumps(True, True)), True) f = file(TESTFN, "w")
veris(cPickle.loads(pickle.dumps(False, True)), False) self.assertIs(f.closed, False)
f.close()
self.assertIs(f.closed, True)
finally:
os.remove(TESTFN)
# Test for specific backwards-compatible pickle values def test_operator(self):
vereq(pickle.dumps(True), "I01\n.") import operator
vereq(pickle.dumps(False), "I00\n.") self.assertIs(operator.truth(0), False)
vereq(cPickle.dumps(True), "I01\n.") self.assertIs(operator.truth(1), True)
vereq(cPickle.dumps(False), "I00\n.") self.assertIs(operator.isCallable(0), False)
vereq(pickle.dumps(True, True), "I01\n.") self.assertIs(operator.isCallable(len), True)
vereq(pickle.dumps(False, True), "I00\n.") self.assertIs(operator.isNumberType(None), False)
vereq(cPickle.dumps(True, True), "I01\n.") self.assertIs(operator.isNumberType(0), True)
vereq(cPickle.dumps(False, True), "I00\n.") self.assertIs(operator.not_(1), False)
self.assertIs(operator.not_(0), True)
self.assertIs(operator.isSequenceType(0), False)
self.assertIs(operator.isSequenceType([]), True)
self.assertIs(operator.contains([], 1), False)
self.assertIs(operator.contains([1], 1), True)
self.assertIs(operator.isMappingType(1), False)
self.assertIs(operator.isMappingType({}), True)
self.assertIs(operator.lt(0, 0), False)
self.assertIs(operator.lt(0, 1), True)
self.assertIs(operator.is_(True, True), True)
self.assertIs(operator.is_(True, False), False)
self.assertIs(operator.is_not(True, True), False)
self.assertIs(operator.is_not(True, False), True)
def test_marshal(self):
import marshal
veris(marshal.loads(marshal.dumps(True)), True)
veris(marshal.loads(marshal.dumps(False)), False)
def test_pickle(self):
import pickle
self.assertIs(pickle.loads(pickle.dumps(True)), True)
self.assertIs(pickle.loads(pickle.dumps(False)), False)
self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
self.assertIs(pickle.loads(pickle.dumps(False, True)), False)
def test_cpickle(self):
import cPickle
self.assertIs(cPickle.loads(cPickle.dumps(True)), True)
self.assertIs(cPickle.loads(cPickle.dumps(False)), False)
self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True)
self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False)
def test_mixedpickle(self):
import pickle, cPickle
self.assertIs(pickle.loads(cPickle.dumps(True)), True)
self.assertIs(pickle.loads(cPickle.dumps(False)), False)
self.assertIs(pickle.loads(cPickle.dumps(True, True)), True)
self.assertIs(pickle.loads(cPickle.dumps(False, True)), False)
self.assertIs(cPickle.loads(pickle.dumps(True)), True)
self.assertIs(cPickle.loads(pickle.dumps(False)), False)
self.assertIs(cPickle.loads(pickle.dumps(True, True)), True)
self.assertIs(cPickle.loads(pickle.dumps(False, True)), False)
def test_picklevalues(self):
import pickle, cPickle
# Test for specific backwards-compatible pickle values
self.assertEqual(pickle.dumps(True), "I01\n.")
self.assertEqual(pickle.dumps(False), "I00\n.")
self.assertEqual(cPickle.dumps(True), "I01\n.")
self.assertEqual(cPickle.dumps(False), "I00\n.")
self.assertEqual(pickle.dumps(True, True), "I01\n.")
self.assertEqual(pickle.dumps(False, True), "I00\n.")
self.assertEqual(cPickle.dumps(True, True), "I01\n.")
self.assertEqual(cPickle.dumps(False, True), "I00\n.")
def test_main():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(BoolTest))
test_support.run_suite(suite)
if __name__ == "__main__":
test_main()
if verbose:
print "All OK"