Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/smoke/crossref/syntax.test.ts
12925 views
1
/*
2
* syntax.test.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { ensureFileRegexMatches } from "../../verify.ts";
8
import { testRender } from "../render/render.ts";
9
import { crossref } from "./utils.ts";
10
import {
11
ExecuteOutput,
12
test,
13
TestContext,
14
TestDescriptor,
15
Verify,
16
} from "../../test.ts";
17
import { assert, fail } from "testing/asserts";
18
import { quarto } from "../../../src/quarto.ts";
19
20
const syntaxQmd = crossref("syntax.qmd", "html");
21
testRender(syntaxQmd.input, "html", false, [
22
ensureFileRegexMatches(syntaxQmd.output.outputPath, [
23
/<div class="figtest-default">[^]*?>Figure&nbsp;1<[^]*?<\/div>/,
24
/<div class="figtest-capitalized">[^]*?>Figure&nbsp;1<[^]*?<\/div>/,
25
/<div class="figtest-prefix">[^]*?>Figure&nbsp;1<[^]*?<\/div>/,
26
/<div class="figtest-noprefix">[^]*?>1<[^]*?<\/div>/,
27
], [
28
/\?@fig-/,
29
]),
30
]);
31
32
// Test that two different syntaxes for a basic figure produce
33
// the same figure output
34
const imgQmd = crossref("figure-syntax-img.qmd", "html");
35
const divQmd = crossref("figure-syntax-div.qmd", "html");
36
const verify: Verify = {
37
name: "Compare output",
38
verify: (_outputs: ExecuteOutput[]) => {
39
const extractBodyRegex = /<body.*?>((.|\n)*)<\/body>/;
40
41
const imgText = Deno.readTextFileSync(imgQmd.output.outputPath);
42
const imgBody = extractBodyRegex.exec(imgText);
43
44
const divText = Deno.readTextFileSync(divQmd.output.outputPath);
45
const divBody = extractBodyRegex.exec(divText);
46
47
if (imgBody && divBody) {
48
assert(
49
imgBody[0] === divBody[0],
50
"Contents of HTML generated for figures with div vs img syntax do not match.",
51
);
52
} else {
53
fail("Unable to extract html body from output.");
54
}
55
56
return Promise.resolve();
57
},
58
};
59
const context: TestContext = {
60
teardown: () => {
61
Deno.removeSync(imgQmd.output.outputPath);
62
Deno.removeSync(imgQmd.output.supportPath, { recursive: true });
63
64
Deno.removeSync(divQmd.output.outputPath);
65
Deno.removeSync(divQmd.output.supportPath, { recursive: true });
66
return Promise.resolve();
67
},
68
};
69
const testDesc: TestDescriptor = { // FIXME: why is this test flaky now? Ask @dragonstyle
70
name: "test html produced by different figure syntax",
71
context,
72
execute: async () => {
73
await quarto(["render", imgQmd.input]);
74
await quarto(["render", divQmd.input]);
75
},
76
verify: [verify],
77
type: "smoke",
78
};
79
test(testDesc);
80
81