Files
MyGolandRelease/internal/app/tabmenu_test.go
T
zerorigin 4ace9c9fb6 fix: 按评估优先级修四项(标签右键/UTF-8 截断/覆盖率输出/.gitignore)
1. 标签页右键菜单接回(回归修复)
   tabmenu.go 的 160 行实现全库零调用点,tabBar 只处理 Clicked。
   补 RightClicked → openPop(tabEntries(path)),右键时先设 active
   (菜单无标题,靠标签高亮传达「关闭左边」相对谁算)。
   新增 tabmenu_test.go 8 个单测。

2. 中文按字节截断导致乱码
   palette.go trimmed[:160]、rename.go s[:140] 会把多字节字符切断。
   trimTo 从 gitlog.go 提到 strutil.go(三个调用方共用),两处改用它;
   n<=0 返回空串。strutil_test.go 6 个单测(含真实 linePreview 路径)。

3. 覆盖率运行不再吞测试输出
   原来 lines 收集后 _ = lines 丢弃,测试失败只看到覆盖率数字。
   改用 ExitCode() 判失败,只回放 FAIL/--- FAIL/panic/# 包/file.go:12 行
   (上限 200),覆盖率页顶部给红底摘要 + 「查看测试输出」按钮。
   coverageFailLines/Summary 纯函数 + 5 个单测。

4. 索引与搜索感知 .gitignore
   新增 ignore.go:dir/、*.ext、/rootonly、带路径、**、! 取反、注释。
   内置跳过扩到 vendor/dist/build/out/target/.next(文件树仍用保守 skipDir)。
   indexState 加 truncated,快速打开底部提示「索引已截断至 N 个文件」。
   ignore_test.go 10 个单测 + 真实遍历集成测试。

验证:go vet ./... 干净;go test ./internal/... 7 包全绿;
e2e 实例 tools/tree 确认菜单 5 项齐全 + tools/shot 截图核对。
2026-11-05 15:43:33 +08:00

160 lines
4.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package app
import (
"path/filepath"
"strings"
"testing"
"github.com/egoist/mygo/ui/editor"
)
// newTabDoc 造一个只带路径与缓冲区的文档,够 tabmenu 用了。
func newTabDoc(p, content string) *Doc {
return &Doc{path: p, buf: editor.NewBuffer(content)}
}
// labels 把菜单条目压成标签列表,便于断言(分隔条记为 "---")。
func labels(es []menuEntry) []string {
out := make([]string, 0, len(es))
for _, e := range es {
if e.divider {
out = append(out, "---")
continue
}
out = append(out, e.label)
}
return out
}
// TestDocIndex 标签位置查找。
func TestDocIndex(t *testing.T) {
a := &App{docs: []*Doc{
newTabDoc("/x/a.go", "a"),
newTabDoc("/x/b.go", "b"),
newTabDoc("/x/c.go", "c"),
}}
if got := a.docIndex("/x/b.go"); got != 1 {
t.Fatalf("docIndex(b) = %d, 想要 1", got)
}
if got := a.docIndex("/x/none.go"); got != -1 {
t.Fatalf("docIndex(none) = %d, 想要 -1", got)
}
}
// TestSavedPaths 只收没有未保存改动的。
func TestSavedPaths(t *testing.T) {
a := &App{docs: []*Doc{
newTabDoc("/x/a.go", "a"),
newTabDoc("/x/b.go", "b"),
}}
a.docs[1].buf.Edit(editor.Edit{Start: 0, End: 0, Text: "// dirty\n"})
got := a.savedPaths()
if len(got) != 1 || got[0] != "/x/a.go" {
t.Fatalf("savedPaths() = %v, 只该有 /x/a.go", got)
}
}
// TestTabEntriesMiddle 中间那个标签:左右都能关,且能关其他。
func TestTabEntriesMiddle(t *testing.T) {
a := &App{docs: []*Doc{
newTabDoc("/x/a.go", "a"),
newTabDoc("/x/b.go", "b"),
newTabDoc("/x/c.go", "c"),
}}
got := labels(a.tabEntries("/x/b.go"))
want := []string{
"关闭",
"关闭左边(1)",
"关闭右边(1)",
"---",
"关闭其他(2)",
"关闭全部(3)",
"---",
"关闭已保存的(3)",
}
if len(got) != len(want) {
t.Fatalf("条目数 %d, 想要 %d:%v", len(got), len(want), got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("第 %d 条 = %q, 想要 %q(全部:%v)", i, got[i], want[i], got)
}
}
}
// TestTabEntriesFirstLast 首尾标签的「关闭左边/右边」应当消失。
func TestTabEntriesFirstLast(t *testing.T) {
a := &App{docs: []*Doc{
newTabDoc("/x/a.go", "a"),
newTabDoc("/x/b.go", "b"),
}}
first := labels(a.tabEntries("/x/a.go"))
if len(first) == 0 || first[0] != "关闭" {
t.Fatalf("首标签菜单异常:%v", first)
}
for _, s := range first {
if strings.HasPrefix(s, "关闭左边") {
t.Fatalf("首标签不该有「关闭左边」:%v", first)
}
}
last := labels(a.tabEntries("/x/b.go"))
for _, s := range last {
if strings.HasPrefix(s, "关闭右边") {
t.Fatalf("末标签不该有「关闭右边」:%v", last)
}
}
}
// TestTabEntriesSingle 只剩一个标签时,没有「关闭其他」「关闭已保存的」。
func TestTabEntriesSingle(t *testing.T) {
a := &App{docs: []*Doc{newTabDoc("/x/a.go", "a")}}
got := labels(a.tabEntries("/x/a.go"))
want := []string{"关闭", "---", "关闭全部(1)", "---", "关闭已保存的(1)"}
if len(got) != len(want) {
t.Fatalf("条目数 %d, 想要 %d:%v", len(got), len(want), got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("第 %d 条 = %q, 想要 %q(全部:%v)", i, got[i], want[i], got)
}
}
}
// TestTabEntriesSavedDisabled 全部未保存时,「关闭已保存的」置灰且不可点。
func TestTabEntriesSavedDisabled(t *testing.T) {
a := &App{docs: []*Doc{
newTabDoc("/x/a.go", "a"),
newTabDoc("/x/b.go", "b"),
}}
a.docs[0].buf.Edit(editor.Edit{Start: 0, End: 0, Text: "// dirty\n"})
a.docs[1].buf.Edit(editor.Edit{Start: 0, End: 0, Text: "// dirty\n"})
es := a.tabEntries("/x/a.go")
last := es[len(es)-1]
if !last.disabled {
t.Fatalf("全脏时「关闭已保存的」应当 disabled:%+v", last)
}
if last.action != nil {
t.Fatalf("disabled 项不该带 action")
}
}
// TestTabEntriesMissingDoc 路径不在标签里时返回 nil(openPop 会因此不弹)。
func TestTabEntriesMissingDoc(t *testing.T) {
a := &App{docs: []*Doc{newTabDoc("/x/a.go", "a")}}
if es := a.tabEntries("/x/none.go"); es != nil {
t.Fatalf("不存在的路径应返回 nil,得到 %v", labels(es))
}
}
// TestClosePathsRange 区间裁剪:越界不该 panic。
func TestClosePathsRange(t *testing.T) {
a := &App{docs: []*Doc{
newTabDoc(filepath.Join("tmp", "a.go"), "a"),
}}
// a.win 为 nil,closePathsOf 会直接返回;这里只验证区间计算不越界。
a.closePaths(-5, 99)
a.closePaths(3, 1)
}