summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ref/control/let.pngbin1484 -> 2050 bytes
-rw-r--r--tests/typ/control/for.typ3
-rw-r--r--tests/typ/control/let.typ26
-rw-r--r--tests/typ/control/while.typ3
-rw-r--r--tests/typ/expr/closure.typ66
-rw-r--r--tests/typeset.rs5
6 files changed, 93 insertions, 10 deletions
diff --git a/tests/ref/control/let.png b/tests/ref/control/let.png
index 8960f8f5..693a8d4f 100644
--- a/tests/ref/control/let.png
+++ b/tests/ref/control/let.png
Binary files differ
diff --git a/tests/typ/control/for.typ b/tests/typ/control/for.typ
index 36bce447..5eaa6ae8 100644
--- a/tests/typ/control/for.typ
+++ b/tests/typ/control/for.typ
@@ -57,9 +57,6 @@
#test(type(for v in () []), "template")
---
-// Error: 14-19 unknown variable
-#let error = error
-
// Uniterable expression.
// Error: 11-15 cannot loop over boolean
#for v in true {}
diff --git a/tests/typ/control/let.typ b/tests/typ/control/let.typ
index 8df29b11..7752ec90 100644
--- a/tests/typ/control/let.typ
+++ b/tests/typ/control/let.typ
@@ -7,9 +7,31 @@
#let x
#test(x, none)
+// Error: 9 expected expression
+#let y =
+#test(y, none)
+
// Manually initialized with one.
-#let x = 1
-#test(x, 1)
+#let z = 1
+#test(z, 1)
+
+---
+// Syntax sugar for function definitions.
+#let background = #239dad
+#let box(body) = box(width: 2cm, height: 1cm, color: background, body)
+#box[Hi!]
+
+// Error: 13 expected body
+#let func(x)
+
+// Error: 2-6 unknown variable
+{func}
+
+// Error: 15 expected expression
+#let func(x) =
+
+// Error: 2-6 unknown variable
+{func}
---
// Termination.
diff --git a/tests/typ/control/while.typ b/tests/typ/control/while.typ
index 7ad70372..acf7951e 100644
--- a/tests/typ/control/while.typ
+++ b/tests/typ/control/while.typ
@@ -26,9 +26,6 @@
#test(type(while false []), "template")
---
-// Error: 14-19 unknown variable
-#let error = error
-
// Condition must be boolean.
// Error: 8-14 expected boolean, found template
#while [nope] [nope]
diff --git a/tests/typ/expr/closure.typ b/tests/typ/expr/closure.typ
new file mode 100644
index 00000000..d05acaa4
--- /dev/null
+++ b/tests/typ/expr/closure.typ
@@ -0,0 +1,66 @@
+// 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)
+}
+
+---
+// Ref: false
+
+// 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)
+}
diff --git a/tests/typeset.rs b/tests/typeset.rs
index e1e55087..c56655f5 100644
--- a/tests/typeset.rs
+++ b/tests/typeset.rs
@@ -315,8 +315,9 @@ fn register_helpers(scope: &mut Scope, panics: Rc<RefCell<Vec<Panic>>>) {
}
};
- scope.def_const("args", ValueFunc::new("args", args));
- scope.def_const("test", ValueFunc::new("test", test));
+ scope.def_const("error", Value::Error);
+ scope.def_const("args", ValueFunc::new(Some("args".into()), args));
+ scope.def_const("test", ValueFunc::new(Some("test".into()), test));
}
fn print_diag(diag: &Diag, map: &LineMap, lines: u32) {