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 | |
Mark Dickinson | b820d7f | 2016-08-22 12:24:46 +0100 | [diff] [blame] | 99 | self.assertEqual(True%1, 0) |
| 100 | self.assertIsNot(True%1, False) |
| 101 | self.assertEqual(True%2, 1) |
| 102 | self.assertIsNot(True%2, True) |
| 103 | self.assertEqual(False%1, 0) |
| 104 | self.assertIsNot(False%1, False) |
| 105 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 106 | for b in False, True: |
| 107 | for i in 0, 1, 2: |
| 108 | self.assertEqual(b**i, int(b)**i) |
| 109 | self.assertIsNot(b**i, bool(int(b)**i)) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 110 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 111 | for a in False, True: |
| 112 | for b in False, True: |
| 113 | self.assertIs(a&b, bool(int(a)&int(b))) |
| 114 | self.assertIs(a|b, bool(int(a)|int(b))) |
| 115 | self.assertIs(a^b, bool(int(a)^int(b))) |
| 116 | self.assertEqual(a&int(b), int(a)&int(b)) |
| 117 | self.assertIsNot(a&int(b), bool(int(a)&int(b))) |
| 118 | self.assertEqual(a|int(b), int(a)|int(b)) |
| 119 | self.assertIsNot(a|int(b), bool(int(a)|int(b))) |
| 120 | self.assertEqual(a^int(b), int(a)^int(b)) |
| 121 | self.assertIsNot(a^int(b), bool(int(a)^int(b))) |
| 122 | self.assertEqual(int(a)&b, int(a)&int(b)) |
| 123 | self.assertIsNot(int(a)&b, bool(int(a)&int(b))) |
| 124 | self.assertEqual(int(a)|b, int(a)|int(b)) |
| 125 | self.assertIsNot(int(a)|b, bool(int(a)|int(b))) |
| 126 | self.assertEqual(int(a)^b, int(a)^int(b)) |
| 127 | self.assertIsNot(int(a)^b, bool(int(a)^int(b))) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 128 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 129 | self.assertIs(1==1, True) |
| 130 | self.assertIs(1==0, False) |
| 131 | self.assertIs(0<1, True) |
| 132 | self.assertIs(1<0, False) |
| 133 | self.assertIs(0<=0, True) |
| 134 | self.assertIs(1<=0, False) |
| 135 | self.assertIs(1>0, True) |
| 136 | self.assertIs(1>1, False) |
| 137 | self.assertIs(1>=1, True) |
| 138 | self.assertIs(0>=1, False) |
| 139 | self.assertIs(0!=1, True) |
| 140 | self.assertIs(0!=0, False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 141 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 142 | x = [1] |
| 143 | self.assertIs(x is x, True) |
| 144 | self.assertIs(x is not x, False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 145 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 146 | self.assertIs(1 in x, True) |
| 147 | self.assertIs(0 in x, False) |
| 148 | self.assertIs(1 not in x, False) |
| 149 | self.assertIs(0 not in x, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 150 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 151 | x = {1: 2} |
| 152 | self.assertIs(x is x, True) |
| 153 | self.assertIs(x is not x, False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 154 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 155 | self.assertIs(1 in x, True) |
| 156 | self.assertIs(0 in x, False) |
| 157 | self.assertIs(1 not in x, False) |
| 158 | self.assertIs(0 not in x, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 159 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 160 | self.assertIs(not True, False) |
| 161 | self.assertIs(not False, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 162 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 163 | def test_convert(self): |
| 164 | self.assertRaises(TypeError, bool, 42, 42) |
| 165 | self.assertIs(bool(10), True) |
| 166 | self.assertIs(bool(1), True) |
| 167 | self.assertIs(bool(-1), True) |
| 168 | self.assertIs(bool(0), False) |
| 169 | self.assertIs(bool("hello"), True) |
| 170 | self.assertIs(bool(""), False) |
| 171 | self.assertIs(bool(), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 172 | |
Serhiy Storchaka | 58d23e6 | 2017-03-06 00:53:39 +0200 | [diff] [blame] | 173 | def test_keyword_args(self): |
Serhiy Storchaka | 2e56424 | 2017-03-06 17:01:06 +0200 | [diff] [blame] | 174 | with self.assertRaisesRegex(TypeError, 'keyword argument'): |
| 175 | bool(x=10) |
Serhiy Storchaka | 58d23e6 | 2017-03-06 00:53:39 +0200 | [diff] [blame] | 176 | |
Martin v. Löwis | ff398c6 | 2007-08-14 21:57:32 +0000 | [diff] [blame] | 177 | def test_format(self): |
| 178 | self.assertEqual("%d" % False, "0") |
| 179 | self.assertEqual("%d" % True, "1") |
| 180 | self.assertEqual("%x" % False, "0") |
| 181 | self.assertEqual("%x" % True, "1") |
| 182 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 183 | def test_hasattr(self): |
| 184 | self.assertIs(hasattr([], "append"), True) |
| 185 | self.assertIs(hasattr([], "wobble"), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 186 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 187 | def test_callable(self): |
Antoine Pitrou | e71362d | 2010-11-27 22:00:11 +0000 | [diff] [blame] | 188 | self.assertIs(callable(len), True) |
| 189 | self.assertIs(callable(1), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 190 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 191 | def test_isinstance(self): |
| 192 | self.assertIs(isinstance(True, bool), True) |
| 193 | self.assertIs(isinstance(False, bool), True) |
| 194 | self.assertIs(isinstance(True, int), True) |
| 195 | self.assertIs(isinstance(False, int), True) |
| 196 | self.assertIs(isinstance(1, bool), False) |
| 197 | self.assertIs(isinstance(0, bool), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 198 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 199 | def test_issubclass(self): |
| 200 | self.assertIs(issubclass(bool, int), True) |
| 201 | self.assertIs(issubclass(int, bool), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 203 | def test_contains(self): |
| 204 | self.assertIs(1 in {}, False) |
| 205 | self.assertIs(1 in {1:1}, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 206 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 207 | def test_string(self): |
| 208 | self.assertIs("xyz".endswith("z"), True) |
| 209 | self.assertIs("xyz".endswith("x"), False) |
| 210 | self.assertIs("xyz0123".isalnum(), True) |
| 211 | self.assertIs("@#$%".isalnum(), False) |
| 212 | self.assertIs("xyz".isalpha(), True) |
| 213 | self.assertIs("@#$%".isalpha(), False) |
| 214 | self.assertIs("0123".isdigit(), True) |
| 215 | self.assertIs("xyz".isdigit(), False) |
| 216 | self.assertIs("xyz".islower(), True) |
| 217 | self.assertIs("XYZ".islower(), False) |
Walter Dörwald | 20badb3 | 2007-05-05 15:57:18 +0000 | [diff] [blame] | 218 | self.assertIs("0123".isdecimal(), True) |
| 219 | self.assertIs("xyz".isdecimal(), False) |
| 220 | self.assertIs("0123".isnumeric(), True) |
| 221 | self.assertIs("xyz".isnumeric(), False) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 222 | self.assertIs(" ".isspace(), True) |
Walter Dörwald | 9bc4712 | 2007-05-05 16:04:46 +0000 | [diff] [blame] | 223 | self.assertIs("\xa0".isspace(), True) |
| 224 | self.assertIs("\u3000".isspace(), True) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 225 | self.assertIs("XYZ".isspace(), False) |
| 226 | self.assertIs("X".istitle(), True) |
| 227 | self.assertIs("x".istitle(), False) |
| 228 | self.assertIs("XYZ".isupper(), True) |
| 229 | self.assertIs("xyz".isupper(), False) |
| 230 | self.assertIs("xyz".startswith("x"), True) |
| 231 | self.assertIs("xyz".startswith("z"), False) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 232 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 233 | def test_boolean(self): |
| 234 | self.assertEqual(True & 1, 1) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 235 | self.assertNotIsInstance(True & 1, bool) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 236 | self.assertIs(True & True, True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 237 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 238 | self.assertEqual(True | 1, 1) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 239 | self.assertNotIsInstance(True | 1, bool) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 240 | self.assertIs(True | True, True) |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 241 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 242 | self.assertEqual(True ^ 1, 0) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 243 | self.assertNotIsInstance(True ^ 1, bool) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 244 | self.assertIs(True ^ True, False) |
| 245 | |
| 246 | def test_fileclosed(self): |
| 247 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 248 | f = open(support.TESTFN, "w") |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 249 | self.assertIs(f.closed, False) |
| 250 | f.close() |
| 251 | self.assertIs(f.closed, True) |
| 252 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 253 | os.remove(support.TESTFN) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | 503ab33 | 2010-03-30 18:56:19 +0000 | [diff] [blame] | 255 | def test_types(self): |
| 256 | # types are always true. |
| 257 | for t in [bool, complex, dict, float, int, list, object, |
| 258 | set, str, tuple, type]: |
| 259 | self.assertIs(bool(t), True) |
| 260 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 261 | def test_operator(self): |
| 262 | import operator |
| 263 | self.assertIs(operator.truth(0), False) |
| 264 | self.assertIs(operator.truth(1), True) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 265 | self.assertIs(operator.not_(1), False) |
| 266 | self.assertIs(operator.not_(0), True) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 267 | self.assertIs(operator.contains([], 1), False) |
| 268 | self.assertIs(operator.contains([1], 1), True) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 269 | self.assertIs(operator.lt(0, 0), False) |
| 270 | self.assertIs(operator.lt(0, 1), True) |
| 271 | self.assertIs(operator.is_(True, True), True) |
| 272 | self.assertIs(operator.is_(True, False), False) |
| 273 | self.assertIs(operator.is_not(True, True), False) |
| 274 | self.assertIs(operator.is_not(True, False), True) |
| 275 | |
| 276 | def test_marshal(self): |
| 277 | import marshal |
Walter Dörwald | 90437c0 | 2003-05-01 13:12:34 +0000 | [diff] [blame] | 278 | self.assertIs(marshal.loads(marshal.dumps(True)), True) |
| 279 | self.assertIs(marshal.loads(marshal.dumps(False)), False) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 280 | |
| 281 | def test_pickle(self): |
| 282 | import pickle |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 283 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 284 | self.assertIs(pickle.loads(pickle.dumps(True, proto)), True) |
| 285 | self.assertIs(pickle.loads(pickle.dumps(False, proto)), False) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 286 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 287 | def test_picklevalues(self): |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 288 | # Test for specific backwards-compatible pickle values |
Guido van Rossum | bf12cdb | 2006-08-17 20:24:18 +0000 | [diff] [blame] | 289 | import pickle |
Guido van Rossum | c4afa10 | 2007-05-15 19:52:52 +0000 | [diff] [blame] | 290 | self.assertEqual(pickle.dumps(True, protocol=0), b"I01\n.") |
| 291 | self.assertEqual(pickle.dumps(False, protocol=0), b"I00\n.") |
| 292 | self.assertEqual(pickle.dumps(True, protocol=1), b"I01\n.") |
| 293 | self.assertEqual(pickle.dumps(False, protocol=1), b"I00\n.") |
| 294 | self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.') |
| 295 | self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 296 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 297 | def test_convert_to_bool(self): |
| 298 | # Verify that TypeError occurs when bad things are returned |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 299 | # from __bool__(). This isn't really a bool test, but |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 300 | # it's related. |
| 301 | check = lambda o: self.assertRaises(TypeError, bool, o) |
| 302 | class Foo(object): |
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(Foo()) |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 306 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 307 | class Bar(object): |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 308 | def __bool__(self): |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 309 | return "Yes" |
| 310 | check(Bar()) |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 311 | |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 312 | class Baz(int): |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 313 | def __bool__(self): |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 314 | return self |
| 315 | check(Baz()) |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 316 | |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 317 | # __bool__() must return a bool not an int |
| 318 | class Spam(int): |
| 319 | def __bool__(self): |
| 320 | return 1 |
| 321 | check(Spam()) |
| 322 | |
| 323 | class Eggs: |
| 324 | def __len__(self): |
| 325 | return -1 |
| 326 | self.assertRaises(ValueError, bool, Eggs()) |
| 327 | |
Serhiy Storchaka | ea36c94 | 2016-05-12 10:37:58 +0300 | [diff] [blame] | 328 | def test_from_bytes(self): |
| 329 | self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False) |
| 330 | self.assertIs(bool.from_bytes(b'abcd', 'little'), True) |
| 331 | |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 332 | def test_sane_len(self): |
| 333 | # this test just tests our assumptions about __len__ |
| 334 | # this will start failing if __len__ changes assertions |
| 335 | for badval in ['illegal', -1, 1 << 32]: |
| 336 | class A: |
| 337 | def __len__(self): |
| 338 | return badval |
| 339 | try: |
| 340 | bool(A()) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 341 | except (Exception) as e_bool: |
| 342 | try: |
| 343 | len(A()) |
| 344 | except (Exception) as e_len: |
| 345 | self.assertEqual(str(e_bool), str(e_len)) |
Jeremy Hylton | 7ff55e6 | 2003-06-27 17:40:16 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 97c1adf | 2016-08-18 09:22:23 -0700 | [diff] [blame] | 347 | def test_blocked(self): |
| 348 | class A: |
| 349 | __bool__ = None |
| 350 | self.assertRaises(TypeError, bool, A()) |
| 351 | |
| 352 | class B: |
| 353 | def __len__(self): |
| 354 | return 10 |
| 355 | __bool__ = None |
| 356 | self.assertRaises(TypeError, bool, B()) |
| 357 | |
Benjamin Peterson | 81b21d7 | 2011-05-25 09:21:46 -0500 | [diff] [blame] | 358 | def test_real_and_imag(self): |
| 359 | self.assertEqual(True.real, 1) |
| 360 | self.assertEqual(True.imag, 0) |
| 361 | self.assertIs(type(True.real), int) |
| 362 | self.assertIs(type(True.imag), int) |
| 363 | self.assertEqual(False.real, 0) |
| 364 | self.assertEqual(False.imag, 0) |
| 365 | self.assertIs(type(False.real), int) |
| 366 | self.assertIs(type(False.imag), int) |
| 367 | |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 368 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 369 | support.run_unittest(BoolTest) |
Walter Dörwald | dbcede5 | 2003-04-25 10:22:01 +0000 | [diff] [blame] | 370 | |
| 371 | if __name__ == "__main__": |
| 372 | test_main() |