Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 1 | # Test properties of bool promised by PEP 285 |
| 2 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | from test import test_support |
| 5 | |
Martin v. Löwis | 2f6d4da | 2002-04-14 10:22:29 +0000 | [diff] [blame] | 6 | import os |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 7 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 8 | class BoolTest(unittest.TestCase): |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 9 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 10 | def assertIs(self, a, b): |
| 11 | self.assert_(a is b) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 12 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 13 | def assertIsNot(self, a, b): |
| 14 | self.assert_(a is not b) |
Martin v. Löwis | 2f6d4da | 2002-04-14 10:22:29 +0000 | [diff] [blame] | 15 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 16 | def test_subclass(self): |
| 17 | try: |
| 18 | class C(bool): |
| 19 | pass |
| 20 | except TypeError: |
| 21 | pass |
| 22 | else: |
| 23 | self.fail("bool should not be subclassable") |
Martin v. Löwis | 2f6d4da | 2002-04-14 10:22:29 +0000 | [diff] [blame] | 24 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 25 | self.assertRaises(TypeError, int.__new__, bool, 0) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 26 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 27 | def test_print(self): |
| 28 | try: |
| 29 | fo = open(test_support.TESTFN, "wb") |
| 30 | print >> fo, False, True |
| 31 | fo.close() |
| 32 | fo = open(test_support.TESTFN, "rb") |
| 33 | self.assertEqual(fo.read(), 'False True\n') |
| 34 | finally: |
| 35 | fo.close() |
| 36 | os.remove(test_support.TESTFN) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 37 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 38 | def test_repr(self): |
| 39 | self.assertEqual(repr(False), 'False') |
| 40 | self.assertEqual(repr(True), 'True') |
| 41 | self.assertEqual(eval(repr(False)), False) |
| 42 | self.assertEqual(eval(repr(True)), True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 43 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 44 | def test_str(self): |
| 45 | self.assertEqual(str(False), 'False') |
| 46 | self.assertEqual(str(True), 'True') |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 47 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 48 | def test_int(self): |
| 49 | self.assertEqual(int(False), 0) |
| 50 | self.assertIsNot(int(False), False) |
| 51 | self.assertEqual(int(True), 1) |
| 52 | self.assertIsNot(int(True), True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 53 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 54 | def test_math(self): |
| 55 | self.assertEqual(+False, 0) |
| 56 | self.assertIsNot(+False, False) |
| 57 | self.assertEqual(-False, 0) |
| 58 | self.assertIsNot(-False, False) |
| 59 | self.assertEqual(abs(False), 0) |
| 60 | self.assertIsNot(abs(False), False) |
| 61 | self.assertEqual(+True, 1) |
| 62 | self.assertIsNot(+True, True) |
| 63 | self.assertEqual(-True, -1) |
| 64 | self.assertEqual(abs(True), 1) |
| 65 | self.assertIsNot(abs(True), True) |
| 66 | self.assertEqual(~False, -1) |
| 67 | self.assertEqual(~True, -2) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 68 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 69 | self.assertEqual(False+2, 2) |
| 70 | self.assertEqual(True+2, 3) |
| 71 | self.assertEqual(2+False, 2) |
| 72 | self.assertEqual(2+True, 3) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 73 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 74 | self.assertEqual(False+False, 0) |
| 75 | self.assertIsNot(False+False, False) |
| 76 | self.assertEqual(False+True, 1) |
| 77 | self.assertIsNot(False+True, True) |
| 78 | self.assertEqual(True+False, 1) |
| 79 | self.assertIsNot(True+False, True) |
| 80 | self.assertEqual(True+True, 2) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 81 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 82 | self.assertEqual(True-True, 0) |
| 83 | self.assertIsNot(True-True, False) |
| 84 | self.assertEqual(False-False, 0) |
| 85 | self.assertIsNot(False-False, False) |
| 86 | self.assertEqual(True-False, 1) |
| 87 | self.assertIsNot(True-False, True) |
| 88 | self.assertEqual(False-True, -1) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 89 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 90 | self.assertEqual(True*1, 1) |
| 91 | self.assertEqual(False*1, 0) |
| 92 | self.assertIsNot(False*1, False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 93 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 94 | self.assertEqual(True/1, 1) |
| 95 | self.assertIsNot(True/1, True) |
| 96 | self.assertEqual(False/1, 0) |
| 97 | self.assertIsNot(False/1, False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 98 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 99 | for b in False, True: |
| 100 | for i in 0, 1, 2: |
| 101 | self.assertEqual(b**i, int(b)**i) |
| 102 | self.assertIsNot(b**i, bool(int(b)**i)) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 103 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 104 | for a in False, True: |
| 105 | for b in False, True: |
| 106 | self.assertIs(a&b, bool(int(a)&int(b))) |
| 107 | self.assertIs(a|b, bool(int(a)|int(b))) |
| 108 | self.assertIs(a^b, bool(int(a)^int(b))) |
| 109 | self.assertEqual(a&int(b), int(a)&int(b)) |
| 110 | self.assertIsNot(a&int(b), bool(int(a)&int(b))) |
| 111 | self.assertEqual(a|int(b), int(a)|int(b)) |
| 112 | self.assertIsNot(a|int(b), bool(int(a)|int(b))) |
| 113 | self.assertEqual(a^int(b), int(a)^int(b)) |
| 114 | self.assertIsNot(a^int(b), bool(int(a)^int(b))) |
| 115 | self.assertEqual(int(a)&b, int(a)&int(b)) |
| 116 | self.assertIsNot(int(a)&b, bool(int(a)&int(b))) |
| 117 | self.assertEqual(int(a)|b, int(a)|int(b)) |
| 118 | self.assertIsNot(int(a)|b, bool(int(a)|int(b))) |
| 119 | self.assertEqual(int(a)^b, int(a)^int(b)) |
| 120 | self.assertIsNot(int(a)^b, bool(int(a)^int(b))) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 121 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 122 | self.assertIs(1==1, True) |
| 123 | self.assertIs(1==0, False) |
| 124 | self.assertIs(0<1, True) |
| 125 | self.assertIs(1<0, False) |
| 126 | self.assertIs(0<=0, True) |
| 127 | self.assertIs(1<=0, False) |
| 128 | self.assertIs(1>0, True) |
| 129 | self.assertIs(1>1, False) |
| 130 | self.assertIs(1>=1, True) |
| 131 | self.assertIs(0>=1, False) |
| 132 | self.assertIs(0!=1, True) |
| 133 | self.assertIs(0!=0, False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 134 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 135 | x = [1] |
| 136 | self.assertIs(x is x, True) |
| 137 | self.assertIs(x is not x, False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 138 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 139 | self.assertIs(1 in x, True) |
| 140 | self.assertIs(0 in x, False) |
| 141 | self.assertIs(1 not in x, False) |
| 142 | self.assertIs(0 not in x, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 143 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 144 | x = {1: 2} |
| 145 | self.assertIs(x is x, True) |
| 146 | self.assertIs(x is not x, False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 147 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 148 | self.assertIs(1 in x, True) |
| 149 | self.assertIs(0 in x, False) |
| 150 | self.assertIs(1 not in x, False) |
| 151 | self.assertIs(0 not in x, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 152 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 153 | self.assertIs(not True, False) |
| 154 | self.assertIs(not False, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 155 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 156 | def test_convert(self): |
| 157 | self.assertRaises(TypeError, bool, 42, 42) |
| 158 | self.assertIs(bool(10), True) |
| 159 | self.assertIs(bool(1), True) |
| 160 | self.assertIs(bool(-1), True) |
| 161 | self.assertIs(bool(0), False) |
| 162 | self.assertIs(bool("hello"), True) |
| 163 | self.assertIs(bool(""), False) |
| 164 | self.assertIs(bool(), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 165 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 166 | def test_hasattr(self): |
| 167 | self.assertIs(hasattr([], "append"), True) |
| 168 | self.assertIs(hasattr([], "wobble"), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 169 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 170 | def test_callable(self): |
| 171 | self.assertIs(callable(len), True) |
| 172 | self.assertIs(callable(1), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 173 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 174 | def test_isinstance(self): |
| 175 | self.assertIs(isinstance(True, bool), True) |
| 176 | self.assertIs(isinstance(False, bool), True) |
| 177 | self.assertIs(isinstance(True, int), True) |
| 178 | self.assertIs(isinstance(False, int), True) |
| 179 | self.assertIs(isinstance(1, bool), False) |
| 180 | self.assertIs(isinstance(0, bool), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 181 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 182 | def test_issubclass(self): |
| 183 | self.assertIs(issubclass(bool, int), True) |
| 184 | self.assertIs(issubclass(int, bool), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 185 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 186 | def test_haskey(self): |
| 187 | self.assertIs({}.has_key(1), False) |
| 188 | self.assertIs({1:1}.has_key(1), True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 189 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 190 | def test_string(self): |
| 191 | self.assertIs("xyz".endswith("z"), True) |
| 192 | self.assertIs("xyz".endswith("x"), False) |
| 193 | self.assertIs("xyz0123".isalnum(), True) |
| 194 | self.assertIs("@#$%".isalnum(), False) |
| 195 | self.assertIs("xyz".isalpha(), True) |
| 196 | self.assertIs("@#$%".isalpha(), False) |
| 197 | self.assertIs("0123".isdigit(), True) |
| 198 | self.assertIs("xyz".isdigit(), False) |
| 199 | self.assertIs("xyz".islower(), True) |
| 200 | self.assertIs("XYZ".islower(), False) |
| 201 | self.assertIs(" ".isspace(), True) |
| 202 | self.assertIs("XYZ".isspace(), False) |
| 203 | self.assertIs("X".istitle(), True) |
| 204 | self.assertIs("x".istitle(), False) |
| 205 | self.assertIs("XYZ".isupper(), True) |
| 206 | self.assertIs("xyz".isupper(), False) |
| 207 | self.assertIs("xyz".startswith("x"), True) |
| 208 | self.assertIs("xyz".startswith("z"), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 209 | |
Walter Dörwald | 90437c0 | 2003-05-01 13:12:34 +0000 | [diff] [blame] | 210 | if test_support.have_unicode: |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 211 | self.assertIs(unicode("xyz", 'ascii').endswith(unicode("z", 'ascii')), True) |
| 212 | self.assertIs(unicode("xyz", 'ascii').endswith(unicode("x", 'ascii')), False) |
| 213 | self.assertIs(unicode("xyz0123", 'ascii').isalnum(), True) |
| 214 | self.assertIs(unicode("@#$%", 'ascii').isalnum(), False) |
| 215 | self.assertIs(unicode("xyz", 'ascii').isalpha(), True) |
| 216 | self.assertIs(unicode("@#$%", 'ascii').isalpha(), False) |
| 217 | self.assertIs(unicode("0123", 'ascii').isdecimal(), True) |
| 218 | self.assertIs(unicode("xyz", 'ascii').isdecimal(), False) |
| 219 | self.assertIs(unicode("0123", 'ascii').isdigit(), True) |
| 220 | self.assertIs(unicode("xyz", 'ascii').isdigit(), False) |
| 221 | self.assertIs(unicode("xyz", 'ascii').islower(), True) |
| 222 | self.assertIs(unicode("XYZ", 'ascii').islower(), False) |
| 223 | self.assertIs(unicode("0123", 'ascii').isnumeric(), True) |
| 224 | self.assertIs(unicode("xyz", 'ascii').isnumeric(), False) |
| 225 | self.assertIs(unicode(" ", 'ascii').isspace(), True) |
| 226 | self.assertIs(unicode("XYZ", 'ascii').isspace(), False) |
| 227 | self.assertIs(unicode("X", 'ascii').istitle(), True) |
| 228 | self.assertIs(unicode("x", 'ascii').istitle(), False) |
| 229 | self.assertIs(unicode("XYZ", 'ascii').isupper(), True) |
| 230 | self.assertIs(unicode("xyz", 'ascii').isupper(), False) |
| 231 | self.assertIs(unicode("xyz", 'ascii').startswith(unicode("x", 'ascii')), True) |
| 232 | self.assertIs(unicode("xyz", 'ascii').startswith(unicode("z", 'ascii')), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 233 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 234 | def test_boolean(self): |
| 235 | self.assertEqual(True & 1, 1) |
| 236 | self.assert_(not isinstance(True & 1, bool)) |
| 237 | self.assertIs(True & True, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 238 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 239 | self.assertEqual(True | 1, 1) |
| 240 | self.assert_(not isinstance(True | 1, bool)) |
| 241 | self.assertIs(True | True, True) |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 242 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 243 | self.assertEqual(True ^ 1, 0) |
| 244 | self.assert_(not isinstance(True ^ 1, bool)) |
| 245 | self.assertIs(True ^ True, False) |
| 246 | |
| 247 | def test_fileclosed(self): |
| 248 | try: |
Walter Dörwald | 90437c0 | 2003-05-01 13:12:34 +0000 | [diff] [blame] | 249 | f = file(test_support.TESTFN, "w") |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 250 | self.assertIs(f.closed, False) |
| 251 | f.close() |
| 252 | self.assertIs(f.closed, True) |
| 253 | finally: |
Walter Dörwald | 90437c0 | 2003-05-01 13:12:34 +0000 | [diff] [blame] | 254 | os.remove(test_support.TESTFN) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 255 | |
| 256 | def test_operator(self): |
| 257 | import operator |
| 258 | self.assertIs(operator.truth(0), False) |
| 259 | self.assertIs(operator.truth(1), True) |
| 260 | self.assertIs(operator.isCallable(0), False) |
| 261 | self.assertIs(operator.isCallable(len), True) |
| 262 | self.assertIs(operator.isNumberType(None), False) |
| 263 | self.assertIs(operator.isNumberType(0), True) |
| 264 | self.assertIs(operator.not_(1), False) |
| 265 | self.assertIs(operator.not_(0), True) |
| 266 | self.assertIs(operator.isSequenceType(0), False) |
| 267 | self.assertIs(operator.isSequenceType([]), True) |
| 268 | self.assertIs(operator.contains([], 1), False) |
| 269 | self.assertIs(operator.contains([1], 1), True) |
| 270 | self.assertIs(operator.isMappingType(1), False) |
| 271 | self.assertIs(operator.isMappingType({}), True) |
| 272 | self.assertIs(operator.lt(0, 0), False) |
| 273 | self.assertIs(operator.lt(0, 1), True) |
| 274 | self.assertIs(operator.is_(True, True), True) |
| 275 | self.assertIs(operator.is_(True, False), False) |
| 276 | self.assertIs(operator.is_not(True, True), False) |
| 277 | self.assertIs(operator.is_not(True, False), True) |
| 278 | |
| 279 | def test_marshal(self): |
| 280 | import marshal |
Walter Dörwald | 90437c0 | 2003-05-01 13:12:34 +0000 | [diff] [blame] | 281 | self.assertIs(marshal.loads(marshal.dumps(True)), True) |
| 282 | self.assertIs(marshal.loads(marshal.dumps(False)), False) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 283 | |
| 284 | def test_pickle(self): |
| 285 | import pickle |
| 286 | self.assertIs(pickle.loads(pickle.dumps(True)), True) |
| 287 | self.assertIs(pickle.loads(pickle.dumps(False)), False) |
| 288 | self.assertIs(pickle.loads(pickle.dumps(True, True)), True) |
| 289 | self.assertIs(pickle.loads(pickle.dumps(False, True)), False) |
| 290 | |
| 291 | def test_cpickle(self): |
| 292 | import cPickle |
| 293 | self.assertIs(cPickle.loads(cPickle.dumps(True)), True) |
| 294 | self.assertIs(cPickle.loads(cPickle.dumps(False)), False) |
| 295 | self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True) |
| 296 | self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False) |
| 297 | |
| 298 | def test_mixedpickle(self): |
| 299 | import pickle, cPickle |
| 300 | self.assertIs(pickle.loads(cPickle.dumps(True)), True) |
| 301 | self.assertIs(pickle.loads(cPickle.dumps(False)), False) |
| 302 | self.assertIs(pickle.loads(cPickle.dumps(True, True)), True) |
| 303 | self.assertIs(pickle.loads(cPickle.dumps(False, True)), False) |
| 304 | |
| 305 | self.assertIs(cPickle.loads(pickle.dumps(True)), True) |
| 306 | self.assertIs(cPickle.loads(pickle.dumps(False)), False) |
| 307 | self.assertIs(cPickle.loads(pickle.dumps(True, True)), True) |
| 308 | self.assertIs(cPickle.loads(pickle.dumps(False, True)), False) |
| 309 | |
| 310 | def test_picklevalues(self): |
| 311 | import pickle, cPickle |
| 312 | |
| 313 | # Test for specific backwards-compatible pickle values |
| 314 | self.assertEqual(pickle.dumps(True), "I01\n.") |
| 315 | self.assertEqual(pickle.dumps(False), "I00\n.") |
| 316 | self.assertEqual(cPickle.dumps(True), "I01\n.") |
| 317 | self.assertEqual(cPickle.dumps(False), "I00\n.") |
| 318 | self.assertEqual(pickle.dumps(True, True), "I01\n.") |
| 319 | self.assertEqual(pickle.dumps(False, True), "I00\n.") |
| 320 | self.assertEqual(cPickle.dumps(True, True), "I01\n.") |
| 321 | self.assertEqual(cPickle.dumps(False, True), "I00\n.") |
| 322 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 323 | def test_convert_to_bool(self): |
| 324 | # Verify that TypeError occurs when bad things are returned |
| 325 | # from __nonzero__(). This isn't really a bool test, but |
| 326 | # it's related. |
| 327 | check = lambda o: self.assertRaises(TypeError, bool, o) |
| 328 | class Foo(object): |
| 329 | def __nonzero__(self): |
| 330 | return self |
| 331 | check(Foo()) |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 332 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 333 | class Bar(object): |
| 334 | def __nonzero__(self): |
| 335 | return "Yes" |
| 336 | check(Bar()) |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 337 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 338 | class Baz(int): |
| 339 | def __nonzero__(self): |
| 340 | return self |
| 341 | check(Baz()) |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 342 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 343 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 344 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 345 | test_support.run_unittest(BoolTest) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 346 | |
| 347 | if __name__ == "__main__": |
| 348 | test_main() |