package app import ( "path/filepath" "strings" "testing" "github.com/egoist/mygo/ui" "github.com/egoist/mygo/ui/editor" ) // menuByID 在菜单项里按 ID 找,找不到返回 false。 func menuByID(items []editorMenuItem, id string) (editorMenuItem, bool) { for _, it := range items { if !it.sep && it.id == id { return it, true } } return editorMenuItem{}, false } // mustMenu 断言某项存在并返回它。 func mustMenu(t *testing.T, items []editorMenuItem, id string) editorMenuItem { t.Helper() it, ok := menuByID(items, id) if !ok { t.Fatalf("菜单里没有 %q", id) } return it } // goReady 是这个项目里最常见的组合:工作区内的 Go 文件 + 语言服务器在线。 func goReady() editorMenuCap { return editorMenuCap{goFile: true, lspReady: true, inRoot: true, onDisk: true} } // TestEditorMenuGoFile 普通 Go 文件:gopls 那几项可用,测试项不出现。 func TestEditorMenuGoFile(t *testing.T) { items := editorMenuItems(goReady()) for _, id := range []string{emCodeActions, emFindRefs, emRename, emFormat, emFind} { if it := mustMenu(t, items, id); !it.enabled { t.Fatalf("%q 应该是可用的", id) } } // 不是 _test.go,跑测试的两项不该露头。 for _, id := range []string{emRunTest, emTestPkg} { if _, ok := menuByID(items, id); ok { t.Fatalf("非测试文件不该有 %q", id) } } } // TestEditorMenuTestFile 测试文件多出跑测试的两项。 func TestEditorMenuTestFile(t *testing.T) { c := goReady() c.testFile = true items := editorMenuItems(c) for _, id := range []string{emRunTest, emTestPkg} { if it := mustMenu(t, items, id); !it.enabled { t.Fatalf("%q 应该可用", id) } } } // TestEditorMenuTestFileOutsideRoot 工作区外打开的测试文件跑不了:没有包路径。 func TestEditorMenuTestFileOutsideRoot(t *testing.T) { c := goReady() c.testFile, c.inRoot = true, false items := editorMenuItems(c) if _, ok := menuByID(items, emRunTest); ok { t.Fatal("不在工作区内不该出现跑测试") } } // TestEditorMenuNonGo 非 Go 文件:gopls 相关的都灰掉,只用通用项。 func TestEditorMenuNonGo(t *testing.T) { c := editorMenuCap{inRoot: true, onDisk: true} // goFile 与 lspReady 都是 false items := editorMenuItems(c) for _, id := range []string{emCodeActions, emFindRefs, emRename, emFormat} { if it := mustMenu(t, items, id); it.enabled { t.Fatalf("非 Go 文件的 %q 应该灰掉", id) } } if it := mustMenu(t, items, emFind); !it.enabled { t.Fatal("「在文件中查找」与语言无关,应该可用") } if it := mustMenu(t, items, emRevealOS); !it.enabled { t.Fatal("「在资源管理器中显示」应该可用") } } // TestEditorMenuNoLSP 语言服务器挂了:代码动作类全灰,但复制路径照旧。 func TestEditorMenuNoLSP(t *testing.T) { c := goReady() c.lspReady = false items := editorMenuItems(c) if it := mustMenu(t, items, emFindRefs); it.enabled { t.Fatal("gopls 不在时「查找引用」应该灰掉") } if it := mustMenu(t, items, emCopyAbs); !it.enabled { t.Fatal("复制绝对路径不该受 gopls 影响") } } // TestEditorMenuReadOnly 只读缓冲区:改文本的项灰掉,只读的项留着。 func TestEditorMenuReadOnly(t *testing.T) { c := goReady() c.readOnly = true items := editorMenuItems(c) for _, id := range []string{emCodeActions, emRename, emFormat} { if it := mustMenu(t, items, id); it.enabled { t.Fatalf("只读文件的 %q 应该灰掉", id) } } if it := mustMenu(t, items, emFindRefs); !it.enabled { t.Fatal("只读文件的「查找引用」仍然可用") } } // TestEditorMenuOutsideRoot 工作区外:相对路径与树内定位都没有意义。 func TestEditorMenuOutsideRoot(t *testing.T) { c := goReady() c.inRoot = false items := editorMenuItems(c) for _, id := range []string{emCopyRel, emRevealTree} { if it := mustMenu(t, items, id); it.enabled { t.Fatalf("工作区外的 %q 应该灰掉", id) } } if it := mustMenu(t, items, emCopyAbs); !it.enabled { t.Fatal("绝对路径在哪儿都能复制") } } // TestCompactEditorMenu 分隔线不留头尾、也不连着两条。 func TestCompactEditorMenu(t *testing.T) { sep := editorMenuItem{sep: true} item := func(id string) editorMenuItem { return editorMenuItem{id: id, label: id, enabled: true} } cases := []struct { name string in []editorMenuItem want []string // "" 表示分隔线 }{ {"连续分隔线合并", []editorMenuItem{item("a"), sep, sep, item("b")}, []string{"a", "", "b"}}, {"去掉开头", []editorMenuItem{sep, sep, item("a")}, []string{"a"}}, {"去掉结尾", []editorMenuItem{item("a"), sep, sep}, []string{"a"}}, {"全是分隔线", []editorMenuItem{sep, sep}, nil}, {"空输入", nil, nil}, {"原样保留", []editorMenuItem{item("a"), sep, item("b")}, []string{"a", "", "b"}}, } for _, c := range cases { got := compactEditorMenu(c.in) if len(got) != len(c.want) { t.Fatalf("%s:长度 %d, 想要 %d", c.name, len(got), len(c.want)) } for i, want := range c.want { isSep := got[i].sep if (want == "") != isSep { t.Fatalf("%s:第 %d 项分隔线状态不对", c.name, i) } if !isSep && got[i].id != want { t.Fatalf("%s:第 %d 项是 %q, 想要 %q", c.name, i, got[i].id, want) } } } } // TestEditorMenuNormalized 真实产出的菜单本身就已经规整过。 func TestEditorMenuNormalized(t *testing.T) { // 非测试文件 + 工作区外,是条件组缺席最多的组合。 items := editorMenuItems(editorMenuCap{goFile: true, lspReady: true}) if len(items) == 0 { t.Fatal("菜单不该是空的") } if items[0].sep || items[len(items)-1].sep { t.Fatal("菜单首尾不该是分隔线") } for i := 1; i < len(items); i++ { if items[i].sep && items[i-1].sep { t.Fatalf("第 %d、%d 项是连着两条分隔线", i-1, i) } } } // TestEditorMenuLabels 每一项都得有文案,且不重名——重名会让用户分不清点的是哪个。 func TestEditorMenuLabels(t *testing.T) { seen := map[string]string{} for _, it := range editorMenuItems(goReady()) { if it.sep { continue } if strings.TrimSpace(it.label) == "" { t.Fatalf("%q 没有文案", it.id) } if prev, dup := seen[it.label]; dup { t.Fatalf("%q 与 %q 文案重复:%s", prev, it.id, it.label) } seen[it.label] = it.id } } // TestEditorMenuShortcut 只给真绑了快捷键的项挂字样。 func TestEditorMenuShortcut(t *testing.T) { cases := []struct { id string mods ui.Modifiers key ui.Key }{ {emCodeActions, ui.Alt, ui.KeyEnter}, {emFindRefs, ui.Shift, ui.KeyF2}, {emRename, 0, ui.KeyF2}, {emFormat, ui.Ctrl | ui.Alt, ui.KeyL}, {emFind, ui.Cmd, ui.KeyF}, } for _, c := range cases { mods, key, ok := editorMenuShortcut(c.id) if !ok { t.Fatalf("%q 应该有快捷键", c.id) } if mods != c.mods || key != c.key { t.Fatalf("%q 的快捷键不对", c.id) } } // 没有绑定快捷键的项不能瞎标。 for _, id := range []string{emRunTest, emTestPkg, emCopyAbs, emCopyRel, emRevealOS, emRevealTree, emHistory} { if _, _, ok := editorMenuShortcut(id); ok { t.Fatalf("%q 没有快捷键却标了", id) } } } // TestMenuRelPath 相对路径统一用正斜杠,出圈时退回绝对路径。 func TestMenuRelPath(t *testing.T) { root := `/w/proj` cases := []struct{ path, want string }{ {`/w/proj/main.go`, "main.go"}, {`/w/proj/internal/app/x.go`, "internal/app/x.go"}, {`/w/other/x.go`, `/w/other/x.go`}, // 不在工作区内 {``, ``}, } for _, c := range cases { if got := menuRelPath(root, c.path); got != c.want { t.Fatalf("menuRelPath(%q) = %q, 想要 %q", c.path, got, c.want) } } if got := menuRelPath("", `/w/proj/main.go`); got != `/w/proj/main.go` { t.Fatalf("没有工作区时应返回原路径,got %q", got) } } // TestEditorMenuOnDoc 从真实文档读出能力:这条链路把 path/只读/工作区串起来。 func TestEditorMenuOnDoc(t *testing.T) { dir := t.TempDir() a := &App{root: dir} goOn := a.newDoc(dir+`/main_test.go`, "package main\n") items := editorMenuItems(a.editorMenuCap(goOn)) // 没有 LSP,代码动作该灰。 if it := mustMenu(t, items, emCodeActions); it.enabled { t.Fatal("没有语言服务器时代码动作应灰") } // 但它在工作区内且是 _test.go,跑测试的项该在(且可用)。 if it := mustMenu(t, items, emRunTest); !it.enabled { t.Fatal("工作区内的测试文件应能跑光标处的测试") } // 换成只读 + 非 Go 的文档,代码动作、格式化都灰,且没有测试项。 ro := a.newDoc(dir+`/notes.txt`, "hello\n") ro.st.ReadOnly = true items = editorMenuItems(a.editorMenuCap(ro)) if it := mustMenu(t, items, emFormat); it.enabled { t.Fatal("只读文件的格式化应灰") } if _, ok := menuByID(items, emRunTest); ok { t.Fatal("txt 文件不该有跑测试") } } // TestEditorMenuShowsInEditor 真把菜单弹出来:右键编辑器,读菜单文案。 // // 这一步是纯函数测试覆盖不到的那一半——钩子有没有真挂在 editor.State 上、 // 框架有没有在它自己那批项后面接上应用层的项、灰掉的项是不是真点不动。 func TestEditorMenuShowsInEditor(t *testing.T) { dir := t.TempDir() a := &App{root: dir} d := a.newDoc(filepath.Join(dir, "main.go"), "package main\n") tt := ui.NewTester(func(c *ui.Context) { editor.View(c, d.st).Fill().AutoFocus() }, 600, 400) tt.Frame() tt.RightClickAt(300, 200) labels := tt.Menu() if len(labels) == 0 { t.Fatal("右键编辑器没有弹出菜单") } for _, want := range []string{"代码动作(Alt+Enter)", "复制相对路径", "复制绝对路径", "在资源管理器中显示"} { if !strings.Contains(strings.Join(labels, "\n"), want) { t.Fatalf("菜单里没有 %q,实际:%q", want, labels) } } // 没有语言服务器,「代码动作」是灰的:点它应当被拒。 if err := tt.ChooseMenuItem("代码动作(Alt+Enter)"); err == nil { t.Fatal("灰掉的菜单项居然能选") } // 换一项能用的:复制绝对路径,走完整条 「菜单 → 动作 → 剪贴板」 链路。 var copied string old := clipWrite clipWrite = func(s string) error { copied = s; return nil } defer func() { clipWrite = old }() if err := tt.ChooseMenuItem("复制绝对路径"); err != nil { t.Fatalf("选「复制绝对路径」失败:%v", err) } tt.Frame() if copied != d.path { t.Fatalf("复制了 %q, 想要 %q", copied, d.path) } // 相对路径那项复制的是相对工作区的写法。 copied = "" tt.RightClickAt(300, 200) if err := tt.ChooseMenuItem("复制相对路径"); err != nil { t.Fatalf("选「复制相对路径」失败:%v", err) } if copied != "main.go" { t.Fatalf("复制了 %q, 想要 main.go", copied) } }