summaryrefslogtreecommitdiff
path: root/tests/typ/code/closure.typ
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-05-18 00:36:11 +0200
committerLaurenz <laurmaedje@gmail.com>2021-05-18 00:36:11 +0200
commit8b58171d7ca036d71b32749286c251cc91bdd10e (patch)
tree4594ab5088edf8eec44f3bafe3fb8fecb13ac61b /tests/typ/code/closure.typ
parent8d67c0ca5eb3486dde97fd281bd4a51d535c600c (diff)
Reorganize test cases
Diffstat (limited to 'tests/typ/code/closure.typ')
-rw-r--r--tests/typ/code/closure.typ64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/typ/code/closure.typ b/tests/typ/code/closure.typ
new file mode 100644
index 00000000..86a6f632
--- /dev/null
+++ b/tests/typ/code/closure.typ
@@ -0,0 +1,64 @@
+// Test closures.
+// Ref: false
+
+---
+
+// Basic closure without captures.
+{
+ let adder = (x, y) => x + y
+ test(adder(2, 3), 5)
+}
+
+// Pass closure as argument and return closure.
+// Also uses shorthand syntax for a single argument.
+{
+ let chain = (f, g) => (x) => f(g(x))
+ let f = x => x + 1
+ let g = x => 2 * x
+ let h = chain(f, g)
+ test(h(2), 5)
+}
+
+// Capture environment.
+{
+ let mark = "?"
+ let greet = {
+ let hi = "Hi"
+ name => {
+ hi + ", " + name + mark
+ }
+ }
+
+ test(greet("Typst"), "Hi, Typst?")
+
+ mark = "!"
+ test(greet("Typst"), "Hi, Typst!")
+}
+
+// Don't leak environment.
+{
+ // Error: 18-19 unknown variable
+ let func() = x
+ let x = "hi"
+
+ test(func(), error)
+}
+
+---
+// Too few arguments.
+{
+ let types(x, y) = "[" + type(x) + ", " + type(y) + "]"
+ test(types(14%, 12pt), "[relative, length]")
+
+ // Error: 16-22 missing argument: y
+ test(types("nope"), "[string, none]")
+}
+
+// Too many arguments.
+{
+ let f(x) = x + 1
+
+ // Error: 2:10-2:15 unexpected argument
+ // Error: 1:17-1:24 unexpected argument
+ f(1, "two", () => x)
+}