File: apptest_boolobject.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (60 lines) | stat: -rw-r--r-- 1,504 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from pytest import raises

def test_bool_callable():
    assert True == bool(1)
    assert False == bool(0)
    assert False == bool()

def test_bool_string():
    assert "True" == str(True)
    assert "False" == str(False)
    assert "True" == repr(True)
    assert "False" == repr(False)

def test_bool_int():
    assert int(True) is 1
    assert int(False) is 0
    assert True.__int__() is 1

def test_bool_ops():
    assert True + True == 2
    assert False | False is False
    assert True | False is True
    assert True & True is True
    assert True ^ True is False
    assert False ^ False is False
    assert True ^ False is True
    assert True & 1 == 1
    assert False & 0 == 0 & 0

def test_bool_int_ops():
    assert True == 1
    assert 1 == True
    assert False == 0
    assert 0 == False

    assert True is not 1
    assert 1 is not True
    assert False is not 0
    assert 0 is not False

def test_new():
    assert bool.__new__(bool, "hi") is True
    assert bool.__new__(bool, "") is False
    raises(TypeError, bool.__new__, int)
    raises(TypeError, bool.__new__, 42)

def test_cant_subclass_bool():
    with raises(TypeError):
        class b(bool): pass

def test_convert_to_bool():
    check = lambda o: raises(TypeError, bool, o)
    class Spam(int):
        def __bool__():
            return 1
    raises(TypeError, bool, Spam())

def test_from_bytes():
    assert bool.from_bytes(b"", 'little') is False
    assert bool.from_bytes(b"dasijldjs" * 157, 'little') is True