package app import ( "io/fs" "os" "path/filepath" "testing" ) func TestExpandAll(t *testing.T) { root := t.TempDir() deep := filepath.Join(root, "a", "b") if err := os.MkdirAll(deep, 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(deep, "x.go"), []byte("package x\n"), 0o644); err != nil { t.Fatal(err) } a := &App{root: root, open: map[string]*bool{}, dirs: map[string][]fs.DirEntry{}} a.ExpandAll() for _, dir := range []string{ filepath.Join(root, "a"), filepath.Join(deep), } { if !*a.openFlag(dir) { t.Fatalf("%s 没有被展开", dir) } } a.CollapseAll() for _, dir := range []string{ filepath.Join(root, "a"), filepath.Join(deep), } { if *a.openFlag(dir) { t.Fatalf("%s 没有被折叠", dir) } } } func TestAddEntriesTargetDir(t *testing.T) { root := t.TempDir() f := filepath.Join(root, "main.go") if err := os.WriteFile(f, []byte("package main\n"), 0o644); err != nil { t.Fatal(err) } a := &App{root: root, open: map[string]*bool{}, dirs: map[string][]fs.DirEntry{}, sel: map[string]bool{}} // 没有选中项时落在工作区根。 if got := len(a.addEntries()); got != 2 { t.Fatalf("应当有两个菜单项,实际 %d", got) } // 选中一个文件时,应当落到它所在的目录(这里是根)。 a.sel[f] = true if got := len(a.addEntries()); got != 2 { t.Fatalf("应当有两个菜单项,实际 %d", got) } }