summaryrefslogtreecommitdiff
path: root/tests/typ/compiler
diff options
context:
space:
mode:
authorMarmare314 <49279081+Marmare314@users.noreply.github.com>2023-04-06 15:26:09 +0200
committerGitHub <noreply@github.com>2023-04-06 15:26:09 +0200
commit0f8219b392e96d3cf7d784ee5d474274169d9918 (patch)
tree60ce53bb076b61b09184f4f1aefa2fa63ebb3ed2 /tests/typ/compiler
parenta73149767c82509b77ccf6996ab0b1125cc9c249 (diff)
Unpacking syntax (#532)
Closes #341
Diffstat (limited to 'tests/typ/compiler')
-rw-r--r--tests/typ/compiler/for.typ21
-rw-r--r--tests/typ/compiler/let.typ153
2 files changed, 167 insertions, 7 deletions
diff --git a/tests/typ/compiler/for.typ b/tests/typ/compiler/for.typ
index 7833aad7..1c780710 100644
--- a/tests/typ/compiler/for.typ
+++ b/tests/typ/compiler/for.typ
@@ -9,7 +9,7 @@
// Dictionary is not traversed in insertion order.
// Should output `Age: 2. Name: Typst.`.
-#for k, v in (Name: "Typst", Age: 2) [
+#for (k, v) in (Name: "Typst", Age: 2) [
#k: #v.
]
@@ -47,22 +47,22 @@
}
// Indices and values of array.
-#for i, v in ("1", "2", "3") {
+#for (i, v) in ("1", "2", "3").enumerate() {
test(repr(i + 1), v)
}
-// Values of dictionary.
+// Pairs of dictionary.
#for v in (a: 4, b: 5) {
out += (v,)
}
// Keys and values of dictionary.
-#for k, v in (a: 6, b: 7) {
+#for (k, v) in (a: 6, b: 7) {
out += (k,)
out += (v,)
}
-#test(out, (1, 2, 3, 4, 5, "a", 6, "b", 7))
+#test(out, (1, 2, 3, ("a", 4), ("b", 5), "a", 6, "b", 7))
// Grapheme clusters of string.
#let first = true
@@ -85,12 +85,19 @@
---
// Keys and values of strings.
-// Error: 6-10 mismatched pattern
-#for k, v in "hi" {
+// Error: 6-12 cannot destructure values of string
+#for (k, v) in "hi" {
dont-care
}
---
+// Destructuring without parentheses.
+// Error: 7 expected keyword `in`. did you mean to use a destructuring pattern?
+// Error: 7 expected keyword `in`
+#for k, v in (a: 4, b: 5) {
+ dont-care
+}
+
// Error: 5 expected identifier
#for
diff --git a/tests/typ/compiler/let.typ b/tests/typ/compiler/let.typ
index aa1132b9..4518f3d4 100644
--- a/tests/typ/compiler/let.typ
+++ b/tests/typ/compiler/let.typ
@@ -33,6 +33,156 @@ Three
#test(v3, 3)
---
+// Ref: false
+// Simple destructuring.
+#let (a, b) = (1, 2)
+#test(a, 1)
+#test(b, 2)
+
+---
+// Ref: false
+// Destructuring with multiple placeholders.
+#let (a, _, c, _) = (1, 2, 3, 4)
+#test(a, 1)
+#test(c, 3)
+
+---
+// Ref: false
+// Destructuring with a sink.
+#let (a, b, ..c) = (1, 2, 3, 4, 5, 6)
+#test(a, 1)
+#test(b, 2)
+#test(c, (3, 4, 5, 6))
+
+---
+// Ref: false
+// Destructuring with a sink in the middle.
+#let (a, ..b, c) = (1, 2, 3, 4, 5, 6)
+#test(a, 1)
+#test(b, (2, 3, 4, 5))
+#test(c, 6)
+
+---
+// Ref: false
+// Destructuring with an empty sink.
+#let (..a, b, c) = (1, 2)
+#test(a, ())
+#test(b, 1)
+#test(c, 2)
+
+---
+// Ref: false
+// Destructuring with an empty sink.
+#let (a, ..b, c) = (1, 2)
+#test(a, 1)
+#test(b, ())
+#test(c, 2)
+
+---
+// Ref: false
+// Destructuring with an empty sink.
+#let (a, b, ..c) = (1, 2)
+#test(a, 1)
+#test(b, 2)
+#test(c, ())
+
+---
+// Ref: false
+// Destructuring with an empty sink and empty array.
+#let (..a) = ()
+#test(a, ())
+
+---
+// Ref: false
+// Destructuring with unnamed sink.
+#let (a, .., b) = (1, 2, 3, 4)
+#test(a, 1)
+#test(b, 4)
+
+// Error: 10-11 at most one binding per identifier is allowed
+#let (a, a) = (1, 2)
+
+// Error: 12-15 at most one destructuring sink is allowed
+#let (..a, ..a) = (1, 2)
+
+// Error: 12-13 at most one binding per identifier is allowed
+#let (a, ..a) = (1, 2)
+
+// Error: 13-14 at most one binding per identifier is allowed
+#let (a: a, a) = (a: 1, b: 2)
+
+---
+// Error: 13-14 not enough elements to destructure
+#let (a, b, c) = (1, 2)
+
+---
+// Error: 6-9 too many elements to destructure
+#let (a) = (1, 2)
+
+---
+// Error: 6-20 not enough elements to destructure
+#let (..a, b, c, d) = (1, 2)
+
+---
+// Error: 6-12 cannot destructure boolean
+#let (a, b) = true
+
+---
+// Ref: false
+// Simple destructuring.
+#let (a: a, b, x: c) = (a: 1, b: 2, x: 3)
+#test(a, 1)
+#test(b, 2)
+#test(c, 3)
+
+---
+// Ref: false
+// Destructuring with a sink.
+#let (a: _, ..b) = (a: 1, b: 2, c: 3)
+#test(b, (b: 2, c: 3))
+
+---
+// Ref: false
+// Destructuring with a sink in the middle.
+#let (a: _, ..b, c: _) = (a: 1, b: 2, c: 3)
+#test(b, (b: 2))
+
+---
+// Ref: false
+// Destructuring with an empty sink.
+#let (a: _, ..b) = (a: 1)
+#test(b, (:))
+
+---
+// Ref: false
+// Destructuring with an empty sink and empty dict.
+#let (..a) = (:)
+#test(a, (:))
+
+---
+// Ref: false
+// Destructuring with unnamed sink.
+#let (a, ..) = (a: 1, b: 2)
+#test(a, 1)
+
+---
+// Error: 10-13 expected identifier, found string
+// Error: 18-19 expected identifier, found integer
+#let (a: "a", b: 2) = (a: 1, b: 2)
+
+---
+// Error: 10-11 destructuring key not found in dictionary
+#let (a, b) = (a: 1)
+
+---
+// Error: 13-14 destructuring key not found in dictionary
+#let (a, b: b) = (a: 1)
+
+---
+// Error: 7-8 cannot destructure named elements from an array
+#let (a: a, b) = (1, 2, 3)
+
+---
// Error: 5 expected identifier
#let
@@ -62,6 +212,9 @@ Three
// Error: 18 expected closing paren
#let v5 = (1, 2 + ; Five
+// Error: 9-13 expected identifier, found boolean
+#let (..true) = false
+
---
// Error: 13 expected equals sign
#let func(x)