import { describe, expect, test } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
// @ts-expect-error: plain CommonJS build script, no type declarations
import { tmJsonToPlist, UUID } from "../scripts/tm-plist.cjs";
const syntaxes = join(import.meta.dir, "../syntaxes");
const json = JSON.parse(
readFileSync(join(syntaxes, "gram.tmLanguage.json"), "utf-8"),
);
const plist: string = tmJsonToPlist(json);
describe("TextMate plist for Sublime Text", () => {
test("the committed gram.tmLanguage is up to date with the JSON grammar", () => {
expect(readFileSync(join(syntaxes, "gram.tmLanguage"), "utf-8")).toBe(
plist,
);
});
test("is deterministic", () => {
expect(tmJsonToPlist(json)).toBe(plist);
});
test("associates .gram files, keeps the scope and a fixed uuid, drops $schema", () => {
expect(plist).toContain(
"fileTypes\n\t\n\t\tgram\n\t",
);
expect(plist).toContain(
"scopeName\n\tsource.gram",
);
expect(plist).toContain(`uuid\n\t${UUID}`);
expect(plist).not.toContain("$schema");
});
test("escapes XML and keeps UTF-8 as is", () => {
const out: string = tmJsonToPlist({
name: "T",
scopeName: "source.t",
patterns: [{ match: '(?<=a)&"°×¼"' }],
});
expect(out).toContain(
"(?<=a)&<b>"°×¼"",
);
});
test("rejects characters XML cannot carry", () => {
expect(() =>
tmJsonToPlist({
name: "T",
scopeName: "s",
patterns: [{ match: "\u0001" }],
}),
).toThrow(/control character/);
});
});