Files
MyGolandRelease/internal/app/testlens_test.go
T
zerorigin c35564b65e 编辑器右键菜单补应用层动作,并做完评估报告"本周做"剩余项
第 9 项。原报告 4.5 判断「编辑器无右键菜单」是错的:框架 ui/editor
自带完整菜单(跳转定义、剪切/复制/粘贴、全选、切换注释、选区/转换/
折叠三个子菜单),State.ContextMenu 只是留给应用层的追加点。所以本次
不重造菜单,只往那个追加点里填应用层独有的 16 项:

- gopls:代码动作、查找引用、重命名符号(要求 Go 文件 + LSP 在线,
  改文本的还要求非只读)
- 编辑:格式化文档、在文件中查找
- 测试:运行光标处的测试 / 当前包(只在 _test.go 出现,普通文件里挂
  一个永远点不动的项是噪音,而它一旦可点又会退化成跑全部测试)
- 定位:在项目树中定位、在资源管理器中显示、本地历史
- 剪贴板:复制相对路径 / 绝对路径

菜单项造在纯函数 editorMenuItems(editorMenuCap) 里,可用性由
goFile/lspReady/inRoot/onDisk/readOnly 五个事实决定;条件插入容易留下
连着两条分隔线或末尾挂一条,统一由 compactEditorMenu 收口。

验证不止单测:TestEditorMenuShowsInEditor 用 ui.NewTester 真把编辑器
渲染出来、真右击、读 tt.Menu() 的菜单文案,再 ChooseMenuItem 走完
「菜单 → 动作 → 剪贴板」整条链路,并断言灰掉的项点不动。框架文档明确
说右键菜单不能用 automation 插座驱动,所以走 Tester 这条路。

顺带一次 gofmt -w 把全仓 gofmt -l 清空(13 个文件,多是 CRLF 与文档
注释空格),并更正报告 4.5、补齐执行状态。

其余"本周做"项(回收站删除、参数引号切分 + Env/WorkDir、日志轮转、
标签同名区分)一并入库。
2026-11-05 16:06:17 +08:00

124 lines
3.2 KiB
Go

package app
import (
"os"
"path/filepath"
"reflect"
"testing"
)
func TestFailureLocPicksTestFile(t *testing.T) {
lines := []string{
"=== RUN TestFoo",
" main_test.go:12: boom",
"--- FAIL: TestFoo (0.00s)",
"FAIL",
}
file, line := failureLoc(lines)
if file != "main_test.go" || line != 12 {
t.Fatalf("应解析出 main_test.go:12,实际 %s:%d", file, line)
}
}
func TestFailureLocAbsolutePath(t *testing.T) {
file, line := failureLoc([]string{` D:\x\y\sub_test.go:7: nope`})
if file != `D:\x\y\sub_test.go` || line != 7 {
t.Fatalf("绝对路径应原样保留,实际 %s:%d", file, line)
}
}
func TestFailureLocNone(t *testing.T) {
if f, l := failureLoc([]string{"--- FAIL: TestX (0.1s)", "panic: boom"}); f != "" || l != 0 {
t.Fatalf("没有 file:line 时应为空,实际 %s:%d", f, l)
}
}
func TestRunFilterFor(t *testing.T) {
cases := map[string]string{
"TestFoo": "^TestFoo$",
"TestFoo/case 1": "^TestFoo$/^case 1$",
"TestFoo/a[b]/sub": "^TestFoo$/^a\\[b\\]$/^sub$",
}
for name, want := range cases {
if got := runFilterFor(name); got != want {
t.Errorf("runFilterFor(%q) = %q,期望 %q", name, got, want)
}
}
}
func TestScanTestFuncs(t *testing.T) {
src := `package app
func helper(t *testing.T) {}
func TestFoo(t *testing.T) {
}
func BenchmarkX(b *testing.B) {
}
func TestBar(t *testing.T) {
}
`
got := scanTestFuncs(src)
want := []testFuncInfo{
{Name: "TestFoo", Line: 4},
{Name: "BenchmarkX", Line: 7},
{Name: "TestBar", Line: 10},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("扫描结果不符,got %+v want %+v", got, want)
}
}
func TestTestFuncAt(t *testing.T) {
src := "package app\n\nfunc TestFoo(t *testing.T) {\n\tx()\n}\n\nfunc TestBar(t *testing.T) {\n}\n"
cases := []struct {
line int
name string
found bool
}{
{0, "", false}, // package 行
{1, "", false}, // 第一个函数之前
{2, "TestFoo", true}, // 声明行本身
{3, "TestFoo", true}, // 函数体内
{4, "TestFoo", true}, // 收尾 } 上仍算在函数里
{5, "", false}, // 函数之间的空行
{6, "TestBar", true},
}
for _, c := range cases {
name, ok := testFuncAt(src, c.line)
if ok != c.found || name != c.name {
t.Errorf("testFuncAt(line=%d) = (%q,%v),期望 (%q,%v)", c.line, name, ok, c.name, c.found)
}
}
}
func TestResolveTestFile(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "go.mod"), []byte("module demo\n\ngo 1.21\n"), 0o644); err != nil {
t.Fatal(err)
}
dir := filepath.Join(root, "internal", "app")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "foo_test.go"), []byte("package app\n"), 0o644); err != nil {
t.Fatal(err)
}
a := &App{root: root}
got := a.resolveTestFile("demo/internal/app", "foo_test.go")
if want := filepath.Join(dir, "foo_test.go"); got != want {
t.Fatalf("应按 module 前缀还原,got %q want %q", got, want)
}
// 绝对路径直接透传。
if got := a.resolveTestFile("demo", root); got != root {
t.Fatalf("绝对路径应透传,got %q", got)
}
// 包路径不在 module 里且文件不存在:不 panic,返回一个猜测路径。
_ = a.resolveTestFile("other/pkg", "zzz_test.go")
}