summaryrefslogtreecommitdiff
path: root/tests/typ/control/for.typ
blob: 294345b5115f7a98b5c0d26a6532d0a11c01aa19 (plain) (blame)
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
// Test for loops.
// Ref: false

---
// Array.

#for x in () {}

#let sum = 0
#for x in (1, 2, 3, 4, 5) {
    sum += x
}

#test(sum, 15)

---
// Dictionary.
// Ref: true
(\ #for k, v in (name: "Typst", age: 2) [
    #h(0.5cm) {k}: {v}, \
])

---
// String.
{
    let out = ""
    let first = true
    for c in "abc" {
        if not first {
            out += ", "
        }
        first = false
        out += c
    }
    test(out, "a, b, c")
}

---
// Uniterable expression.
// Error: 11-15 cannot loop over boolean
#for v in true {}

// Make sure that we don't complain twice.
// Error: 11-18 cannot add integer and string
#for v in 1 + "2" {}

// Error: 14-17 cannot apply '-' to string
#let error = -""
#let result = for v in (1, 2, 3) {
    if v < 2 [Ok] else {error}
}
#test(result, error)