github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/path/path_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package path 6 7 import ( 8 "runtime" 9 "testing" 10 ) 11 12 type PathTest struct { 13 path, result string 14 } 15 16 var cleantests = []PathTest{ 17 // Already clean 18 {"", "."}, 19 {"abc", "abc"}, 20 {"abc/def", "abc/def"}, 21 {"a/b/c", "a/b/c"}, 22 {".", "."}, 23 {"..", ".."}, 24 {"../..", "../.."}, 25 {"../../abc", "../../abc"}, 26 {"/abc", "/abc"}, 27 {"/", "/"}, 28 29 // Remove trailing slash 30 {"abc/", "abc"}, 31 {"abc/def/", "abc/def"}, 32 {"a/b/c/", "a/b/c"}, 33 {"./", "."}, 34 {"../", ".."}, 35 {"../../", "../.."}, 36 {"/abc/", "/abc"}, 37 38 // Remove doubled slash 39 {"abc//def//ghi", "abc/def/ghi"}, 40 {"//abc", "/abc"}, 41 {"///abc", "/abc"}, 42 {"//abc//", "/abc"}, 43 {"abc//", "abc"}, 44 45 // Remove . elements 46 {"abc/./def", "abc/def"}, 47 {"/./abc/def", "/abc/def"}, 48 {"abc/.", "abc"}, 49 50 // Remove .. elements 51 {"abc/def/ghi/../jkl", "abc/def/jkl"}, 52 {"abc/def/../ghi/../jkl", "abc/jkl"}, 53 {"abc/def/..", "abc"}, 54 {"abc/def/../..", "."}, 55 {"/abc/def/../..", "/"}, 56 {"abc/def/../../..", ".."}, 57 {"/abc/def/../../..", "/"}, 58 {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"}, 59 60 // Combinations 61 {"abc/./../def", "def"}, 62 {"abc//./../def", "def"}, 63 {"abc/../../././../def", "../../def"}, 64 } 65 66 func TestClean(t *testing.T) { 67 for _, test := range cleantests { 68 if s := Clean(test.path); s != test.result { 69 t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result) 70 } 71 if s := Clean(test.result); s != test.result { 72 t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result) 73 } 74 } 75 } 76 77 func TestCleanMallocs(t *testing.T) { 78 if testing.Short() { 79 t.Skip("skipping malloc count in short mode") 80 } 81 if runtime.GOMAXPROCS(0) > 1 { 82 t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1") 83 return 84 } 85 86 for _, test := range cleantests { 87 allocs := testing.AllocsPerRun(100, func() { Clean(test.result) }) 88 if allocs > 0 { 89 t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs) 90 } 91 } 92 } 93 94 type SplitTest struct { 95 path, dir, file string 96 } 97 98 var splittests = []SplitTest{ 99 {"a/b", "a/", "b"}, 100 {"a/b/", "a/b/", ""}, 101 {"a/", "a/", ""}, 102 {"a", "", "a"}, 103 {"/", "/", ""}, 104 } 105 106 func TestSplit(t *testing.T) { 107 for _, test := range splittests { 108 if d, f := Split(test.path); d != test.dir || f != test.file { 109 t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file) 110 } 111 } 112 } 113 114 type JoinTest struct { 115 elem []string 116 path string 117 } 118 119 var jointests = []JoinTest{ 120 // zero parameters 121 {[]string{}, ""}, 122 123 // one parameter 124 {[]string{""}, ""}, 125 {[]string{"a"}, "a"}, 126 127 // two parameters 128 {[]string{"a", "b"}, "a/b"}, 129 {[]string{"a", ""}, "a"}, 130 {[]string{"", "b"}, "b"}, 131 {[]string{"/", "a"}, "/a"}, 132 {[]string{"/", ""}, "/"}, 133 {[]string{"a/", "b"}, "a/b"}, 134 {[]string{"a/", ""}, "a"}, 135 {[]string{"", ""}, ""}, 136 } 137 138 func TestJoin(t *testing.T) { 139 for _, test := range jointests { 140 if p := Join(test.elem...); p != test.path { 141 t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path) 142 } 143 } 144 } 145 146 type ExtTest struct { 147 path, ext string 148 } 149 150 var exttests = []ExtTest{ 151 {"path.go", ".go"}, 152 {"path.pb.go", ".go"}, 153 {"a.dir/b", ""}, 154 {"a.dir/b.go", ".go"}, 155 {"a.dir/", ""}, 156 } 157 158 func TestExt(t *testing.T) { 159 for _, test := range exttests { 160 if x := Ext(test.path); x != test.ext { 161 t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext) 162 } 163 } 164 } 165 166 var basetests = []PathTest{ 167 // Already clean 168 {"", "."}, 169 {".", "."}, 170 {"/.", "."}, 171 {"/", "/"}, 172 {"////", "/"}, 173 {"x/", "x"}, 174 {"abc", "abc"}, 175 {"abc/def", "def"}, 176 {"a/b/.x", ".x"}, 177 {"a/b/c.", "c."}, 178 {"a/b/c.x", "c.x"}, 179 } 180 181 func TestBase(t *testing.T) { 182 for _, test := range basetests { 183 if s := Base(test.path); s != test.result { 184 t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result) 185 } 186 } 187 } 188 189 var dirtests = []PathTest{ 190 {"", "."}, 191 {".", "."}, 192 {"/.", "/"}, 193 {"/", "/"}, 194 {"////", "/"}, 195 {"/foo", "/"}, 196 {"x/", "x"}, 197 {"abc", "."}, 198 {"abc/def", "abc"}, 199 {"abc////def", "abc"}, 200 {"a/b/.x", "a/b"}, 201 {"a/b/c.", "a/b"}, 202 {"a/b/c.x", "a/b"}, 203 } 204 205 func TestDir(t *testing.T) { 206 for _, test := range dirtests { 207 if s := Dir(test.path); s != test.result { 208 t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result) 209 } 210 } 211 } 212 213 type IsAbsTest struct { 214 path string 215 isAbs bool 216 } 217 218 var isAbsTests = []IsAbsTest{ 219 {"", false}, 220 {"/", true}, 221 {"/usr/bin/gcc", true}, 222 {"..", false}, 223 {"/a/../bb", true}, 224 {".", false}, 225 {"./", false}, 226 {"lala", false}, 227 } 228 229 func TestIsAbs(t *testing.T) { 230 for _, test := range isAbsTests { 231 if r := IsAbs(test.path); r != test.isAbs { 232 t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs) 233 } 234 } 235 }