summaryrefslogtreecommitdiff
path: root/tests
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
parent22214a1e0a79666caefd486e41828f015878ecb0 (diff)
Pure functions!
Diffstat (limited to 'tests')
-rw-r--r--tests/typ/code/closure.typ13
-rw-r--r--tests/typ/code/return.typ24
-rw-r--r--tests/typ/code/spread.typ10
-rw-r--r--tests/typ/layout/stack-1.typ11
-rw-r--r--tests/typeset.rs6
5 files changed, 31 insertions, 33 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)")
}
---
diff --git a/tests/typ/layout/stack-1.typ b/tests/typ/layout/stack-1.typ
index 19a00de5..0ecbe246 100644
--- a/tests/typ/layout/stack-1.typ
+++ b/tests/typ/layout/stack-1.typ
@@ -7,13 +7,14 @@
30pt, 50%, 20pt, 100%,
)
-#let shaded = {
- let v = 0%
- let next() = { v += 10%; rgb(v, v, v) }
- w => rect(width: w, height: 10pt, fill: next())
+#let shaded(i, w) = {
+ let v = (i + 1) * 10%
+ rect(width: w, height: 10pt, fill: rgb(v, v, v))
}
-#let items = for w in widths { (align(right, shaded(w)),) }
+#let items = for i, w in widths {
+ (align(right, shaded(i, w)),)
+}
#set page(width: 50pt, margins: 0pt)
#stack(dir: btt, ..items)
diff --git a/tests/typeset.rs b/tests/typeset.rs
index b334ae9a..72bdb431 100644
--- a/tests/typeset.rs
+++ b/tests/typeset.rs
@@ -70,10 +70,10 @@ fn main() {
);
styles.set(TextNode::SIZE, TextSize(Length::pt(10.0).into()));
- // Hook up an assert function into the global scope.
+ // Hook up two more colors and an assert function into the global scope.
let mut std = typst::library::new();
- std.def_const("conifer", RgbaColor::new(0x9f, 0xEB, 0x52, 0xFF));
- std.def_const("forest", RgbaColor::new(0x43, 0xA1, 0x27, 0xFF));
+ std.define("conifer", RgbaColor::new(0x9f, 0xEB, 0x52, 0xFF));
+ std.define("forest", RgbaColor::new(0x43, 0xA1, 0x27, 0xFF));
std.def_fn("test", move |_, args| {
let lhs = args.expect::<Value>("left-hand side")?;
let rhs = args.expect::<Value>("right-hand side")?;