summaryrefslogtreecommitdiff
path: root/tests/typ/code/break-continue.typ
blob: fb3222fa29e2e4515d7105de86d7976afa7e564d (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Test break and continue in loops.
// Ref: false

---
// Test break.

#let var = 0
#let error = false

#for i in range(10) {
  var += i
  if i > 5 {
    break
    error = true
  }
}

#test(var, 21)
#test(error, false)

---
// Test joining with break.

#let i = 0
#let x = while true {
  i += 1
  str(i)
  if i >= 5 {
    "."
    break
  }
}

#test(x, "12345.")

---
// Test continue.

#let i = 0
#let x = 0

#while x < 8 {
  i += 1
  if mod(i, 3) == 0 {
    continue
  }
  x += i
}

// If continue did not work, this would equal 10.
#test(x, 12)

---
// Test joining with continue.

#let x = for i in range(5) {
  "a"
  if mod(i, 3) == 0 {
    "_"
    continue
  }
  str(i)
}

#test(x, "a_a1a2a_a4")

---
// Test break outside of loop.
#let f() = {
  // Error: 3-8 cannot break outside of loop
  break
}

#for i in range(1) {
  f()
}

---
// Test break in function call.
#let identity(x) = x
#let out = for i in range(5) {
  "A"
  identity({
    "B"
    break
  })
  "C"
}

#test(out, "AB")

---
// Test continue outside of loop.

// Error: 12-20 cannot continue outside of loop
#let x = { continue }

---
// Error: 1-10 cannot continue outside of loop
#continue

---
// Ref: true
// Should output `Hello World 🌎`.
#for _ in range(10) {
  [Hello ]
  [World {
    [🌎]
    break
  }]
}

---
// Ref: true
// Should output `Some` in red, `Some` in blue and `Last` in green.
// Everything should be in smallcaps.
#for color in (red, blue, green, yellow) [
  #set text("Roboto")
  #show it => text(fill: color, it)
  #smallcaps(if color != green [
    Some
  ] else [
    Last
    #break
  ])
]

---
// Ref: true
// Test break in set rule.
// Should output `Hi` in blue.
#for i in range(10) {
  [Hello]
  set text(blue, ..break)
  [Not happening]
}

---
// Test second block during break flow.
// Ref: true

#for i in range(10) {
  table(
    { [A]; break },
    for _ in range(3) [B]
  )
}