diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-03-18 23:36:18 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-03-18 23:43:58 +0100 |
| commit | beca01c826ee51c9ee6d5eadd7e5ef10f7fb9f58 (patch) | |
| tree | e0ebb40b8775bba3b4be7bc47dceda3d349e2ac0 /tests/typ/utility | |
| parent | 77d153d315a2a5909840ebcd47491e4cef14428b (diff) | |
Methods
Diffstat (limited to 'tests/typ/utility')
| -rw-r--r-- | tests/typ/utility/basics.typ | 60 | ||||
| -rw-r--r-- | tests/typ/utility/collection.typ | 99 | ||||
| -rw-r--r-- | tests/typ/utility/math.typ | 27 | ||||
| -rw-r--r-- | tests/typ/utility/numbering.typ | 19 | ||||
| -rw-r--r-- | tests/typ/utility/string.typ | 52 |
5 files changed, 153 insertions, 104 deletions
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() +} |
