package main import ( "strings" "testing" ) // TestGuessLangExisting covers languages detected before the enry switch and // still expected to work after it. func TestGuessLangExisting(t *testing.T) { cases := map[string]string{ "package main\n\nfunc main() {}\n": "go", "def foo():\n return 1\n": "python", "const x = 1;\nconsole.log(x);\n": "javascript", "{\"a\": 1, \"b\": [2, 3]}\n": "json", "hello world just some text": "text", "": "", "fn main() {\n let x = 1;\n}\n": "rust", "#include \nint main() { printf(\"hi\"); }\n": "c", "SELECT id, name FROM users WHERE active = 1;\n": "sql", "title: demo\nitems:\n - one\n - two\n": "yaml", "# Demo\n\nsome *markdown* text with a [link](http://x)\n": "markdown", "#!/bin/bash\nset -euo pipefail\necho hi\n": "bash", } for src, want := range cases { if got := guessLang(src); got != want { t.Errorf("guessLang(%q) = %q, want %q", src, got, want) } } } // TestGuessLangNewLanguages covers the languages added to the dropdown as part // of the enry integration (#41). func TestGuessLangNewLanguages(t *testing.T) { cases := map[string]string{ "interface User {\n name: string;\n age: number;\n}\n": "typescript", "\n\nhi\n\n": "html", ".container {\n display: flex;\n padding: 4px;\n}\n": "css", "\nx\n": "xml", "\n": "html", "FROM alpine:3.19\nCOPY app /app\n": "dockerfile", "FROM ubuntu:24.04\nRUN apt-get update\n": "dockerfile", "diff --git a/x.txt b/x.txt\nindex 123..456 100644\n": "diff", "--- a/config.yml\n+++ b/config.yml\n@@ -1,2 +1,3 @@\n": "diff", } for src, want := range cases { if got := guessLang(src); got != want { t.Errorf("guessLang(%q) = %q, want %q", src, got, want) } } } // TestGuessLangCanonical verifies enry display names are mapped/lowercased to // our stored ids. func TestGuessLangCanonical(t *testing.T) { if got := guessLang("FROM debian:12\nCMD [\"sh\"]\n"); got != "dockerfile" { t.Errorf("Dockerfile canonical mapping failed: got %q", got) } if got := guessLang("#!/bin/sh\necho hi\n"); got != "bash" { t.Errorf("Shell canonical mapping failed: got %q", got) } // uncurated languages still come back lowercase if got := guessLang("

{{.Name}}

\n"); got != strings.ToLower(got) { t.Errorf("expected lowercase output, got %q", got) } }