Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/text-highlighting.test.ts
12924 views
1
/*
2
* text-highlighting.test.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { unitTest } from "../test.ts";
8
import { assertEquals } from "testing/asserts";
9
import {
10
getHighlightTheme,
11
hasAdaptiveTheme,
12
hasTextHighlighting,
13
isAdaptiveTheme,
14
} from "../../src/quarto-core/text-highlighting.ts";
15
import { FormatPandoc } from "../../src/config/types.ts";
16
17
// getHighlightTheme tests
18
19
// deno-lint-ignore require-await
20
unitTest("getHighlightTheme - returns default when no options set", async () => {
21
const pandoc: FormatPandoc = {};
22
assertEquals(getHighlightTheme(pandoc), "arrow");
23
});
24
25
// deno-lint-ignore require-await
26
unitTest("getHighlightTheme - returns syntax-highlighting when set", async () => {
27
const pandoc: FormatPandoc = { "syntax-highlighting": "github" };
28
assertEquals(getHighlightTheme(pandoc), "github");
29
});
30
31
// deno-lint-ignore require-await
32
unitTest("getHighlightTheme - returns highlight-style as fallback", async () => {
33
const pandoc: FormatPandoc = { "highlight-style": "monokai" };
34
assertEquals(getHighlightTheme(pandoc), "monokai");
35
});
36
37
// deno-lint-ignore require-await
38
unitTest("getHighlightTheme - syntax-highlighting takes precedence", async () => {
39
const pandoc: FormatPandoc = {
40
"syntax-highlighting": "github",
41
"highlight-style": "monokai",
42
};
43
assertEquals(getHighlightTheme(pandoc), "github");
44
});
45
46
// hasTextHighlighting tests
47
48
// deno-lint-ignore require-await
49
unitTest("text-highlighting - returns true when no options set (default applies)", async () => {
50
const pandoc: FormatPandoc = {};
51
assertEquals(hasTextHighlighting(pandoc), true);
52
});
53
54
// deno-lint-ignore require-await
55
unitTest("text-highlighting - returns false when disabled with 'none'", async () => {
56
const pandoc: FormatPandoc = { "syntax-highlighting": "none" };
57
assertEquals(hasTextHighlighting(pandoc), false);
58
});
59
60
// deno-lint-ignore require-await
61
unitTest("text-highlighting - returns true for explicit theme", async () => {
62
const pandoc: FormatPandoc = { "syntax-highlighting": "github" };
63
assertEquals(hasTextHighlighting(pandoc), true);
64
});
65
66
// deno-lint-ignore require-await
67
unitTest("text-highlighting - returns true for deprecated highlight-style", async () => {
68
const pandoc: FormatPandoc = { "highlight-style": "monokai" };
69
assertEquals(hasTextHighlighting(pandoc), true);
70
});
71
72
// deno-lint-ignore require-await
73
unitTest("text-highlighting - syntax-highlighting takes precedence over highlight-style", async () => {
74
// New option takes precedence - "none" disables regardless of deprecated option
75
const pandoc: FormatPandoc = {
76
"syntax-highlighting": "none",
77
"highlight-style": "github",
78
};
79
// "none" is truthy so it's selected, then hasTextHighlighting returns false
80
assertEquals(hasTextHighlighting(pandoc), false);
81
});
82
83
// deno-lint-ignore require-await
84
unitTest("text-highlighting - returns false for deprecated highlight-style: none", async () => {
85
const pandoc: FormatPandoc = { "highlight-style": "none" };
86
assertEquals(hasTextHighlighting(pandoc), false);
87
});
88
89
// isAdaptiveTheme tests
90
91
// deno-lint-ignore require-await
92
unitTest("isAdaptiveTheme - returns true for known adaptive themes", async () => {
93
const adaptiveThemes = [
94
"a11y",
95
"arrow",
96
"atom-one",
97
"ayu",
98
"breeze",
99
"github",
100
"gruvbox",
101
"monochrome",
102
];
103
for (const theme of adaptiveThemes) {
104
assertEquals(isAdaptiveTheme(theme), true, `Expected ${theme} to be adaptive`);
105
}
106
});
107
108
// deno-lint-ignore require-await
109
unitTest("isAdaptiveTheme - returns false for non-adaptive themes", async () => {
110
const nonAdaptiveThemes = ["monokai", "tango", "zenburn", "kate", "pygments"];
111
for (const theme of nonAdaptiveThemes) {
112
assertEquals(isAdaptiveTheme(theme), false, `Expected ${theme} to be non-adaptive`);
113
}
114
});
115
116
// deno-lint-ignore require-await
117
unitTest("isAdaptiveTheme - returns true for object with dark and light", async () => {
118
const theme = { dark: "monokai", light: "tango" };
119
assertEquals(isAdaptiveTheme(theme), true);
120
});
121
122
// deno-lint-ignore require-await
123
unitTest("isAdaptiveTheme - returns false for object without both dark and light", async () => {
124
// Only dark
125
assertEquals(isAdaptiveTheme({ dark: "monokai" }), false);
126
// Only light
127
assertEquals(isAdaptiveTheme({ light: "tango" }), false);
128
// Neither (empty object)
129
assertEquals(isAdaptiveTheme({}), false);
130
});
131
132
// hasAdaptiveTheme tests
133
134
// deno-lint-ignore require-await
135
unitTest("hasAdaptiveTheme - returns true for adaptive syntax-highlighting", async () => {
136
const pandoc: FormatPandoc = { "syntax-highlighting": "github" };
137
assertEquals(hasAdaptiveTheme(pandoc), true);
138
});
139
140
// deno-lint-ignore require-await
141
unitTest("hasAdaptiveTheme - returns false for non-adaptive theme", async () => {
142
const pandoc: FormatPandoc = { "syntax-highlighting": "monokai" };
143
assertEquals(hasAdaptiveTheme(pandoc), false);
144
});
145
146
// deno-lint-ignore require-await
147
unitTest("hasAdaptiveTheme - returns true for deprecated highlight-style", async () => {
148
const pandoc: FormatPandoc = { "highlight-style": "github" };
149
assertEquals(hasAdaptiveTheme(pandoc), true);
150
});
151
152
// deno-lint-ignore require-await
153
unitTest("hasAdaptiveTheme - uses default theme when nothing set", async () => {
154
// Default theme is "arrow" which is adaptive
155
const pandoc: FormatPandoc = {};
156
assertEquals(hasAdaptiveTheme(pandoc), true);
157
});
158
159