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
61
62
63
64
|
# Tests of Starlark control flow
load("assert.star", "assert")
def controlflow():
# elif
x = 0
if True:
x=1
elif False:
assert.fail("else of true")
else:
assert.fail("else of else of true")
assert.true(x)
x = 0
if False:
assert.fail("then of false")
elif True:
x = 1
else:
assert.fail("else of true")
assert.true(x)
x = 0
if False:
assert.fail("then of false")
elif False:
assert.fail("then of false")
else:
x = 1
assert.true(x)
controlflow()
def loops():
y = ""
for x in [1, 2, 3, 4, 5]:
if x == 2:
continue
if x == 4:
break
y = y + str(x)
return y
assert.eq(loops(), "13")
# return
g = 123
def f(x):
for g in (1, 2, 3):
if g == x:
return g
assert.eq(f(2), 2)
assert.eq(f(4), None) # falling off end => return None
assert.eq(g, 123) # unchanged by local use of g in function
# infinite sequences
def fib(n):
seq = []
for x in fibonacci: # fibonacci is an infinite iterable defined in eval_test.go
if len(seq) == n:
break
seq.append(x)
return seq
assert.eq(fib(10), [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
|