Debian Codegraph @

starlark/testdata/bool.star

raw
1 # Tests of Starlark 'bool'
2
3 load("assert.star", "assert")
4
5 # truth
6 assert.true(True)
7 assert.true(not False)
8 assert.true(not not True)
9 assert.true(not not 1 >= 1)
10
11 # precedence of not
12 assert.true(not not 2 > 1)
13 # assert.true(not (not 2) > 1) # TODO(adonovan): fix: gives error for False > 1.
14 # assert.true(not ((not 2) > 1)) # TODO(adonovan): fix
15 # assert.true(not ((not (not 2)) > 1)) # TODO(adonovan): fix
16 # assert.true(not not not (2 > 1))
17
18 # bool conversion
19 assert.eq(
20 [bool(), bool(1), bool(0), bool("hello"), bool("")],
21 [False, True, False, True, False],
22 )
23
24 # comparison
25 assert.true(None == None)
26 assert.true(None != False)
27 assert.true(None != True)
28 assert.eq(1 == 1, True)
29 assert.eq(1 == 2, False)
30 assert.true(False == False)
31 assert.true(True == True)
32
33 # ordered comparison
34 assert.true(False < True)
35 assert.true(False <= True)
36 assert.true(False <= False)
37 assert.true(True > False)
38 assert.true(True >= False)
39 assert.true(True >= True)
40
41 # conditional expression
42 assert.eq(1 if 3 > 2 else 0, 1)
43 assert.eq(1 if "foo" else 0, 1)
44 assert.eq(1 if "" else 0, 0)
45
46 # short-circuit evaluation of 'and' and 'or':
47 # 'or' yields the first true operand, or the last if all are false.
48 assert.eq(0 or "" or [] or 0, 0)
49 assert.eq(0 or "" or [] or 123 or 1 // 0, 123)
50 assert.fails(lambda : 0 or "" or [] or 0 or 1 // 0, "division by zero")
51
52 # 'and' yields the first false operand, or the last if all are true.
53 assert.eq(1 and "a" and [1] and 123, 123)
54 assert.eq(1 and "a" and [1] and 0 and 1 // 0, 0)
55 assert.fails(lambda : 1 and "a" and [1] and 123 and 1 // 0, "division by zero")
56
57 # Built-ins that want a bool want an actual bool, not a truth value.
58 # See github.com/bazelbuild/starlark/issues/30
59 assert.eq(''.splitlines(True), [])
60 assert.fails(lambda: ''.splitlines(1), 'got int, want bool')
61 assert.fails(lambda: ''.splitlines("hello"), 'got string, want bool')
62 assert.fails(lambda: ''.splitlines(0.0), 'got float, want bool')