| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { tokenize } from "@/components/ratatui/highlight"; |
| 3 | |
| 4 | function kinds(code: string, language: "rust" | "toml" | "sh" = "rust") { |
| 5 | return tokenize(code, language).map((span) => [span.text, span.kind] as const); |
| 6 | } |
| 7 | |
| 8 | describe("code tinting", () => { |
| 9 | it("marks Rust keywords, strings, numbers, macros and comments", () => { |
| 10 | const spans = kinds('pub fn draw(area: Rect) {\n // paint it\n let n = 42;\n vec!["x"]\n}'); |
| 11 | expect(spans).toContainEqual(["pub", "keyword"]); |
| 12 | expect(spans).toContainEqual(["fn", "keyword"]); |
| 13 | expect(spans).toContainEqual(["// paint it", "comment"]); |
| 14 | expect(spans).toContainEqual(["42", "number"]); |
| 15 | expect(spans).toContainEqual(["vec", "macro"]); |
| 16 | expect(spans).toContainEqual(['"x"', "string"]); |
| 17 | }); |
| 18 | |
| 19 | it("keeps escaped quotes inside a string and never drops text", () => { |
| 20 | const code = 'let s = "say \\"hi\\""; // done'; |
| 21 | const spans = kinds(code); |
| 22 | expect(spans.map(([text]) => text).join("")).toBe(code); |
| 23 | expect(spans).toContainEqual(['"say \\"hi\\""', "string"]); |
| 24 | }); |
| 25 | |
| 26 | it("tints shell and TOML comments without Rust keywords", () => { |
| 27 | const sh = kinds("cargo run --example starter # go", "sh"); |
| 28 | expect(sh).toContainEqual(["# go", "comment"]); |
| 29 | expect(sh.some(([, kind]) => kind === "keyword")).toBe(false); |
| 30 | const toml = kinds('[dependencies]\nname = "x" # pinned', "toml"); |
| 31 | expect(toml).toContainEqual(["# pinned", "comment"]); |
| 32 | expect(toml).toContainEqual(['"x"', "string"]); |
| 33 | }); |
| 34 | }); |
| 35 |