49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package lsp
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestDetectAndLanguageID(t *testing.T) {
|
|
for _, tc := range []struct{ path, adapter, language string }{{"x.go", "gopls", "go"}, {"x.cpp", "clangd", "cpp"}, {"x.php", "intelephense", "php"}, {"x.py", "", "python"}, {"x.unknown", "", "plaintext"}} {
|
|
a := Detect(tc.path)
|
|
if tc.adapter == "" {
|
|
if a != nil {
|
|
t.Fatalf("Detect(%q)=%v", tc.path, a.Name)
|
|
}
|
|
} else if a == nil || a.Name != tc.adapter {
|
|
t.Fatalf("Detect(%q)=%v", tc.path, a)
|
|
}
|
|
if got := LanguageID(tc.path); got != tc.language {
|
|
t.Errorf("LanguageID(%q)=%q want %q", tc.path, got, tc.language)
|
|
}
|
|
}
|
|
}
|
|
func TestURIs(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "x.go")
|
|
if got := URIToPath(FileURI(p)); got != p {
|
|
t.Fatalf("round trip=%q want %q", got, p)
|
|
}
|
|
if got := URIToPath("https://example"); got != "https://example" {
|
|
t.Fatal(got)
|
|
}
|
|
}
|
|
func TestFindRoot(t *testing.T) {
|
|
d := t.TempDir()
|
|
sub := filepath.Join(d, "a", "b")
|
|
if err := os.MkdirAll(sub, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(d, "go.mod"), []byte("module x"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := FindRoot(filepath.Join(sub, "x.go"), []string{"go.mod"}, ""); got != d {
|
|
t.Fatalf("root=%q want %q", got, d)
|
|
}
|
|
if got := FindRoot(filepath.Join(sub, "x.go"), nil, "fallback"); got != "fallback" {
|
|
t.Fatal(got)
|
|
}
|
|
}
|