OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library path_test; | |
6 | |
7 import 'dart:io' as io; | |
8 | |
9 import '../../../../pkg/unittest/lib/unittest.dart'; | |
10 import '../../../pub/path.dart' as path; | |
11 | |
12 main() { | |
13 var builder = new path.Builder(style: path.Style.posix, root: '/root/path'); | |
14 | |
15 if (new path.Builder().style == path.Style.posix) { | |
16 group('absolute', () { | |
17 expect(path.absolute('a/b.txt'), path.join(path.current, 'a/b.txt')); | |
18 expect(path.absolute('/a/b.txt'), '/a/b.txt'); | |
19 }); | |
20 } | |
21 | |
22 test('separator', () { | |
23 expect(builder.separator, '/'); | |
24 }); | |
25 | |
26 test('extension', () { | |
27 expect(builder.extension(''), ''); | |
28 expect(builder.extension('foo.dart'), '.dart'); | |
29 expect(builder.extension('foo.dart.js'), '.js'); | |
30 expect(builder.extension('a.b/c'), ''); | |
31 expect(builder.extension('a.b/c.d'), '.d'); | |
32 expect(builder.extension('~/.bashrc'), ''); | |
33 expect(builder.extension(r'a.b\c'), r'.b\c'); | |
34 }); | |
35 | |
36 test('filename', () { | |
37 expect(builder.filename(''), ''); | |
38 expect(builder.filename('a'), 'a'); | |
39 expect(builder.filename('a/b'), 'b'); | |
40 expect(builder.filename('a/b/c'), 'c'); | |
41 expect(builder.filename('a/b.c'), 'b.c'); | |
42 expect(builder.filename('a/'), ''); | |
43 expect(builder.filename('a/.'), '.'); | |
44 expect(builder.filename(r'a/b\c'), r'b\c'); | |
45 }); | |
46 | |
47 test('filenameWithoutExtension', () { | |
48 expect(builder.filenameWithoutExtension(''), ''); | |
49 expect(builder.filenameWithoutExtension('a'), 'a'); | |
50 expect(builder.filenameWithoutExtension('a/b'), 'b'); | |
51 expect(builder.filenameWithoutExtension('a/b/c'), 'c'); | |
52 expect(builder.filenameWithoutExtension('a/b.c'), 'b'); | |
53 expect(builder.filenameWithoutExtension('a/'), ''); | |
54 expect(builder.filenameWithoutExtension('a/.'), '.'); | |
55 expect(builder.filenameWithoutExtension(r'a/b\c'), r'b\c'); | |
56 expect(builder.filenameWithoutExtension('a/.bashrc'), '.bashrc'); | |
57 expect(builder.filenameWithoutExtension('a/b/c.d.e'), 'c.d'); | |
58 }); | |
59 | |
60 test('isAbsolute', () { | |
61 expect(builder.isAbsolute(''), false); | |
62 expect(builder.isAbsolute('a'), false); | |
63 expect(builder.isAbsolute('a/b'), false); | |
64 expect(builder.isAbsolute('/a'), true); | |
65 expect(builder.isAbsolute('/a/b'), true); | |
66 expect(builder.isAbsolute('~'), false); | |
67 expect(builder.isAbsolute('.'), false); | |
68 expect(builder.isAbsolute('../a'), false); | |
69 expect(builder.isAbsolute('C:/a'), false); | |
70 expect(builder.isAbsolute(r'C:\a'), false); | |
71 expect(builder.isAbsolute(r'\\a'), false); | |
72 }); | |
73 | |
74 test('isRelative', () { | |
75 expect(builder.isRelative(''), true); | |
76 expect(builder.isRelative('a'), true); | |
77 expect(builder.isRelative('a/b'), true); | |
78 expect(builder.isRelative('/a'), false); | |
79 expect(builder.isRelative('/a/b'), false); | |
80 expect(builder.isRelative('~'), true); | |
81 expect(builder.isRelative('.'), true); | |
82 expect(builder.isRelative('../a'), true); | |
83 expect(builder.isRelative('C:/a'), true); | |
84 expect(builder.isRelative(r'C:\a'), true); | |
85 expect(builder.isRelative(r'\\a'), true); | |
86 }); | |
87 | |
88 group('join', () { | |
89 test('allows up to eight parts', () { | |
90 expect(builder.join('a'), 'a'); | |
91 expect(builder.join('a', 'b'), 'a/b'); | |
92 expect(builder.join('a', 'b', 'c'), 'a/b/c'); | |
93 expect(builder.join('a', 'b', 'c', 'd'), 'a/b/c/d'); | |
94 expect(builder.join('a', 'b', 'c', 'd', 'e'), 'a/b/c/d/e'); | |
95 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f'), 'a/b/c/d/e/f'); | |
96 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g'), 'a/b/c/d/e/f/g'); | |
97 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'), | |
98 'a/b/c/d/e/f/g/h'); | |
99 }); | |
100 | |
101 test('does not add separator if a part ends in one', () { | |
102 expect(builder.join('a/', 'b', 'c/', 'd'), 'a/b/c/d'); | |
103 expect(builder.join('a\\', 'b'), r'a\/b'); | |
104 }); | |
105 | |
106 test('ignores parts before an absolute path', () { | |
107 expect(builder.join('a', '/b', '/c', 'd'), '/c/d'); | |
108 expect(builder.join('a', r'c:\b', 'c', 'd'), r'a/c:\b/c/d'); | |
109 expect(builder.join('a', r'\\b', 'c', 'd'), r'a/\\b/c/d'); | |
110 }); | |
111 }); | |
112 | |
113 group('normalize', () { | |
114 test('simple cases', () { | |
115 expect(builder.normalize(''), ''); | |
116 expect(builder.normalize('.'), '.'); | |
117 expect(builder.normalize('..'), '..'); | |
118 expect(builder.normalize('a'), 'a'); | |
119 expect(builder.normalize('/'), '/'); | |
120 expect(builder.normalize(r'\'), r'\'); | |
121 }); | |
122 | |
123 test('collapses redundant separators', () { | |
124 expect(builder.normalize(r'a/b/c'), r'a/b/c'); | |
125 expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d'); | |
126 }); | |
127 | |
128 test('does not collapse separators for other platform', () { | |
129 expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c'); | |
130 }); | |
131 | |
132 test('eliminates "." parts', () { | |
133 expect(builder.normalize('./'), '.'); | |
134 expect(builder.normalize('/.'), '/'); | |
135 expect(builder.normalize('/./'), '/'); | |
136 expect(builder.normalize('./.'), '.'); | |
137 expect(builder.normalize('a/./b'), 'a/b'); | |
138 expect(builder.normalize('a/.b/c'), 'a/.b/c'); | |
139 expect(builder.normalize('a/././b/./c'), 'a/b/c'); | |
140 expect(builder.normalize('././a'), 'a'); | |
141 expect(builder.normalize('a/./.'), 'a'); | |
142 }); | |
143 | |
144 test('eliminates ".." parts', () { | |
145 expect(builder.normalize('..'), '..'); | |
146 expect(builder.normalize('../'), '..'); | |
147 expect(builder.normalize('../../..'), '../../..'); | |
148 expect(builder.normalize('../../../'), '../../..'); | |
149 expect(builder.normalize('/..'), '/'); | |
150 expect(builder.normalize('/../../..'), '/'); | |
151 expect(builder.normalize('/../../../a'), '/a'); | |
152 expect(builder.normalize('a/..'), '.'); | |
153 expect(builder.normalize('a/b/..'), 'a'); | |
154 expect(builder.normalize('a/../b'), 'b'); | |
155 expect(builder.normalize('a/./../b'), 'b'); | |
156 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | |
157 expect(builder.normalize('a/b/../../../../c'), '../../c'); | |
158 }); | |
159 | |
160 test('does not walk before root on absolute paths', () { | |
161 expect(builder.normalize('..'), '..'); | |
162 expect(builder.normalize('../'), '..'); | |
163 expect(builder.normalize('/..'), '/'); | |
164 expect(builder.normalize('a/..'), '.'); | |
165 expect(builder.normalize('a/b/..'), 'a'); | |
166 expect(builder.normalize('a/../b'), 'b'); | |
167 expect(builder.normalize('a/./../b'), 'b'); | |
168 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | |
169 expect(builder.normalize('a/b/../../../../c'), '../../c'); | |
170 }); | |
171 | |
172 test('removes trailing separators', () { | |
173 expect(builder.normalize('./'), '.'); | |
174 expect(builder.normalize('.//'), '.'); | |
175 expect(builder.normalize('a/'), 'a'); | |
176 expect(builder.normalize('a/b/'), 'a/b'); | |
177 expect(builder.normalize('a/b///'), 'a/b'); | |
178 }); | |
179 }); | |
180 | |
181 group('relative', () { | |
182 group('from absolute root', () { | |
183 test('given absolute path in root', () { | |
184 expect(builder.relative('/'), '../..'); | |
185 expect(builder.relative('/root'), '..'); | |
186 expect(builder.relative('/root/path'), '.'); | |
187 expect(builder.relative('/root/path/a'), 'a'); | |
188 expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt'); | |
189 expect(builder.relative('/root/a/b.txt'), '../a/b.txt'); | |
190 }); | |
191 | |
192 test('given absolute path outside of root', () { | |
193 expect(builder.relative('/a/b'), '../../a/b'); | |
194 expect(builder.relative('/root/path/a'), 'a'); | |
195 expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt'); | |
196 expect(builder.relative('/root/a/b.txt'), '../a/b.txt'); | |
197 }); | |
198 | |
199 test('given relative path', () { | |
200 // The path is considered relative to the root, so it basically just | |
201 // normalizes. | |
202 expect(builder.relative(''), '.'); | |
203 expect(builder.relative('.'), '.'); | |
204 expect(builder.relative('a'), 'a'); | |
205 expect(builder.relative('a/b.txt'), 'a/b.txt'); | |
206 expect(builder.relative('../a/b.txt'), '../a/b.txt'); | |
207 expect(builder.relative('a/./b/../c.txt'), 'a/c.txt'); | |
208 }); | |
209 }); | |
210 | |
211 group('from relative root', () { | |
212 var r = new path.Builder(style: path.Style.posix, root: 'foo/bar'); | |
213 | |
214 // These tests rely on the current working directory, so don't do the | |
215 // right thing if you run them on the wrong platform. | |
216 if (io.Platform.operatingSystem != 'windows') { | |
217 test('given absolute path', () { | |
218 var b = new path.Builder(style: path.Style.posix); | |
219 expect(r.relative('/'), b.join(b.relative('/'), '../..')); | |
220 expect(r.relative('/a/b'), b.join(b.relative('/'), '../../a/b')); | |
221 }); | |
222 } | |
223 | |
224 test('given relative path', () { | |
225 // The path is considered relative to the root, so it basically just | |
226 // normalizes. | |
227 expect(r.relative(''), '.'); | |
228 expect(r.relative('.'), '.'); | |
229 expect(r.relative('..'), '..'); | |
230 expect(r.relative('a'), 'a'); | |
231 expect(r.relative('a/b.txt'), 'a/b.txt'); | |
232 expect(r.relative('../a/b.txt'), '../a/b.txt'); | |
233 expect(r.relative('a/./b/../c.txt'), 'a/c.txt'); | |
234 }); | |
235 }); | |
236 }); | |
237 | |
238 group('resolve', () { | |
239 test('allows up to seven parts', () { | |
240 expect(builder.resolve('a'), '/root/path/a'); | |
241 expect(builder.resolve('a', 'b'), '/root/path/a/b'); | |
242 expect(builder.resolve('a', 'b', 'c'), '/root/path/a/b/c'); | |
243 expect(builder.resolve('a', 'b', 'c', 'd'), '/root/path/a/b/c/d'); | |
244 expect(builder.resolve('a', 'b', 'c', 'd', 'e'), '/root/path/a/b/c/d/e'); | |
245 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f'), '/root/path/a/b/c/d/ e/f'); | |
246 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f', 'g'), '/root/path/a/b /c/d/e/f/g'); | |
Bob Nystrom
2012/12/08 03:36:53
Long lines.
nweiz
2012/12/08 03:43:45
Done.
| |
247 }); | |
248 | |
249 test('does not add separator if a part ends in one', () { | |
250 expect(builder.resolve('a/', 'b', 'c/', 'd'), '/root/path/a/b/c/d'); | |
251 expect(builder.resolve(r'a\', 'b'), r'/root/path/a\/b'); | |
252 }); | |
253 | |
254 test('ignores parts before an absolute path', () { | |
255 expect(builder.resolve('a', '/b', '/c', 'd'), '/c/d'); | |
256 expect(builder.resolve('a', r'c:\b', 'c', 'd'), r'/root/path/a/c:\b/c/d'); | |
257 expect(builder.resolve('a', r'\\b', 'c', 'd'), r'/root/path/a/\\b/c/d'); | |
258 }); | |
259 }); | |
260 | |
261 test('withoutExtension', () { | |
262 expect(builder.withoutExtension(''), ''); | |
263 expect(builder.withoutExtension('a'), 'a'); | |
264 expect(builder.withoutExtension('.a'), '.a'); | |
265 expect(builder.withoutExtension('a.b'), 'a'); | |
266 expect(builder.withoutExtension('a/b.c'), 'a/b'); | |
267 expect(builder.withoutExtension('a/b.c.d'), 'a/b.c'); | |
268 expect(builder.withoutExtension('a/'), 'a/'); | |
269 expect(builder.withoutExtension('a/b/'), 'a/b/'); | |
270 expect(builder.withoutExtension('a/.'), 'a/.'); | |
271 expect(builder.withoutExtension('a/.b'), 'a/.b'); | |
272 expect(builder.withoutExtension('a.b/c'), 'a.b/c'); | |
273 expect(builder.withoutExtension(r'a/b\c'), r'a/b\c'); | |
274 expect(builder.withoutExtension(r'a/b\c.d'), r'a/b\c'); | |
275 }); | |
276 } | |
OLD | NEW |