summaryrefslogtreecommitdiff
path: root/tests/typ
diff options
context:
space:
mode:
Diffstat (limited to 'tests/typ')
-rw-r--r--tests/typ/code/for.typ8
-rw-r--r--tests/typ/code/if.typ8
-rw-r--r--tests/typ/code/methods.typ50
-rw-r--r--tests/typ/code/ops-invalid.typ12
-rw-r--r--tests/typ/code/ops-prec.typ2
-rw-r--r--tests/typ/code/ops.typ18
-rw-r--r--tests/typ/code/target.typ2
-rw-r--r--tests/typ/graphics/line.typ4
-rw-r--r--tests/typ/graphics/shape-fill-stroke.typ30
-rw-r--r--tests/typ/text/deco.typ4
-rw-r--r--tests/typ/utility/basics.typ60
-rw-r--r--tests/typ/utility/collection.typ99
-rw-r--r--tests/typ/utility/math.typ27
-rw-r--r--tests/typ/utility/numbering.typ19
-rw-r--r--tests/typ/utility/string.typ52
15 files changed, 242 insertions, 153 deletions
diff --git a/tests/typ/code/for.typ b/tests/typ/code/for.typ
index e161ba84..822f7423 100644
--- a/tests/typ/code/for.typ
+++ b/tests/typ/code/for.typ
@@ -32,10 +32,10 @@
// Should output `2345`.
#for v in (1, 2, 3, 4, 5, 6, 7) [#if v >= 2 and v <= 5 { repr(v) }]
-// Loop over captured arguments.
-#let f1(..args) = for v in args { (repr(v),) }
-#let f2(..args) = for k, v in args { (repr(k) + ": " + repr(v),) }
-#let f(..args) = join(sep: ", ", ..f1(..args), ..f2(..args))
+// Map captured arguments.
+#let f1(..args) = args.positional().map(repr)
+#let f2(..args) = args.named().pairs((k, v) => repr(k) + ": " + repr(v))
+#let f(..args) = (f1(..args) + f2(..args)).join(", ")
#f(1, a: 2)
---
diff --git a/tests/typ/code/if.typ b/tests/typ/code/if.typ
index 0ab5c495..0d87c689 100644
--- a/tests/typ/code/if.typ
+++ b/tests/typ/code/if.typ
@@ -60,10 +60,10 @@
#let nth(n) = {
str(n)
- (if n == 1 { "st" }
- else if n == 2 { "nd" }
- else if n == 3 { "rd" }
- else { "th" })
+ if n == 1 { "st" }
+ else if n == 2 { "nd" }
+ else if n == 3 { "rd" }
+ else { "th" }
}
#test(nth(1), "1st")
diff --git a/tests/typ/code/methods.typ b/tests/typ/code/methods.typ
new file mode 100644
index 00000000..b5eff78d
--- /dev/null
+++ b/tests/typ/code/methods.typ
@@ -0,0 +1,50 @@
+// Test method calls.
+// Ref: false
+
+---
+// Test whitespace around dot.
+#test( "Hi there" . split() , ("Hi", "there"))
+
+---
+// Test mutating indexed value.
+{
+ let matrix = (((1,), (2,)), ((3,), (4,)))
+ matrix(1)(0).push(5)
+ test(matrix, (((1,), (2,)), ((3, 5), (4,))))
+}
+
+---
+// Test multiline chain in code block.
+{
+ let rewritten = "Hello. This is a sentence. And one more."
+ .split(".")
+ .map(s => s.trim())
+ .filter(s => s != "")
+ .map(s => s + "!")
+ .join([\ ])
+
+ test(rewritten, [Hello!\ This is a sentence!\ And one more!])
+}
+
+---
+// Error: 2:3-2:16 type array has no method `fun`
+#let numbers = ()
+{ numbers.fun() }
+
+---
+// Error: 2:3-2:44 cannot mutate a temporary value
+#let numbers = (1, 2, 3)
+{ numbers.map(v => v / 2).sorted().map(str).remove(4) }
+
+---
+// Error: 2:3-2:19 cannot mutate a temporary value
+#let numbers = (1, 2, 3)
+{ numbers.sorted() = 1 }
+
+---
+// Error: 3-6 cannot mutate a constant
+{ box = 1 }
+
+---
+// Error: 3-6 cannot mutate a constant
+{ box.push(1) }
diff --git a/tests/typ/code/ops-invalid.typ b/tests/typ/code/ops-invalid.typ
index 184e20cf..68bce4af 100644
--- a/tests/typ/code/ops-invalid.typ
+++ b/tests/typ/code/ops-invalid.typ
@@ -65,19 +65,11 @@
{ let x = 1; x += "2" }
---
-// Error: 13-14 expected argument list, found integer
-{ test with 2 }
-
----
-// Error: 3-4 expected function, found integer
-{ 1 with () }
-
----
-// Error: 3-6 cannot access this expression mutably
+// Error: 3-6 cannot mutate a temporary value
{ (x) = "" }
---
-// Error: 3-8 cannot access this expression mutably
+// Error: 3-8 cannot mutate a temporary value
{ 1 + 2 += 3 }
---
diff --git a/tests/typ/code/ops-prec.typ b/tests/typ/code/ops-prec.typ
index 2cec0d04..23afcc5f 100644
--- a/tests/typ/code/ops-prec.typ
+++ b/tests/typ/code/ops-prec.typ
@@ -13,7 +13,7 @@
#test(not "b" == "b", false)
// Assignment binds stronger than boolean operations.
-// Error: 2-7 cannot access this expression mutably
+// Error: 2-7 cannot mutate a temporary value
{not x = "a"}
---
diff --git a/tests/typ/code/ops.typ b/tests/typ/code/ops.typ
index 899ee71c..79743f5d 100644
--- a/tests/typ/code/ops.typ
+++ b/tests/typ/code/ops.typ
@@ -184,21 +184,19 @@
{"a" not}
---
-// Test `with` operator.
+// Test `with` method.
// Apply positional arguments.
#let add(x, y) = x + y
-#test((add with (2))(4), 6)
-
-// Let .. with .. syntax.
-#let f = add
-#let f with (2)
-#test(f(4), 6)
+#test(add.with(2)(3), 5)
+#test(add.with(2).with(3)(), 5)
+#test((add.with(2))(4), 6)
+#test((add.with(2).with(3))(), 5)
// Make sure that named arguments are overridable.
#let inc(x, y: 1) = x + y
#test(inc(1), 2)
-#let inc with (y: 2)
-#test(inc(2), 4)
-#test(inc(2, y: 4), 6)
+#let inc2 = inc.with(y: 2)
+#test(inc2(2), 4)
+#test(inc2(2, y: 4), 6)
diff --git a/tests/typ/code/target.typ b/tests/typ/code/target.typ
index 73516817..6c321592 100644
--- a/tests/typ/code/target.typ
+++ b/tests/typ/code/target.typ
@@ -7,6 +7,6 @@
#let d = 3
#let value = [hi]
#let item(a, b) = a + b
-#let fn = rect with (fill: conifer, padding: 5pt)
+#let fn = rect.with(fill: conifer, padding: 5pt)
Some _includable_ text.
diff --git a/tests/typ/graphics/line.typ b/tests/typ/graphics/line.typ
index 452e52f3..050ce05c 100644
--- a/tests/typ/graphics/line.typ
+++ b/tests/typ/graphics/line.typ
@@ -34,10 +34,10 @@
#line(length: +30%, origin: (25.4%, 48%), angle: -36deg)
#line(length: +30%, origin: (25.6%, 48%), angle: -72deg)
#line(length: +32%, origin: (8.50%, 02%), angle: 34deg)
- ]
+ ]
]
-#align(center, grid(columns: (1fr, ) * 3, ..((star(20pt, thickness: .5pt), ) * 9)))
+#align(center, grid(columns: (1fr,) * 3, ..((star(20pt, thickness: .5pt),) * 9)))
---
// Test errors.
diff --git a/tests/typ/graphics/shape-fill-stroke.typ b/tests/typ/graphics/shape-fill-stroke.typ
index 935f3bc7..dd5b9ee8 100644
--- a/tests/typ/graphics/shape-fill-stroke.typ
+++ b/tests/typ/graphics/shape-fill-stroke.typ
@@ -1,22 +1,22 @@
// Test shape fill & stroke.
---
-#let rect with (width: 20pt, height: 10pt)
-#let items = for i, rect in (
- rect(stroke: none),
- rect(),
- rect(fill: none),
- rect(thickness: 2pt),
- rect(stroke: eastern),
- rect(stroke: eastern, thickness: 2pt),
- rect(fill: eastern),
- rect(fill: eastern, stroke: none),
- rect(fill: forest, stroke: none, thickness: 2pt),
- rect(fill: forest, stroke: conifer),
- rect(fill: forest, stroke: black, thickness: 2pt),
- rect(fill: forest, stroke: conifer, thickness: 2pt),
+#let variant = rect.with(width: 20pt, height: 10pt)
+#let items = for i, item in (
+ variant(stroke: none),
+ variant(),
+ variant(fill: none),
+ variant(thickness: 2pt),
+ variant(stroke: eastern),
+ variant(stroke: eastern, thickness: 2pt),
+ variant(fill: eastern),
+ variant(fill: eastern, stroke: none),
+ variant(fill: forest, stroke: none, thickness: 2pt),
+ variant(fill: forest, stroke: conifer),
+ variant(fill: forest, stroke: black, thickness: 2pt),
+ variant(fill: forest, stroke: conifer, thickness: 2pt),
) {
- (align(horizon)[{i + 1}.], rect, [])
+ (align(horizon)[{i + 1}.], item, [])
}
#grid(
diff --git a/tests/typ/text/deco.typ b/tests/typ/text/deco.typ
index e0693ca3..a9f380b9 100644
--- a/tests/typ/text/deco.typ
+++ b/tests/typ/text/deco.typ
@@ -19,8 +19,8 @@
#overline(underline[Running amongst the wolves.])
---
-#let redact = strike with (10pt, extent: 5%)
-#let highlight = strike with (
+#let redact = strike.with(10pt, extent: 5%)
+#let highlight = strike.with(
stroke: rgb("abcdef88"),
thickness: 10pt,
extent: 5%,
diff --git a/tests/typ/utility/basics.typ b/tests/typ/utility/basics.typ
index 7fccc781..83d192c4 100644
--- a/tests/typ/utility/basics.typ
+++ b/tests/typ/utility/basics.typ
@@ -21,64 +21,4 @@
// Test the `type` function.
#test(type(1), "integer")
#test(type(ltr), "direction")
-
----
-// Test the `repr` function.
-#test(repr(ltr), "ltr")
-#test(repr((1, 2, false, )), "(1, 2, false)")
-
----
-// Test the `join` function.
-#test(join(), none)
-#test(join(sep: false), none)
-#test(join(1), 1)
-#test(join("a", "b", "c"), "abc")
-#test("(" + join("a", "b", "c", sep: ", ") + ")", "(a, b, c)")
-
----
-// Test content joining.
-// Ref: true
-#join([One], [Two], [Three], sep: [, ]).
-
----
-// Error: 11-24 cannot join boolean with boolean
-#test(join(true, false))
-
----
-// Error: 11-29 cannot join string with integer
-#test(join("a", "b", sep: 1))
-
----
-// Test conversion functions.
-#test(int(false), 0)
-#test(int(true), 1)
-#test(int(10), 10)
-#test(int("150"), 150)
#test(type(10 / 3), "float")
-#test(int(10 / 3), 3)
-#test(float(10), 10.0)
-#test(float("31.4e-1"), 3.14)
-#test(type(float(10)), "float")
-#test(str(123), "123")
-#test(str(50.14), "50.14")
-#test(len(str(10 / 3)) > 10, true)
-
----
-// Error: 6-10 cannot convert length to integer
-#int(10pt)
-
----
-// Error: 8-13 cannot convert function to float
-#float(float)
-
----
-// Error: 6-8 cannot convert content to string
-#str([])
-
----
-// Error: 6-12 invalid integer
-#int("nope")
-
----
-// Error: 8-15 invalid float
-#float("1.2.3")
diff --git a/tests/typ/utility/collection.typ b/tests/typ/utility/collection.typ
index e8be07b5..924200cb 100644
--- a/tests/typ/utility/collection.typ
+++ b/tests/typ/utility/collection.typ
@@ -2,42 +2,91 @@
// Ref: false
---
-// Test the `len` function.
-#test(len(()), 0)
-#test(len(("A", "B", "C")), 3)
-#test(len("Hello World!"), 12)
-#test(len((a: 1, b: 2)), 2)
+// Test the `len` method.
+#test(().len(), 0)
+#test(("A", "B", "C").len(), 3)
+#test("Hello World!".len(), 12)
+#test((a: 1, b: 2).len(), 2)
---
-// Error: 5-7 missing argument: collection
-#len()
+// Test the `push` and `pop` methods.
+{
+ let tasks = (a: (1, 2, 3), b: (4, 5, 6))
+ tasks("a").pop()
+ tasks("b").push(7)
+ test(tasks("a"), (1, 2))
+ test(tasks("b"), (4, 5, 6, 7))
+}
---
-// Error: 6-10 expected string, array or dictionary, found length
-#len(12pt)
+// Test the `insert` and `remove` methods.
+{
+ let array = (0, 1, 2, 4, 5)
+ array.insert(3, 3)
+ test(array, range(6))
+ array.remove(1)
+ test(array, (0, 2, 3, 4, 5))
+}
---
-// Test the `upper` and `lower` functions.
-#let memes = "ArE mEmEs gReAt?";
-#test(lower(memes), "are memes great?")
-#test(upper(memes), "ARE MEMES GREAT?")
-#test(upper("Ελλάδα"), "ΕΛΛΆΔΑ")
+// Test the `find` method.
+#test(("Hi", "❤️", "Love").find("❤️"), 1)
+#test(("Bye", "💘", "Apart").find("❤️"), none)
---
-// Error: 8-9 expected string or content, found integer
-#upper(1)
+// Test the `slice` method.
+#test((1, 2, 3, 4).slice(2), (3, 4))
+#test(range(10).slice(2, 6), (2, 3, 4, 5))
+#test(range(10).slice(4, count: 3), (4, 5, 6))
---
-// Test the `sorted` function.
-#test(sorted(()), ())
-#test(sorted((true, false) * 10), (false,) * 10 + (true,) * 10)
-#test(sorted(("it", "the", "hi", "text")), ("hi", "it", "text", "the"))
-#test(sorted((2, 1, 3, 10, 5, 8, 6, -7, 2)), (-7, 1, 2, 2, 3, 5, 6, 8, 10))
+// Error: 3-31 array index out of bounds (index: 12, len: 10)
+{ range(10).slice(9, count: 3) }
---
-// Error: 9-21 cannot order string and integer
-#sorted((1, 2, "ab"))
+// Error: 2:17-2:19 missing argument: index
+#let numbers = ()
+{ numbers.insert() }
---
-// Error: 9-24 cannot order content and content
-#sorted(([Hi], [There]))
+// Test the `join` method.
+#test(().join(), none)
+#test((1,).join(), 1)
+#test(("a", "b", "c").join(), "abc")
+#test("(" + ("a", "b", "c").join(", ") + ")", "(a, b, c)")
+
+---
+// Error: 2-22 cannot join boolean with boolean
+{(true, false).join()}
+
+---
+// Error: 2-20 cannot join string with integer
+{("a", "b").join(1)}
+
+---
+// Test joining content.
+// Ref: true
+{([One], [Two], [Three]).join([, ], last: [ and ])}.
+
+---
+// Test the `sorted` method.
+#test(().sorted(), ())
+#test(((true, false) * 10).sorted(), (false,) * 10 + (true,) * 10)
+#test(("it", "the", "hi", "text").sorted(), ("hi", "it", "text", "the"))
+#test((2, 1, 3, 10, 5, 8, 6, -7, 2).sorted(), (-7, 1, 2, 2, 3, 5, 6, 8, 10))
+
+---
+// Error: 2-26 cannot order content and content
+{([Hi], [There]).sorted()}
+
+---
+// Test dictionary methods.
+#let dict = (a: 3, c: 2, b: 1)
+#test("c" in dict, true)
+#test(dict.len(), 3)
+#test(dict.values(), (3, 1, 2))
+#test(dict.pairs((k, v) => k + str(v)).join(), "a3b1c2")
+
+{ dict.remove("c") }
+#test("c" in dict, false)
+#test(dict, (a: 3, b: 1))
diff --git a/tests/typ/utility/math.typ b/tests/typ/utility/math.typ
index ec62dbd2..d4ac7aa2 100644
--- a/tests/typ/utility/math.typ
+++ b/tests/typ/utility/math.typ
@@ -2,6 +2,33 @@
// Ref: false
---
+// Test conversion to numbers.
+#test(int(false), 0)
+#test(int(true), 1)
+#test(int(10), 10)
+#test(int("150"), 150)
+#test(int(10 / 3), 3)
+#test(float(10), 10.0)
+#test(float("31.4e-1"), 3.14)
+#test(type(float(10)), "float")
+
+---
+// Error: 6-10 cannot convert length to integer
+#int(10pt)
+
+---
+// Error: 8-13 cannot convert function to float
+#float(float)
+
+---
+// Error: 6-12 invalid integer
+#int("nope")
+
+---
+// Error: 8-15 invalid float
+#float("1.2.3")
+
+---
// Test the `abs` function.
#test(abs(-3), 3)
#test(abs(3), 3)
diff --git a/tests/typ/utility/numbering.typ b/tests/typ/utility/numbering.typ
deleted file mode 100644
index 65dc12d0..00000000
--- a/tests/typ/utility/numbering.typ
+++ /dev/null
@@ -1,19 +0,0 @@
-// Test numbering formatting functions.
-
----
-#upper("Abc 8")
-#upper[def]
-
-#lower("SCREAMING MUST BE SILENCED in " + roman(1672) + " years")
-
-#for i in range(9) {
- symbol(i)
- [ and ]
- roman(i)
- [ for #i]
- parbreak()
-}
-
----
-// Error: 9-11 must be at least zero
-#symbol(-1)
diff --git a/tests/typ/utility/string.typ b/tests/typ/utility/string.typ
new file mode 100644
index 00000000..9b57e833
--- /dev/null
+++ b/tests/typ/utility/string.typ
@@ -0,0 +1,52 @@
+// Test string related methods.
+// Ref: false
+
+---
+// Test conversion to string.
+#test(str(123), "123")
+#test(str(50.14), "50.14")
+#test(str(10 / 3).len() > 10, true)
+#test(repr(ltr), "ltr")
+#test(repr((1, 2, false, )), "(1, 2, false)")
+
+---
+// Error: 6-8 cannot convert content to string
+#str([])
+
+---
+// Test the `split` and `trim` methods.
+#test(
+ "Typst, LaTeX, Word, InDesign".split(",").map(s => s.trim()),
+ ("Typst", "LaTeX", "Word", "InDesign"),
+)
+
+---
+// Test the `upper` and `lower` functions.
+#let memes = "ArE mEmEs gReAt?";
+#test(lower(memes), "are memes great?")
+#test(upper(memes), "ARE MEMES GREAT?")
+#test(upper("Ελλάδα"), "ΕΛΛΆΔΑ")
+
+---
+// Error: 8-9 expected string or content, found integer
+#upper(1)
+
+---
+// Error: 9-11 must be at least zero
+#symbol(-1)
+
+---
+// Test integrated lower, upper and symbols.
+// Ref: true
+#upper("Abc 8")
+#upper[def]
+
+#lower("SCREAMING MUST BE SILENCED in " + roman(1672) + " years")
+
+#for i in range(9) {
+ symbol(i)
+ [ and ]
+ roman(i)
+ [ for #i]
+ parbreak()
+}