From 05727bfc3a9cfd45a8e2028dfd0806f7a8f88015 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 20 Feb 2021 17:53:40 +0100 Subject: =?UTF-8?q?Reorganize=20tests=20=F0=9F=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/typ/control/for.typ | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/typ/control/for.typ (limited to 'tests/typ/control/for.typ') 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) -- cgit v1.2.3