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 'package:unittest/unittest.dart'; | |
10 import 'package:path/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('rootPrefix', () { | |
37 expect(builder.rootPrefix(''), ''); | |
38 expect(builder.rootPrefix('a'), ''); | |
39 expect(builder.rootPrefix('a/b'), ''); | |
40 expect(builder.rootPrefix('/a/c'), '/'); | |
41 expect(builder.rootPrefix('/'), '/'); | |
42 }); | |
43 | |
44 test('dirname', () { | |
45 expect(builder.dirname(''), '.'); | |
46 expect(builder.dirname('a'), '.'); | |
47 expect(builder.dirname('a/b'), 'a'); | |
48 expect(builder.dirname('a/b/c'), 'a/b'); | |
49 expect(builder.dirname('a/b.c'), 'a'); | |
50 expect(builder.dirname('a/'), '.'); | |
51 expect(builder.dirname('a/.'), 'a'); | |
52 expect(builder.dirname(r'a\b/c'), r'a\b'); | |
53 expect(builder.dirname('/a'), '/'); | |
54 expect(builder.dirname('///a'), '/'); | |
55 expect(builder.dirname('/'), '/'); | |
56 expect(builder.dirname('///'), '/'); | |
57 expect(builder.dirname('a/b/'), 'a'); | |
58 expect(builder.dirname(r'a/b\c'), 'a'); | |
59 expect(builder.dirname('a//'), '.'); | |
60 expect(builder.dirname('a/b//'), 'a'); | |
61 expect(builder.dirname('a//b'), 'a'); | |
62 }); | |
63 | |
64 test('basename', () { | |
65 expect(builder.basename(''), ''); | |
66 expect(builder.basename('a'), 'a'); | |
67 expect(builder.basename('a/b'), 'b'); | |
68 expect(builder.basename('a/b/c'), 'c'); | |
69 expect(builder.basename('a/b.c'), 'b.c'); | |
70 expect(builder.basename('a/'), 'a'); | |
71 expect(builder.basename('a/.'), '.'); | |
72 expect(builder.basename(r'a\b/c'), 'c'); | |
73 expect(builder.basename('/a'), 'a'); | |
74 expect(builder.basename('/'), '/'); | |
75 expect(builder.basename('a/b/'), 'b'); | |
76 expect(builder.basename(r'a/b\c'), r'b\c'); | |
77 expect(builder.basename('a//'), 'a'); | |
78 expect(builder.basename('a/b//'), 'b'); | |
79 expect(builder.basename('a//b'), 'b'); | |
80 }); | |
81 | |
82 test('basenameWithoutExtension', () { | |
83 expect(builder.basenameWithoutExtension(''), ''); | |
84 expect(builder.basenameWithoutExtension('a'), 'a'); | |
85 expect(builder.basenameWithoutExtension('a/b'), 'b'); | |
86 expect(builder.basenameWithoutExtension('a/b/c'), 'c'); | |
87 expect(builder.basenameWithoutExtension('a/b.c'), 'b'); | |
88 expect(builder.basenameWithoutExtension('a/'), 'a'); | |
89 expect(builder.basenameWithoutExtension('a/.'), '.'); | |
90 expect(builder.basenameWithoutExtension(r'a/b\c'), r'b\c'); | |
91 expect(builder.basenameWithoutExtension('a/.bashrc'), '.bashrc'); | |
92 expect(builder.basenameWithoutExtension('a/b/c.d.e'), 'c.d'); | |
93 expect(builder.basenameWithoutExtension('a//'), 'a'); | |
94 expect(builder.basenameWithoutExtension('a/b//'), 'b'); | |
95 expect(builder.basenameWithoutExtension('a//b'), 'b'); | |
96 expect(builder.basenameWithoutExtension('a/b.c/'), 'b'); | |
97 expect(builder.basenameWithoutExtension('a/b.c//'), 'b'); | |
98 }); | |
99 | |
100 test('isAbsolute', () { | |
101 expect(builder.isAbsolute(''), false); | |
102 expect(builder.isAbsolute('a'), false); | |
103 expect(builder.isAbsolute('a/b'), false); | |
104 expect(builder.isAbsolute('/a'), true); | |
105 expect(builder.isAbsolute('/a/b'), true); | |
106 expect(builder.isAbsolute('~'), false); | |
107 expect(builder.isAbsolute('.'), false); | |
108 expect(builder.isAbsolute('../a'), false); | |
109 expect(builder.isAbsolute('C:/a'), false); | |
110 expect(builder.isAbsolute(r'C:\a'), false); | |
111 expect(builder.isAbsolute(r'\\a'), false); | |
112 }); | |
113 | |
114 test('isRelative', () { | |
115 expect(builder.isRelative(''), true); | |
116 expect(builder.isRelative('a'), true); | |
117 expect(builder.isRelative('a/b'), true); | |
118 expect(builder.isRelative('/a'), false); | |
119 expect(builder.isRelative('/a/b'), false); | |
120 expect(builder.isRelative('~'), true); | |
121 expect(builder.isRelative('.'), true); | |
122 expect(builder.isRelative('../a'), true); | |
123 expect(builder.isRelative('C:/a'), true); | |
124 expect(builder.isRelative(r'C:\a'), true); | |
125 expect(builder.isRelative(r'\\a'), true); | |
126 }); | |
127 | |
128 group('join', () { | |
129 test('allows up to eight parts', () { | |
130 expect(builder.join('a'), 'a'); | |
131 expect(builder.join('a', 'b'), 'a/b'); | |
132 expect(builder.join('a', 'b', 'c'), 'a/b/c'); | |
133 expect(builder.join('a', 'b', 'c', 'd'), 'a/b/c/d'); | |
134 expect(builder.join('a', 'b', 'c', 'd', 'e'), 'a/b/c/d/e'); | |
135 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f'), 'a/b/c/d/e/f'); | |
136 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g'), 'a/b/c/d/e/f/g'); | |
137 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'), | |
138 'a/b/c/d/e/f/g/h'); | |
139 }); | |
140 | |
141 test('does not add separator if a part ends in one', () { | |
142 expect(builder.join('a/', 'b', 'c/', 'd'), 'a/b/c/d'); | |
143 expect(builder.join('a\\', 'b'), r'a\/b'); | |
144 }); | |
145 | |
146 test('ignores parts before an absolute path', () { | |
147 expect(builder.join('a', '/', 'b', 'c'), '/b/c'); | |
148 expect(builder.join('a', '/b', '/c', 'd'), '/c/d'); | |
149 expect(builder.join('a', r'c:\b', 'c', 'd'), r'a/c:\b/c/d'); | |
150 expect(builder.join('a', r'\\b', 'c', 'd'), r'a/\\b/c/d'); | |
151 }); | |
152 | |
153 test('ignores trailing nulls', () { | |
154 expect(builder.join('a', null), equals('a')); | |
155 expect(builder.join('a', 'b', 'c', null, null), equals('a/b/c')); | |
156 }); | |
157 | |
158 test('disallows intermediate nulls', () { | |
159 expect(() => builder.join('a', null, 'b'), throwsArgumentError); | |
160 expect(() => builder.join(null, 'a'), throwsArgumentError); | |
161 }); | |
162 }); | |
163 | |
164 group('split', () { | |
165 test('simple cases', () { | |
166 expect(builder.split(''), []); | |
167 expect(builder.split('.'), ['.']); | |
168 expect(builder.split('..'), ['..']); | |
169 expect(builder.split('foo'), equals(['foo'])); | |
170 expect(builder.split('foo/bar.txt'), equals(['foo', 'bar.txt'])); | |
171 expect(builder.split('foo/bar/baz'), equals(['foo', 'bar', 'baz'])); | |
172 expect(builder.split('foo/../bar/./baz'), | |
173 equals(['foo', '..', 'bar', '.', 'baz'])); | |
174 expect(builder.split('foo//bar///baz'), equals(['foo', 'bar', 'baz'])); | |
175 expect(builder.split('foo/\\/baz'), equals(['foo', '\\', 'baz'])); | |
176 expect(builder.split('.'), equals(['.'])); | |
177 expect(builder.split(''), equals([])); | |
178 expect(builder.split('foo/'), equals(['foo'])); | |
179 expect(builder.split('//'), equals(['/'])); | |
180 }); | |
181 | |
182 test('includes the root for absolute paths', () { | |
183 expect(builder.split('/foo/bar/baz'), equals(['/', 'foo', 'bar', 'baz'])); | |
184 expect(builder.split('/'), equals(['/'])); | |
185 }); | |
186 }); | |
187 | |
188 group('normalize', () { | |
189 test('simple cases', () { | |
190 expect(builder.normalize(''), ''); | |
191 expect(builder.normalize('.'), '.'); | |
192 expect(builder.normalize('..'), '..'); | |
193 expect(builder.normalize('a'), 'a'); | |
194 expect(builder.normalize('/'), '/'); | |
195 expect(builder.normalize(r'\'), r'\'); | |
196 }); | |
197 | |
198 test('collapses redundant separators', () { | |
199 expect(builder.normalize(r'a/b/c'), r'a/b/c'); | |
200 expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d'); | |
201 }); | |
202 | |
203 test('does not collapse separators for other platform', () { | |
204 expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c'); | |
205 }); | |
206 | |
207 test('eliminates "." parts', () { | |
208 expect(builder.normalize('./'), '.'); | |
209 expect(builder.normalize('/.'), '/'); | |
210 expect(builder.normalize('/./'), '/'); | |
211 expect(builder.normalize('./.'), '.'); | |
212 expect(builder.normalize('a/./b'), 'a/b'); | |
213 expect(builder.normalize('a/.b/c'), 'a/.b/c'); | |
214 expect(builder.normalize('a/././b/./c'), 'a/b/c'); | |
215 expect(builder.normalize('././a'), 'a'); | |
216 expect(builder.normalize('a/./.'), 'a'); | |
217 }); | |
218 | |
219 test('eliminates ".." parts', () { | |
220 expect(builder.normalize('..'), '..'); | |
221 expect(builder.normalize('../'), '..'); | |
222 expect(builder.normalize('../../..'), '../../..'); | |
223 expect(builder.normalize('../../../'), '../../..'); | |
224 expect(builder.normalize('/..'), '/'); | |
225 expect(builder.normalize('/../../..'), '/'); | |
226 expect(builder.normalize('/../../../a'), '/a'); | |
227 expect(builder.normalize('a/..'), '.'); | |
228 expect(builder.normalize('a/b/..'), 'a'); | |
229 expect(builder.normalize('a/../b'), 'b'); | |
230 expect(builder.normalize('a/./../b'), 'b'); | |
231 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | |
232 expect(builder.normalize('a/b/../../../../c'), '../../c'); | |
233 }); | |
234 | |
235 test('does not walk before root on absolute paths', () { | |
236 expect(builder.normalize('..'), '..'); | |
237 expect(builder.normalize('../'), '..'); | |
238 expect(builder.normalize('/..'), '/'); | |
239 expect(builder.normalize('a/..'), '.'); | |
240 expect(builder.normalize('a/b/..'), 'a'); | |
241 expect(builder.normalize('a/../b'), 'b'); | |
242 expect(builder.normalize('a/./../b'), 'b'); | |
243 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | |
244 expect(builder.normalize('a/b/../../../../c'), '../../c'); | |
245 }); | |
246 | |
247 test('removes trailing separators', () { | |
248 expect(builder.normalize('./'), '.'); | |
249 expect(builder.normalize('.//'), '.'); | |
250 expect(builder.normalize('a/'), 'a'); | |
251 expect(builder.normalize('a/b/'), 'a/b'); | |
252 expect(builder.normalize('a/b///'), 'a/b'); | |
253 }); | |
254 }); | |
255 | |
256 group('relative', () { | |
257 group('from absolute root', () { | |
258 test('given absolute path in root', () { | |
259 expect(builder.relative('/'), '../..'); | |
260 expect(builder.relative('/root'), '..'); | |
261 expect(builder.relative('/root/path'), '.'); | |
262 expect(builder.relative('/root/path/a'), 'a'); | |
263 expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt'); | |
264 expect(builder.relative('/root/a/b.txt'), '../a/b.txt'); | |
265 }); | |
266 | |
267 test('given absolute path outside of root', () { | |
268 expect(builder.relative('/a/b'), '../../a/b'); | |
269 expect(builder.relative('/root/path/a'), 'a'); | |
270 expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt'); | |
271 expect(builder.relative('/root/a/b.txt'), '../a/b.txt'); | |
272 }); | |
273 | |
274 test('given relative path', () { | |
275 // The path is considered relative to the root, so it basically just | |
276 // normalizes. | |
277 expect(builder.relative(''), '.'); | |
278 expect(builder.relative('.'), '.'); | |
279 expect(builder.relative('a'), 'a'); | |
280 expect(builder.relative('a/b.txt'), 'a/b.txt'); | |
281 expect(builder.relative('../a/b.txt'), '../a/b.txt'); | |
282 expect(builder.relative('a/./b/../c.txt'), 'a/c.txt'); | |
283 }); | |
284 }); | |
285 | |
286 group('from relative root', () { | |
287 var r = new path.Builder(style: path.Style.posix, root: 'foo/bar'); | |
288 | |
289 test('given absolute path', () { | |
290 expect(r.relative('/'), equals('/')); | |
291 expect(r.relative('/a/b'), equals('/a/b')); | |
292 }); | |
293 | |
294 test('given relative path', () { | |
295 // The path is considered relative to the root, so it basically just | |
296 // normalizes. | |
297 expect(r.relative(''), '.'); | |
298 expect(r.relative('.'), '.'); | |
299 expect(r.relative('..'), '..'); | |
300 expect(r.relative('a'), 'a'); | |
301 expect(r.relative('a/b.txt'), 'a/b.txt'); | |
302 expect(r.relative('../a/b.txt'), '../a/b.txt'); | |
303 expect(r.relative('a/./b/../c.txt'), 'a/c.txt'); | |
304 }); | |
305 }); | |
306 | |
307 test('from a root with extension', () { | |
308 var r = new path.Builder(style: path.Style.posix, root: '/dir.ext'); | |
309 expect(r.relative('/dir.ext/file'), 'file'); | |
310 }); | |
311 | |
312 test('with a root parameter', () { | |
313 expect(builder.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz')); | |
314 expect(builder.relative('..', from: '/foo/bar'), equals('../../root')); | |
315 expect(builder.relative('/foo/bar/baz', from: 'foo/bar'), | |
316 equals('../../../../foo/bar/baz')); | |
317 expect(builder.relative('..', from: 'foo/bar'), equals('../../..')); | |
318 }); | |
319 | |
320 test('with a root parameter and a relative root', () { | |
321 var r = new path.Builder(style: path.Style.posix, root: 'relative/root'); | |
322 expect(r.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz')); | |
323 expect(() => r.relative('..', from: '/foo/bar'), throwsArgumentError); | |
324 expect(r.relative('/foo/bar/baz', from: 'foo/bar'), | |
325 equals('/foo/bar/baz')); | |
326 expect(r.relative('..', from: 'foo/bar'), equals('../../..')); | |
327 }); | |
328 }); | |
329 | |
330 group('resolve', () { | |
331 test('allows up to seven parts', () { | |
332 expect(builder.resolve('a'), '/root/path/a'); | |
333 expect(builder.resolve('a', 'b'), '/root/path/a/b'); | |
334 expect(builder.resolve('a', 'b', 'c'), '/root/path/a/b/c'); | |
335 expect(builder.resolve('a', 'b', 'c', 'd'), '/root/path/a/b/c/d'); | |
336 expect(builder.resolve('a', 'b', 'c', 'd', 'e'), '/root/path/a/b/c/d/e'); | |
337 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f'), | |
338 '/root/path/a/b/c/d/e/f'); | |
339 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f', 'g'), | |
340 '/root/path/a/b/c/d/e/f/g'); | |
341 }); | |
342 | |
343 test('does not add separator if a part ends in one', () { | |
344 expect(builder.resolve('a/', 'b', 'c/', 'd'), '/root/path/a/b/c/d'); | |
345 expect(builder.resolve(r'a\', 'b'), r'/root/path/a\/b'); | |
346 }); | |
347 | |
348 test('ignores parts before an absolute path', () { | |
349 expect(builder.resolve('a', '/b', '/c', 'd'), '/c/d'); | |
350 expect(builder.resolve('a', r'c:\b', 'c', 'd'), r'/root/path/a/c:\b/c/d'); | |
351 expect(builder.resolve('a', r'\\b', 'c', 'd'), r'/root/path/a/\\b/c/d'); | |
352 }); | |
353 }); | |
354 | |
355 test('withoutExtension', () { | |
356 expect(builder.withoutExtension(''), ''); | |
357 expect(builder.withoutExtension('a'), 'a'); | |
358 expect(builder.withoutExtension('.a'), '.a'); | |
359 expect(builder.withoutExtension('a.b'), 'a'); | |
360 expect(builder.withoutExtension('a/b.c'), 'a/b'); | |
361 expect(builder.withoutExtension('a/b.c.d'), 'a/b.c'); | |
362 expect(builder.withoutExtension('a/'), 'a/'); | |
363 expect(builder.withoutExtension('a/b/'), 'a/b/'); | |
364 expect(builder.withoutExtension('a/.'), 'a/.'); | |
365 expect(builder.withoutExtension('a/.b'), 'a/.b'); | |
366 expect(builder.withoutExtension('a.b/c'), 'a.b/c'); | |
367 expect(builder.withoutExtension(r'a.b\c'), r'a'); | |
368 expect(builder.withoutExtension(r'a/b\c'), r'a/b\c'); | |
369 expect(builder.withoutExtension(r'a/b\c.d'), r'a/b\c'); | |
370 expect(builder.withoutExtension('a/b.c/'), 'a/b/'); | |
371 expect(builder.withoutExtension('a/b.c//'), 'a/b//'); | |
372 }); | |
373 } | |
OLD | NEW |