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 import 'package:unittest/unittest.dart'; | |
6 import 'package:pathos/path.dart' as path; | |
7 | |
8 main() { | |
9 var builder = new path.Builder(style: path.Style.url, | |
10 root: 'http://dartlang.org/root/path'); | |
11 | |
12 test('separator', () { | |
13 expect(builder.separator, '/'); | |
14 }); | |
15 | |
16 test('extension', () { | |
17 expect(builder.extension(''), ''); | |
18 expect(builder.extension('foo.dart'), '.dart'); | |
19 expect(builder.extension('foo.dart.js'), '.js'); | |
20 expect(builder.extension('a.b/c'), ''); | |
21 expect(builder.extension('a.b/c.d'), '.d'); | |
22 expect(builder.extension(r'a.b\c'), r'.b\c'); | |
23 }); | |
24 | |
25 test('rootPrefix', () { | |
26 expect(builder.rootPrefix(''), ''); | |
27 expect(builder.rootPrefix('a'), ''); | |
28 expect(builder.rootPrefix('a/b'), ''); | |
29 expect(builder.rootPrefix('http://dartlang.org/a/c'), | |
30 'http://dartlang.org'); | |
31 expect(builder.rootPrefix('file:///a/c'), 'file://'); | |
32 expect(builder.rootPrefix('/a/c'), '/'); | |
33 expect(builder.rootPrefix('http://dartlang.org/'), 'http://dartlang.org'); | |
34 expect(builder.rootPrefix('file:///'), 'file://'); | |
35 expect(builder.rootPrefix('http://dartlang.org'), 'http://dartlang.org'); | |
36 expect(builder.rootPrefix('file://'), 'file://'); | |
37 expect(builder.rootPrefix('/'), '/'); | |
38 }); | |
39 | |
40 test('dirname', () { | |
41 expect(builder.dirname(''), '.'); | |
42 expect(builder.dirname('a'), '.'); | |
43 expect(builder.dirname('a/b'), 'a'); | |
44 expect(builder.dirname('a/b/c'), 'a/b'); | |
45 expect(builder.dirname('a/b.c'), 'a'); | |
46 expect(builder.dirname('a/'), '.'); | |
47 expect(builder.dirname('a/.'), 'a'); | |
48 expect(builder.dirname(r'a\b/c'), r'a\b'); | |
49 expect(builder.dirname('http://dartlang.org/a'), 'http://dartlang.org'); | |
50 expect(builder.dirname('file:///a'), 'file://'); | |
51 expect(builder.dirname('/a'), '/'); | |
52 expect(builder.dirname('http://dartlang.org///a'), 'http://dartlang.org'); | |
53 expect(builder.dirname('file://///a'), 'file://'); | |
54 expect(builder.dirname('///a'), '/'); | |
55 expect(builder.dirname('http://dartlang.org/'), 'http://dartlang.org'); | |
56 expect(builder.dirname('http://dartlang.org'), 'http://dartlang.org'); | |
57 expect(builder.dirname('file:///'), 'file://'); | |
58 expect(builder.dirname('file://'), 'file://'); | |
59 expect(builder.dirname('/'), '/'); | |
60 expect(builder.dirname('http://dartlang.org///'), 'http://dartlang.org'); | |
61 expect(builder.dirname('file://///'), 'file://'); | |
62 expect(builder.dirname('///'), '/'); | |
63 expect(builder.dirname('a/b/'), 'a'); | |
64 expect(builder.dirname(r'a/b\c'), 'a'); | |
65 expect(builder.dirname('a//'), '.'); | |
66 expect(builder.dirname('a/b//'), 'a'); | |
67 expect(builder.dirname('a//b'), 'a'); | |
68 }); | |
69 | |
70 test('basename', () { | |
71 expect(builder.basename(''), ''); | |
72 expect(builder.basename('a'), 'a'); | |
73 expect(builder.basename('a/b'), 'b'); | |
74 expect(builder.basename('a/b/c'), 'c'); | |
75 expect(builder.basename('a/b.c'), 'b.c'); | |
76 expect(builder.basename('a/'), 'a'); | |
77 expect(builder.basename('a/.'), '.'); | |
78 expect(builder.basename(r'a\b/c'), 'c'); | |
79 expect(builder.basename('http://dartlang.org/a'), 'a'); | |
80 expect(builder.basename('file:///a'), 'a'); | |
81 expect(builder.basename('/a'), 'a'); | |
82 expect(builder.basename('http://dartlang.org/'), 'http://dartlang.org'); | |
83 expect(builder.basename('http://dartlang.org'), 'http://dartlang.org'); | |
84 expect(builder.basename('file:///'), 'file://'); | |
85 expect(builder.basename('file://'), 'file://'); | |
86 expect(builder.basename('/'), '/'); | |
87 expect(builder.basename('a/b/'), 'b'); | |
88 expect(builder.basename(r'a/b\c'), r'b\c'); | |
89 expect(builder.basename('a//'), 'a'); | |
90 expect(builder.basename('a/b//'), 'b'); | |
91 expect(builder.basename('a//b'), 'b'); | |
92 }); | |
93 | |
94 test('basenameWithoutExtension', () { | |
95 expect(builder.basenameWithoutExtension(''), ''); | |
96 expect(builder.basenameWithoutExtension('a'), 'a'); | |
97 expect(builder.basenameWithoutExtension('a/b'), 'b'); | |
98 expect(builder.basenameWithoutExtension('a/b/c'), 'c'); | |
99 expect(builder.basenameWithoutExtension('a/b.c'), 'b'); | |
100 expect(builder.basenameWithoutExtension('a/'), 'a'); | |
101 expect(builder.basenameWithoutExtension('a/.'), '.'); | |
102 expect(builder.basenameWithoutExtension(r'a/b\c'), r'b\c'); | |
103 expect(builder.basenameWithoutExtension('a/.bashrc'), '.bashrc'); | |
104 expect(builder.basenameWithoutExtension('a/b/c.d.e'), 'c.d'); | |
105 expect(builder.basenameWithoutExtension('a//'), 'a'); | |
106 expect(builder.basenameWithoutExtension('a/b//'), 'b'); | |
107 expect(builder.basenameWithoutExtension('a//b'), 'b'); | |
108 expect(builder.basenameWithoutExtension('a/b.c/'), 'b'); | |
109 expect(builder.basenameWithoutExtension('a/b.c//'), 'b'); | |
110 }); | |
111 | |
112 test('isAbsolute', () { | |
113 expect(builder.isAbsolute(''), false); | |
114 expect(builder.isAbsolute('a'), false); | |
115 expect(builder.isAbsolute('a/b'), false); | |
116 expect(builder.isAbsolute('http://dartlang.org/a'), true); | |
117 expect(builder.isAbsolute('file:///a'), true); | |
118 expect(builder.isAbsolute('/a'), true); | |
119 expect(builder.isAbsolute('http://dartlang.org/a/b'), true); | |
120 expect(builder.isAbsolute('file:///a/b'), true); | |
121 expect(builder.isAbsolute('/a/b'), true); | |
122 expect(builder.isAbsolute('http://dartlang.org/'), true); | |
123 expect(builder.isAbsolute('file:///'), true); | |
124 expect(builder.isAbsolute('http://dartlang.org'), true); | |
125 expect(builder.isAbsolute('file://'), true); | |
126 expect(builder.isAbsolute('/'), true); | |
127 expect(builder.isAbsolute('~'), false); | |
128 expect(builder.isAbsolute('.'), false); | |
129 expect(builder.isAbsolute('../a'), false); | |
130 expect(builder.isAbsolute('C:/a'), false); | |
131 expect(builder.isAbsolute(r'C:\a'), false); | |
132 expect(builder.isAbsolute(r'\\a'), false); | |
133 }); | |
134 | |
135 test('isRelative', () { | |
136 expect(builder.isRelative(''), true); | |
137 expect(builder.isRelative('a'), true); | |
138 expect(builder.isRelative('a/b'), true); | |
139 expect(builder.isRelative('http://dartlang.org/a'), false); | |
140 expect(builder.isRelative('file:///a'), false); | |
141 expect(builder.isRelative('/a'), false); | |
142 expect(builder.isRelative('http://dartlang.org/a/b'), false); | |
143 expect(builder.isRelative('file:///a/b'), false); | |
144 expect(builder.isRelative('/a/b'), false); | |
145 expect(builder.isRelative('http://dartlang.org/'), false); | |
146 expect(builder.isRelative('file:///'), false); | |
147 expect(builder.isRelative('http://dartlang.org'), false); | |
148 expect(builder.isRelative('file://'), false); | |
149 expect(builder.isRelative('/'), false); | |
150 expect(builder.isRelative('~'), true); | |
151 expect(builder.isRelative('.'), true); | |
152 expect(builder.isRelative('../a'), true); | |
153 expect(builder.isRelative('C:/a'), true); | |
154 expect(builder.isRelative(r'C:\a'), true); | |
155 expect(builder.isRelative(r'\\a'), true); | |
156 }); | |
157 | |
158 test('isRootRelative', () { | |
159 expect(builder.isRootRelative(''), false); | |
160 expect(builder.isRootRelative('a'), false); | |
161 expect(builder.isRootRelative('a/b'), false); | |
162 expect(builder.isRootRelative('http://dartlang.org/a'), false); | |
163 expect(builder.isRootRelative('file:///a'), false); | |
164 expect(builder.isRootRelative('/a'), true); | |
165 expect(builder.isRootRelative('http://dartlang.org/a/b'), false); | |
166 expect(builder.isRootRelative('file:///a/b'), false); | |
167 expect(builder.isRootRelative('/a/b'), true); | |
168 expect(builder.isRootRelative('http://dartlang.org/'), false); | |
169 expect(builder.isRootRelative('file:///'), false); | |
170 expect(builder.isRootRelative('http://dartlang.org'), false); | |
171 expect(builder.isRootRelative('file://'), false); | |
172 expect(builder.isRootRelative('/'), true); | |
173 expect(builder.isRootRelative('~'), false); | |
174 expect(builder.isRootRelative('.'), false); | |
175 expect(builder.isRootRelative('../a'), false); | |
176 expect(builder.isRootRelative('C:/a'), false); | |
177 expect(builder.isRootRelative(r'C:\a'), false); | |
178 expect(builder.isRootRelative(r'\\a'), false); | |
179 }); | |
180 | |
181 group('join', () { | |
182 test('allows up to eight parts', () { | |
183 expect(builder.join('a'), 'a'); | |
184 expect(builder.join('a', 'b'), 'a/b'); | |
185 expect(builder.join('a', 'b', 'c'), 'a/b/c'); | |
186 expect(builder.join('a', 'b', 'c', 'd'), 'a/b/c/d'); | |
187 expect(builder.join('a', 'b', 'c', 'd', 'e'), 'a/b/c/d/e'); | |
188 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f'), 'a/b/c/d/e/f'); | |
189 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g'), 'a/b/c/d/e/f/g'); | |
190 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'), | |
191 'a/b/c/d/e/f/g/h'); | |
192 }); | |
193 | |
194 test('does not add separator if a part ends in one', () { | |
195 expect(builder.join('a/', 'b', 'c/', 'd'), 'a/b/c/d'); | |
196 expect(builder.join('a\\', 'b'), r'a\/b'); | |
197 }); | |
198 | |
199 test('ignores parts before an absolute path', () { | |
200 expect(builder.join('a', 'http://dartlang.org', 'b', 'c'), | |
201 'http://dartlang.org/b/c'); | |
202 expect(builder.join('a', 'file://', 'b', 'c'), 'file:///b/c'); | |
203 expect(builder.join('a', '/', 'b', 'c'), '/b/c'); | |
204 expect(builder.join('a', '/b', 'http://dartlang.org/c', 'd'), | |
205 'http://dartlang.org/c/d'); | |
206 expect(builder.join( | |
207 'a', 'http://google.com/b', 'http://dartlang.org/c', 'd'), | |
208 'http://dartlang.org/c/d'); | |
209 expect(builder.join('a', '/b', '/c', 'd'), '/c/d'); | |
210 expect(builder.join('a', r'c:\b', 'c', 'd'), r'a/c:\b/c/d'); | |
211 expect(builder.join('a', r'\\b', 'c', 'd'), r'a/\\b/c/d'); | |
212 }); | |
213 | |
214 test('preserves roots before a root-relative path', () { | |
215 expect(builder.join('http://dartlang.org', 'a', '/b', 'c'), | |
216 'http://dartlang.org/b/c'); | |
217 expect(builder.join('file://', 'a', '/b', 'c'), 'file:///b/c'); | |
218 expect(builder.join('file://', 'a', '/b', 'c', '/d'), 'file:///d'); | |
219 }); | |
220 | |
221 test('ignores trailing nulls', () { | |
222 expect(builder.join('a', null), equals('a')); | |
223 expect(builder.join('a', 'b', 'c', null, null), equals('a/b/c')); | |
224 }); | |
225 | |
226 test('disallows intermediate nulls', () { | |
227 expect(() => builder.join('a', null, 'b'), throwsArgumentError); | |
228 expect(() => builder.join(null, 'a'), throwsArgumentError); | |
229 }); | |
230 }); | |
231 | |
232 group('joinAll', () { | |
233 test('allows more than eight parts', () { | |
234 expect(builder.joinAll(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']), | |
235 'a/b/c/d/e/f/g/h/i'); | |
236 }); | |
237 | |
238 test('ignores parts before an absolute path', () { | |
239 expect(builder.joinAll(['a', 'http://dartlang.org', 'b', 'c']), | |
240 'http://dartlang.org/b/c'); | |
241 expect(builder.joinAll(['a', 'file://', 'b', 'c']), 'file:///b/c'); | |
242 expect(builder.joinAll(['a', '/', 'b', 'c']), '/b/c'); | |
243 expect(builder.joinAll(['a', '/b', 'http://dartlang.org/c', 'd']), | |
244 'http://dartlang.org/c/d'); | |
245 expect(builder.joinAll( | |
246 ['a', 'http://google.com/b', 'http://dartlang.org/c', 'd']), | |
247 'http://dartlang.org/c/d'); | |
248 expect(builder.joinAll(['a', '/b', '/c', 'd']), '/c/d'); | |
249 expect(builder.joinAll(['a', r'c:\b', 'c', 'd']), r'a/c:\b/c/d'); | |
250 expect(builder.joinAll(['a', r'\\b', 'c', 'd']), r'a/\\b/c/d'); | |
251 }); | |
252 | |
253 test('preserves roots before a root-relative path', () { | |
254 expect(builder.joinAll(['http://dartlang.org', 'a', '/b', 'c']), | |
255 'http://dartlang.org/b/c'); | |
256 expect(builder.joinAll(['file://', 'a', '/b', 'c']), 'file:///b/c'); | |
257 expect(builder.joinAll(['file://', 'a', '/b', 'c', '/d']), 'file:///d'); | |
258 }); | |
259 }); | |
260 | |
261 group('split', () { | |
262 test('simple cases', () { | |
263 expect(builder.split(''), []); | |
264 expect(builder.split('.'), ['.']); | |
265 expect(builder.split('..'), ['..']); | |
266 expect(builder.split('foo'), equals(['foo'])); | |
267 expect(builder.split('foo/bar.txt'), equals(['foo', 'bar.txt'])); | |
268 expect(builder.split('foo/bar/baz'), equals(['foo', 'bar', 'baz'])); | |
269 expect(builder.split('foo/../bar/./baz'), | |
270 equals(['foo', '..', 'bar', '.', 'baz'])); | |
271 expect(builder.split('foo//bar///baz'), equals(['foo', 'bar', 'baz'])); | |
272 expect(builder.split('foo/\\/baz'), equals(['foo', '\\', 'baz'])); | |
273 expect(builder.split('.'), equals(['.'])); | |
274 expect(builder.split(''), equals([])); | |
275 expect(builder.split('foo/'), equals(['foo'])); | |
276 expect(builder.split('http://dartlang.org//'), | |
277 equals(['http://dartlang.org'])); | |
278 expect(builder.split('file:////'), equals(['file://'])); | |
279 expect(builder.split('//'), equals(['/'])); | |
280 }); | |
281 | |
282 test('includes the root for absolute paths', () { | |
283 expect(builder.split('http://dartlang.org/foo/bar/baz'), | |
284 equals(['http://dartlang.org', 'foo', 'bar', 'baz'])); | |
285 expect(builder.split('file:///foo/bar/baz'), | |
286 equals(['file://', 'foo', 'bar', 'baz'])); | |
287 expect(builder.split('/foo/bar/baz'), equals(['/', 'foo', 'bar', 'baz'])); | |
288 expect(builder.split('http://dartlang.org/'), | |
289 equals(['http://dartlang.org'])); | |
290 expect(builder.split('http://dartlang.org'), | |
291 equals(['http://dartlang.org'])); | |
292 expect(builder.split('file:///'), equals(['file://'])); | |
293 expect(builder.split('file://'), equals(['file://'])); | |
294 expect(builder.split('/'), equals(['/'])); | |
295 }); | |
296 }); | |
297 | |
298 group('normalize', () { | |
299 test('simple cases', () { | |
300 expect(builder.normalize(''), ''); | |
301 expect(builder.normalize('.'), '.'); | |
302 expect(builder.normalize('..'), '..'); | |
303 expect(builder.normalize('a'), 'a'); | |
304 expect(builder.normalize('http://dartlang.org/'), 'http://dartlang.org'); | |
305 expect(builder.normalize('http://dartlang.org'), 'http://dartlang.org'); | |
306 expect(builder.normalize('file://'), 'file://'); | |
307 expect(builder.normalize('file:///'), 'file://'); | |
308 expect(builder.normalize('/'), '/'); | |
309 expect(builder.normalize(r'\'), r'\'); | |
310 }); | |
311 | |
312 test('collapses redundant separators', () { | |
313 expect(builder.normalize(r'a/b/c'), r'a/b/c'); | |
314 expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d'); | |
315 }); | |
316 | |
317 test('does not collapse separators for other platform', () { | |
318 expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c'); | |
319 }); | |
320 | |
321 test('eliminates "." parts', () { | |
322 expect(builder.normalize('./'), '.'); | |
323 expect(builder.normalize('http://dartlang.org/.'), 'http://dartlang.org'); | |
324 expect(builder.normalize('file:///.'), 'file://'); | |
325 expect(builder.normalize('/.'), '/'); | |
326 expect(builder.normalize('http://dartlang.org/./'), | |
327 'http://dartlang.org'); | |
328 expect(builder.normalize('file:///./'), 'file://'); | |
329 expect(builder.normalize('/./'), '/'); | |
330 expect(builder.normalize('./.'), '.'); | |
331 expect(builder.normalize('a/./b'), 'a/b'); | |
332 expect(builder.normalize('a/.b/c'), 'a/.b/c'); | |
333 expect(builder.normalize('a/././b/./c'), 'a/b/c'); | |
334 expect(builder.normalize('././a'), 'a'); | |
335 expect(builder.normalize('a/./.'), 'a'); | |
336 }); | |
337 | |
338 test('eliminates ".." parts', () { | |
339 expect(builder.normalize('..'), '..'); | |
340 expect(builder.normalize('../'), '..'); | |
341 expect(builder.normalize('../../..'), '../../..'); | |
342 expect(builder.normalize('../../../'), '../../..'); | |
343 expect(builder.normalize('http://dartlang.org/..'), | |
344 'http://dartlang.org'); | |
345 expect(builder.normalize('file:///..'), 'file://'); | |
346 expect(builder.normalize('/..'), '/'); | |
347 expect(builder.normalize('http://dartlang.org/../../..'), | |
348 'http://dartlang.org'); | |
349 expect(builder.normalize('file:///../../..'), 'file://'); | |
350 expect(builder.normalize('/../../..'), '/'); | |
351 expect(builder.normalize('http://dartlang.org/../../../a'), | |
352 'http://dartlang.org/a'); | |
353 expect(builder.normalize('file:///../../../a'), 'file:///a'); | |
354 expect(builder.normalize('/../../../a'), '/a'); | |
355 expect(builder.normalize('a/..'), '.'); | |
356 expect(builder.normalize('a/b/..'), 'a'); | |
357 expect(builder.normalize('a/../b'), 'b'); | |
358 expect(builder.normalize('a/./../b'), 'b'); | |
359 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | |
360 expect(builder.normalize('a/b/../../../../c'), '../../c'); | |
361 }); | |
362 | |
363 test('does not walk before root on absolute paths', () { | |
364 expect(builder.normalize('..'), '..'); | |
365 expect(builder.normalize('../'), '..'); | |
366 expect(builder.normalize('http://dartlang.org/..'), | |
367 'http://dartlang.org'); | |
368 expect(builder.normalize('file:///..'), 'file://'); | |
369 expect(builder.normalize('/..'), '/'); | |
370 expect(builder.normalize('a/..'), '.'); | |
371 expect(builder.normalize('a/b/..'), 'a'); | |
372 expect(builder.normalize('a/../b'), 'b'); | |
373 expect(builder.normalize('a/./../b'), 'b'); | |
374 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | |
375 expect(builder.normalize('a/b/../../../../c'), '../../c'); | |
376 }); | |
377 | |
378 test('removes trailing separators', () { | |
379 expect(builder.normalize('./'), '.'); | |
380 expect(builder.normalize('.//'), '.'); | |
381 expect(builder.normalize('a/'), 'a'); | |
382 expect(builder.normalize('a/b/'), 'a/b'); | |
383 expect(builder.normalize('a/b///'), 'a/b'); | |
384 }); | |
385 }); | |
386 | |
387 group('relative', () { | |
388 group('from absolute root', () { | |
389 test('given absolute path in root', () { | |
390 expect(builder.relative('http://dartlang.org'), '../..'); | |
391 expect(builder.relative('http://dartlang.org/'), '../..'); | |
392 expect(builder.relative('/'), '../..'); | |
393 expect(builder.relative('http://dartlang.org/root'), '..'); | |
394 expect(builder.relative('/root'), '..'); | |
395 expect(builder.relative('http://dartlang.org/root/path'), '.'); | |
396 expect(builder.relative('/root/path'), '.'); | |
397 expect(builder.relative('http://dartlang.org/root/path/a'), 'a'); | |
398 expect(builder.relative('/root/path/a'), 'a'); | |
399 expect(builder.relative('http://dartlang.org/root/path/a/b.txt'), | |
400 'a/b.txt'); | |
401 expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt'); | |
402 expect(builder.relative('http://dartlang.org/root/a/b.txt'), | |
403 '../a/b.txt'); | |
404 expect(builder.relative('/root/a/b.txt'), '../a/b.txt'); | |
405 }); | |
406 | |
407 test('given absolute path outside of root', () { | |
408 expect(builder.relative('http://dartlang.org/a/b'), '../../a/b'); | |
409 expect(builder.relative('/a/b'), '../../a/b'); | |
410 expect(builder.relative('http://dartlang.org/root/path/a'), 'a'); | |
411 expect(builder.relative('/root/path/a'), 'a'); | |
412 expect(builder.relative('http://dartlang.org/root/path/a/b.txt'), | |
413 'a/b.txt'); | |
414 expect(builder.relative('http://dartlang.org/root/path/a/b.txt'), | |
415 'a/b.txt'); | |
416 expect(builder.relative('http://dartlang.org/root/a/b.txt'), | |
417 '../a/b.txt'); | |
418 }); | |
419 | |
420 test('given absolute path with different hostname/protocol', () { | |
421 expect(builder.relative(r'http://google.com/a/b'), | |
422 r'http://google.com/a/b'); | |
423 expect(builder.relative(r'file:///a/b'), | |
424 r'file:///a/b'); | |
425 }); | |
426 | |
427 test('given relative path', () { | |
428 // The path is considered relative to the root, so it basically just | |
429 // normalizes. | |
430 expect(builder.relative(''), '.'); | |
431 expect(builder.relative('.'), '.'); | |
432 expect(builder.relative('a'), 'a'); | |
433 expect(builder.relative('a/b.txt'), 'a/b.txt'); | |
434 expect(builder.relative('../a/b.txt'), '../a/b.txt'); | |
435 expect(builder.relative('a/./b/../c.txt'), 'a/c.txt'); | |
436 }); | |
437 | |
438 // Regression | |
439 test('from root-only path', () { | |
440 expect(builder.relative('http://dartlang.org', | |
441 from: 'http://dartlang.org'), | |
442 '.'); | |
443 expect(builder.relative('http://dartlang.org/root/path', | |
444 from: 'http://dartlang.org'), | |
445 'root/path'); | |
446 }); | |
447 }); | |
448 | |
449 group('from relative root', () { | |
450 var r = new path.Builder(style: path.Style.url, root: 'foo/bar'); | |
451 | |
452 test('given absolute path', () { | |
453 expect(r.relative('http://google.com/'), equals('http://google.com')); | |
454 expect(r.relative('http://google.com'), equals('http://google.com')); | |
455 expect(r.relative('file:///'), equals('file://')); | |
456 expect(r.relative('file://'), equals('file://')); | |
457 expect(r.relative('/'), equals('/')); | |
458 expect(r.relative('/a/b'), equals('/a/b')); | |
459 }); | |
460 | |
461 test('given relative path', () { | |
462 // The path is considered relative to the root, so it basically just | |
463 // normalizes. | |
464 expect(r.relative(''), '.'); | |
465 expect(r.relative('.'), '.'); | |
466 expect(r.relative('..'), '..'); | |
467 expect(r.relative('a'), 'a'); | |
468 expect(r.relative('a/b.txt'), 'a/b.txt'); | |
469 expect(r.relative('../a/b.txt'), '../a/b.txt'); | |
470 expect(r.relative('a/./b/../c.txt'), 'a/c.txt'); | |
471 }); | |
472 }); | |
473 | |
474 group('from root-relative root', () { | |
475 var r = new path.Builder(style: path.Style.url, root: '/foo/bar'); | |
476 | |
477 test('given absolute path', () { | |
478 expect(r.relative('http://google.com/'), equals('http://google.com')); | |
479 expect(r.relative('http://google.com'), equals('http://google.com')); | |
480 expect(r.relative('file:///'), equals('file://')); | |
481 expect(r.relative('file://'), equals('file://')); | |
482 expect(r.relative('/'), equals('../..')); | |
483 expect(r.relative('/a/b'), equals('../../a/b')); | |
484 }); | |
485 | |
486 test('given relative path', () { | |
487 // The path is considered relative to the root, so it basically just | |
488 // normalizes. | |
489 expect(r.relative(''), '.'); | |
490 expect(r.relative('.'), '.'); | |
491 expect(r.relative('..'), '..'); | |
492 expect(r.relative('a'), 'a'); | |
493 expect(r.relative('a/b.txt'), 'a/b.txt'); | |
494 expect(r.relative('../a/b.txt'), '../a/b.txt'); | |
495 expect(r.relative('a/./b/../c.txt'), 'a/c.txt'); | |
496 }); | |
497 }); | |
498 | |
499 test('from a root with extension', () { | |
500 var r = new path.Builder(style: path.Style.url, root: '/dir.ext'); | |
501 expect(r.relative('/dir.ext/file'), 'file'); | |
502 }); | |
503 | |
504 test('with a root parameter', () { | |
505 expect(builder.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz')); | |
506 expect( | |
507 builder.relative('/foo/bar/baz', from: 'http://dartlang.org/foo/bar'), | |
508 equals('baz')); | |
509 expect( | |
510 builder.relative('http://dartlang.org/foo/bar/baz', from: '/foo/bar'), | |
511 equals('baz')); | |
512 expect(builder.relative('http://dartlang.org/foo/bar/baz', | |
513 from: 'file:///foo/bar'), | |
514 equals('http://dartlang.org/foo/bar/baz')); | |
515 expect(builder.relative('http://dartlang.org/foo/bar/baz', | |
516 from: 'http://dartlang.org/foo/bar'), equals('baz')); | |
517 expect( | |
518 builder.relative('/foo/bar/baz', from: 'file:///foo/bar'), | |
519 equals('http://dartlang.org/foo/bar/baz')); | |
520 expect( | |
521 builder.relative('file:///foo/bar/baz', from: '/foo/bar'), | |
522 equals('file:///foo/bar/baz')); | |
523 | |
524 expect(builder.relative('..', from: '/foo/bar'), equals('../../root')); | |
525 expect(builder.relative('..', from: 'http://dartlang.org/foo/bar'), | |
526 equals('../../root')); | |
527 expect(builder.relative('..', from: 'file:///foo/bar'), | |
528 equals('http://dartlang.org/root')); | |
529 expect(builder.relative('..', from: '/foo/bar'), equals('../../root')); | |
530 | |
531 expect(builder.relative('http://dartlang.org/foo/bar/baz', | |
532 from: 'foo/bar'), | |
533 equals('../../../../foo/bar/baz')); | |
534 expect(builder.relative('file:///foo/bar/baz', from: 'foo/bar'), | |
535 equals('file:///foo/bar/baz')); | |
536 expect(builder.relative('/foo/bar/baz', from: 'foo/bar'), | |
537 equals('../../../../foo/bar/baz')); | |
538 | |
539 expect(builder.relative('..', from: 'foo/bar'), equals('../../..')); | |
540 }); | |
541 | |
542 test('with a root parameter and a relative root', () { | |
543 var r = new path.Builder(style: path.Style.url, root: 'relative/root'); | |
544 expect(r.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz')); | |
545 expect( | |
546 r.relative('/foo/bar/baz', from: 'http://dartlang.org/foo/bar'), | |
547 equals('/foo/bar/baz')); | |
548 expect( | |
549 r.relative('http://dartlang.org/foo/bar/baz', from: '/foo/bar'), | |
550 equals('http://dartlang.org/foo/bar/baz')); | |
551 expect(r.relative('http://dartlang.org/foo/bar/baz', | |
552 from: 'file:///foo/bar'), | |
553 equals('http://dartlang.org/foo/bar/baz')); | |
554 expect(r.relative('http://dartlang.org/foo/bar/baz', | |
555 from: 'http://dartlang.org/foo/bar'), equals('baz')); | |
556 | |
557 expect(r.relative('http://dartlang.org/foo/bar/baz', from: 'foo/bar'), | |
558 equals('http://dartlang.org/foo/bar/baz')); | |
559 expect(r.relative('file:///foo/bar/baz', from: 'foo/bar'), | |
560 equals('file:///foo/bar/baz')); | |
561 expect(r.relative('/foo/bar/baz', from: 'foo/bar'), | |
562 equals('/foo/bar/baz')); | |
563 | |
564 expect(r.relative('..', from: 'foo/bar'), equals('../../..')); | |
565 }); | |
566 | |
567 test('from a . root', () { | |
568 var r = new path.Builder(style: path.Style.url, root: '.'); | |
569 expect(r.relative('http://dartlang.org/foo/bar/baz'), | |
570 equals('http://dartlang.org/foo/bar/baz')); | |
571 expect(r.relative('file:///foo/bar/baz'), equals('file:///foo/bar/baz')); | |
572 expect(r.relative('/foo/bar/baz'), equals('/foo/bar/baz')); | |
573 expect(r.relative('foo/bar/baz'), equals('foo/bar/baz')); | |
574 }); | |
575 }); | |
576 | |
577 group('resolve', () { | |
578 test('allows up to seven parts', () { | |
579 expect(builder.resolve('a'), 'http://dartlang.org/root/path/a'); | |
580 expect(builder.resolve('a', 'b'), 'http://dartlang.org/root/path/a/b'); | |
581 expect(builder.resolve('a', 'b', 'c'), | |
582 'http://dartlang.org/root/path/a/b/c'); | |
583 expect(builder.resolve('a', 'b', 'c', 'd'), | |
584 'http://dartlang.org/root/path/a/b/c/d'); | |
585 expect(builder.resolve('a', 'b', 'c', 'd', 'e'), | |
586 'http://dartlang.org/root/path/a/b/c/d/e'); | |
587 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f'), | |
588 'http://dartlang.org/root/path/a/b/c/d/e/f'); | |
589 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f', 'g'), | |
590 'http://dartlang.org/root/path/a/b/c/d/e/f/g'); | |
591 }); | |
592 | |
593 test('does not add separator if a part ends in one', () { | |
594 expect(builder.resolve('a/', 'b', 'c/', 'd'), | |
595 'http://dartlang.org/root/path/a/b/c/d'); | |
596 expect(builder.resolve(r'a\', 'b'), | |
597 r'http://dartlang.org/root/path/a\/b'); | |
598 }); | |
599 | |
600 test('ignores parts before an absolute path', () { | |
601 expect(builder.resolve('a', '/b', '/c', 'd'), 'http://dartlang.org/c/d'); | |
602 expect(builder.resolve('a', '/b', 'file:///c', 'd'), 'file:///c/d'); | |
603 expect(builder.resolve('a', r'c:\b', 'c', 'd'), | |
604 r'http://dartlang.org/root/path/a/c:\b/c/d'); | |
605 expect(builder.resolve('a', r'\\b', 'c', 'd'), | |
606 r'http://dartlang.org/root/path/a/\\b/c/d'); | |
607 }); | |
608 }); | |
609 | |
610 test('withoutExtension', () { | |
611 expect(builder.withoutExtension(''), ''); | |
612 expect(builder.withoutExtension('a'), 'a'); | |
613 expect(builder.withoutExtension('.a'), '.a'); | |
614 expect(builder.withoutExtension('a.b'), 'a'); | |
615 expect(builder.withoutExtension('a/b.c'), 'a/b'); | |
616 expect(builder.withoutExtension('a/b.c.d'), 'a/b.c'); | |
617 expect(builder.withoutExtension('a/'), 'a/'); | |
618 expect(builder.withoutExtension('a/b/'), 'a/b/'); | |
619 expect(builder.withoutExtension('a/.'), 'a/.'); | |
620 expect(builder.withoutExtension('a/.b'), 'a/.b'); | |
621 expect(builder.withoutExtension('a.b/c'), 'a.b/c'); | |
622 expect(builder.withoutExtension(r'a.b\c'), r'a'); | |
623 expect(builder.withoutExtension(r'a/b\c'), r'a/b\c'); | |
624 expect(builder.withoutExtension(r'a/b\c.d'), r'a/b\c'); | |
625 expect(builder.withoutExtension('a/b.c/'), 'a/b/'); | |
626 expect(builder.withoutExtension('a/b.c//'), 'a/b//'); | |
627 }); | |
628 | |
629 | |
630 test('fromUri', () { | |
631 expect(builder.fromUri(Uri.parse('http://dartlang.org/path/to/foo')), | |
632 'http://dartlang.org/path/to/foo'); | |
633 expect(builder.fromUri(Uri.parse('http://dartlang.org/path/to/foo/')), | |
634 'http://dartlang.org/path/to/foo/'); | |
635 expect(builder.fromUri(Uri.parse('file:///path/to/foo')), | |
636 'file:///path/to/foo'); | |
637 expect(builder.fromUri(Uri.parse('foo/bar')), 'foo/bar'); | |
638 expect(builder.fromUri(Uri.parse('http://dartlang.org/path/to/foo%23bar')), | |
639 'http://dartlang.org/path/to/foo%23bar'); | |
640 }); | |
641 | |
642 test('toUri', () { | |
643 expect(builder.toUri('http://dartlang.org/path/to/foo'), | |
644 Uri.parse('http://dartlang.org/path/to/foo')); | |
645 expect(builder.toUri('http://dartlang.org/path/to/foo/'), | |
646 Uri.parse('http://dartlang.org/path/to/foo/')); | |
647 expect(builder.toUri('file:///path/to/foo'), | |
648 Uri.parse('file:///path/to/foo')); | |
649 expect(builder.toUri('foo/bar'), Uri.parse('foo/bar')); | |
650 expect(builder.toUri('http://dartlang.org/path/to/foo%23bar'), | |
651 Uri.parse('http://dartlang.org/path/to/foo%23bar')); | |
652 }); | |
653 } | |
OLD | NEW |