Open History to see the last commit
1import { getFiletypeFromFileName } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3
4import { getSyntaxHighlighterPromise } from "./syntaxHighlighting";
5import { DIFF_THEME_NAMES } from "./diffRendering";
6
7describe("dialect file types", () => {
8 it.each([
9 ["src/init.luau", "lua"],
10 ["styles/page.xsl", "xml"],
11 ["styles/page.xslt", "xml"],
12 ])("%s highlights as %s", (path, language) => {
13 expect(getFiletypeFromFileName(path)).toBe(language);
14 });
15});
16
17const SAMPLES = [
18 {
19 name: "assembly",
20 code: "mov eax, 42 ; answer\nret",
21 hints: [
22 "asm",
23 "assembly",
24 "s",
25 ...["asm", "ASM", "s", "S", "nasm", "NASM"].map((ext) => `src/start.${ext}`),
26 ],
27 },
28 {
29 name: "verilog",
30 code: "module top(input wire clk);\n reg [7:0] q; // count\nendmodule",
31 hints: ["verilog", "src/top.v", "src/defs.vh"],
32 },
33 {
34 name: "systemverilog",
35 code: "module top(input logic clk);\n always_ff @(posedge clk) q <= 8'd1; // count\nendmodule",
36 hints: ["systemverilog", "system-verilog", "src/top.sv", "src/defs.svh"],
37 },
38 {
39 name: "vhdl",
40 code: "entity top is\n port (clk : in std_logic); -- clock\nend entity;",
41 hints: ["vhdl", "src/top.vhd", "src/top.vhdl"],
42 },
43].flatMap(({ name, code, hints }) => hints.map((hint) => ({ name, code, hint })));
44
45it.each(SAMPLES)("highlights $name in $hint", async ({ code, hint }) => {
46 const language = hint.includes("/") ? getFiletypeFromFileName(hint) : hint;
47 const highlighter = await getSyntaxHighlighterPromise(language);
48 for (const theme of Object.values(DIFF_THEME_NAMES)) {
49 const { tokens } = highlighter.codeToTokens(code, { lang: language, theme });
50 expect(tokens.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe(code);
51 expect(new Set(tokens.flat().map((token) => token.color)).size).toBeGreaterThan(1);
52 }
53});