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