| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | CODE_BLOCK_LANGUAGES, |
| 5 | codeLanguageLabel, |
| 6 | languageOfInfoString, |
| 7 | withInfoStringLanguage, |
| 8 | } from "./composer-code-languages"; |
| 9 | |
| 10 | describe("languageOfInfoString", () => { |
| 11 | it.each([ |
| 12 | ["", ""], |
| 13 | ["ts", "ts"], |
| 14 | [" ts ", "ts"], |
| 15 | ["js title=example", "js"], |
| 16 | ["typescript ", "typescript"], |
| 17 | ])("reads %j as %j", (info, language) => { |
| 18 | expect(languageOfInfoString(info)).toBe(language); |
| 19 | }); |
| 20 | }); |
| 21 | |
| 22 | describe("withInfoStringLanguage", () => { |
| 23 | it.each([ |
| 24 | ["", "ts", "ts"], |
| 25 | ["js", "python", "python"], |
| 26 | // Anything after the language belongs to the fence and stays put. |
| 27 | ["js title=example", "ts", "ts title=example"], |
| 28 | ["js {1,3}", "ts", "ts {1,3}"], |
| 29 | // Plain text clears the language... |
| 30 | ["js", "", ""], |
| 31 | // ...but a bare `title=x` would read as the language, so it keeps `text`. |
| 32 | ["js title=example", "", "text title=example"], |
| 33 | // Whitespace between the fence and the language is the user's formatting. |
| 34 | [" js title=x", "python", " python title=x"], |
| 35 | [" js title=x", "", " text title=x"], |
| 36 | [" js", "go", " go"], |
| 37 | [" ", "go", " go"], |
| 38 | ])("sets %j to %j as %j", (info, language, expected) => { |
| 39 | expect(withInfoStringLanguage(info, language)).toBe(expected); |
| 40 | }); |
| 41 | }); |
| 42 | |
| 43 | describe("codeLanguageLabel", () => { |
| 44 | it.each([ |
| 45 | ["", "Plain text"], |
| 46 | ["text", "Plain text"], |
| 47 | ["cpp", "C++"], |
| 48 | ["ts", "TypeScript"], |
| 49 | ["arduino", "arduino"], |
| 50 | ])("labels %j as %j", (language, label) => { |
| 51 | expect(codeLanguageLabel(language)).toBe(label); |
| 52 | }); |
| 53 | |
| 54 | it("lists every language once, plain text first", () => { |
| 55 | const ids = CODE_BLOCK_LANGUAGES.map((language) => language.id); |
| 56 | expect(ids[0]).toBe(""); |
| 57 | expect(new Set(ids).size).toBe(ids.length); |
| 58 | }); |
| 59 | }); |