blob: 13b585223f834216410fa8d78962421346c944ab [file] [log] [blame]
Russ Cox16b38b52009-04-07 00:40:07 -07001// 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
5package path
6
7import (
Albert Strasheim0a71a5b2013-03-06 15:52:32 -08008 "runtime"
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08009 "testing"
Russ Cox16b38b52009-04-07 00:40:07 -070010)
11
Rob Pikeb6122b02011-12-22 14:08:34 -080012type PathTest struct {
13 path, result string
Russ Cox16b38b52009-04-07 00:40:07 -070014}
15
Rob Pikeb6122b02011-12-22 14:08:34 -080016var cleantests = []PathTest{
Russ Cox16b38b52009-04-07 00:40:07 -070017 // Already clean
Robert Griesemer34788912010-10-22 10:06:33 -070018 {"", "."},
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 {"/", "/"},
Russ Cox16b38b52009-04-07 00:40:07 -070028
29 // Remove trailing slash
Robert Griesemer34788912010-10-22 10:06:33 -070030 {"abc/", "abc"},
31 {"abc/def/", "abc/def"},
32 {"a/b/c/", "a/b/c"},
33 {"./", "."},
34 {"../", ".."},
35 {"../../", "../.."},
36 {"/abc/", "/abc"},
Russ Cox16b38b52009-04-07 00:40:07 -070037
38 // Remove doubled slash
Robert Griesemer34788912010-10-22 10:06:33 -070039 {"abc//def//ghi", "abc/def/ghi"},
40 {"//abc", "/abc"},
41 {"///abc", "/abc"},
42 {"//abc//", "/abc"},
43 {"abc//", "abc"},
Russ Cox16b38b52009-04-07 00:40:07 -070044
45 // Remove . elements
Robert Griesemer34788912010-10-22 10:06:33 -070046 {"abc/./def", "abc/def"},
47 {"/./abc/def", "/abc/def"},
48 {"abc/.", "abc"},
Russ Cox16b38b52009-04-07 00:40:07 -070049
50 // Remove .. elements
Robert Griesemer34788912010-10-22 10:06:33 -070051 {"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"},
Russ Cox16b38b52009-04-07 00:40:07 -070059
60 // Combinations
Robert Griesemer34788912010-10-22 10:06:33 -070061 {"abc/./../def", "def"},
62 {"abc//./../def", "def"},
63 {"abc/../../././../def", "../../def"},
Russ Cox16b38b52009-04-07 00:40:07 -070064}
65
66func TestClean(t *testing.T) {
Russ Coxca6a0fe2009-09-15 09:41:59 -070067 for _, test := range cleantests {
Rob Pikeb6122b02011-12-22 14:08:34 -080068 if s := Clean(test.path); s != test.result {
69 t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
Russ Cox16b38b52009-04-07 00:40:07 -070070 }
Russ Cox95253722012-06-27 16:52:36 -040071 if s := Clean(test.result); s != test.result {
72 t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
73 }
74 }
Rob Pikef5787262013-08-21 14:00:45 +100075}
Russ Cox95253722012-06-27 16:52:36 -040076
Rob Pikef5787262013-08-21 14:00:45 +100077func TestCleanMallocs(t *testing.T) {
78 if testing.Short() {
79 t.Skip("skipping malloc count in short mode")
80 }
Albert Strasheim0a71a5b2013-03-06 15:52:32 -080081 if runtime.GOMAXPROCS(0) > 1 {
82 t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
83 return
84 }
85
Kyle Lemons9bfd3c32013-02-02 22:52:29 -050086 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)
Russ Cox95253722012-06-27 16:52:36 -040090 }
91 }
Russ Cox16b38b52009-04-07 00:40:07 -070092}
93
94type SplitTest struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080095 path, dir, file string
Russ Cox16b38b52009-04-07 00:40:07 -070096}
97
Russ Cox094f1d52009-10-08 15:14:54 -070098var splittests = []SplitTest{
Robert Griesemer34788912010-10-22 10:06:33 -070099 {"a/b", "a/", "b"},
100 {"a/b/", "a/b/", ""},
101 {"a/", "a/", ""},
102 {"a", "", "a"},
103 {"/", "/", ""},
Russ Cox16b38b52009-04-07 00:40:07 -0700104}
105
106func TestSplit(t *testing.T) {
Russ Coxca6a0fe2009-09-15 09:41:59 -0700107 for _, test := range splittests {
Russ Cox16b38b52009-04-07 00:40:07 -0700108 if d, f := Split(test.path); d != test.dir || f != test.file {
Robert Griesemer40621d52009-11-09 12:07:39 -0800109 t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)
Russ Cox16b38b52009-04-07 00:40:07 -0700110 }
111 }
112}
113
114type JoinTest struct {
Stephen Weinbergbc43cc32010-02-05 02:39:33 -0800115 elem []string
116 path string
Russ Cox16b38b52009-04-07 00:40:07 -0700117}
118
Russ Cox094f1d52009-10-08 15:14:54 -0700119var jointests = []JoinTest{
Stephen Weinbergbc43cc32010-02-05 02:39:33 -0800120 // zero parameters
Robert Griesemer34788912010-10-22 10:06:33 -0700121 {[]string{}, ""},
Stephen Weinbergbc43cc32010-02-05 02:39:33 -0800122
123 // one parameter
Robert Griesemer34788912010-10-22 10:06:33 -0700124 {[]string{""}, ""},
125 {[]string{"a"}, "a"},
Stephen Weinbergbc43cc32010-02-05 02:39:33 -0800126
127 // two parameters
Robert Griesemer34788912010-10-22 10:06:33 -0700128 {[]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{"", ""}, ""},
Stephen Weinbergbc43cc32010-02-05 02:39:33 -0800136}
137
138// join takes a []string and passes it to Join.
139func join(elem []string, args ...string) string {
140 args = elem
Russ Cox2ee420f2010-09-24 11:55:48 -0400141 return Join(args...)
Russ Cox7cbec412009-04-07 21:53:39 -0700142}
143
144func TestJoin(t *testing.T) {
Russ Coxca6a0fe2009-09-15 09:41:59 -0700145 for _, test := range jointests {
Stephen Weinbergbc43cc32010-02-05 02:39:33 -0800146 if p := join(test.elem); p != test.path {
147 t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
Russ Cox7cbec412009-04-07 21:53:39 -0700148 }
149 }
Russ Cox16b38b52009-04-07 00:40:07 -0700150}
151
152type ExtTest struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800153 path, ext string
Russ Cox16b38b52009-04-07 00:40:07 -0700154}
155
Russ Cox094f1d52009-10-08 15:14:54 -0700156var exttests = []ExtTest{
Robert Griesemer34788912010-10-22 10:06:33 -0700157 {"path.go", ".go"},
158 {"path.pb.go", ".go"},
159 {"a.dir/b", ""},
160 {"a.dir/b.go", ".go"},
161 {"a.dir/", ""},
Russ Cox16b38b52009-04-07 00:40:07 -0700162}
Russ Cox7cbec412009-04-07 21:53:39 -0700163
164func TestExt(t *testing.T) {
Russ Coxca6a0fe2009-09-15 09:41:59 -0700165 for _, test := range exttests {
Russ Cox7cbec412009-04-07 21:53:39 -0700166 if x := Ext(test.path); x != test.ext {
Robert Griesemer40621d52009-11-09 12:07:39 -0800167 t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext)
Russ Cox7cbec412009-04-07 21:53:39 -0700168 }
169 }
170}
Robert Griesemer4adad652009-10-19 11:48:04 -0700171
Rob Pikeb6122b02011-12-22 14:08:34 -0800172var basetests = []PathTest{
Rob Pike62b5c7c2010-06-09 19:59:22 -0700173 // Already clean
Robert Griesemer34788912010-10-22 10:06:33 -0700174 {"", "."},
175 {".", "."},
176 {"/.", "."},
177 {"/", "/"},
178 {"////", "/"},
179 {"x/", "x"},
180 {"abc", "abc"},
181 {"abc/def", "def"},
182 {"a/b/.x", ".x"},
183 {"a/b/c.", "c."},
184 {"a/b/c.x", "c.x"},
Rob Pike62b5c7c2010-06-09 19:59:22 -0700185}
186
187func TestBase(t *testing.T) {
188 for _, test := range basetests {
Rob Pikeb6122b02011-12-22 14:08:34 -0800189 if s := Base(test.path); s != test.result {
190 t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result)
191 }
192 }
193}
194
195var dirtests = []PathTest{
196 {"", "."},
197 {".", "."},
198 {"/.", "/"},
199 {"/", "/"},
200 {"////", "/"},
201 {"/foo", "/"},
202 {"x/", "x"},
203 {"abc", "."},
204 {"abc/def", "abc"},
Rob Pikeb7627d32012-08-30 11:16:41 -0700205 {"abc////def", "abc"},
Rob Pikeb6122b02011-12-22 14:08:34 -0800206 {"a/b/.x", "a/b"},
207 {"a/b/c.", "a/b"},
208 {"a/b/c.x", "a/b"},
209}
210
211func TestDir(t *testing.T) {
212 for _, test := range dirtests {
213 if s := Dir(test.path); s != test.result {
214 t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result)
Rob Pike62b5c7c2010-06-09 19:59:22 -0700215 }
216 }
217}
Ivan Krasindfb2af62010-09-09 01:42:43 -0400218
219type IsAbsTest struct {
220 path string
221 isAbs bool
222}
223
224var isAbsTests = []IsAbsTest{
Robert Griesemer34788912010-10-22 10:06:33 -0700225 {"", false},
226 {"/", true},
227 {"/usr/bin/gcc", true},
228 {"..", false},
229 {"/a/../bb", true},
230 {".", false},
231 {"./", false},
232 {"lala", false},
Ivan Krasindfb2af62010-09-09 01:42:43 -0400233}
234
235func TestIsAbs(t *testing.T) {
236 for _, test := range isAbsTests {
237 if r := IsAbs(test.path); r != test.isAbs {
238 t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)
239 }
240 }
241}