70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package codeintel
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestInterpolate(t *testing.T) {
|
|
captures := map[string]string{"name": "Server", "value": "x + y"}
|
|
for _, test := range []struct{ template, want string }{
|
|
{"$name($value)", "Server(x + y)"},
|
|
{"${name} = $value", "Server = x + y"},
|
|
{"$$name", "$name"},
|
|
{"$missing", "$missing"},
|
|
} {
|
|
if got := Interpolate(test.template, captures); got != test.want {
|
|
t.Errorf("Interpolate(%q) = %q, want %q", test.template, got, test.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLanguageFor(t *testing.T) {
|
|
for _, tc := range []struct{ path, name, want string }{{"x.go", "", "go"}, {"x.tsx", "", "tsx"}, {"x.yml", "", "yaml"}, {"x.md", "", "markdown"}, {"x.go", "python", "python"}, {"x.unknown", "", ""}} {
|
|
l, ok := LanguageFor(tc.path, tc.name)
|
|
if (tc.want != "") != ok || ok && l.Name != tc.want {
|
|
t.Errorf("LanguageFor(%q,%q)=%q,%v", tc.path, tc.name, l.Name, ok)
|
|
}
|
|
}
|
|
}
|
|
func TestQueryAndDependencies(t *testing.T) {
|
|
d := t.TempDir()
|
|
p := d + "/main.go"
|
|
src := "package main\nimport \"fmt\"\nfunc Hello() { fmt.Println(\"x\") }\n"
|
|
if err := os.WriteFile(p, []byte(src), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cs, err := Query(p, "", "(function_declaration name: (identifier) @name)")
|
|
if err != nil || len(cs) != 1 || cs[0].Text != "Hello" {
|
|
t.Fatalf("query=%+v err=%v", cs, err)
|
|
}
|
|
deps, err := Dependencies(p, "")
|
|
if err != nil || len(deps) == 0 || !strings.Contains(deps[0], "import") {
|
|
t.Fatalf("deps=%v err=%v", deps, err)
|
|
}
|
|
}
|
|
func TestReplaceMatches(t *testing.T) {
|
|
d := t.TempDir()
|
|
p := d + "/main.go"
|
|
if err := os.WriteFile(p, []byte("package main\nfunc A() {}\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := ReplaceMatches(p, "", "(identifier) @match", "$match")
|
|
if err != nil || !strings.Contains(string(out), "func A") {
|
|
t.Fatalf("replace=%q err=%v", out, err)
|
|
}
|
|
}
|
|
|
|
func TestMarkdownHeadings(t *testing.T) {
|
|
d := t.TempDir()
|
|
p := d + "/README.md"
|
|
if err := os.WriteFile(p, []byte("# Title\n\n## Section\ntext\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cs, err := Query(p, "", "(atx_heading) @heading")
|
|
if err != nil || len(cs) != 2 || cs[0].Line != 1 || cs[1].Line != 3 {
|
|
t.Fatalf("headings=%+v err=%v", cs, err)
|
|
}
|
|
}
|