Files
MyGolandRelease/internal/app/palette_test.go
T
zerorigin a80c0f886a MyGoland 首次提交:Go 轻量 IDE
- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲)
- 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表)
- 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记
- Git:状态着色、选择性暂存提交、pull/push、历史并排对比
- 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复
- 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈
- 设计文档与验证记录见 docs/plans/
2026-11-04 16:37:05 +08:00

103 lines
2.7 KiB
Go

package app
import (
"os"
"path/filepath"
"testing"
)
func TestFuzzyScore(t *testing.T) {
cases := []struct {
path, query string
want bool
}{
{"main.go", "main", true},
{"main.go", "mg", true},
{"internal/app/view.go", "view", true},
{"internal/app/view.go", "iaview", true},
{"main.go", "xyz", false},
{"", "", true},
}
for _, c := range cases {
if _, ok := fuzzyScore(c.path, c.query); ok != c.want {
t.Fatalf("fuzzyScore(%q,%q)=%v 期望 %v", c.path, c.query, ok, c.want)
}
}
}
// TestFuzzyScorePrefersBasename 确认按文件名命中的排在目录命中的前面。
func TestFuzzyScorePrefersBasename(t *testing.T) {
a, ok1 := fuzzyScore("internal/app/view.go", "view")
b, ok2 := fuzzyScore("view/internal/other.go", "view")
if !ok1 || !ok2 {
t.Fatal("两条都该命中")
}
if a <= b {
t.Fatalf("文件名命中应得分更高: %d vs %d", a, b)
}
}
func TestQuickResultsRanking(t *testing.T) {
a := &App{root: t.TempDir()}
a.index.mu.Lock()
a.index.root = a.root
a.index.files = []string{"internal/app/view.go", "view/readme.md", "main.go"}
a.index.ready = true
a.index.mu.Unlock()
got := a.quickResults("view", 10)
if len(got) != 2 {
t.Fatalf("应命中 2 个,实际 %d", len(got))
}
if got[0].Label != "view.go" {
t.Fatalf("文件名命中应排第一,实际 %q", got[0].Label)
}
}
func TestSearchInWorkspace(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "sub"), 0o755); err != nil {
t.Fatal(err)
}
files := map[string]string{
"a.go": "package a\n\nfunc Needle() {}\n",
"sub/b.go": "package b\n// needle 在这里\n",
"c.txt": "nothing\n",
}
for name, body := range files {
if err := os.WriteFile(filepath.Join(root, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
a := &App{root: root}
got := a.searchInWorkspace("needle", 10, searchOpts{})
if len(got) != 2 {
t.Fatalf("应命中 2 行,实际 %d: %+v", len(got), got)
}
seen := map[string]int{}
for _, it := range got {
seen[it.Label] = it.Line
}
if seen["a.go"] != 3 {
t.Fatalf("a.go 命中行应为 3,实际 %d", seen["a.go"])
}
if seen[filepath.ToSlash(filepath.Join("sub", "b.go"))] != 2 {
t.Fatalf("sub/b.go 命中行应为 2,实际 %v", seen)
}
}
func TestSearchSkipsSkippedDirs(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "node_modules"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "node_modules", "x.js"), []byte("needle\n"), 0o644); err != nil {
t.Fatal(err)
}
a := &App{root: root}
if got := a.searchInWorkspace("needle", 10, searchOpts{}); len(got) != 0 {
t.Fatalf("应当跳过 node_modules,实际命中 %d", len(got))
}
}