| 1 | from pytest import raises |
| 2 | |
| 3 | def test_bool_callable(): |
| 4 | assert True == bool(1) |
| 5 | assert False == bool(0) |
| 6 | assert False == bool() |
| 7 | |
| 8 | def test_bool_string(): |
| 9 | assert "True" == str(True) |
| 10 | assert "False" == str(False) |
| 11 | assert "True" == repr(True) |
| 12 | assert "False" == repr(False) |
| 13 | |
| 14 | def test_bool_int(): |
| 15 | assert int(True) is 1 |
| 16 | assert int(False) is 0 |
| 17 | assert True.__int__() is 1 |
| 18 | |
| 19 | def test_bool_ops(): |
| 20 | assert True + True == 2 |
| 21 | assert False | False is False |
| 22 | assert True | False is True |
| 23 | assert True & True is True |
| 24 | assert True ^ True is False |
| 25 | assert False ^ False is False |
| 26 | assert True ^ False is True |
| 27 | assert True & 1 == 1 |
| 28 | assert False & 0 == 0 & 0 |
| 29 | |
| 30 | def test_bool_int_ops(): |
| 31 | assert True == 1 |
| 32 | assert 1 == True |
| 33 | assert False == 0 |
| 34 | assert 0 == False |
| 35 | |
| 36 | assert True is not 1 |
| 37 | assert 1 is not True |
| 38 | assert False is not 0 |
| 39 | assert 0 is not False |
| 40 | |
| 41 | def test_new(): |
| 42 | assert bool.__new__(bool, "hi") is True |
| 43 | assert bool.__new__(bool, "") is False |
| 44 | raises(TypeError, bool.__new__, int) |
| 45 | raises(TypeError, bool.__new__, 42) |
| 46 | |
| 47 | def test_cant_subclass_bool(): |
| 48 | with raises(TypeError): |
| 49 | class b(bool): pass |
| 50 | |
| 51 | def test_convert_to_bool(): |
| 52 | check = lambda o: raises(TypeError, bool, o) |
| 53 | class Spam(int): |
| 54 | def __bool__(): |
| 55 | return 1 |
| 56 | raises(TypeError, bool, Spam()) |
| 57 | |
| 58 | def test_from_bytes(): |
| 59 | assert bool.from_bytes(b"", 'little') is False |
| 60 | assert bool.from_bytes(b"dasijldjs" * 157, 'little') is True |