OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library path.test.posix_test; | 5 library path.test.posix_test; |
6 | 6 |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'package:path/path.dart' as path; | 8 import 'package:path/path.dart' as path; |
9 | 9 |
10 main() { | 10 main() { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 expect(builder.join('', ''), ''); | 158 expect(builder.join('', ''), ''); |
159 expect(builder.join('', 'a'), 'a'); | 159 expect(builder.join('', 'a'), 'a'); |
160 expect(builder.join('a', '', 'b', '', '', '', 'c'), 'a/b/c'); | 160 expect(builder.join('a', '', 'b', '', '', '', 'c'), 'a/b/c'); |
161 expect(builder.join('a', 'b', ''), 'a/b'); | 161 expect(builder.join('a', 'b', ''), 'a/b'); |
162 }); | 162 }); |
163 | 163 |
164 test('disallows intermediate nulls', () { | 164 test('disallows intermediate nulls', () { |
165 expect(() => builder.join('a', null, 'b'), throwsArgumentError); | 165 expect(() => builder.join('a', null, 'b'), throwsArgumentError); |
166 expect(() => builder.join(null, 'a'), throwsArgumentError); | 166 expect(() => builder.join(null, 'a'), throwsArgumentError); |
167 }); | 167 }); |
168 | |
169 test('misc join tests ported from Path class', () { | |
170 expect(builder.join('a/', 'b/c/'), 'a/b/c/'); | |
Bob Nystrom
2013/07/18 20:45:08
How about making a "preserves trailing separators"
Bill Hesse
2013/07/22 11:35:22
Done.
| |
171 expect(builder.join('a/b/./c/..//', 'd/.././..//e/f//'), | |
172 'a/b/./c/..//d/.././..//e/f//'); | |
173 expect(builder.join('a/b', 'c/../../../..'), 'a/b/c/../../../..'); | |
Bob Nystrom
2013/07/18 20:45:08
This is a good test. Might be nice to have somethi
Bill Hesse
2013/07/22 11:35:22
Done.
| |
174 expect(builder.join('', ''), ''); | |
Bob Nystrom
2013/07/18 20:45:08
This is covered by line 158.
Bill Hesse
2013/07/22 11:35:22
Done.
| |
175 expect(builder.join('a/b/c/', '/d'), '/d'); | |
Bob Nystrom
2013/07/18 20:45:08
This is covered by line 140.
Bill Hesse
2013/07/22 11:35:22
Done.
| |
176 expect(builder.join('a', 'b${builder.separator}'), 'a/b/'); | |
Bob Nystrom
2013/07/18 20:45:08
You can just do "/" instead of ${builder.separator
Bill Hesse
2013/07/22 11:35:22
We already have a test for literal / at the end.
S
Bob Nystrom
2013/07/22 20:53:11
We already test what builder.separator returns, so
Bill Hesse
2013/07/23 16:07:00
Removed - I'm used to writing VM tests, where more
| |
177 }); | |
168 }); | 178 }); |
169 | 179 |
170 group('joinAll', () { | 180 group('joinAll', () { |
171 test('allows more than eight parts', () { | 181 test('allows more than eight parts', () { |
172 expect(builder.joinAll(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']), | 182 expect(builder.joinAll(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']), |
173 'a/b/c/d/e/f/g/h/i'); | 183 'a/b/c/d/e/f/g/h/i'); |
174 }); | 184 }); |
175 | 185 |
176 test('does not add separator if a part ends in one', () { | 186 test('does not add separator if a part ends in one', () { |
177 expect(builder.joinAll(['a/', 'b', 'c/', 'd']), 'a/b/c/d'); | 187 expect(builder.joinAll(['a/', 'b', 'c/', 'd']), 'a/b/c/d'); |
(...skipping 27 matching lines...) Expand all Loading... | |
205 }); | 215 }); |
206 | 216 |
207 test('includes the root for absolute paths', () { | 217 test('includes the root for absolute paths', () { |
208 expect(builder.split('/foo/bar/baz'), equals(['/', 'foo', 'bar', 'baz'])); | 218 expect(builder.split('/foo/bar/baz'), equals(['/', 'foo', 'bar', 'baz'])); |
209 expect(builder.split('/'), equals(['/'])); | 219 expect(builder.split('/'), equals(['/'])); |
210 }); | 220 }); |
211 }); | 221 }); |
212 | 222 |
213 group('normalize', () { | 223 group('normalize', () { |
214 test('simple cases', () { | 224 test('simple cases', () { |
215 expect(builder.normalize(''), ''); | 225 expect(builder.normalize(''), '.'); |
216 expect(builder.normalize('.'), '.'); | 226 expect(builder.normalize('.'), '.'); |
217 expect(builder.normalize('..'), '..'); | 227 expect(builder.normalize('..'), '..'); |
218 expect(builder.normalize('a'), 'a'); | 228 expect(builder.normalize('a'), 'a'); |
219 expect(builder.normalize('/'), '/'); | 229 expect(builder.normalize('/'), '/'); |
220 expect(builder.normalize(r'\'), r'\'); | 230 expect(builder.normalize(r'\'), r'\'); |
231 expect(builder.normalize('C:/'), 'C:'); | |
232 expect(builder.normalize(r'C:\'), r'C:\'); | |
233 expect(builder.normalize(r'\\'), r'\\'); | |
Bob Nystrom
2013/07/18 20:45:08
Yay tests!
| |
221 }); | 234 }); |
222 | 235 |
223 test('collapses redundant separators', () { | 236 test('collapses redundant separators', () { |
224 expect(builder.normalize(r'a/b/c'), r'a/b/c'); | 237 expect(builder.normalize(r'a/b/c'), r'a/b/c'); |
225 expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d'); | 238 expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d'); |
226 }); | 239 }); |
227 | 240 |
228 test('does not collapse separators for other platform', () { | 241 test('does not collapse separators for other platform', () { |
229 expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c'); | 242 expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c'); |
230 }); | 243 }); |
(...skipping 11 matching lines...) Expand all Loading... | |
242 }); | 255 }); |
243 | 256 |
244 test('eliminates ".." parts', () { | 257 test('eliminates ".." parts', () { |
245 expect(builder.normalize('..'), '..'); | 258 expect(builder.normalize('..'), '..'); |
246 expect(builder.normalize('../'), '..'); | 259 expect(builder.normalize('../'), '..'); |
247 expect(builder.normalize('../../..'), '../../..'); | 260 expect(builder.normalize('../../..'), '../../..'); |
248 expect(builder.normalize('../../../'), '../../..'); | 261 expect(builder.normalize('../../../'), '../../..'); |
249 expect(builder.normalize('/..'), '/'); | 262 expect(builder.normalize('/..'), '/'); |
250 expect(builder.normalize('/../../..'), '/'); | 263 expect(builder.normalize('/../../..'), '/'); |
251 expect(builder.normalize('/../../../a'), '/a'); | 264 expect(builder.normalize('/../../../a'), '/a'); |
265 expect(builder.normalize('c:/..'), '.'); | |
266 expect(builder.normalize('A:/../../..'), '../..'); | |
Bob Nystrom
2013/07/18 20:45:08
Nice.
| |
252 expect(builder.normalize('a/..'), '.'); | 267 expect(builder.normalize('a/..'), '.'); |
253 expect(builder.normalize('a/b/..'), 'a'); | 268 expect(builder.normalize('a/b/..'), 'a'); |
254 expect(builder.normalize('a/../b'), 'b'); | 269 expect(builder.normalize('a/../b'), 'b'); |
255 expect(builder.normalize('a/./../b'), 'b'); | 270 expect(builder.normalize('a/./../b'), 'b'); |
256 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | 271 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); |
257 expect(builder.normalize('a/b/../../../../c'), '../../c'); | 272 expect(builder.normalize('a/b/../../../../c'), '../../c'); |
273 expect(builder.normalize(r'z/a/b/../../..\../c'), r'z/..\../c'); | |
274 expect(builder.normalize(r'a/b\c/../d'), 'a/d'); | |
258 }); | 275 }); |
259 | 276 |
260 test('does not walk before root on absolute paths', () { | 277 test('does not walk before root on absolute paths', () { |
261 expect(builder.normalize('..'), '..'); | 278 expect(builder.normalize('..'), '..'); |
262 expect(builder.normalize('../'), '..'); | 279 expect(builder.normalize('../'), '..'); |
280 expect(builder.normalize('http://dartlang.org/..'), 'http:'); | |
281 expect(builder.normalize('http://dartlang.org/../../a'), 'a'); | |
282 expect(builder.normalize('file:///..'), '.'); | |
283 expect(builder.normalize('file:///../../a'), '../a'); | |
263 expect(builder.normalize('/..'), '/'); | 284 expect(builder.normalize('/..'), '/'); |
264 expect(builder.normalize('a/..'), '.'); | 285 expect(builder.normalize('a/..'), '.'); |
286 expect(builder.normalize('../a'), '../a'); | |
287 expect(builder.normalize('/../a'), '/a'); | |
288 expect(builder.normalize('c:/../a'), 'a'); | |
289 expect(builder.normalize('/../a'), '/a'); | |
265 expect(builder.normalize('a/b/..'), 'a'); | 290 expect(builder.normalize('a/b/..'), 'a'); |
291 expect(builder.normalize('../a/b/..'), '../a'); | |
266 expect(builder.normalize('a/../b'), 'b'); | 292 expect(builder.normalize('a/../b'), 'b'); |
267 expect(builder.normalize('a/./../b'), 'b'); | 293 expect(builder.normalize('a/./../b'), 'b'); |
268 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | 294 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); |
269 expect(builder.normalize('a/b/../../../../c'), '../../c'); | 295 expect(builder.normalize('a/b/../../../../c'), '../../c'); |
296 expect(builder.normalize('a/b/c/../../..d/./.e/f././'), 'a/..d/.e/f.'); | |
270 }); | 297 }); |
271 | 298 |
272 test('removes trailing separators', () { | 299 test('removes trailing separators', () { |
273 expect(builder.normalize('./'), '.'); | 300 expect(builder.normalize('./'), '.'); |
274 expect(builder.normalize('.//'), '.'); | 301 expect(builder.normalize('.//'), '.'); |
275 expect(builder.normalize('a/'), 'a'); | 302 expect(builder.normalize('a/'), 'a'); |
276 expect(builder.normalize('a/b/'), 'a/b'); | 303 expect(builder.normalize('a/b/'), 'a/b'); |
304 expect(builder.normalize(r'a/b\'), r'a/b\'); | |
277 expect(builder.normalize('a/b///'), 'a/b'); | 305 expect(builder.normalize('a/b///'), 'a/b'); |
278 }); | 306 }); |
279 }); | 307 }); |
280 | 308 |
281 group('relative', () { | 309 group('relative', () { |
282 group('from absolute root', () { | 310 group('from absolute root', () { |
283 test('given absolute path in root', () { | 311 test('given absolute path in root', () { |
284 expect(builder.relative('/'), '../..'); | 312 expect(builder.relative('/'), '../..'); |
285 expect(builder.relative('/root'), '..'); | 313 expect(builder.relative('/root'), '..'); |
286 expect(builder.relative('/root/path'), '.'); | 314 expect(builder.relative('/root/path'), '.'); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
422 }); | 450 }); |
423 | 451 |
424 test('toUri', () { | 452 test('toUri', () { |
425 expect(builder.toUri('/path/to/foo'), Uri.parse('file:///path/to/foo')); | 453 expect(builder.toUri('/path/to/foo'), Uri.parse('file:///path/to/foo')); |
426 expect(builder.toUri('/path/to/foo/'), Uri.parse('file:///path/to/foo/')); | 454 expect(builder.toUri('/path/to/foo/'), Uri.parse('file:///path/to/foo/')); |
427 expect(builder.toUri('/'), Uri.parse('file:///')); | 455 expect(builder.toUri('/'), Uri.parse('file:///')); |
428 expect(builder.toUri('foo/bar'), Uri.parse('foo/bar')); | 456 expect(builder.toUri('foo/bar'), Uri.parse('foo/bar')); |
429 expect(builder.toUri('/path/to/foo#bar'), | 457 expect(builder.toUri('/path/to/foo#bar'), |
430 Uri.parse('file:///path/to/foo%23bar')); | 458 Uri.parse('file:///path/to/foo%23bar')); |
431 }); | 459 }); |
460 | |
461 void testGetters(String p, List components, String properties) { | |
Bob Nystrom
2013/07/18 20:45:08
Do you think it's worth adding these? I find them
Bill Hesse
2013/07/22 11:35:22
Removed. Some tests, like for '.', '..', and 'gul
| |
462 final int DIRNAME = 0; | |
463 final int FILENAME = 1; | |
464 final int FILENAME_NO_EXTENSION = 2; | |
465 final int WITHOUT_EXTENSION = 3; | |
466 final int EXTENSION = 4; | |
467 final int ROOT_PREFIX = 5; | |
468 expect(components[DIRNAME], builder.dirname(p)); | |
469 expect(components[FILENAME], builder.basename(p)); | |
470 expect(components[FILENAME_NO_EXTENSION], | |
471 builder.basenameWithoutExtension(p)); | |
472 expect(components[WITHOUT_EXTENSION], | |
473 builder.withoutExtension(p)); | |
474 expect(components[EXTENSION], builder.extension(p)); | |
475 expect(builder.isAbsolute(p), properties.contains('absolute')); | |
476 expect(components[ROOT_PREFIX], builder.rootPrefix(p)); | |
477 if (properties.contains('normalized')) { | |
478 expect(p, builder.normalize(p)); | |
479 } else { | |
480 expect(p, isNot(equals(builder.normalize(p)))); | |
481 } | |
482 } | |
483 | |
484 test('getters', () { | |
485 testGetters("/foo/bar/fisk.hest", | |
486 ['/foo/bar', 'fisk.hest', 'fisk', '/foo/bar/fisk', | |
487 '.hest', '/'], | |
488 'absolute normalized'); | |
489 testGetters('', | |
490 ['.', '', '', '', '', ''], | |
491 ''); // Issue with normalize('') == '.' change. | |
492 // This corner case leaves a trailing slash for the root. | |
493 testGetters('/', | |
494 ['/', '/', '/', '/', '', '/'], | |
495 'absolute normalized'); | |
496 testGetters('.', | |
497 ['.', '.', '.', '.', '', ''], | |
498 'normalized'); | |
499 testGetters('..', | |
500 ['.', '..', '..', '..', '', ''], | |
501 'normalized'); | |
502 testGetters('/ab,;- .c.d', | |
503 ['/', 'ab,;- .c.d', 'ab,;- .c', '/ab,;- .c', '.d', '/'], | |
504 'absolute normalized'); | |
505 | |
506 // Normalized and non-normalized cases | |
507 testGetters("a/b/../c/./d/e", | |
508 ['a/b/../c/./d', 'e', 'e', 'a/b/../c/./d/e', '', ''], | |
509 ''); | |
510 testGetters("a./b../..c/.d/e", | |
511 ['a./b../..c/.d', 'e', 'e', 'a./b../..c/.d/e', '', ''], | |
512 'normalized'); | |
513 testGetters("a/b/", | |
514 ['a', 'b', 'b', 'a/b/', '', ''], | |
515 ''); | |
516 // .. is allowed at the beginning of a normalized relative path. | |
517 testGetters("../../a/b/c/d", | |
518 ['../../a/b/c', 'd', 'd', '../../a/b/c/d', '', ''], | |
519 'normalized'); | |
520 | |
521 // '.' at the end of a path is not considered an extension. | |
522 testGetters("a/b.c/.", | |
523 ['a/b.c', '.', '.', 'a/b.c/.', '', ''], | |
524 ''); | |
525 // '..' at the end of a path is not considered an extension. | |
526 testGetters("a/bc/../..", | |
527 ['a/bc/..', '..', '..', 'a/bc/../..', '', ''], | |
528 ''); | |
529 // '.foo' at the end of a path is not considered an extension. | |
530 testGetters("a/bc/../.foo", | |
531 ['a/bc/..', '.foo', '.foo', 'a/bc/../.foo', '', ''], | |
532 ''); | |
533 // '.foo.bar' at the end of a path is has extension '.bar'. | |
534 testGetters("a/bc/../.foo.bar", | |
535 ['a/bc/..', '.foo.bar', '.foo', 'a/bc/../.foo', '.bar', ''], | |
536 ''); | |
537 | |
538 // Make sure that backslashes are uninterpreted on other platforms. | |
539 testGetters(r"c:\foo\bar\fisk.hest", | |
540 ['.', r'c:\foo\bar\fisk.hest', r'c:\foo\bar\fisk', | |
541 r'c:\foo\bar\fisk', '.hest', ''], | |
542 'normalized'); | |
543 testGetters(r"/foo\bar/bif/fisk.hest", | |
544 [r'/foo\bar/bif', 'fisk.hest', 'fisk', r'/foo\bar/bif/fisk', | |
545 '.hest', '/'], | |
546 'absolute normalized'); | |
547 testGetters(r"//foo\bar///bif////fisk.hest", | |
548 [r'//foo\bar///bif', 'fisk.hest', 'fisk', | |
549 r'//foo\bar///bif////fisk', '.hest', '/'], | |
550 'absolute'); | |
551 testGetters(r"/foo\ bar/bif/gule\ fisk.hest", | |
552 [r'/foo\ bar/bif', r'gule\ fisk.hest', r'gule\ fisk', | |
553 r'/foo\ bar/bif/gule\ fisk', '.hest', '/'], | |
554 'absolute normalized'); | |
555 }); | |
432 } | 556 } |
OLD | NEW |