| 1 | // Run: tsx src/__tests__/language-highlighting.test.ts |
| 2 | |
| 3 | import { resolveLang } from "../lib/highlight"; |
| 4 | import { |
| 5 | FILE_LANGUAGE_BY_EXTENSION, |
| 6 | FILE_LANGUAGE_BY_NAME, |
| 7 | extToLang, |
| 8 | pathToLang, |
| 9 | } from "../lib/lang"; |
| 10 | import { formatSelectionReference, languageFor } from "../lib/selectedTextContext"; |
| 11 | |
| 12 | let passed = 0; |
| 13 | let failed = 0; |
| 14 | |
| 15 | function eq(actual: unknown, expected: unknown, label: string) { |
| 16 | if (actual === expected) { |
| 17 | process.stdout.write(` PASS ${label}\n`); |
| 18 | passed += 1; |
| 19 | } else { |
| 20 | process.stdout.write(` FAIL ${label}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}\n`); |
| 21 | failed += 1; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | console.log("\nlanguage highlighting"); |
| 26 | |
| 27 | const pathCases = [ |
| 28 | ["src/App.cs", "csharp", "csharp"], |
| 29 | ["src/Component.tsx", "tsx", "typescript"], |
| 30 | ["src/view.xaml", "xaml", "xml"], |
| 31 | ["src/index.html", "html", "xml"], |
| 32 | ["config/app.toml", "toml", "ini"], |
| 33 | ["Dockerfile", "dockerfile", "dockerfile"], |
| 34 | ["containers/Dockerfile.dev", "dockerfile", "dockerfile"], |
| 35 | ["C:\\repo\\Containerfile.windows", "dockerfile", "dockerfile"], |
| 36 | ["Makefile", "makefile", "makefile"], |
| 37 | ["build/GNUmakefile.release", "makefile", "makefile"], |
| 38 | ["CMakeLists.txt", "cmake", "cmake"], |
| 39 | ["public/.htaccess", "apache", "apache"], |
| 40 | ["config/httpd.conf", "apache", "apache"], |
| 41 | ["config/nginx.conf", "nginx", "nginx"], |
| 42 | ["build/module.makefile", "makefile", "makefile"], |
| 43 | ] as const; |
| 44 | |
| 45 | for (const [path, semantic, lexer] of pathCases) { |
| 46 | eq(pathToLang(path), semantic, `${path} has one semantic path mapping`); |
| 47 | eq(extToLang(path), semantic, `${path} keeps tool diffs consistent`); |
| 48 | eq(languageFor(path), semantic, `${path} keeps file previews consistent`); |
| 49 | eq(resolveLang(semantic), lexer, `${path} resolves to a registered lexer`); |
| 50 | } |
| 51 | |
| 52 | const mappedLanguages = new Set([ |
| 53 | ...Object.values(FILE_LANGUAGE_BY_EXTENSION), |
| 54 | ...Object.values(FILE_LANGUAGE_BY_NAME), |
| 55 | ]); |
| 56 | for (const language of mappedLanguages) { |
| 57 | eq(resolveLang(language) !== "", true, `${language} mapping has a registered lexer`); |
| 58 | } |
| 59 | |
| 60 | for (const [path, fenceTag] of [ |
| 61 | ["Component.tsx", "tsx"], |
| 62 | ["template.html", "html"], |
| 63 | ["settings.toml", "toml"], |
| 64 | ["View.xaml", "xaml"], |
| 65 | ] as const) { |
| 66 | const fenceLine = formatSelectionReference(path, "sample").split("\n")[2]; |
| 67 | eq(fenceLine, `\`\`\`${fenceTag}`, `${path} preserves its provider-visible semantic fence tag`); |
| 68 | } |
| 69 | |
| 70 | console.log(`\n${passed} passed, ${failed} failed, ${passed + failed} total`); |
| 71 | if (failed > 0) process.exit(1); |
| 72 |