Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/yaml.test.ts
6449 views
1
/*
2
* yaml.test.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*
6
*/
7
import { unitTest } from "../test.ts";
8
import { assert } from "testing/asserts";
9
import { Metadata } from "../../src/config/types.ts";
10
import { readYamlFromString } from "../../src/core/yaml.ts";
11
12
import { readAnnotatedYamlFromString } from "../../src/core/lib/yaml-intelligence/annotated-yaml.ts";
13
import { yamlValidationUnitTest } from "./schema-validation/utils.ts";
14
15
const yamlStr = `
16
project:
17
type: website
18
other:
19
array:
20
- foo
21
- bar
22
`;
23
24
// deno-lint-ignore require-await
25
unitTest("yaml", async () => {
26
const yaml = readYamlFromString(yamlStr) as Metadata;
27
28
// Tests of the result
29
assert(
30
(yaml.project as Metadata).type === "website",
31
"Project type not read properly",
32
);
33
assert(
34
Array.isArray((yaml.other as Metadata).array) &&
35
((yaml.other as Metadata).array as string[]).length === 2,
36
"Other array key not read properly",
37
);
38
});
39
40
const circularYml = "foo: &foo\n bar: *foo";
41
42
// deno-lint-ignore require-await
43
unitTest("yaml-circular-should-fail", async () => {
44
try {
45
readYamlFromString(circularYml);
46
assert(false, "circular structure should have raised");
47
} catch (_e) {
48
// we expect to raise
49
}
50
try {
51
readAnnotatedYamlFromString(circularYml);
52
assert(false, "circular structure should have raised");
53
} catch (_e) {
54
// we expect to raise
55
}
56
});
57
58
const sharedYml = "foo:\n bar: &bar\n baz: bah\n baz: *bar";
59
// deno-lint-ignore require-await
60
unitTest("yaml-shared-should-pass", async () => {
61
readYamlFromString(sharedYml);
62
readYamlFromString(circularYml);
63
});
64
65
const exprYml = `label: fig-test
66
fig-cap: !expr paste("Air Quality")`;
67
68
// deno-lint-ignore require-await
69
unitTest("yaml-expr-tag-should-pass", async () => {
70
// deno-lint-ignore no-explicit-any
71
const yml = readYamlFromString(exprYml) as any;
72
assert(yml["fig-cap"].tag === "!expr");
73
assert(yml["fig-cap"].value === 'paste("Air Quality")');
74
});
75
76
// deno-lint-ignore require-await
77
yamlValidationUnitTest("annotated-yaml-expr-tag-should-pass", async () => {
78
// deno-lint-ignore no-explicit-any
79
const yml = readAnnotatedYamlFromString(exprYml).result as any;
80
assert(yml["fig-cap"].tag === "!expr");
81
assert(yml["fig-cap"].value === 'paste("Air Quality")');
82
});
83
84