1# Tests of Starlark 'bool' 2 3load("assert.star", "assert") 4 5# truth 6assert.true(True) 7assert.true(not False) 8assert.true(not not True) 9assert.true(not not 1 >= 1) 10 11# precedence of not 12assert.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 19assert.eq( 20 [bool(), bool(1), bool(0), bool("hello"), bool("")], 21 [False, True, False, True, False], 22) 23 24# comparison 25assert.true(None == None) 26assert.true(None != False) 27assert.true(None != True) 28assert.eq(1 == 1, True) 29assert.eq(1 == 2, False) 30assert.true(False == False) 31assert.true(True == True) 32 33# ordered comparison 34assert.true(False < True) 35assert.true(False <= True) 36assert.true(False <= False) 37assert.true(True > False) 38assert.true(True >= False) 39assert.true(True >= True) 40 41# conditional expression 42assert.eq(1 if 3 > 2 else 0, 1) 43assert.eq(1 if "foo" else 0, 1) 44assert.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. 48assert.eq(0 or "" or [] or 0, 0) 49assert.eq(0 or "" or [] or 123 or 1 // 0, 123) 50assert.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. 53assert.eq(1 and "a" and [1] and 123, 123) 54assert.eq(1 and "a" and [1] and 0 and 1 // 0, 0) 55assert.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 59assert.eq(''.splitlines(True), []) 60assert.fails(lambda: ''.splitlines(1), 'got int, want bool') 61assert.fails(lambda: ''.splitlines("hello"), 'got string, want bool') 62assert.fails(lambda: ''.splitlines(0.0), 'got float, want bool') 63