1 | n/a | # Test properties of bool promised by PEP 285 |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | from test import support |
---|
5 | n/a | |
---|
6 | n/a | import os |
---|
7 | n/a | |
---|
8 | n/a | class BoolTest(unittest.TestCase): |
---|
9 | n/a | |
---|
10 | n/a | def test_subclass(self): |
---|
11 | n/a | try: |
---|
12 | n/a | class C(bool): |
---|
13 | n/a | pass |
---|
14 | n/a | except TypeError: |
---|
15 | n/a | pass |
---|
16 | n/a | else: |
---|
17 | n/a | self.fail("bool should not be subclassable") |
---|
18 | n/a | |
---|
19 | n/a | self.assertRaises(TypeError, int.__new__, bool, 0) |
---|
20 | n/a | |
---|
21 | n/a | def test_print(self): |
---|
22 | n/a | try: |
---|
23 | n/a | fo = open(support.TESTFN, "w") |
---|
24 | n/a | print(False, True, file=fo) |
---|
25 | n/a | fo.close() |
---|
26 | n/a | fo = open(support.TESTFN, "r") |
---|
27 | n/a | self.assertEqual(fo.read(), 'False True\n') |
---|
28 | n/a | finally: |
---|
29 | n/a | fo.close() |
---|
30 | n/a | os.remove(support.TESTFN) |
---|
31 | n/a | |
---|
32 | n/a | def test_repr(self): |
---|
33 | n/a | self.assertEqual(repr(False), 'False') |
---|
34 | n/a | self.assertEqual(repr(True), 'True') |
---|
35 | n/a | self.assertEqual(eval(repr(False)), False) |
---|
36 | n/a | self.assertEqual(eval(repr(True)), True) |
---|
37 | n/a | |
---|
38 | n/a | def test_str(self): |
---|
39 | n/a | self.assertEqual(str(False), 'False') |
---|
40 | n/a | self.assertEqual(str(True), 'True') |
---|
41 | n/a | |
---|
42 | n/a | def test_int(self): |
---|
43 | n/a | self.assertEqual(int(False), 0) |
---|
44 | n/a | self.assertIsNot(int(False), False) |
---|
45 | n/a | self.assertEqual(int(True), 1) |
---|
46 | n/a | self.assertIsNot(int(True), True) |
---|
47 | n/a | |
---|
48 | n/a | def test_float(self): |
---|
49 | n/a | self.assertEqual(float(False), 0.0) |
---|
50 | n/a | self.assertIsNot(float(False), False) |
---|
51 | n/a | self.assertEqual(float(True), 1.0) |
---|
52 | n/a | self.assertIsNot(float(True), True) |
---|
53 | n/a | |
---|
54 | n/a | def test_math(self): |
---|
55 | n/a | self.assertEqual(+False, 0) |
---|
56 | n/a | self.assertIsNot(+False, False) |
---|
57 | n/a | self.assertEqual(-False, 0) |
---|
58 | n/a | self.assertIsNot(-False, False) |
---|
59 | n/a | self.assertEqual(abs(False), 0) |
---|
60 | n/a | self.assertIsNot(abs(False), False) |
---|
61 | n/a | self.assertEqual(+True, 1) |
---|
62 | n/a | self.assertIsNot(+True, True) |
---|
63 | n/a | self.assertEqual(-True, -1) |
---|
64 | n/a | self.assertEqual(abs(True), 1) |
---|
65 | n/a | self.assertIsNot(abs(True), True) |
---|
66 | n/a | self.assertEqual(~False, -1) |
---|
67 | n/a | self.assertEqual(~True, -2) |
---|
68 | n/a | |
---|
69 | n/a | self.assertEqual(False+2, 2) |
---|
70 | n/a | self.assertEqual(True+2, 3) |
---|
71 | n/a | self.assertEqual(2+False, 2) |
---|
72 | n/a | self.assertEqual(2+True, 3) |
---|
73 | n/a | |
---|
74 | n/a | self.assertEqual(False+False, 0) |
---|
75 | n/a | self.assertIsNot(False+False, False) |
---|
76 | n/a | self.assertEqual(False+True, 1) |
---|
77 | n/a | self.assertIsNot(False+True, True) |
---|
78 | n/a | self.assertEqual(True+False, 1) |
---|
79 | n/a | self.assertIsNot(True+False, True) |
---|
80 | n/a | self.assertEqual(True+True, 2) |
---|
81 | n/a | |
---|
82 | n/a | self.assertEqual(True-True, 0) |
---|
83 | n/a | self.assertIsNot(True-True, False) |
---|
84 | n/a | self.assertEqual(False-False, 0) |
---|
85 | n/a | self.assertIsNot(False-False, False) |
---|
86 | n/a | self.assertEqual(True-False, 1) |
---|
87 | n/a | self.assertIsNot(True-False, True) |
---|
88 | n/a | self.assertEqual(False-True, -1) |
---|
89 | n/a | |
---|
90 | n/a | self.assertEqual(True*1, 1) |
---|
91 | n/a | self.assertEqual(False*1, 0) |
---|
92 | n/a | self.assertIsNot(False*1, False) |
---|
93 | n/a | |
---|
94 | n/a | self.assertEqual(True/1, 1) |
---|
95 | n/a | self.assertIsNot(True/1, True) |
---|
96 | n/a | self.assertEqual(False/1, 0) |
---|
97 | n/a | self.assertIsNot(False/1, False) |
---|
98 | n/a | |
---|
99 | n/a | self.assertEqual(True%1, 0) |
---|
100 | n/a | self.assertIsNot(True%1, False) |
---|
101 | n/a | self.assertEqual(True%2, 1) |
---|
102 | n/a | self.assertIsNot(True%2, True) |
---|
103 | n/a | self.assertEqual(False%1, 0) |
---|
104 | n/a | self.assertIsNot(False%1, False) |
---|
105 | n/a | |
---|
106 | n/a | for b in False, True: |
---|
107 | n/a | for i in 0, 1, 2: |
---|
108 | n/a | self.assertEqual(b**i, int(b)**i) |
---|
109 | n/a | self.assertIsNot(b**i, bool(int(b)**i)) |
---|
110 | n/a | |
---|
111 | n/a | for a in False, True: |
---|
112 | n/a | for b in False, True: |
---|
113 | n/a | self.assertIs(a&b, bool(int(a)&int(b))) |
---|
114 | n/a | self.assertIs(a|b, bool(int(a)|int(b))) |
---|
115 | n/a | self.assertIs(a^b, bool(int(a)^int(b))) |
---|
116 | n/a | self.assertEqual(a&int(b), int(a)&int(b)) |
---|
117 | n/a | self.assertIsNot(a&int(b), bool(int(a)&int(b))) |
---|
118 | n/a | self.assertEqual(a|int(b), int(a)|int(b)) |
---|
119 | n/a | self.assertIsNot(a|int(b), bool(int(a)|int(b))) |
---|
120 | n/a | self.assertEqual(a^int(b), int(a)^int(b)) |
---|
121 | n/a | self.assertIsNot(a^int(b), bool(int(a)^int(b))) |
---|
122 | n/a | self.assertEqual(int(a)&b, int(a)&int(b)) |
---|
123 | n/a | self.assertIsNot(int(a)&b, bool(int(a)&int(b))) |
---|
124 | n/a | self.assertEqual(int(a)|b, int(a)|int(b)) |
---|
125 | n/a | self.assertIsNot(int(a)|b, bool(int(a)|int(b))) |
---|
126 | n/a | self.assertEqual(int(a)^b, int(a)^int(b)) |
---|
127 | n/a | self.assertIsNot(int(a)^b, bool(int(a)^int(b))) |
---|
128 | n/a | |
---|
129 | n/a | self.assertIs(1==1, True) |
---|
130 | n/a | self.assertIs(1==0, False) |
---|
131 | n/a | self.assertIs(0<1, True) |
---|
132 | n/a | self.assertIs(1<0, False) |
---|
133 | n/a | self.assertIs(0<=0, True) |
---|
134 | n/a | self.assertIs(1<=0, False) |
---|
135 | n/a | self.assertIs(1>0, True) |
---|
136 | n/a | self.assertIs(1>1, False) |
---|
137 | n/a | self.assertIs(1>=1, True) |
---|
138 | n/a | self.assertIs(0>=1, False) |
---|
139 | n/a | self.assertIs(0!=1, True) |
---|
140 | n/a | self.assertIs(0!=0, False) |
---|
141 | n/a | |
---|
142 | n/a | x = [1] |
---|
143 | n/a | self.assertIs(x is x, True) |
---|
144 | n/a | self.assertIs(x is not x, False) |
---|
145 | n/a | |
---|
146 | n/a | self.assertIs(1 in x, True) |
---|
147 | n/a | self.assertIs(0 in x, False) |
---|
148 | n/a | self.assertIs(1 not in x, False) |
---|
149 | n/a | self.assertIs(0 not in x, True) |
---|
150 | n/a | |
---|
151 | n/a | x = {1: 2} |
---|
152 | n/a | self.assertIs(x is x, True) |
---|
153 | n/a | self.assertIs(x is not x, False) |
---|
154 | n/a | |
---|
155 | n/a | self.assertIs(1 in x, True) |
---|
156 | n/a | self.assertIs(0 in x, False) |
---|
157 | n/a | self.assertIs(1 not in x, False) |
---|
158 | n/a | self.assertIs(0 not in x, True) |
---|
159 | n/a | |
---|
160 | n/a | self.assertIs(not True, False) |
---|
161 | n/a | self.assertIs(not False, True) |
---|
162 | n/a | |
---|
163 | n/a | def test_convert(self): |
---|
164 | n/a | self.assertRaises(TypeError, bool, 42, 42) |
---|
165 | n/a | self.assertIs(bool(10), True) |
---|
166 | n/a | self.assertIs(bool(1), True) |
---|
167 | n/a | self.assertIs(bool(-1), True) |
---|
168 | n/a | self.assertIs(bool(0), False) |
---|
169 | n/a | self.assertIs(bool("hello"), True) |
---|
170 | n/a | self.assertIs(bool(""), False) |
---|
171 | n/a | self.assertIs(bool(), False) |
---|
172 | n/a | |
---|
173 | n/a | def test_format(self): |
---|
174 | n/a | self.assertEqual("%d" % False, "0") |
---|
175 | n/a | self.assertEqual("%d" % True, "1") |
---|
176 | n/a | self.assertEqual("%x" % False, "0") |
---|
177 | n/a | self.assertEqual("%x" % True, "1") |
---|
178 | n/a | |
---|
179 | n/a | def test_hasattr(self): |
---|
180 | n/a | self.assertIs(hasattr([], "append"), True) |
---|
181 | n/a | self.assertIs(hasattr([], "wobble"), False) |
---|
182 | n/a | |
---|
183 | n/a | def test_callable(self): |
---|
184 | n/a | self.assertIs(callable(len), True) |
---|
185 | n/a | self.assertIs(callable(1), False) |
---|
186 | n/a | |
---|
187 | n/a | def test_isinstance(self): |
---|
188 | n/a | self.assertIs(isinstance(True, bool), True) |
---|
189 | n/a | self.assertIs(isinstance(False, bool), True) |
---|
190 | n/a | self.assertIs(isinstance(True, int), True) |
---|
191 | n/a | self.assertIs(isinstance(False, int), True) |
---|
192 | n/a | self.assertIs(isinstance(1, bool), False) |
---|
193 | n/a | self.assertIs(isinstance(0, bool), False) |
---|
194 | n/a | |
---|
195 | n/a | def test_issubclass(self): |
---|
196 | n/a | self.assertIs(issubclass(bool, int), True) |
---|
197 | n/a | self.assertIs(issubclass(int, bool), False) |
---|
198 | n/a | |
---|
199 | n/a | def test_contains(self): |
---|
200 | n/a | self.assertIs(1 in {}, False) |
---|
201 | n/a | self.assertIs(1 in {1:1}, True) |
---|
202 | n/a | |
---|
203 | n/a | def test_string(self): |
---|
204 | n/a | self.assertIs("xyz".endswith("z"), True) |
---|
205 | n/a | self.assertIs("xyz".endswith("x"), False) |
---|
206 | n/a | self.assertIs("xyz0123".isalnum(), True) |
---|
207 | n/a | self.assertIs("@#$%".isalnum(), False) |
---|
208 | n/a | self.assertIs("xyz".isalpha(), True) |
---|
209 | n/a | self.assertIs("@#$%".isalpha(), False) |
---|
210 | n/a | self.assertIs("0123".isdigit(), True) |
---|
211 | n/a | self.assertIs("xyz".isdigit(), False) |
---|
212 | n/a | self.assertIs("xyz".islower(), True) |
---|
213 | n/a | self.assertIs("XYZ".islower(), False) |
---|
214 | n/a | self.assertIs("0123".isdecimal(), True) |
---|
215 | n/a | self.assertIs("xyz".isdecimal(), False) |
---|
216 | n/a | self.assertIs("0123".isnumeric(), True) |
---|
217 | n/a | self.assertIs("xyz".isnumeric(), False) |
---|
218 | n/a | self.assertIs(" ".isspace(), True) |
---|
219 | n/a | self.assertIs("\xa0".isspace(), True) |
---|
220 | n/a | self.assertIs("\u3000".isspace(), True) |
---|
221 | n/a | self.assertIs("XYZ".isspace(), False) |
---|
222 | n/a | self.assertIs("X".istitle(), True) |
---|
223 | n/a | self.assertIs("x".istitle(), False) |
---|
224 | n/a | self.assertIs("XYZ".isupper(), True) |
---|
225 | n/a | self.assertIs("xyz".isupper(), False) |
---|
226 | n/a | self.assertIs("xyz".startswith("x"), True) |
---|
227 | n/a | self.assertIs("xyz".startswith("z"), False) |
---|
228 | n/a | |
---|
229 | n/a | def test_boolean(self): |
---|
230 | n/a | self.assertEqual(True & 1, 1) |
---|
231 | n/a | self.assertNotIsInstance(True & 1, bool) |
---|
232 | n/a | self.assertIs(True & True, True) |
---|
233 | n/a | |
---|
234 | n/a | self.assertEqual(True | 1, 1) |
---|
235 | n/a | self.assertNotIsInstance(True | 1, bool) |
---|
236 | n/a | self.assertIs(True | True, True) |
---|
237 | n/a | |
---|
238 | n/a | self.assertEqual(True ^ 1, 0) |
---|
239 | n/a | self.assertNotIsInstance(True ^ 1, bool) |
---|
240 | n/a | self.assertIs(True ^ True, False) |
---|
241 | n/a | |
---|
242 | n/a | def test_fileclosed(self): |
---|
243 | n/a | try: |
---|
244 | n/a | f = open(support.TESTFN, "w") |
---|
245 | n/a | self.assertIs(f.closed, False) |
---|
246 | n/a | f.close() |
---|
247 | n/a | self.assertIs(f.closed, True) |
---|
248 | n/a | finally: |
---|
249 | n/a | os.remove(support.TESTFN) |
---|
250 | n/a | |
---|
251 | n/a | def test_types(self): |
---|
252 | n/a | # types are always true. |
---|
253 | n/a | for t in [bool, complex, dict, float, int, list, object, |
---|
254 | n/a | set, str, tuple, type]: |
---|
255 | n/a | self.assertIs(bool(t), True) |
---|
256 | n/a | |
---|
257 | n/a | def test_operator(self): |
---|
258 | n/a | import operator |
---|
259 | n/a | self.assertIs(operator.truth(0), False) |
---|
260 | n/a | self.assertIs(operator.truth(1), True) |
---|
261 | n/a | self.assertIs(operator.not_(1), False) |
---|
262 | n/a | self.assertIs(operator.not_(0), True) |
---|
263 | n/a | self.assertIs(operator.contains([], 1), False) |
---|
264 | n/a | self.assertIs(operator.contains([1], 1), True) |
---|
265 | n/a | self.assertIs(operator.lt(0, 0), False) |
---|
266 | n/a | self.assertIs(operator.lt(0, 1), True) |
---|
267 | n/a | self.assertIs(operator.is_(True, True), True) |
---|
268 | n/a | self.assertIs(operator.is_(True, False), False) |
---|
269 | n/a | self.assertIs(operator.is_not(True, True), False) |
---|
270 | n/a | self.assertIs(operator.is_not(True, False), True) |
---|
271 | n/a | |
---|
272 | n/a | def test_marshal(self): |
---|
273 | n/a | import marshal |
---|
274 | n/a | self.assertIs(marshal.loads(marshal.dumps(True)), True) |
---|
275 | n/a | self.assertIs(marshal.loads(marshal.dumps(False)), False) |
---|
276 | n/a | |
---|
277 | n/a | def test_pickle(self): |
---|
278 | n/a | import pickle |
---|
279 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
280 | n/a | self.assertIs(pickle.loads(pickle.dumps(True, proto)), True) |
---|
281 | n/a | self.assertIs(pickle.loads(pickle.dumps(False, proto)), False) |
---|
282 | n/a | |
---|
283 | n/a | def test_picklevalues(self): |
---|
284 | n/a | # Test for specific backwards-compatible pickle values |
---|
285 | n/a | import pickle |
---|
286 | n/a | self.assertEqual(pickle.dumps(True, protocol=0), b"I01\n.") |
---|
287 | n/a | self.assertEqual(pickle.dumps(False, protocol=0), b"I00\n.") |
---|
288 | n/a | self.assertEqual(pickle.dumps(True, protocol=1), b"I01\n.") |
---|
289 | n/a | self.assertEqual(pickle.dumps(False, protocol=1), b"I00\n.") |
---|
290 | n/a | self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.') |
---|
291 | n/a | self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.') |
---|
292 | n/a | |
---|
293 | n/a | def test_convert_to_bool(self): |
---|
294 | n/a | # Verify that TypeError occurs when bad things are returned |
---|
295 | n/a | # from __bool__(). This isn't really a bool test, but |
---|
296 | n/a | # it's related. |
---|
297 | n/a | check = lambda o: self.assertRaises(TypeError, bool, o) |
---|
298 | n/a | class Foo(object): |
---|
299 | n/a | def __bool__(self): |
---|
300 | n/a | return self |
---|
301 | n/a | check(Foo()) |
---|
302 | n/a | |
---|
303 | n/a | class Bar(object): |
---|
304 | n/a | def __bool__(self): |
---|
305 | n/a | return "Yes" |
---|
306 | n/a | check(Bar()) |
---|
307 | n/a | |
---|
308 | n/a | class Baz(int): |
---|
309 | n/a | def __bool__(self): |
---|
310 | n/a | return self |
---|
311 | n/a | check(Baz()) |
---|
312 | n/a | |
---|
313 | n/a | # __bool__() must return a bool not an int |
---|
314 | n/a | class Spam(int): |
---|
315 | n/a | def __bool__(self): |
---|
316 | n/a | return 1 |
---|
317 | n/a | check(Spam()) |
---|
318 | n/a | |
---|
319 | n/a | class Eggs: |
---|
320 | n/a | def __len__(self): |
---|
321 | n/a | return -1 |
---|
322 | n/a | self.assertRaises(ValueError, bool, Eggs()) |
---|
323 | n/a | |
---|
324 | n/a | def test_from_bytes(self): |
---|
325 | n/a | self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False) |
---|
326 | n/a | self.assertIs(bool.from_bytes(b'abcd', 'little'), True) |
---|
327 | n/a | |
---|
328 | n/a | def test_sane_len(self): |
---|
329 | n/a | # this test just tests our assumptions about __len__ |
---|
330 | n/a | # this will start failing if __len__ changes assertions |
---|
331 | n/a | for badval in ['illegal', -1, 1 << 32]: |
---|
332 | n/a | class A: |
---|
333 | n/a | def __len__(self): |
---|
334 | n/a | return badval |
---|
335 | n/a | try: |
---|
336 | n/a | bool(A()) |
---|
337 | n/a | except (Exception) as e_bool: |
---|
338 | n/a | try: |
---|
339 | n/a | len(A()) |
---|
340 | n/a | except (Exception) as e_len: |
---|
341 | n/a | self.assertEqual(str(e_bool), str(e_len)) |
---|
342 | n/a | |
---|
343 | n/a | def test_blocked(self): |
---|
344 | n/a | class A: |
---|
345 | n/a | __bool__ = None |
---|
346 | n/a | self.assertRaises(TypeError, bool, A()) |
---|
347 | n/a | |
---|
348 | n/a | class B: |
---|
349 | n/a | def __len__(self): |
---|
350 | n/a | return 10 |
---|
351 | n/a | __bool__ = None |
---|
352 | n/a | self.assertRaises(TypeError, bool, B()) |
---|
353 | n/a | |
---|
354 | n/a | def test_real_and_imag(self): |
---|
355 | n/a | self.assertEqual(True.real, 1) |
---|
356 | n/a | self.assertEqual(True.imag, 0) |
---|
357 | n/a | self.assertIs(type(True.real), int) |
---|
358 | n/a | self.assertIs(type(True.imag), int) |
---|
359 | n/a | self.assertEqual(False.real, 0) |
---|
360 | n/a | self.assertEqual(False.imag, 0) |
---|
361 | n/a | self.assertIs(type(False.real), int) |
---|
362 | n/a | self.assertIs(type(False.imag), int) |
---|
363 | n/a | |
---|
364 | n/a | def test_main(): |
---|
365 | n/a | support.run_unittest(BoolTest) |
---|
366 | n/a | |
---|
367 | n/a | if __name__ == "__main__": |
---|
368 | n/a | test_main() |
---|