summaryrefslogtreecommitdiff
path: root/tests/typ/code
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-05-26 17:14:44 +0200
committerLaurenz <laurmaedje@gmail.com>2022-05-26 17:14:44 +0200
commit806d9f0d9ab381500318f3e106b9c20c5eabccb7 (patch)
tree7466967220be358c4fd8c5e26f0c3ca501fafa97 /tests/typ/code
parent22214a1e0a79666caefd486e41828f015878ecb0 (diff)
Pure functions!
Diffstat (limited to 'tests/typ/code')
-rw-r--r--tests/typ/code/closure.typ13
-rw-r--r--tests/typ/code/return.typ24
-rw-r--r--tests/typ/code/spread.typ10
3 files changed, 22 insertions, 25 deletions
diff --git a/tests/typ/code/closure.typ b/tests/typ/code/closure.typ
index aa7bc5b9..e9389e13 100644
--- a/tests/typ/code/closure.typ
+++ b/tests/typ/code/closure.typ
@@ -31,7 +31,7 @@
---
// Capture environment.
{
- let mark = "?"
+ let mark = "!"
let greet = {
let hi = "Hi"
name => {
@@ -39,9 +39,10 @@
}
}
- test(greet("Typst"), "Hi, Typst?")
+ test(greet("Typst"), "Hi, Typst!")
- mark = "!"
+ // Changing the captured variable after the closure definition has no effect.
+ mark = "?"
test(greet("Typst"), "Hi, Typst!")
}
@@ -71,12 +72,12 @@
// For loop bindings.
{
let v = (1, 2, 3)
- let s = 0
let f() = {
+ let s = 0
for v in v { s += v }
+ s
}
- f()
- test(s, 6)
+ test(f(), 6)
}
---
diff --git a/tests/typ/code/return.typ b/tests/typ/code/return.typ
index 8db99a81..0eea394e 100644
--- a/tests/typ/code/return.typ
+++ b/tests/typ/code/return.typ
@@ -55,30 +55,28 @@
---
// Test that the expression is evaluated to the end.
-#let y = 1
-#let identity(x, ..rest) = x
-#let f(x) = {
- identity(
- ..return,
- x + 1,
- y = 2,
- )
+#let sum(..args) = {
+ let s = 0
+ for v in args.positional() {
+ s += v
+ }
+ s
+}
+
+#let f() = {
+ sum(..return, 1, 2, 3)
"nope"
}
-#test(f(1), 2)
-#test(y, 2)
+#test(f(), 6)
---
// Test value return from content.
#let x = 3
#let f() = [
Hello 😀
- { x = 1 }
#return "nope"
- { x = 2 }
World
]
#test(f(), "nope")
-#test(x, 1)
diff --git a/tests/typ/code/spread.typ b/tests/typ/code/spread.typ
index 86dbfd98..ff661ead 100644
--- a/tests/typ/code/spread.typ
+++ b/tests/typ/code/spread.typ
@@ -23,16 +23,14 @@
}
---
-// Test storing arguments in a variable.
+// Test doing things with arguments.
{
- let args
- let save(..sink) = {
- args = sink
+ let save(..args) = {
+ test(type(args), "arguments")
+ test(repr(args), "(1, 2, three: true)")
}
save(1, 2, three: true)
- test(type(args), "arguments")
- test(repr(args), "(1, 2, three: true)")
}
---