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