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