summaryrefslogtreecommitdiff
path: root/tests/typ
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-02-27 11:05:16 +0100
committerGitHub <noreply@github.com>2024-02-27 10:05:16 +0000
commit145723b1ef4fa23f1f6665b8907dfe79d0bf83cf (patch)
tree02a7de661ddd5dafa75dfce3e3c8b45a7333b9dc /tests/typ
parente9ee00a7c0df083663ff5ccca162238b88525e14 (diff)
New context system (#3497)
Diffstat (limited to 'tests/typ')
-rw-r--r--tests/typ/bugs/2650-cjk-latin-spacing-meta.typ3
-rw-r--r--tests/typ/bugs/cite-locate.typ2
-rw-r--r--tests/typ/bugs/math-realize.typ2
-rw-r--r--tests/typ/bugs/pagebreak-numbering.typ1
-rw-r--r--tests/typ/compiler/content-field.typ5
-rw-r--r--tests/typ/compiler/methods.typ21
-rw-r--r--tests/typ/compiler/recursion.typ4
-rw-r--r--tests/typ/compiler/selector-logical.typ7
-rw-r--r--tests/typ/layout/page-marginals.typ6
-rw-r--r--tests/typ/math/attach-p3.typ4
-rw-r--r--tests/typ/meta/context-compatibility.typ29
-rw-r--r--tests/typ/meta/context.typ181
-rw-r--r--tests/typ/meta/counter.typ18
-rw-r--r--tests/typ/meta/figure-caption.typ2
-rw-r--r--tests/typ/meta/query-before-after.typ7
-rw-r--r--tests/typ/meta/query-figure.typ6
-rw-r--r--tests/typ/meta/query-header.typ20
-rw-r--r--tests/typ/meta/state.typ26
18 files changed, 272 insertions, 72 deletions
diff --git a/tests/typ/bugs/2650-cjk-latin-spacing-meta.typ b/tests/typ/bugs/2650-cjk-latin-spacing-meta.typ
index e234e651..12c7ea41 100644
--- a/tests/typ/bugs/2650-cjk-latin-spacing-meta.typ
+++ b/tests/typ/bugs/2650-cjk-latin-spacing-meta.typ
@@ -1,6 +1,5 @@
// https://github.com/typst/typst/issues/2650
-#let with-locate(body) = locate(loc => body)
测a试
-测#with-locate[a]试
+测#context [a]试
diff --git a/tests/typ/bugs/cite-locate.typ b/tests/typ/bugs/cite-locate.typ
index 4eb95b54..c576c45e 100644
--- a/tests/typ/bugs/cite-locate.typ
+++ b/tests/typ/bugs/cite-locate.typ
@@ -17,7 +17,7 @@
caption: [A pirate @arrgh in @intro],
)
-#locate(loc => [Citation @distress on page #loc.page()])
+#context [Citation @distress on page #here().page()]
#pagebreak()
#bibliography("/files/works.bib", style: "chicago-notes")
diff --git a/tests/typ/bugs/math-realize.typ b/tests/typ/bugs/math-realize.typ
index d7991542..10d8b78e 100644
--- a/tests/typ/bugs/math-realize.typ
+++ b/tests/typ/bugs/math-realize.typ
@@ -4,7 +4,7 @@
---
#let my = $pi$
#let f1 = box(baseline: 10pt, [f])
-#let f2 = style(sty => f1)
+#let f2 = context f1
#show math.vec: [nope]
$ pi a $
diff --git a/tests/typ/bugs/pagebreak-numbering.typ b/tests/typ/bugs/pagebreak-numbering.typ
index d805c2c6..a9fae3e4 100644
--- a/tests/typ/bugs/pagebreak-numbering.typ
+++ b/tests/typ/bugs/pagebreak-numbering.typ
@@ -10,4 +10,3 @@ This and next page should not be numbered
#counter(page).update(1)
This page should
-
diff --git a/tests/typ/compiler/content-field.typ b/tests/typ/compiler/content-field.typ
index dab4ec4b..96ce1dca 100644
--- a/tests/typ/compiler/content-field.typ
+++ b/tests/typ/compiler/content-field.typ
@@ -17,10 +17,7 @@
---
// Test it with query.
#set raw(lang: "rust")
-#locate(loc => {
- let elem = query(<myraw>, loc).first()
- elem.lang
-})
+#context query(<myraw>).first().lang
`raw` <myraw>
---
diff --git a/tests/typ/compiler/methods.typ b/tests/typ/compiler/methods.typ
index 9eed5aed..2dce8892 100644
--- a/tests/typ/compiler/methods.typ
+++ b/tests/typ/compiler/methods.typ
@@ -83,20 +83,19 @@
---
// Test length `to-absolute` method.
-
#set text(size: 12pt)
-#style(styles => {
- test((6pt).to-absolute(styles), 6pt)
- test((6pt + 10em).to-absolute(styles), 126pt)
- test((10em).to-absolute(styles), 120pt)
-})
+#context {
+ test((6pt).to-absolute(), 6pt)
+ test((6pt + 10em).to-absolute(), 126pt)
+ test((10em).to-absolute(), 120pt)
+}
#set text(size: 64pt)
-#style(styles => {
- test((6pt).to-absolute(styles), 6pt)
- test((6pt + 10em).to-absolute(styles), 646pt)
- test((10em).to-absolute(styles), 640pt)
-})
+#context {
+ test((6pt).to-absolute(), 6pt)
+ test((6pt + 10em).to-absolute(), 646pt)
+ test((10em).to-absolute(), 640pt)
+}
---
// Error: 2-21 cannot convert a length with non-zero em units (`-6pt + 10.5em`) to pt
diff --git a/tests/typ/compiler/recursion.typ b/tests/typ/compiler/recursion.typ
index f5f870b5..421b638b 100644
--- a/tests/typ/compiler/recursion.typ
+++ b/tests/typ/compiler/recursion.typ
@@ -44,8 +44,8 @@
---
// Test cyclic imports during layout.
-// Error: 14-37 maximum layout depth exceeded
-// Hint: 14-37 try to reduce the amount of nesting in your layout
+// Error: 2-38 maximum show rule depth exceeded
+// Hint: 2-38 check whether the show rule matches its own output
#layout(_ => include "recursion.typ")
---
diff --git a/tests/typ/compiler/selector-logical.typ b/tests/typ/compiler/selector-logical.typ
index 64f97384..5369e4c7 100644
--- a/tests/typ/compiler/selector-logical.typ
+++ b/tests/typ/compiler/selector-logical.typ
@@ -13,10 +13,9 @@
#figure([Iguana], kind: "iguana", supplement: none)
== I
-#let test-selector(selector, ref) = locate(loc => {
- let elems = query(selector, loc)
- test(elems.map(e => e.body), ref)
-})
+#let test-selector(selector, ref) = context {
+ test(query(selector).map(e => e.body), ref)
+}
// Test `or`.
#test-selector(
diff --git a/tests/typ/layout/page-marginals.typ b/tests/typ/layout/page-marginals.typ
index d65591e6..589fb299 100644
--- a/tests/typ/layout/page-marginals.typ
+++ b/tests/typ/layout/page-marginals.typ
@@ -6,10 +6,10 @@
h(1fr)
text(0.8em)[_Chapter 1_]
},
- footer: align(center)[\~ #counter(page).display() \~],
- background: counter(page).display(n => if n <= 2 {
+ footer: context align(center)[\~ #counter(page).display() \~],
+ background: context if counter(page).get().first() <= 2 {
place(center + horizon, circle(radius: 1cm, fill: luma(90%)))
- })
+ }
)
But, soft! what light through yonder window breaks? It is the east, and Juliet
diff --git a/tests/typ/math/attach-p3.typ b/tests/typ/math/attach-p3.typ
index c681af2a..3591c248 100644
--- a/tests/typ/math/attach-p3.typ
+++ b/tests/typ/math/attach-p3.typ
@@ -11,8 +11,8 @@ $ scripts(sum)_1^2 != sum_1^2 $
$ limits(integral)_a^b != integral_a^b $
---
-// Error: 30-34 unknown variable: oops
-$ attach(A, t: #locate(it => oops)) $
+// Error: 25-29 unknown variable: oops
+$ attach(A, t: #context oops) $
---
// Show and let rules for limits and scripts
diff --git a/tests/typ/meta/context-compatibility.typ b/tests/typ/meta/context-compatibility.typ
new file mode 100644
index 00000000..60124255
--- /dev/null
+++ b/tests/typ/meta/context-compatibility.typ
@@ -0,0 +1,29 @@
+// Test compatibility with the pre-context way of things.
+// Ref: false
+
+---
+#let s = state("x", 0)
+#let compute(expr) = [
+ #s.update(x =>
+ eval(expr.replace("x", str(x)))
+ )
+ New value is #s.display().
+]
+
+#locate(loc => {
+ let elem = query(<here>, loc).first()
+ test(s.at(elem.location()), 13)
+})
+
+#compute("10") \
+#compute("x + 3") \
+*Here.* <here> \
+#compute("x * 2") \
+#compute("x - 5")
+
+---
+#style(styles => measure([it], styles).width < 20pt)
+
+---
+#counter(heading).update(10)
+#counter(heading).display(n => test(n, 10))
diff --git a/tests/typ/meta/context.typ b/tests/typ/meta/context.typ
new file mode 100644
index 00000000..729d9fa2
--- /dev/null
+++ b/tests/typ/meta/context.typ
@@ -0,0 +1,181 @@
+// Test context expressions.
+// Ref: false
+
+---
+// Test that context body is parsed as atomic expression.
+#let c = [#context "hello".]
+#test(c.children.first().func(), (context none).func())
+#test(c.children.last(), [.])
+
+---
+// Test that manual construction is forbidden.
+// Error: 2-25 cannot be constructed manually
+#(context none).func()()
+
+---
+// Test that `here()` yields the context element's location.
+#context test(query(here()).first().func(), (context none).func())
+
+---
+// Test whether context is retained in nested function.
+#let translate(..args) = args.named().at(text.lang)
+#set text(lang: "de")
+#context test(translate(de: "Inhalt", en: "Contents"), "Inhalt")
+
+---
+// Test whether context is retained in built-in callback.
+#set text(lang: "de")
+#context test(
+ ("en", "de", "fr").sorted(key: v => v != text.lang),
+ ("de", "en", "fr"),
+)
+
+---
+// Test `locate` + `here`.
+#context test(here().position().y, 10pt)
+
+---
+// Test `locate`.
+#v(10pt)
+= Introduction <intro>
+#context test(locate(<intro>).position().y, 20pt)
+
+---
+// Error: 10-25 label `<intro>` does not exist in the document
+#context locate(<intro>)
+
+---
+= Introduction <intro>
+= Introduction <intro>
+
+// Error: 10-25 label `<intro>` occurs multiple times in the document
+#context locate(<intro>)
+
+---
+#v(10pt)
+= Introduction <intro>
+#context test(locate(heading).position().y, 20pt)
+
+---
+// Error: 10-25 selector does not match any element
+#context locate(heading)
+
+---
+= Introduction <intro>
+= Introduction <intro>
+
+// Error: 10-25 selector matches multiple elements
+#context locate(heading)
+
+---
+// Test `counter`.
+#let c = counter("heading")
+#c.update(2)
+#c.update(n => n + 2)
+#context test(c.get(), (4,))
+#c.update(n => n - 3)
+#context test(c.at(here()), (1,))
+
+---
+// Test `state.at` outside of context.
+// Error: 2-26 can only be used when context is known
+// Hint: 2-26 try wrapping this in a `context` expression
+// Hint: 2-26 the `context` expression should wrap everything that depends on this function
+#state("key").at(<label>)
+
+---
+// Test `counter.at` outside of context.
+// Error: 2-28 can only be used when context is known
+// Hint: 2-28 try wrapping this in a `context` expression
+// Hint: 2-28 the `context` expression should wrap everything that depends on this function
+#counter("key").at(<label>)
+
+---
+// Test `measure`.
+#let f(lo, hi) = context {
+ let h = measure[Hello].height
+ assert(h > lo)
+ assert(h < hi)
+}
+#text(10pt, f(6pt, 8pt))
+#text(20pt, f(13pt, 14pt))
+
+---
+// Test basic get rule.
+#context test(text.lang, "en")
+#set text(lang: "de")
+#context test(text.lang, "de")
+#text(lang: "es", context test(text.lang, "es"))
+
+---
+// Test folding.
+#set rect(stroke: red)
+#context {
+ test(type(rect.stroke), stroke)
+ test(rect.stroke.paint, red)
+}
+#[
+ #set rect(stroke: 4pt)
+ #context test(rect.stroke, 4pt + red)
+]
+#context test(rect.stroke, stroke(red))
+
+---
+// We have one collision: `figure.caption` could be both the element and a get
+// rule for the `caption` field, which is settable. We always prefer the
+// element. It's unfortunate, but probably nobody writes
+// `set figure(caption: ..)` anyway.
+#test(type(figure.caption), function)
+#context test(type(figure.caption), function)
+
+---
+// Error: 10-31 Assertion failed: "en" != "de"
+#context test(text.lang, "de")
+
+---
+// Error: 15-20 function `text` does not contain field `langs`
+#context text.langs
+
+---
+// Error: 18-22 function `heading` does not contain field `body`
+#context heading.body
+
+---
+// Error: 7-11 can only be used when context is known
+// Hint: 7-11 try wrapping this in a `context` expression
+// Hint: 7-11 the `context` expression should wrap everything that depends on this function
+#text.lang
+
+---
+// Error: 7-12 function `text` does not contain field `langs`
+#text.langs
+
+---
+// Error: 10-14 function `heading` does not contain field `body`
+#heading.body
+
+---
+// Test that show rule establishes context.
+#set heading(numbering: "1.")
+#show heading: it => test(
+ counter(heading).get(),
+ (intro: (1,), back: (2,)).at(str(it.label)),
+)
+
+= Introduction <intro>
+= Background <back>
+
+---
+// Test that show rule on non-locatable element allows `query`.
+// Error: 18-47 Assertion failed: 2 != 3
+#show emph: _ => test(query(heading).len(), 3)
+#show strong: _ => test(query(heading).len(), 2)
+= Introduction
+= Background
+*Hi* _there_
+
+---
+// Test error when captured variable is assigned to.
+#let i = 0
+// Error: 11-12 variables from outside the context expression are read-only and cannot be modified
+#context (i = 1)
diff --git a/tests/typ/meta/counter.typ b/tests/typ/meta/counter.typ
index 6b5797de..6d72f246 100644
--- a/tests/typ/meta/counter.typ
+++ b/tests/typ/meta/counter.typ
@@ -4,21 +4,21 @@
// Count with string key.
#let mine = counter("mine!")
-Final: #locate(loc => mine.final(loc).at(0)) \
+Final: #context mine.final().at(0) \
#mine.step()
-First: #mine.display() \
+First: #context mine.display() \
#mine.update(7)
-#mine.display("1 of 1", both: true) \
+#context mine.display("1 of 1", both: true) \
#mine.step()
#mine.step()
-Second: #mine.display("I")
+Second: #context mine.display("I")
#mine.update(n => n * 2)
#mine.step()
---
// Count labels.
#let label = <heya>
-#let count = counter(label).display()
+#let count = context counter(label).display()
#let elem(it) = [#box(it) #label]
#elem[hey, there!] #count \
@@ -31,17 +31,17 @@ Second: #mine.display("I")
#counter(heading).step()
= Alpha
-In #counter(heading).display()
+In #context counter(heading).display()
== Beta
#set heading(numbering: none)
= Gamma
#heading(numbering: "I.")[Delta]
-At Beta, it was #locate(loc => {
- let it = query(heading, loc).find(it => it.body == [Beta])
+At Beta, it was #context {
+ let it = query(heading).find(it => it.body == [Beta])
numbering(it.numbering, ..counter(heading).at(it.location()))
-})
+}
---
// Count figures.
diff --git a/tests/typ/meta/figure-caption.typ b/tests/typ/meta/figure-caption.typ
index 0188ebca..0cdc2bbb 100644
--- a/tests/typ/meta/figure-caption.typ
+++ b/tests/typ/meta/figure-caption.typ
@@ -45,7 +45,7 @@
#show figure.caption: it => emph[
#it.body
(#it.supplement
- #it.counter.display(it.numbering))
+ #context it.counter.display(it.numbering))
]
#figure(
diff --git a/tests/typ/meta/query-before-after.typ b/tests/typ/meta/query-before-after.typ
index 3ddc9402..5f134093 100644
--- a/tests/typ/meta/query-before-after.typ
+++ b/tests/typ/meta/query-before-after.typ
@@ -57,12 +57,11 @@
#set heading(outlined: true, numbering: "1.")
-// This is purposefully an empty
-#locate(loc => [
+#context [
Non-outlined elements:
- #(query(selector(heading).and(heading.where(outlined: false)), loc)
+ #(query(selector(heading).and(heading.where(outlined: false)))
.map(it => it.body).join(", "))
-])
+]
#heading("A", outlined: false)
#heading("B", outlined: true)
diff --git a/tests/typ/meta/query-figure.typ b/tests/typ/meta/query-figure.typ
index 0540d65a..590c8e87 100644
--- a/tests/typ/meta/query-figure.typ
+++ b/tests/typ/meta/query-figure.typ
@@ -11,8 +11,8 @@
#show figure: set image(width: 80%)
= List of Figures
-#locate(it => {
- let elements = query(selector(figure).after(it), it)
+#context {
+ let elements = query(selector(figure).after(here()))
for it in elements [
Figure
#numbering(it.numbering,
@@ -21,7 +21,7 @@
#box(width: 1fr, repeat[.])
#counter(page).at(it.location()).first() \
]
-})
+}
#figure(
image("/files/glacier.jpg"),
diff --git a/tests/typ/meta/query-header.typ b/tests/typ/meta/query-header.typ
index dd83c128..5cbaa995 100644
--- a/tests/typ/meta/query-header.typ
+++ b/tests/typ/meta/query-header.typ
@@ -4,19 +4,17 @@
#set page(
paper: "a7",
margin: (y: 1cm, x: 0.5cm),
- header: {
+ header: context {
smallcaps[Typst Academy]
h(1fr)
- locate(it => {
- let after = query(selector(heading).after(it), it)
- let before = query(selector(heading).before(it), it)
- let elem = if before.len() != 0 {
- before.last()
- } else if after.len() != 0 {
- after.first()
- }
- emph(elem.body)
- })
+ let after = query(selector(heading).after(here()))
+ let before = query(selector(heading).before(here()))
+ let elem = if before.len() != 0 {
+ before.last()
+ } else if after.len() != 0 {
+ after.first()
+ }
+ emph(elem.body)
}
)
diff --git a/tests/typ/meta/state.typ b/tests/typ/meta/state.typ
index 86dc70a5..3fa8ece7 100644
--- a/tests/typ/meta/state.typ
+++ b/tests/typ/meta/state.typ
@@ -9,20 +9,20 @@
$ 2 + 3 $
#s.update(double)
-Is: #s.display(),
-Was: #locate(location => {
- let it = query(math.equation, location).first()
+Is: #context s.get(),
+Was: #context {
+ let it = query(math.equation).first()
s.at(it.location())
-}).
+}.
---
// Try same key with different initial value.
-#state("key", 2).display()
+#context state("key", 2).get()
#state("key").update(x => x + 1)
-#state("key", 2).display()
-#state("key", 3).display()
+#context state("key", 2).get()
+#context state("key", 3).get()
#state("key").update(x => x + 1)
-#state("key", 2).display()
+#context state("key", 2).get()
---
#set page(width: 200pt)
@@ -30,15 +30,15 @@ Was: #locate(location => {
#let ls = state("lorem", lorem(1000).split("."))
#let loremum(count) = {
- ls.display(list => list.slice(0, count).join(".").trim() + ".")
+ context ls.get().slice(0, count).join(".").trim() + "."
ls.update(list => list.slice(count))
}
#let fs = state("fader", red)
#let trait(title) = block[
- #fs.display(color => text(fill: color)[
+ #context text(fill: fs.get())[
*#title:* #loremum(1)
- ])
+ ]
#fs.update(color => color.lighten(30%))
]
@@ -52,5 +52,5 @@ Was: #locate(location => {
// Warning: layout did not converge within 5 attempts
// Hint: check if any states or queries are updating themselves
#let s = state("s", 1)
-#locate(loc => s.update(s.final(loc) + 1))
-#s.display()
+#context s.update(s.final() + 1)
+#context s.get()