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) }