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.
12 type PathTest
struct {
16 var cleantests
= []PathTest
{
20 {"abc/def", "abc/def"},
25 {"../../abc", "../../abc"},
29 // Remove trailing slash
31 {"abc/def/", "abc/def"},
38 // Remove doubled slash
39 {"abc//def//ghi", "abc/def/ghi"},
46 {"abc/./def", "abc/def"},
47 {"/./abc/def", "/abc/def"},
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"},
61 {"abc/./../def", "def"},
62 {"abc//./../def", "def"},
63 {"abc/../../././../def", "../../def"},
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
)
71 if s
:= Clean(test
.result
); s
!= test
.result
{
72 t
.Errorf("Clean(%q) = %q, want %q", test
.result
, s
, test
.result
)
77 func TestCleanMallocs(t
*testing
.T
) {
79 t
.Skip("skipping malloc count in short mode")
81 if runtime
.GOMAXPROCS(0) > 1 {
82 t
.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
86 t
.Log("Skipping AllocsPerRun for gccgo")
89 for _
, test
:= range cleantests
{
90 allocs
:= testing
.AllocsPerRun(100, func() { Clean(test
.result
) })
92 t
.Errorf("Clean(%q): %v allocs, want zero", test
.result
, allocs
)
97 type SplitTest
struct {
98 path
, dir
, file
string
101 var splittests
= []SplitTest
{
103 {"a/b/", "a/b/", ""},
109 func TestSplit(t
*testing
.T
) {
110 for _
, test
:= range splittests
{
111 if d
, f
:= Split(test
.path
); d
!= test
.dir || f
!= test
.file
{
112 t
.Errorf("Split(%q) = %q, %q, want %q, %q", test
.path
, d
, f
, test
.dir
, test
.file
)
117 type JoinTest
struct {
122 var jointests
= []JoinTest
{
128 {[]string{"a"}, "a"},
131 {[]string{"a", "b"}, "a/b"},
132 {[]string{"a", ""}, "a"},
133 {[]string{"", "b"}, "b"},
134 {[]string{"/", "a"}, "/a"},
135 {[]string{"/", ""}, "/"},
136 {[]string{"a/", "b"}, "a/b"},
137 {[]string{"a/", ""}, "a"},
138 {[]string{"", ""}, ""},
141 func TestJoin(t
*testing
.T
) {
142 for _
, test
:= range jointests
{
143 if p
:= Join(test
.elem
...); p
!= test
.path
{
144 t
.Errorf("join(%q) = %q, want %q", test
.elem
, p
, test
.path
)
149 type ExtTest
struct {
153 var exttests
= []ExtTest
{
155 {"path.pb.go", ".go"},
157 {"a.dir/b.go", ".go"},
161 func TestExt(t
*testing
.T
) {
162 for _
, test
:= range exttests
{
163 if x
:= Ext(test
.path
); x
!= test
.ext
{
164 t
.Errorf("Ext(%q) = %q, want %q", test
.path
, x
, test
.ext
)
169 var basetests
= []PathTest
{
184 func TestBase(t
*testing
.T
) {
185 for _
, test
:= range basetests
{
186 if s
:= Base(test
.path
); s
!= test
.result
{
187 t
.Errorf("Base(%q) = %q, want %q", test
.path
, s
, test
.result
)
192 var dirtests
= []PathTest
{
202 {"abc////def", "abc"},
208 func TestDir(t
*testing
.T
) {
209 for _
, test
:= range dirtests
{
210 if s
:= Dir(test
.path
); s
!= test
.result
{
211 t
.Errorf("Dir(%q) = %q, want %q", test
.path
, s
, test
.result
)
216 type IsAbsTest
struct {
221 var isAbsTests
= []IsAbsTest
{
224 {"/usr/bin/gcc", true},
232 func TestIsAbs(t
*testing
.T
) {
233 for _
, test
:= range isAbsTests
{
234 if r
:= IsAbs(test
.path
); r
!= test
.isAbs
{
235 t
.Errorf("IsAbs(%q) = %v, want %v", test
.path
, r
, test
.isAbs
)