OSDN Git Service

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