From a9fdff244aef859449a76e5f762ee7c343a8ddcc Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 10 Mar 2023 20:47:23 +0100 Subject: Expose content representation more --- tests/typ/compiler/content-field.typ | 41 ++++++++++++++++++++++++++++++++++++ tests/typ/compiler/field.typ | 2 +- tests/typ/compiler/label.typ | 2 +- tests/typ/compiler/ops.typ | 10 +++++---- tests/typ/compiler/repr.typ | 7 ++---- tests/typ/compiler/show-node.typ | 2 +- 6 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 tests/typ/compiler/content-field.typ (limited to 'tests/typ') diff --git a/tests/typ/compiler/content-field.typ b/tests/typ/compiler/content-field.typ new file mode 100644 index 00000000..0eeee458 --- /dev/null +++ b/tests/typ/compiler/content-field.typ @@ -0,0 +1,41 @@ +// Integrated test for content fields. + +#let compute(formula, ..vars) = { + let vars = vars.named() + let f(node) = { + let func = node.func() + if func == text { + let text = node.text + if regex("^\d+$") in text { + int(text) + } else if text in vars { + int(vars.at(text)) + } else { + panic("unknown math variable: " + text) + } + } else if func == math.attach { + let value = f(node.base) + if node.has("top") { + value = calc.pow(value, f(node.top)) + } + value + } else if node.has("children") { + node + .children + .filter(v => v != [ ]) + .split[+] + .map(xs => xs.fold(1, (prod, v) => prod * f(v))) + .fold(0, (sum, v) => sum + v) + } + } + let result = f(formula.body) + [With ] + vars + .pairs() + .map(p => $#p.first() = #p.last()$) + .join(", ", last: " and ") + [ we have:] + $ formula = result $ +} + +#compute($x y + y^2$, x: 2, y: 3) diff --git a/tests/typ/compiler/field.typ b/tests/typ/compiler/field.typ index 27b31f7d..342ae75b 100644 --- a/tests/typ/compiler/field.typ +++ b/tests/typ/compiler/field.typ @@ -31,7 +31,7 @@ #false.ok --- -// Error: 29-32 unknown field `fun` +// Error: 29-32 content does not contain field "fun" #show heading: node => node.fun = A diff --git a/tests/typ/compiler/label.typ b/tests/typ/compiler/label.typ index d4ff15d7..58b0dee9 100644 --- a/tests/typ/compiler/label.typ +++ b/tests/typ/compiler/label.typ @@ -22,7 +22,7 @@ The end. --- // Test labelled text. #show "t": it => { - set text(blue) if it.label == + set text(blue) if it.has("label") and it.label == it } diff --git a/tests/typ/compiler/ops.typ b/tests/typ/compiler/ops.typ index baa2898a..a29003ed 100644 --- a/tests/typ/compiler/ops.typ +++ b/tests/typ/compiler/ops.typ @@ -152,11 +152,13 @@ #test(test == test, true) #test((() => {}) == (() => {}), false) -// Content cannot be compared. +// Content compares field by field. #let t = [a] -#test(t == t, false) -#test([] == [], false) -#test([a] == [a], false) +#test(t == t, true) +#test([] == [], true) +#test([a] == [a], true) +#test(grid[a] == grid[a], true) +#test(grid[a] == grid[b], false) --- // Test comparison operators. diff --git a/tests/typ/compiler/repr.typ b/tests/typ/compiler/repr.typ index ed6cd84a..cfe428eb 100644 --- a/tests/typ/compiler/repr.typ +++ b/tests/typ/compiler/repr.typ @@ -30,16 +30,13 @@ #rgb("f7a205") \ #(2pt + rgb("f7a205")) ---- // Strings and escaping. -#raw(repr("hi"), lang: "typc") \ +#raw(repr("hi"), lang: "typc") #repr("a\n[]\"\u{1F680}string") ---- // Content. -#raw(repr[*{"H" + "i"} there*]) +#raw(lang: "typc", repr[*Hey*]) ---- // Functions are invisible. Nothing #let f(x) = x diff --git a/tests/typ/compiler/show-node.typ b/tests/typ/compiler/show-node.typ index f87b1971..0c1c1f52 100644 --- a/tests/typ/compiler/show-node.typ +++ b/tests/typ/compiler/show-node.typ @@ -78,7 +78,7 @@ Another text. = Heading --- -// Error: 25-29 unknown field `page` +// Error: 25-29 content does not contain field "page" #show heading: it => it.page = Heading -- cgit v1.2.3