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 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 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 test_subclass(self): |
| 11 | try: |
| 12 | class C(bool): |
| 13 | pass |
| 14 | except TypeError: |
| 15 | pass |
| 16 | else: |
| 17 | self.fail("bool should not be subclassable") |
Martin v. Löwis | 2f6d4da | 2002-04-14 10:22:29 +0000 | [diff] [blame] | 18 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 19 | self.assertRaises(TypeError, int.__new__, bool, 0) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 20 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 21 | def test_print(self): |
| 22 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 23 | fo = open(support.TESTFN, "w") |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 24 | print(False, True, file=fo) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 25 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 26 | fo = open(support.TESTFN, "r") |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 27 | self.assertEqual(fo.read(), 'False True\n') |
| 28 | finally: |
| 29 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 30 | os.remove(support.TESTFN) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 31 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 32 | def test_repr(self): |
| 33 | self.assertEqual(repr(False), 'False') |
| 34 | self.assertEqual(repr(True), 'True') |
| 35 | self.assertEqual(eval(repr(False)), False) |
| 36 | self.assertEqual(eval(repr(True)), True) |
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_str(self): |
| 39 | self.assertEqual(str(False), 'False') |
| 40 | self.assertEqual(str(True), 'True') |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 41 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 42 | def test_int(self): |
| 43 | self.assertEqual(int(False), 0) |
| 44 | self.assertIsNot(int(False), False) |
| 45 | self.assertEqual(int(True), 1) |
| 46 | self.assertIsNot(int(True), True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 47 | |
Antoine Pitrou | 503ab33 | 2010-03-30 18:56:19 +0000 | [diff] [blame] | 48 | def test_float(self): |
| 49 | self.assertEqual(float(False), 0.0) |
| 50 | self.assertIsNot(float(False), False) |
| 51 | self.assertEqual(float(True), 1.0) |
| 52 | self.assertIsNot(float(True), True) |
| 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 | |
Martin v. Löwis | ff398c6 | 2007-08-14 21:57:32 +0000 | [diff] [blame] | 166 | def test_format(self): |
| 167 | self.assertEqual("%d" % False, "0") |
| 168 | self.assertEqual("%d" % True, "1") |
| 169 | self.assertEqual("%x" % False, "0") |
| 170 | self.assertEqual("%x" % True, "1") |
| 171 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 172 | def test_hasattr(self): |
| 173 | self.assertIs(hasattr([], "append"), True) |
| 174 | self.assertIs(hasattr([], "wobble"), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 175 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 176 | def test_callable(self): |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 177 | self.assertIs(hasattr(len, '__call__'), True) |
| 178 | self.assertIs(hasattr(1, '__call__'), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 179 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 180 | def test_isinstance(self): |
| 181 | self.assertIs(isinstance(True, bool), True) |
| 182 | self.assertIs(isinstance(False, bool), True) |
| 183 | self.assertIs(isinstance(True, int), True) |
| 184 | self.assertIs(isinstance(False, int), True) |
| 185 | self.assertIs(isinstance(1, bool), False) |
| 186 | self.assertIs(isinstance(0, bool), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 187 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 188 | def test_issubclass(self): |
| 189 | self.assertIs(issubclass(bool, int), True) |
| 190 | self.assertIs(issubclass(int, bool), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 192 | def test_contains(self): |
| 193 | self.assertIs(1 in {}, False) |
| 194 | self.assertIs(1 in {1:1}, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 195 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 196 | def test_string(self): |
| 197 | self.assertIs("xyz".endswith("z"), True) |
| 198 | self.assertIs("xyz".endswith("x"), False) |
| 199 | self.assertIs("xyz0123".isalnum(), True) |
| 200 | self.assertIs("@#$%".isalnum(), False) |
| 201 | self.assertIs("xyz".isalpha(), True) |
| 202 | self.assertIs("@#$%".isalpha(), False) |
| 203 | self.assertIs("0123".isdigit(), True) |
| 204 | self.assertIs("xyz".isdigit(), False) |
| 205 | self.assertIs("xyz".islower(), True) |
| 206 | self.assertIs("XYZ".islower(), False) |
Walter Dörwald | 20badb3 | 2007-05-05 15:57:18 +0000 | [diff] [blame] | 207 | self.assertIs("0123".isdecimal(), True) |
| 208 | self.assertIs("xyz".isdecimal(), False) |
| 209 | self.assertIs("0123".isnumeric(), True) |
| 210 | self.assertIs("xyz".isnumeric(), False) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 211 | self.assertIs(" ".isspace(), True) |
Walter Dörwald | 9bc4712 | 2007-05-05 16:04:46 +0000 | [diff] [blame] | 212 | self.assertIs("\xa0".isspace(), True) |
| 213 | self.assertIs("\u3000".isspace(), True) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 214 | self.assertIs("XYZ".isspace(), False) |
| 215 | self.assertIs("X".istitle(), True) |
| 216 | self.assertIs("x".istitle(), False) |
| 217 | self.assertIs("XYZ".isupper(), True) |
| 218 | self.assertIs("xyz".isupper(), False) |
| 219 | self.assertIs("xyz".startswith("x"), True) |
| 220 | self.assertIs("xyz".startswith("z"), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 221 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 222 | def test_boolean(self): |
| 223 | self.assertEqual(True & 1, 1) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 224 | self.assertNotIsInstance(True & 1, bool) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 225 | self.assertIs(True & True, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 226 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 227 | self.assertEqual(True | 1, 1) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 228 | self.assertNotIsInstance(True | 1, bool) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 229 | self.assertIs(True | True, True) |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 230 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 231 | self.assertEqual(True ^ 1, 0) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 232 | self.assertNotIsInstance(True ^ 1, bool) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 233 | self.assertIs(True ^ True, False) |
| 234 | |
| 235 | def test_fileclosed(self): |
| 236 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 237 | f = open(support.TESTFN, "w") |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 238 | self.assertIs(f.closed, False) |
| 239 | f.close() |
| 240 | self.assertIs(f.closed, True) |
| 241 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 242 | os.remove(support.TESTFN) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 243 | |
Antoine Pitrou | 503ab33 | 2010-03-30 18:56:19 +0000 | [diff] [blame] | 244 | def test_types(self): |
| 245 | # types are always true. |
| 246 | for t in [bool, complex, dict, float, int, list, object, |
| 247 | set, str, tuple, type]: |
| 248 | self.assertIs(bool(t), True) |
| 249 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 250 | def test_operator(self): |
| 251 | import operator |
| 252 | self.assertIs(operator.truth(0), False) |
| 253 | self.assertIs(operator.truth(1), True) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 254 | self.assertIs(operator.not_(1), False) |
| 255 | self.assertIs(operator.not_(0), True) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 256 | self.assertIs(operator.contains([], 1), False) |
| 257 | self.assertIs(operator.contains([1], 1), True) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 258 | self.assertIs(operator.lt(0, 0), False) |
| 259 | self.assertIs(operator.lt(0, 1), True) |
| 260 | self.assertIs(operator.is_(True, True), True) |
| 261 | self.assertIs(operator.is_(True, False), False) |
| 262 | self.assertIs(operator.is_not(True, True), False) |
| 263 | self.assertIs(operator.is_not(True, False), True) |
| 264 | |
| 265 | def test_marshal(self): |
| 266 | import marshal |
Walter Dörwald | 90437c0 | 2003-05-01 13:12:34 +0000 | [diff] [blame] | 267 | self.assertIs(marshal.loads(marshal.dumps(True)), True) |
| 268 | self.assertIs(marshal.loads(marshal.dumps(False)), False) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 269 | |
| 270 | def test_pickle(self): |
| 271 | import pickle |
| 272 | self.assertIs(pickle.loads(pickle.dumps(True)), True) |
| 273 | self.assertIs(pickle.loads(pickle.dumps(False)), False) |
| 274 | self.assertIs(pickle.loads(pickle.dumps(True, True)), True) |
| 275 | self.assertIs(pickle.loads(pickle.dumps(False, True)), False) |
| 276 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 277 | def test_picklevalues(self): |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 278 | # Test for specific backwards-compatible pickle values |
Guido van Rossum | bf12cdb | 2006-08-17 20:24:18 +0000 | [diff] [blame] | 279 | import pickle |
Guido van Rossum | c4afa10 | 2007-05-15 19:52:52 +0000 | [diff] [blame] | 280 | self.assertEqual(pickle.dumps(True, protocol=0), b"I01\n.") |
| 281 | self.assertEqual(pickle.dumps(False, protocol=0), b"I00\n.") |
| 282 | self.assertEqual(pickle.dumps(True, protocol=1), b"I01\n.") |
| 283 | self.assertEqual(pickle.dumps(False, protocol=1), b"I00\n.") |
| 284 | self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.') |
| 285 | self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 286 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 287 | def test_convert_to_bool(self): |
| 288 | # Verify that TypeError occurs when bad things are returned |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 289 | # from __bool__(). This isn't really a bool test, but |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 290 | # it's related. |
| 291 | check = lambda o: self.assertRaises(TypeError, bool, o) |
| 292 | class Foo(object): |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 293 | def __bool__(self): |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 294 | return self |
| 295 | check(Foo()) |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 296 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 297 | class Bar(object): |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 298 | def __bool__(self): |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 299 | return "Yes" |
| 300 | check(Bar()) |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 301 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 302 | class Baz(int): |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 303 | def __bool__(self): |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 304 | return self |
| 305 | check(Baz()) |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 306 | |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 307 | # __bool__() must return a bool not an int |
| 308 | class Spam(int): |
| 309 | def __bool__(self): |
| 310 | return 1 |
| 311 | check(Spam()) |
| 312 | |
| 313 | class Eggs: |
| 314 | def __len__(self): |
| 315 | return -1 |
| 316 | self.assertRaises(ValueError, bool, Eggs()) |
| 317 | |
| 318 | def test_sane_len(self): |
| 319 | # this test just tests our assumptions about __len__ |
| 320 | # this will start failing if __len__ changes assertions |
| 321 | for badval in ['illegal', -1, 1 << 32]: |
| 322 | class A: |
| 323 | def __len__(self): |
| 324 | return badval |
| 325 | try: |
| 326 | bool(A()) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 327 | except (Exception) as e_bool: |
| 328 | try: |
| 329 | len(A()) |
| 330 | except (Exception) as e_len: |
| 331 | self.assertEqual(str(e_bool), str(e_len)) |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 332 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 333 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 334 | support.run_unittest(BoolTest) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 335 | |
| 336 | if __name__ == "__main__": |
| 337 | test_main() |