package app import ( "testing" "github.com/egoist/mygo/ui/editor" ) func docAt(p string) *Doc { return &Doc{path: p, buf: editor.NewBuffer("")} } // TestTabLabelUnique 没有重名就只显示文件名。 func TestTabLabelUnique(t *testing.T) { a := &App{docs: []*Doc{docAt("/w/main.go"), docAt("/w/util.go")}} if got := a.tabLabel(a.docs[0]); got != "main.go" { t.Fatalf("got %q", got) } } // TestTabLabelSameName 同名时带上能区分的那一层目录。 func TestTabLabelSameName(t *testing.T) { a := &App{docs: []*Doc{docAt("/w/a/main.go"), docAt("/w/b/main.go")}} if got := a.tabLabel(a.docs[0]); got != "a/main.go" { t.Fatalf("第一个标签 = %q, 想要 a/main.go", got) } if got := a.tabLabel(a.docs[1]); got != "b/main.go" { t.Fatalf("第二个标签 = %q, 想要 b/main.go", got) } } // TestTabLabelDeeper 一层不够时继续往上取。 func TestTabLabelDeeper(t *testing.T) { a := &App{docs: []*Doc{ docAt("/w/internal/x/main.go"), docAt("/w/cmd/x/main.go"), }} got0 := a.tabLabel(a.docs[0]) got1 := a.tabLabel(a.docs[1]) if got0 == got1 { t.Fatalf("两个标签仍重名:%q", got0) } if got0 != "internal/x/main.go" { t.Fatalf("got %q", got0) } } // TestTabLabelWindowsPath Windows 反斜杠路径也要正确切段。 func TestTabLabelWindowsPath(t *testing.T) { a := &App{docs: []*Doc{ docAt(`D:\w\a\main.go`), docAt(`D:\w\b\main.go`), }} if got := a.tabLabel(a.docs[0]); got != "a/main.go" { t.Fatalf("got %q", got) } } // TestTabLabelCaseInsensitive 大小写不同的同名文件也算重名(Windows 语义)。 func TestTabLabelCaseInsensitive(t *testing.T) { a := &App{docs: []*Doc{docAt("/w/a/Main.go"), docAt("/w/b/main.go")}} if got := a.tabLabel(a.docs[0]); got == "Main.go" { t.Fatalf("大小写同名应加目录区分,got %q", got) } }