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