diff options
| author | Martin Haug <mhaug@live.de> | 2022-05-27 16:39:06 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-27 16:39:06 +0200 |
| commit | 73086b5a7c1b0f9f638165803c237901499adb64 (patch) | |
| tree | c120a2449aaf325cb675ea3363ee69758a734d86 /tests | |
| parent | 99cb655832161d4ebec73273a15453a8f6acc1b7 (diff) | |
| parent | 8ba11b0722599892499337b3272cec38945d11de (diff) | |
Merge pull request #71 from typst/pins
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ref/layout/locate-break.png | bin | 0 -> 122 bytes | |||
| -rw-r--r-- | tests/ref/layout/locate-group.png | bin | 0 -> 3881 bytes | |||
| -rw-r--r-- | tests/ref/layout/locate.png | bin | 0 -> 2795 bytes | |||
| -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/locate-break.typ | 5 | ||||
| -rw-r--r-- | tests/typ/layout/locate-group.typ | 62 | ||||
| -rw-r--r-- | tests/typ/layout/locate.typ | 22 | ||||
| -rw-r--r-- | tests/typ/layout/stack-1.typ | 11 | ||||
| -rw-r--r-- | tests/typeset.rs | 17 |
11 files changed, 131 insertions, 33 deletions
diff --git a/tests/ref/layout/locate-break.png b/tests/ref/layout/locate-break.png Binary files differnew file mode 100644 index 00000000..25abb4a0 --- /dev/null +++ b/tests/ref/layout/locate-break.png diff --git a/tests/ref/layout/locate-group.png b/tests/ref/layout/locate-group.png Binary files differnew file mode 100644 index 00000000..e38a4a32 --- /dev/null +++ b/tests/ref/layout/locate-group.png diff --git a/tests/ref/layout/locate.png b/tests/ref/layout/locate.png Binary files differnew file mode 100644 index 00000000..015c5a6d --- /dev/null +++ b/tests/ref/layout/locate.png 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/locate-break.typ b/tests/typ/layout/locate-break.typ new file mode 100644 index 00000000..28631cfa --- /dev/null +++ b/tests/typ/layout/locate-break.typ @@ -0,0 +1,5 @@ +// Test locate with crazy pagebreaks. + +--- +#set page(height: 10pt) +{3 * locate(me => me.page * pagebreak())} diff --git a/tests/typ/layout/locate-group.typ b/tests/typ/layout/locate-group.typ new file mode 100644 index 00000000..5c022f5a --- /dev/null +++ b/tests/typ/layout/locate-group.typ @@ -0,0 +1,62 @@ +// Test locatable groups. + +--- +// Test counting. +#let letters = group("\u{1F494}") +#let counter = letters.entry( + (me, all) => [{1 + me.index} / {all.len()}] +) + +#counter \ +#box(counter) \ +#counter \ + +--- +// Test minimal citation engine with references before the document. +#let cited = group("citations") +#let num(cited, key) = { + let index = 0 + for item in cited { + if item.value == key { + index = item.index + break + } + } + [\[{index + 1}\]] +} + +#let cite(key) = cited.entry(value: key, (_, all) => num(all, key)) +{cited.all(all => grid( + columns: (auto, 1fr), + gutter: 5pt, + ..{ + let seen = () + for item in all { + if item.value not in seen { + seen.push(item.value) + (num(all, item.value), item.value) + } + } + } +))} + +As shown in #cite("abc") and #cite("def") and #cite("abc") ... + +--- +// Test that `all` contains `me`. +// Ref: false +#show it: heading as group("headings").entry( + (me, all) => { + let last + for prev in all { + last = prev + if prev.index == me.index { + break + } + } + assert(last == me) + } +) + += A +== B diff --git a/tests/typ/layout/locate.typ b/tests/typ/layout/locate.typ new file mode 100644 index 00000000..ec2262c5 --- /dev/null +++ b/tests/typ/layout/locate.typ @@ -0,0 +1,22 @@ +// Test locate me. + +--- +#set page(height: 60pt) +#let pin = locate(me => box({ + let c(length) = str(int(length / 1pt ) ) + square(size: 1.5pt, fill: blue) + h(0.15em) + text(0.5em)[{me.page}, #c(me.x), #c(me.y)] +})) + +#place(rotate(origin: top + left, 25deg, move(dx: 40pt, pin))) + +#pin +#h(10pt) +#box(pin) \ +#pin + +#place(bottom + right, pin) + +#pagebreak() +#align(center + horizon, pin + [\ ] + pin) 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) diff --git a/tests/typeset.rs b/tests/typeset.rs index b334ae9a..0d603125 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -70,10 +70,10 @@ fn main() { ); styles.set(TextNode::SIZE, TextSize(Length::pt(10.0).into())); - // Hook up an assert function into the global scope. + // Hook up helpers into the global scope. let mut std = typst::library::new(); - std.def_const("conifer", RgbaColor::new(0x9f, 0xEB, 0x52, 0xFF)); - std.def_const("forest", RgbaColor::new(0x43, 0xA1, 0x27, 0xFF)); + std.define("conifer", RgbaColor::new(0x9f, 0xEB, 0x52, 0xFF)); + std.define("forest", RgbaColor::new(0x43, 0xA1, 0x27, 0xFF)); std.def_fn("test", move |_, args| { let lhs = args.expect::<Value>("left-hand side")?; let rhs = args.expect::<Value>("right-hand side")?; @@ -82,6 +82,17 @@ fn main() { } Ok(Value::None) }); + std.def_fn("print", move |_, args| { + print!("> "); + for (i, value) in args.all::<Value>()?.into_iter().enumerate() { + if i > 0 { + print!(", ") + } + print!("{value:?}"); + } + println!(); + Ok(Value::None) + }); // Create loader and context. let loader = FsLoader::new().with_path(FONT_DIR); |
