summaryrefslogtreecommitdiff
path: root/tests/typ/control
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-02-20 17:53:40 +0100
committerLaurenz <laurmaedje@gmail.com>2021-02-20 23:34:33 +0100
commit05727bfc3a9cfd45a8e2028dfd0806f7a8f88015 (patch)
tree6c0b66eb2a9dff224cb39eb6ccb478656a706c04 /tests/typ/control
parent927341d93ae29678095e3b874bd68bfc57d4bc05 (diff)
Reorganize tests 🔀
Diffstat (limited to 'tests/typ/control')
-rw-r--r--tests/typ/control/for-invalid.typ32
-rw-r--r--tests/typ/control/for-pattern.typ30
-rw-r--r--tests/typ/control/for-value.typ20
-rw-r--r--tests/typ/control/for.typ52
-rw-r--r--tests/typ/control/if-invalid.typ28
-rw-r--r--tests/typ/control/if-value.typ21
-rw-r--r--tests/typ/control/if.typ45
-rw-r--r--tests/typ/control/let-invalid.typ20
-rw-r--r--tests/typ/control/let-terminated.typ28
-rw-r--r--tests/typ/control/let.typ11
10 files changed, 287 insertions, 0 deletions
diff --git a/tests/typ/control/for-invalid.typ b/tests/typ/control/for-invalid.typ
new file mode 100644
index 00000000..c8bdebdd
--- /dev/null
+++ b/tests/typ/control/for-invalid.typ
@@ -0,0 +1,32 @@
+// Test invalid for loop syntax.
+
+---
+// Error: 5-5 expected identifier
+#for
+
+// Error: 7-7 expected keyword `in`
+#for v
+
+// Error: 10-10 expected expression
+#for v in
+
+// Error: 15-15 expected body
+#for v in iter
+
+---
+// Should output `v in iter`.
+// Error: 5 expected identifier
+#for
+v in iter {}
+
+// Should output `A thing`.
+// Error: 7-10 expected identifier, found string
+A#for "v" thing.
+
+// Should output `in iter`.
+// Error: 6-9 expected identifier, found string
+#for "v" in iter {}
+
+// Should output `+ b in iter`.
+// Error: 7 expected keyword `in`
+#for a + b in iter {}
diff --git a/tests/typ/control/for-pattern.typ b/tests/typ/control/for-pattern.typ
new file mode 100644
index 00000000..38253b33
--- /dev/null
+++ b/tests/typ/control/for-pattern.typ
@@ -0,0 +1,30 @@
+// Test for loop patterns.
+// Ref: false
+
+---
+#let out = ()
+
+// Values of array.
+#for v in (1, 2, 3) {
+ out += (v,)
+}
+
+// Values of dictionary.
+#for v in (a: 4, b: 5) {
+ out += (v,)
+}
+
+// Keys and values of dictionary.
+#for k, v in (a: 6, b: 7) {
+ out += (k,)
+ out += (v,)
+}
+
+#test(out, (1, 2, 3, 4, 5, "a", 6, "b", 7))
+
+---
+// Keys and values of array.
+// Error: 6-10 mismatched pattern
+#for k, v in (-1, -2, -3) {
+ dont-care
+}
diff --git a/tests/typ/control/for-value.typ b/tests/typ/control/for-value.typ
new file mode 100644
index 00000000..3ab80716
--- /dev/null
+++ b/tests/typ/control/for-value.typ
@@ -0,0 +1,20 @@
+// Test return value of for loops.
+
+---
+// Template body yields template.
+// Should output `234`.
+#for v in (1, 2, 3, 4) [#if v >= 2 [{v}]]
+
+---
+// Block body yields template.
+// Should output `[1st, 2nd, 3rd, 4th, 5th, 6th]`.
+{
+ "[" + for v in (1, 2, 3, 4, 5, 6) {
+ (if v > 1 [, ]
+ + [{v}]
+ + if v == 1 [st]
+ + if v == 2 [nd]
+ + if v == 3 [rd]
+ + if v >= 4 [th])
+ } + "]"
+}
diff --git a/tests/typ/control/for.typ b/tests/typ/control/for.typ
new file mode 100644
index 00000000..294345b5
--- /dev/null
+++ b/tests/typ/control/for.typ
@@ -0,0 +1,52 @@
+// Test for loops.
+// Ref: false
+
+---
+// Array.
+
+#for x in () {}
+
+#let sum = 0
+#for x in (1, 2, 3, 4, 5) {
+ sum += x
+}
+
+#test(sum, 15)
+
+---
+// Dictionary.
+// Ref: true
+(\ #for k, v in (name: "Typst", age: 2) [
+ #h(0.5cm) {k}: {v}, \
+])
+
+---
+// String.
+{
+ let out = ""
+ let first = true
+ for c in "abc" {
+ if not first {
+ out += ", "
+ }
+ first = false
+ out += c
+ }
+ test(out, "a, b, c")
+}
+
+---
+// Uniterable expression.
+// Error: 11-15 cannot loop over boolean
+#for v in true {}
+
+// Make sure that we don't complain twice.
+// Error: 11-18 cannot add integer and string
+#for v in 1 + "2" {}
+
+// Error: 14-17 cannot apply '-' to string
+#let error = -""
+#let result = for v in (1, 2, 3) {
+ if v < 2 [Ok] else {error}
+}
+#test(result, error)
diff --git a/tests/typ/control/if-invalid.typ b/tests/typ/control/if-invalid.typ
new file mode 100644
index 00000000..6d2deab1
--- /dev/null
+++ b/tests/typ/control/if-invalid.typ
@@ -0,0 +1,28 @@
+// Test invalid if syntax.
+
+---
+// Error: 4 expected expression
+#if
+
+// Error: 4 expected expression
+{if}
+
+// Error: 6 expected body
+#if x
+
+// Error: 1-6 unexpected keyword `else`
+#else {}
+
+---
+// Should output `x`.
+// Error: 4 expected expression
+#if
+x {}
+
+// Should output `something`.
+// Error: 6 expected body
+#if x something
+
+// Should output `A thing.`
+// Error: 20 expected body
+A#if false {} #else thing
diff --git a/tests/typ/control/if-value.typ b/tests/typ/control/if-value.typ
new file mode 100644
index 00000000..d7124255
--- /dev/null
+++ b/tests/typ/control/if-value.typ
@@ -0,0 +1,21 @@
+// Test return value of if expressions.
+// Ref: false
+
+---
+{
+ let x = 1
+ let y = 2
+ let z
+
+ // Returns if branch.
+ z = if x < y { "ok" }
+ test(z, "ok")
+
+ // Returns else branch.
+ z = if x > y { "bad" } else { "ok" }
+ test(z, "ok")
+
+ // Missing else evaluates to none.
+ z = if x > y { "bad" }
+ test(z, none)
+}
diff --git a/tests/typ/control/if.typ b/tests/typ/control/if.typ
new file mode 100644
index 00000000..4ed6b649
--- /dev/null
+++ b/tests/typ/control/if.typ
@@ -0,0 +1,45 @@
+// Test if-else expressions.
+
+---
+// Test condition evaluation.
+#if 1 < 2 [
+ Ok.
+]
+
+#if true == false [
+ Bad, but we {dont-care}!
+]
+
+---
+// Brace in condition.
+#if {true} [
+ Ok.
+]
+
+// Multi-line condition with parens.
+#if (
+ 1 + 1
+ == 1
+) {
+ nope
+} #else {
+ "Ok."
+}
+
+// Multiline.
+#if false [
+ Bad.
+] #else {
+ let pt = "."
+ "Ok" + pt
+}
+
+---
+// Condition must be boolean.
+// If it isn't, neither branch is evaluated.
+// Error: 5-14 expected boolean, found string
+#if "a" + "b" { nope } #else { nope }
+
+// Make sure that we don't complain twice.
+// Error: 5-12 cannot add integer and string
+#if 1 + "2" {}
diff --git a/tests/typ/control/let-invalid.typ b/tests/typ/control/let-invalid.typ
new file mode 100644
index 00000000..f29353ed
--- /dev/null
+++ b/tests/typ/control/let-invalid.typ
@@ -0,0 +1,20 @@
+// Test invalid let binding syntax.
+
+---
+// Error: 5 expected identifier
+#let
+
+// Error: 6-9 expected identifier, found string
+#let "v"
+
+// Should output `1`.
+// Error: 7 expected semicolon or line break
+#let v 1
+
+// Error: 9 expected expression
+#let v =
+
+---
+// Should output `= 1`.
+// Error: 6-9 expected identifier, found string
+#let "v" = 1
diff --git a/tests/typ/control/let-terminated.typ b/tests/typ/control/let-terminated.typ
new file mode 100644
index 00000000..623265e0
--- /dev/null
+++ b/tests/typ/control/let-terminated.typ
@@ -0,0 +1,28 @@
+// Test termination of let statements.
+
+---
+// Terminated by line break.
+#let v1 = 1
+One
+
+// Terminated by semicolon.
+#let v2 = 2; Two
+
+// Terminated by semicolon and line break.
+#let v3 = 3;
+Three
+
+// Terminated because expression ends.
+// Error: 12 expected semicolon or line break
+#let v4 = 4 Four
+
+// Terminated by semicolon even though we are in a paren group.
+// Error: 2:19 expected expression
+// Error: 1:19 expected closing paren
+#let v5 = (1, 2 + ; Five
+
+#test(v1, 1)
+#test(v2, 2)
+#test(v3, 3)
+#test(v4, 4)
+#test(v5, (1, 2))
diff --git a/tests/typ/control/let.typ b/tests/typ/control/let.typ
new file mode 100644
index 00000000..e609d3a9
--- /dev/null
+++ b/tests/typ/control/let.typ
@@ -0,0 +1,11 @@
+// Test let bindings.
+// Ref: false
+
+---
+// Automatically initialized with none.
+#let x
+#test(x, none)
+
+// Manually initialized with one.
+#let x = 1
+#test(x, 1)