Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/pandoc-formats.test.ts
6448 views
1
import { unitTest } from "../test.ts";
2
import { assert } from "testing/asserts";
3
import {
4
FormatDescriptor,
5
parseFormatString,
6
} from "../../src/core/pandoc/pandoc-formats.ts";
7
8
unitTest(
9
"pandoc-format",
10
// deno-lint-ignore require-await
11
async () => {
12
const tests: Record<string, FormatDescriptor> = {
13
"pdf": {
14
baseFormat: "pdf",
15
variants: [],
16
modifiers: [],
17
formatWithVariants: "pdf",
18
},
19
"acm-pdf": {
20
baseFormat: "pdf",
21
extension: "acm",
22
variants: [],
23
modifiers: [],
24
formatWithVariants: "pdf",
25
},
26
"acm-pdf+draft": {
27
baseFormat: "pdf",
28
extension: "acm",
29
variants: [],
30
modifiers: ["+draft"],
31
formatWithVariants: "pdf",
32
},
33
"acm-2023-pdf+draft": {
34
baseFormat: "pdf",
35
extension: "acm-2023",
36
variants: [],
37
modifiers: ["+draft"],
38
formatWithVariants: "pdf",
39
},
40
"gfm-rebase_relative_paths": {
41
baseFormat: "gfm",
42
variants: ["-rebase_relative_paths"],
43
modifiers: [],
44
formatWithVariants: "gfm-rebase_relative_paths",
45
},
46
"gfm-rebase_relative_paths+markdown_in_html_blocks": {
47
baseFormat: "gfm",
48
variants: ["-rebase_relative_paths", "+markdown_in_html_blocks"],
49
modifiers: [],
50
formatWithVariants: "gfm-rebase_relative_paths+markdown_in_html_blocks",
51
},
52
};
53
54
Object.keys(tests).forEach((test) => {
55
const parsed = parseFormatString(test);
56
assertDescriptorsEqual(parsed, tests[test]);
57
});
58
},
59
);
60
61
function assertDescriptorsEqual(
62
desc1: FormatDescriptor,
63
desc2: FormatDescriptor,
64
) {
65
const msg = (text: string) => {
66
return `${text}\n${JSON.stringify(desc1, undefined, 2)}\n${
67
JSON.stringify(desc2, undefined, 2)
68
}`;
69
};
70
71
assert(
72
desc1.baseFormat === desc2.baseFormat,
73
msg("mismatching base format"),
74
);
75
assert(
76
desc1.formatWithVariants === desc2.formatWithVariants,
77
msg("mismatching format variant string"),
78
);
79
assert(
80
arraysEqual(desc1.modifiers, desc2.modifiers),
81
msg("mismatching modifiers"),
82
);
83
assert(
84
arraysEqual(desc1.variants, desc2.variants),
85
msg("mismatching variants"),
86
);
87
assert(
88
desc1.extension === desc2.extension,
89
msg("mismatching format extension"),
90
);
91
}
92
93
function arraysEqual(arr1: string[], arr2: string[]) {
94
if (arr1.length !== arr2.length) {
95
return false;
96
}
97
98
const filtered = arr1.filter((val, i) => {
99
return val === arr2[i];
100
});
101
return filtered.length === arr1.length;
102
}
103
104