Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

code-lines.test.ts

26 lines943 Bytes

1import { highlight } from "sugar-high";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { withLineIndents } from "./code-lines";
5​
6function indentsOf(html: string): string[] {
7 return [...html.matchAll(/--indent:(\d+)ch/g)].map((match) => match[1] ?? "");
8}
9​
10describe("withLineIndents", () => {
11 it("tags every line with its indentation", () => {
12 const code = "notes.update(id, (draft) => {\n draft.title = title;\n\n deeper();\n});";
13 expect(indentsOf(withLineIndents(highlight(code), code))).toEqual(["0", "2", "0", "4", "0"]);
14 });
15​
16 it("counts tabs as two spaces", () => {
17 const code = "a\n\tb";
18 expect(indentsOf(withLineIndents(highlight(code), code))).toEqual(["0", "2"]);
19 });
20​
21 it("leaves the highlighted tokens alone", () => {
22 const code = 'const x = "y";';
23 const html = highlight(code);
24 expect(withLineIndents(html, code).replace(' style="--indent:0ch"', "")).toBe(html);
25 });
26});