summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-02-28 15:47:46 +0100
committerGitHub <noreply@github.com>2022-02-28 15:47:46 +0100
commitb63c21c91d99a1554a019dc275f955d3e6a34271 (patch)
treeae79152aba0a7b863be3574dd16d5d12e1e311df /tests
parent4f85fc3acd840ff8802035abcdcf2191689bb628 (diff)
parent4f09233bdae8f79ebafed43e8135f1a0285bd370 (diff)
Merge pull request #65 from typst/control-flow
Add break, continue, and return
Diffstat (limited to 'tests')
-rw-r--r--tests/ref/code/return.pngbin0 -> 2946 bytes
-rw-r--r--tests/typ/code/break-continue.typ73
-rw-r--r--tests/typ/code/return.typ51
3 files changed, 121 insertions, 3 deletions
diff --git a/tests/ref/code/return.png b/tests/ref/code/return.png
new file mode 100644
index 00000000..50410887
--- /dev/null
+++ b/tests/ref/code/return.png
Binary files differ
diff --git a/tests/typ/code/break-continue.typ b/tests/typ/code/break-continue.typ
index 2b38cf77..60dac44d 100644
--- a/tests/typ/code/break-continue.typ
+++ b/tests/typ/code/break-continue.typ
@@ -2,13 +2,84 @@
// Ref: false
---
+// Test break.
+
+#let var = 0
+#let error = false
+
#for i in range(10) {
+ var += i
if i > 5 {
- // Error: 5-10 break is not yet implemented
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
+}
+
+#f()
+
+---
+// Test continue outside of loop.
+
+// Error: 12-20 cannot continue outside of loop
+#let x = { continue }
+
---
// Error: 1-10 unexpected keyword `continue`
#continue
diff --git a/tests/typ/code/return.typ b/tests/typ/code/return.typ
index bd30c46f..9ee3aed7 100644
--- a/tests/typ/code/return.typ
+++ b/tests/typ/code/return.typ
@@ -2,9 +2,56 @@
// Ref: false
---
+// Test return with value.
#let f(x) = {
- // Error: 3-15 return is not yet implemented
return x + 1
}
-#f(1)
+#test(f(1), 2)
+
+---
+// Test return with joining.
+
+#let f(x) = {
+ "a"
+ if x == 0 {
+ return "b"
+ } else if x == 1 {
+ "c"
+ } else {
+ "d"
+ return
+ "e"
+ }
+}
+
+#test(f(0), "b")
+#test(f(1), "ac")
+#test(f(2), "ad")
+
+---
+// Test return with joining and template.
+// Ref: true
+
+#let f(text, caption: none) = {
+ text
+ if caption == none {
+ [\.]
+ return
+ }
+ [, ]
+ emph(caption)
+ [\.]
+}
+
+#f(caption: [with caption])[My figure]
+
+#f[My other figure]
+
+---
+// Test return outside of function.
+
+#for x in range(5) {
+ // Error: 3-9 cannot return outside of function
+ return
+}