package app import ( "path/filepath" "testing" ) func TestWithin(t *testing.T) { root := filepath.Join("C:", "work") cases := []struct { path string want bool }{ {filepath.Join(root, "a.go"), true}, {filepath.Join(root, "sub", "b.go"), true}, {filepath.Join("C:", "workshop", "c.go"), false}, // 同前缀的兄弟目录不算 {filepath.Join("C:", "other", "d.go"), false}, } for _, c := range cases { if got := within(root, c.path); got != c.want { t.Fatalf("within(%q)=%v 期望 %v", c.path, got, c.want) } } if !within(root, root) { t.Fatal("根目录本身应算在内") } } // TestWithinCaseInsensitive Windows 路径大小写不敏感。 func TestWithinCaseInsensitive(t *testing.T) { root := `C:/Work` if !within(root, `c:/work/sub/a.go`) { t.Fatal("大小写不同也应算在目录内") } }