0001 # Test properties of bool promised by PEP 285 0002 0003 import unittest 0004 from test import test_support 0005 0006 import os 0007 0008 class BoolTest(unittest.TestCase): 0009 0010 def assertIs(self, a, b): 0011 self.assert_(a is b) 0012 0013 def assertIsNot(self, a, b): 0014 self.assert_(a is not b) 0015 0016 def test_subclass(self): 0017 try: 0018 class C(bool): 0019 pass 0020 except TypeError: 0021 pass 0022 else: 0023 self.fail("bool should not be subclassable") 0024 0025 self.assertRaises(TypeError, int.__new__, bool, 0) 0026 0027 def test_print(self): 0028 try: 0029 fo = open(test_support.TESTFN, "wb") 0030 print >> fo, False, True 0031 fo.close() 0032 fo = open(test_support.TESTFN, "rb") 0033 self.assertEqual(fo.read(), 'False True\n') 0034 finally: 0035 fo.close() 0036 os.remove(test_support.TESTFN) 0037 0038 def test_repr(self): 0039 self.assertEqual(repr(False), 'False') 0040 self.assertEqual(repr(True), 'True') 0041 self.assertEqual(eval(repr(False)), False) 0042 self.assertEqual(eval(repr(True)), True) 0043 0044 def test_str(self): 0045 self.assertEqual(str(False), 'False') 0046 self.assertEqual(str(True), 'True') 0047 0048 def test_int(self): 0049 self.assertEqual(int(False), 0) 0050 self.assertIsNot(int(False), False) 0051 self.assertEqual(int(True), 1) 0052 self.assertIsNot(int(True), True) 0053 0054 def test_math(self): 0055 self.assertEqual(+False, 0) 0056 self.assertIsNot(+False, False) 0057 self.assertEqual(-False, 0) 0058 self.assertIsNot(-False, False) 0059 self.assertEqual(abs(False), 0) 0060 self.assertIsNot(abs(False), False) 0061 self.assertEqual(+True, 1) 0062 self.assertIsNot(+True, True) 0063 self.assertEqual(-True, -1) 0064 self.assertEqual(abs(True), 1) 0065 self.assertIsNot(abs(True), True) 0066 self.assertEqual(~False, -1) 0067 self.assertEqual(~True, -2) 0068 0069 self.assertEqual(False+2, 2) 0070 self.assertEqual(True+2, 3) 0071 self.assertEqual(2+False, 2) 0072 self.assertEqual(2+True, 3) 0073 0074 self.assertEqual(False+False, 0) 0075 self.assertIsNot(False+False, False) 0076 self.assertEqual(False+True, 1) 0077 self.assertIsNot(False+True, True) 0078 self.assertEqual(True+False, 1) 0079 self.assertIsNot(True+False, True) 0080 self.assertEqual(True+True, 2) 0081 0082 self.assertEqual(True-True, 0) 0083 self.assertIsNot(True-True, False) 0084 self.assertEqual(False-False, 0) 0085 self.assertIsNot(False-False, False) 0086 self.assertEqual(True-False, 1) 0087 self.assertIsNot(True-False, True) 0088 self.assertEqual(False-True, -1) 0089 0090 self.assertEqual(True*1, 1) 0091 self.assertEqual(False*1, 0) 0092 self.assertIsNot(False*1, False) 0093 0094 self.assertEqual(True/1, 1) 0095 self.assertIsNot(True/1, True) 0096 self.assertEqual(False/1, 0) 0097 self.assertIsNot(False/1, False) 0098 0099 for b in False, True: 0100 for i in 0, 1, 2: 0101 self.assertEqual(b**i, int(b)**i) 0102 self.assertIsNot(b**i, bool(int(b)**i)) 0103 0104 for a in False, True: 0105 for b in False, True: 0106 self.assertIs(a&b, bool(int(a)&int(b))) 0107 self.assertIs(a|b, bool(int(a)|int(b))) 0108 self.assertIs(a^b, bool(int(a)^int(b))) 0109 self.assertEqual(a&int(b), int(a)&int(b)) 0110 self.assertIsNot(a&int(b), bool(int(a)&int(b))) 0111 self.assertEqual(a|int(b), int(a)|int(b)) 0112 self.assertIsNot(a|int(b), bool(int(a)|int(b))) 0113 self.assertEqual(a^int(b), int(a)^int(b)) 0114 self.assertIsNot(a^int(b), bool(int(a)^int(b))) 0115 self.assertEqual(int(a)&b, int(a)&int(b)) 0116 self.assertIsNot(int(a)&b, bool(int(a)&int(b))) 0117 self.assertEqual(int(a)|b, int(a)|int(b)) 0118 self.assertIsNot(int(a)|b, bool(int(a)|int(b))) 0119 self.assertEqual(int(a)^b, int(a)^int(b)) 0120 self.assertIsNot(int(a)^b, bool(int(a)^int(b))) 0121 0122 self.assertIs(1==1, True) 0123 self.assertIs(1==0, False) 0124 self.assertIs(0<1, True) 0125 self.assertIs(1<0, False) 0126 self.assertIs(0<=0, True) 0127 self.assertIs(1<=0, False) 0128 self.assertIs(1>0, True) 0129 self.assertIs(1>1, False) 0130 self.assertIs(1>=1, True) 0131 self.assertIs(0>=1, False) 0132 self.assertIs(0!=1, True) 0133 self.assertIs(0!=0, False) 0134 0135 x = [1] 0136 self.assertIs(x is x, True) 0137 self.assertIs(x is not x, False) 0138 0139 self.assertIs(1 in x, True) 0140 self.assertIs(0 in x, False) 0141 self.assertIs(1 not in x, False) 0142 self.assertIs(0 not in x, True) 0143 0144 x = {1: 2} 0145 self.assertIs(x is x, True) 0146 self.assertIs(x is not x, False) 0147 0148 self.assertIs(1 in x, True) 0149 self.assertIs(0 in x, False) 0150 self.assertIs(1 not in x, False) 0151 self.assertIs(0 not in x, True) 0152 0153 self.assertIs(not True, False) 0154 self.assertIs(not False, True) 0155 0156 def test_convert(self): 0157 self.assertRaises(TypeError, bool, 42, 42) 0158 self.assertIs(bool(10), True) 0159 self.assertIs(bool(1), True) 0160 self.assertIs(bool(-1), True) 0161 self.assertIs(bool(0), False) 0162 self.assertIs(bool("hello"), True) 0163 self.assertIs(bool(""), False) 0164 self.assertIs(bool(), False) 0165 0166 def test_hasattr(self): 0167 self.assertIs(hasattr([], "append"), True) 0168 self.assertIs(hasattr([], "wobble"), False) 0169 0170 def test_callable(self): 0171 self.assertIs(callable(len), True) 0172 self.assertIs(callable(1), False) 0173 0174 def test_isinstance(self): 0175 self.assertIs(isinstance(True, bool), True) 0176 self.assertIs(isinstance(False, bool), True) 0177 self.assertIs(isinstance(True, int), True) 0178 self.assertIs(isinstance(False, int), True) 0179 self.assertIs(isinstance(1, bool), False) 0180 self.assertIs(isinstance(0, bool), False) 0181 0182 def test_issubclass(self): 0183 self.assertIs(issubclass(bool, int), True) 0184 self.assertIs(issubclass(int, bool), False) 0185 0186 def test_haskey(self): 0187 self.assertIs({}.has_key(1), False) 0188 self.assertIs({1:1}.has_key(1), True) 0189 0190 def test_string(self): 0191 self.assertIs("xyz".endswith("z"), True) 0192 self.assertIs("xyz".endswith("x"), False) 0193 self.assertIs("xyz0123".isalnum(), True) 0194 self.assertIs("@#$%".isalnum(), False) 0195 self.assertIs("xyz".isalpha(), True) 0196 self.assertIs("@#$%".isalpha(), False) 0197 self.assertIs("0123".isdigit(), True) 0198 self.assertIs("xyz".isdigit(), False) 0199 self.assertIs("xyz".islower(), True) 0200 self.assertIs("XYZ".islower(), False) 0201 self.assertIs(" ".isspace(), True) 0202 self.assertIs("XYZ".isspace(), False) 0203 self.assertIs("X".istitle(), True) 0204 self.assertIs("x".istitle(), False) 0205 self.assertIs("XYZ".isupper(), True) 0206 self.assertIs("xyz".isupper(), False) 0207 self.assertIs("xyz".startswith("x"), True) 0208 self.assertIs("xyz".startswith("z"), False) 0209 0210 if test_support.have_unicode: 0211 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("z", 'ascii')), True) 0212 self.assertIs(unicode("xyz", 'ascii').endswith(unicode("x", 'ascii')), False) 0213 self.assertIs(unicode("xyz0123", 'ascii').isalnum(), True) 0214 self.assertIs(unicode("@#$%", 'ascii').isalnum(), False) 0215 self.assertIs(unicode("xyz", 'ascii').isalpha(), True) 0216 self.assertIs(unicode("@#$%", 'ascii').isalpha(), False) 0217 self.assertIs(unicode("0123", 'ascii').isdecimal(), True) 0218 self.assertIs(unicode("xyz", 'ascii').isdecimal(), False) 0219 self.assertIs(unicode("0123", 'ascii').isdigit(), True) 0220 self.assertIs(unicode("xyz", 'ascii').isdigit(), False) 0221 self.assertIs(unicode("xyz", 'ascii').islower(), True) 0222 self.assertIs(unicode("XYZ", 'ascii').islower(), False) 0223 self.assertIs(unicode("0123", 'ascii').isnumeric(), True) 0224 self.assertIs(unicode("xyz", 'ascii').isnumeric(), False) 0225 self.assertIs(unicode(" ", 'ascii').isspace(), True) 0226 self.assertIs(unicode("XYZ", 'ascii').isspace(), False) 0227 self.assertIs(unicode("X", 'ascii').istitle(), True) 0228 self.assertIs(unicode("x", 'ascii').istitle(), False) 0229 self.assertIs(unicode("XYZ", 'ascii').isupper(), True) 0230 self.assertIs(unicode("xyz", 'ascii').isupper(), False) 0231 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("x", 'ascii')), True) 0232 self.assertIs(unicode("xyz", 'ascii').startswith(unicode("z", 'ascii')), False) 0233 0234 def test_boolean(self): 0235 self.assertEqual(True & 1, 1) 0236 self.assert_(not isinstance(True & 1, bool)) 0237 self.assertIs(True & True, True) 0238 0239 self.assertEqual(True | 1, 1) 0240 self.assert_(not isinstance(True | 1, bool)) 0241 self.assertIs(True | True, True) 0242 0243 self.assertEqual(True ^ 1, 0) 0244 self.assert_(not isinstance(True ^ 1, bool)) 0245 self.assertIs(True ^ True, False) 0246 0247 def test_fileclosed(self): 0248 try: 0249 f = file(test_support.TESTFN, "w") 0250 self.assertIs(f.closed, False) 0251 f.close() 0252 self.assertIs(f.closed, True) 0253 finally: 0254 os.remove(test_support.TESTFN) 0255 0256 def test_operator(self): 0257 import operator 0258 self.assertIs(operator.truth(0), False) 0259 self.assertIs(operator.truth(1), True) 0260 self.assertIs(operator.isCallable(0), False) 0261 self.assertIs(operator.isCallable(len), True) 0262 self.assertIs(operator.isNumberType(None), False) 0263 self.assertIs(operator.isNumberType(0), True) 0264 self.assertIs(operator.not_(1), False) 0265 self.assertIs(operator.not_(0), True) 0266 self.assertIs(operator.isSequenceType(0), False) 0267 self.assertIs(operator.isSequenceType([]), True) 0268 self.assertIs(operator.contains([], 1), False) 0269 self.assertIs(operator.contains([1], 1), True) 0270 self.assertIs(operator.isMappingType(1), False) 0271 self.assertIs(operator.isMappingType({}), True) 0272 self.assertIs(operator.lt(0, 0), False) 0273 self.assertIs(operator.lt(0, 1), True) 0274 self.assertIs(operator.is_(True, True), True) 0275 self.assertIs(operator.is_(True, False), False) 0276 self.assertIs(operator.is_not(True, True), False) 0277 self.assertIs(operator.is_not(True, False), True) 0278 0279 def test_marshal(self): 0280 import marshal 0281 self.assertIs(marshal.loads(marshal.dumps(True)), True) 0282 self.assertIs(marshal.loads(marshal.dumps(False)), False) 0283 0284 def test_pickle(self): 0285 import pickle 0286 self.assertIs(pickle.loads(pickle.dumps(True)), True) 0287 self.assertIs(pickle.loads(pickle.dumps(False)), False) 0288 self.assertIs(pickle.loads(pickle.dumps(True, True)), True) 0289 self.assertIs(pickle.loads(pickle.dumps(False, True)), False) 0290 0291 def test_cpickle(self): 0292 import cPickle 0293 self.assertIs(cPickle.loads(cPickle.dumps(True)), True) 0294 self.assertIs(cPickle.loads(cPickle.dumps(False)), False) 0295 self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True) 0296 self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False) 0297 0298 def test_mixedpickle(self): 0299 import pickle, cPickle 0300 self.assertIs(pickle.loads(cPickle.dumps(True)), True) 0301 self.assertIs(pickle.loads(cPickle.dumps(False)), False) 0302 self.assertIs(pickle.loads(cPickle.dumps(True, True)), True) 0303 self.assertIs(pickle.loads(cPickle.dumps(False, True)), False) 0304 0305 self.assertIs(cPickle.loads(pickle.dumps(True)), True) 0306 self.assertIs(cPickle.loads(pickle.dumps(False)), False) 0307 self.assertIs(cPickle.loads(pickle.dumps(True, True)), True) 0308 self.assertIs(cPickle.loads(pickle.dumps(False, True)), False) 0309 0310 def test_picklevalues(self): 0311 import pickle, cPickle 0312 0313 # Test for specific backwards-compatible pickle values 0314 self.assertEqual(pickle.dumps(True), "I01\n.") 0315 self.assertEqual(pickle.dumps(False), "I00\n.") 0316 self.assertEqual(cPickle.dumps(True), "I01\n.") 0317 self.assertEqual(cPickle.dumps(False), "I00\n.") 0318 self.assertEqual(pickle.dumps(True, True), "I01\n.") 0319 self.assertEqual(pickle.dumps(False, True), "I00\n.") 0320 self.assertEqual(cPickle.dumps(True, True), "I01\n.") 0321 self.assertEqual(cPickle.dumps(False, True), "I00\n.") 0322 0323 def test_convert_to_bool(self): 0324 # Verify that TypeError occurs when bad things are returned 0325 # from __nonzero__(). This isn't really a bool test, but 0326 # it's related. 0327 check = lambda o: self.assertRaises(TypeError, bool, o) 0328 class Foo(object): 0329 def __nonzero__(self): 0330 return self 0331 check(Foo()) 0332 0333 class Bar(object): 0334 def __nonzero__(self): 0335 return "Yes" 0336 check(Bar()) 0337 0338 class Baz(int): 0339 def __nonzero__(self): 0340 return self 0341 check(Baz()) 0342 0343 0344 def test_main(): 0345 test_support.run_unittest(BoolTest) 0346 0347 if __name__ == "__main__": 0348 test_main() 0349
Generated by PyXR 0.9.4