2009-04-07 01:40:07 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package path
|
|
|
|
|
|
|
|
import (
|
2009-10-08 16:14:54 -06:00
|
|
|
"testing";
|
2009-04-07 01:40:07 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type CleanTest struct {
|
2009-10-08 16:14:54 -06:00
|
|
|
path, clean string;
|
2009-04-07 01:40:07 -06:00
|
|
|
}
|
|
|
|
|
2009-10-08 16:14:54 -06:00
|
|
|
var cleantests = []CleanTest{
|
2009-04-07 01:40:07 -06:00
|
|
|
// Already clean
|
|
|
|
CleanTest{"", "."},
|
|
|
|
CleanTest{"abc", "abc"},
|
|
|
|
CleanTest{"abc/def", "abc/def"},
|
|
|
|
CleanTest{"a/b/c", "a/b/c"},
|
|
|
|
CleanTest{".", "."},
|
|
|
|
CleanTest{"..", ".."},
|
|
|
|
CleanTest{"../..", "../.."},
|
|
|
|
CleanTest{"../../abc", "../../abc"},
|
|
|
|
CleanTest{"/abc", "/abc"},
|
|
|
|
CleanTest{"/", "/"},
|
|
|
|
|
|
|
|
// Remove trailing slash
|
|
|
|
CleanTest{"abc/", "abc"},
|
|
|
|
CleanTest{"abc/def/", "abc/def"},
|
|
|
|
CleanTest{"a/b/c/", "a/b/c"},
|
|
|
|
CleanTest{"./", "."},
|
|
|
|
CleanTest{"../", ".."},
|
|
|
|
CleanTest{"../../", "../.."},
|
|
|
|
CleanTest{"/abc/", "/abc"},
|
|
|
|
|
|
|
|
// Remove doubled slash
|
|
|
|
CleanTest{"abc//def//ghi", "abc/def/ghi"},
|
|
|
|
CleanTest{"//abc", "/abc"},
|
|
|
|
CleanTest{"///abc", "/abc"},
|
|
|
|
CleanTest{"//abc//", "/abc"},
|
|
|
|
CleanTest{"abc//", "abc"},
|
|
|
|
|
|
|
|
// Remove . elements
|
|
|
|
CleanTest{"abc/./def", "abc/def"},
|
|
|
|
CleanTest{"/./abc/def", "/abc/def"},
|
|
|
|
CleanTest{"abc/.", "abc"},
|
|
|
|
|
|
|
|
// Remove .. elements
|
|
|
|
CleanTest{"abc/def/ghi/../jkl", "abc/def/jkl"},
|
|
|
|
CleanTest{"abc/def/../ghi/../jkl", "abc/jkl"},
|
|
|
|
CleanTest{"abc/def/..", "abc"},
|
|
|
|
CleanTest{"abc/def/../..", "."},
|
|
|
|
CleanTest{"/abc/def/../..", "/"},
|
|
|
|
CleanTest{"abc/def/../../..", ".."},
|
|
|
|
CleanTest{"/abc/def/../../..", "/"},
|
|
|
|
CleanTest{"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
|
|
|
|
|
|
|
|
// Combinations
|
|
|
|
CleanTest{"abc/./../def", "def"},
|
|
|
|
CleanTest{"abc//./../def", "def"},
|
|
|
|
CleanTest{"abc/../../././../def", "../../def"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClean(t *testing.T) {
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, test := range cleantests {
|
2009-04-07 01:40:07 -06:00
|
|
|
if s := Clean(test.path); s != test.clean {
|
|
|
|
t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.clean);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SplitTest struct {
|
2009-10-08 16:14:54 -06:00
|
|
|
path, dir, file string;
|
2009-04-07 01:40:07 -06:00
|
|
|
}
|
|
|
|
|
2009-10-08 16:14:54 -06:00
|
|
|
var splittests = []SplitTest{
|
2009-04-07 01:40:07 -06:00
|
|
|
SplitTest{"a/b", "a/", "b"},
|
|
|
|
SplitTest{"a/b/", "a/b/", ""},
|
|
|
|
SplitTest{"a/", "a/", ""},
|
|
|
|
SplitTest{"a", "", "a"},
|
|
|
|
SplitTest{"/", "/", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplit(t *testing.T) {
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, test := range splittests {
|
2009-04-07 01:40:07 -06:00
|
|
|
if d, f := Split(test.path); d != test.dir || f != test.file {
|
|
|
|
t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type JoinTest struct {
|
2009-10-08 16:14:54 -06:00
|
|
|
dir, file, path string;
|
2009-04-07 01:40:07 -06:00
|
|
|
}
|
|
|
|
|
2009-10-08 16:14:54 -06:00
|
|
|
var jointests = []JoinTest{
|
2009-04-07 01:40:07 -06:00
|
|
|
JoinTest{"a", "b", "a/b"},
|
2009-04-07 22:53:39 -06:00
|
|
|
JoinTest{"a", "", "a"},
|
2009-04-07 01:40:07 -06:00
|
|
|
JoinTest{"", "b", "b"},
|
|
|
|
JoinTest{"/", "a", "/a"},
|
|
|
|
JoinTest{"/", "", "/"},
|
|
|
|
JoinTest{"a/", "b", "a/b"},
|
2009-04-07 22:53:39 -06:00
|
|
|
JoinTest{"a/", "", "a"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoin(t *testing.T) {
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, test := range jointests {
|
2009-04-07 22:53:39 -06:00
|
|
|
if p := Join(test.dir, test.file); p != test.path {
|
|
|
|
t.Errorf("Join(%q, %q) = %q, want %q", test.dir, test.file, p, test.path);
|
|
|
|
}
|
|
|
|
}
|
2009-04-07 01:40:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExtTest struct {
|
2009-10-08 16:14:54 -06:00
|
|
|
path, ext string;
|
2009-04-07 01:40:07 -06:00
|
|
|
}
|
|
|
|
|
2009-10-08 16:14:54 -06:00
|
|
|
var exttests = []ExtTest{
|
2009-04-07 01:40:07 -06:00
|
|
|
ExtTest{"path.go", ".go"},
|
|
|
|
ExtTest{"path.pb.go", ".go"},
|
|
|
|
ExtTest{"a.dir/b", ""},
|
|
|
|
ExtTest{"a.dir/b.go", ".go"},
|
|
|
|
ExtTest{"a.dir/", ""},
|
|
|
|
}
|
2009-04-07 22:53:39 -06:00
|
|
|
|
|
|
|
func TestExt(t *testing.T) {
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, test := range exttests {
|
2009-04-07 22:53:39 -06:00
|
|
|
if x := Ext(test.path); x != test.ext {
|
|
|
|
t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|