diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-05-26 17:14:44 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-05-26 17:14:44 +0200 |
| commit | 806d9f0d9ab381500318f3e106b9c20c5eabccb7 (patch) | |
| tree | 7466967220be358c4fd8c5e26f0c3ca501fafa97 /tests/typ | |
| parent | 22214a1e0a79666caefd486e41828f015878ecb0 (diff) | |
Pure functions!
Diffstat (limited to 'tests/typ')
| -rw-r--r-- | tests/typ/code/closure.typ | 13 | ||||
| -rw-r--r-- | tests/typ/code/return.typ | 24 | ||||
| -rw-r--r-- | tests/typ/code/spread.typ | 10 | ||||
| -rw-r--r-- | tests/typ/layout/stack-1.typ | 11 |
4 files changed, 28 insertions, 30 deletions
diff --git a/tests/typ/code/closure.typ b/tests/typ/code/closure.typ index aa7bc5b9..e9389e13 100644 --- a/tests/typ/code/closure.typ +++ b/tests/typ/code/closure.typ @@ -31,7 +31,7 @@ --- // Capture environment. { - let mark = "?" + let mark = "!" let greet = { let hi = "Hi" name => { @@ -39,9 +39,10 @@ } } - test(greet("Typst"), "Hi, Typst?") + test(greet("Typst"), "Hi, Typst!") - mark = "!" + // Changing the captured variable after the closure definition has no effect. + mark = "?" test(greet("Typst"), "Hi, Typst!") } @@ -71,12 +72,12 @@ // For loop bindings. { let v = (1, 2, 3) - let s = 0 let f() = { + let s = 0 for v in v { s += v } + s } - f() - test(s, 6) + test(f(), 6) } --- diff --git a/tests/typ/code/return.typ b/tests/typ/code/return.typ index 8db99a81..0eea394e 100644 --- a/tests/typ/code/return.typ +++ b/tests/typ/code/return.typ @@ -55,30 +55,28 @@ --- // Test that the expression is evaluated to the end. -#let y = 1 -#let identity(x, ..rest) = x -#let f(x) = { - identity( - ..return, - x + 1, - y = 2, - ) +#let sum(..args) = { + let s = 0 + for v in args.positional() { + s += v + } + s +} + +#let f() = { + sum(..return, 1, 2, 3) "nope" } -#test(f(1), 2) -#test(y, 2) +#test(f(), 6) --- // Test value return from content. #let x = 3 #let f() = [ Hello 😀 - { x = 1 } #return "nope" - { x = 2 } World ] #test(f(), "nope") -#test(x, 1) diff --git a/tests/typ/code/spread.typ b/tests/typ/code/spread.typ index 86dbfd98..ff661ead 100644 --- a/tests/typ/code/spread.typ +++ b/tests/typ/code/spread.typ @@ -23,16 +23,14 @@ } --- -// Test storing arguments in a variable. +// Test doing things with arguments. { - let args - let save(..sink) = { - args = sink + let save(..args) = { + test(type(args), "arguments") + test(repr(args), "(1, 2, three: true)") } save(1, 2, three: true) - test(type(args), "arguments") - test(repr(args), "(1, 2, three: true)") } --- diff --git a/tests/typ/layout/stack-1.typ b/tests/typ/layout/stack-1.typ index 19a00de5..0ecbe246 100644 --- a/tests/typ/layout/stack-1.typ +++ b/tests/typ/layout/stack-1.typ @@ -7,13 +7,14 @@ 30pt, 50%, 20pt, 100%, ) -#let shaded = { - let v = 0% - let next() = { v += 10%; rgb(v, v, v) } - w => rect(width: w, height: 10pt, fill: next()) +#let shaded(i, w) = { + let v = (i + 1) * 10% + rect(width: w, height: 10pt, fill: rgb(v, v, v)) } -#let items = for w in widths { (align(right, shaded(w)),) } +#let items = for i, w in widths { + (align(right, shaded(i, w)),) +} #set page(width: 50pt, margins: 0pt) #stack(dir: btt, ..items) |
