Chromium Code Reviews| 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 import 'package:unittest/unittest.dart'; | 5 import 'package:unittest/unittest.dart'; |
| 6 import 'package:path/path.dart' as path; | 6 import 'package:path/path.dart' as path; |
| 7 | 7 |
|
Bob Nystrom
2013/07/18 20:45:08
Ditto comments on the POSIX tests here.
| |
| 8 main() { | 8 main() { |
| 9 var builder = new path.Builder(style: path.Style.url, | 9 var builder = new path.Builder(style: path.Style.url, |
| 10 root: 'http://dartlang.org/root/path'); | 10 root: 'http://dartlang.org/root/path'); |
| 11 | 11 |
| 12 test('separator', () { | 12 test('separator', () { |
| 13 expect(builder.separator, '/'); | 13 expect(builder.separator, '/'); |
| 14 }); | 14 }); |
| 15 | 15 |
| 16 test('extension', () { | 16 test('extension', () { |
| 17 expect(builder.extension(''), ''); | 17 expect(builder.extension(''), ''); |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 expect(builder.join('', ''), ''); | 228 expect(builder.join('', ''), ''); |
| 229 expect(builder.join('', 'a'), 'a'); | 229 expect(builder.join('', 'a'), 'a'); |
| 230 expect(builder.join('a', '', 'b', '', '', '', 'c'), 'a/b/c'); | 230 expect(builder.join('a', '', 'b', '', '', '', 'c'), 'a/b/c'); |
| 231 expect(builder.join('a', 'b', ''), 'a/b'); | 231 expect(builder.join('a', 'b', ''), 'a/b'); |
| 232 }); | 232 }); |
| 233 | 233 |
| 234 test('disallows intermediate nulls', () { | 234 test('disallows intermediate nulls', () { |
| 235 expect(() => builder.join('a', null, 'b'), throwsArgumentError); | 235 expect(() => builder.join('a', null, 'b'), throwsArgumentError); |
| 236 expect(() => builder.join(null, 'a'), throwsArgumentError); | 236 expect(() => builder.join(null, 'a'), throwsArgumentError); |
| 237 }); | 237 }); |
| 238 | |
| 239 test('misc join tests ported from Path class', () { | |
| 240 expect(builder.join('a/', 'b/c/'), 'a/b/c/'); | |
| 241 expect(builder.join('a/b/./c/..//', 'd/.././..//e/f//'), | |
| 242 'a/b/./c/..//d/.././..//e/f//'); | |
| 243 expect(builder.join('a/b', 'c/../../../..'), 'a/b/c/../../../..'); | |
| 244 expect(builder.join('', ''), ''); | |
| 245 expect(builder.join('a/b/c/', '/d'), '/d'); | |
| 246 expect(builder.join('a', 'b${builder.separator}'), 'a/b/'); | |
| 247 }); | |
| 238 }); | 248 }); |
| 239 | 249 |
| 240 group('joinAll', () { | 250 group('joinAll', () { |
| 241 test('allows more than eight parts', () { | 251 test('allows more than eight parts', () { |
| 242 expect(builder.joinAll(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']), | 252 expect(builder.joinAll(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']), |
| 243 'a/b/c/d/e/f/g/h/i'); | 253 'a/b/c/d/e/f/g/h/i'); |
| 244 }); | 254 }); |
| 245 | 255 |
| 246 test('ignores parts before an absolute path', () { | 256 test('ignores parts before an absolute path', () { |
| 247 expect(builder.joinAll(['a', 'http://dartlang.org', 'b', 'c']), | 257 expect(builder.joinAll(['a', 'http://dartlang.org', 'b', 'c']), |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 expect(builder.split('http://dartlang.org'), | 308 expect(builder.split('http://dartlang.org'), |
| 299 equals(['http://dartlang.org'])); | 309 equals(['http://dartlang.org'])); |
| 300 expect(builder.split('file:///'), equals(['file://'])); | 310 expect(builder.split('file:///'), equals(['file://'])); |
| 301 expect(builder.split('file://'), equals(['file://'])); | 311 expect(builder.split('file://'), equals(['file://'])); |
| 302 expect(builder.split('/'), equals(['/'])); | 312 expect(builder.split('/'), equals(['/'])); |
| 303 }); | 313 }); |
| 304 }); | 314 }); |
| 305 | 315 |
| 306 group('normalize', () { | 316 group('normalize', () { |
| 307 test('simple cases', () { | 317 test('simple cases', () { |
| 308 expect(builder.normalize(''), ''); | 318 expect(builder.normalize(''), '.'); |
| 309 expect(builder.normalize('.'), '.'); | 319 expect(builder.normalize('.'), '.'); |
| 310 expect(builder.normalize('..'), '..'); | 320 expect(builder.normalize('..'), '..'); |
| 311 expect(builder.normalize('a'), 'a'); | 321 expect(builder.normalize('a'), 'a'); |
| 312 expect(builder.normalize('http://dartlang.org/'), 'http://dartlang.org'); | 322 expect(builder.normalize('http://dartlang.org/'), 'http://dartlang.org'); |
| 313 expect(builder.normalize('http://dartlang.org'), 'http://dartlang.org'); | 323 expect(builder.normalize('http://dartlang.org'), 'http://dartlang.org'); |
| 314 expect(builder.normalize('file://'), 'file://'); | 324 expect(builder.normalize('file://'), 'file://'); |
| 315 expect(builder.normalize('file:///'), 'file://'); | 325 expect(builder.normalize('file:///'), 'file://'); |
| 316 expect(builder.normalize('/'), '/'); | 326 expect(builder.normalize('/'), '/'); |
| 317 expect(builder.normalize(r'\'), r'\'); | 327 expect(builder.normalize(r'\'), r'\'); |
| 328 expect(builder.normalize('C:/'), 'C:'); | |
| 329 expect(builder.normalize(r'C:\'), r'C:\'); | |
| 330 expect(builder.normalize(r'\\'), r'\\'); | |
| 318 }); | 331 }); |
| 319 | 332 |
| 320 test('collapses redundant separators', () { | 333 test('collapses redundant separators', () { |
| 321 expect(builder.normalize(r'a/b/c'), r'a/b/c'); | 334 expect(builder.normalize(r'a/b/c'), r'a/b/c'); |
| 322 expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d'); | 335 expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d'); |
| 323 }); | 336 }); |
| 324 | 337 |
| 325 test('does not collapse separators for other platform', () { | 338 test('does not collapse separators for other platform', () { |
| 326 expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c'); | 339 expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c'); |
| 327 }); | 340 }); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 353 expect(builder.normalize('file:///..'), 'file://'); | 366 expect(builder.normalize('file:///..'), 'file://'); |
| 354 expect(builder.normalize('/..'), '/'); | 367 expect(builder.normalize('/..'), '/'); |
| 355 expect(builder.normalize('http://dartlang.org/../../..'), | 368 expect(builder.normalize('http://dartlang.org/../../..'), |
| 356 'http://dartlang.org'); | 369 'http://dartlang.org'); |
| 357 expect(builder.normalize('file:///../../..'), 'file://'); | 370 expect(builder.normalize('file:///../../..'), 'file://'); |
| 358 expect(builder.normalize('/../../..'), '/'); | 371 expect(builder.normalize('/../../..'), '/'); |
| 359 expect(builder.normalize('http://dartlang.org/../../../a'), | 372 expect(builder.normalize('http://dartlang.org/../../../a'), |
| 360 'http://dartlang.org/a'); | 373 'http://dartlang.org/a'); |
| 361 expect(builder.normalize('file:///../../../a'), 'file:///a'); | 374 expect(builder.normalize('file:///../../../a'), 'file:///a'); |
| 362 expect(builder.normalize('/../../../a'), '/a'); | 375 expect(builder.normalize('/../../../a'), '/a'); |
| 376 expect(builder.normalize('c:/..'), '.'); | |
| 377 expect(builder.normalize('A:/../../..'), '../..'); | |
| 363 expect(builder.normalize('a/..'), '.'); | 378 expect(builder.normalize('a/..'), '.'); |
| 364 expect(builder.normalize('a/b/..'), 'a'); | 379 expect(builder.normalize('a/b/..'), 'a'); |
| 365 expect(builder.normalize('a/../b'), 'b'); | 380 expect(builder.normalize('a/../b'), 'b'); |
| 366 expect(builder.normalize('a/./../b'), 'b'); | 381 expect(builder.normalize('a/./../b'), 'b'); |
| 367 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | 382 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); |
| 368 expect(builder.normalize('a/b/../../../../c'), '../../c'); | 383 expect(builder.normalize('a/b/../../../../c'), '../../c'); |
| 384 expect(builder.normalize('z/a/b/../../..\../c'), 'z/..\../c'); | |
| 385 expect(builder.normalize('a/b\c/../d'), 'a/d'); | |
| 369 }); | 386 }); |
| 370 | 387 |
| 371 test('does not walk before root on absolute paths', () { | 388 test('does not walk before root on absolute paths', () { |
| 372 expect(builder.normalize('..'), '..'); | 389 expect(builder.normalize('..'), '..'); |
| 373 expect(builder.normalize('../'), '..'); | 390 expect(builder.normalize('../'), '..'); |
| 374 expect(builder.normalize('http://dartlang.org/..'), | 391 expect(builder.normalize('http://dartlang.org/..'), |
| 375 'http://dartlang.org'); | 392 'http://dartlang.org'); |
| 393 expect(builder.normalize('http://dartlang.org/../a'), | |
| 394 'http://dartlang.org/a'); | |
| 376 expect(builder.normalize('file:///..'), 'file://'); | 395 expect(builder.normalize('file:///..'), 'file://'); |
| 396 expect(builder.normalize('file:///../a'), 'file:///a'); | |
| 377 expect(builder.normalize('/..'), '/'); | 397 expect(builder.normalize('/..'), '/'); |
| 378 expect(builder.normalize('a/..'), '.'); | 398 expect(builder.normalize('a/..'), '.'); |
| 399 expect(builder.normalize('../a'), '../a'); | |
| 400 expect(builder.normalize('/../a'), '/a'); | |
| 401 expect(builder.normalize('c:/../a'), 'a'); | |
| 402 expect(builder.normalize('/../a'), '/a'); | |
| 379 expect(builder.normalize('a/b/..'), 'a'); | 403 expect(builder.normalize('a/b/..'), 'a'); |
| 404 expect(builder.normalize('../a/b/..'), '../a'); | |
| 380 expect(builder.normalize('a/../b'), 'b'); | 405 expect(builder.normalize('a/../b'), 'b'); |
| 381 expect(builder.normalize('a/./../b'), 'b'); | 406 expect(builder.normalize('a/./../b'), 'b'); |
| 382 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); | 407 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d'); |
| 383 expect(builder.normalize('a/b/../../../../c'), '../../c'); | 408 expect(builder.normalize('a/b/../../../../c'), '../../c'); |
| 409 expect(builder.normalize('a/b/c/../../..d/./.e/f././'), 'a/..d/.e/f.'); | |
| 384 }); | 410 }); |
| 385 | 411 |
| 386 test('removes trailing separators', () { | 412 test('removes trailing separators', () { |
| 387 expect(builder.normalize('./'), '.'); | 413 expect(builder.normalize('./'), '.'); |
| 388 expect(builder.normalize('.//'), '.'); | 414 expect(builder.normalize('.//'), '.'); |
| 389 expect(builder.normalize('a/'), 'a'); | 415 expect(builder.normalize('a/'), 'a'); |
| 390 expect(builder.normalize('a/b/'), 'a/b'); | 416 expect(builder.normalize('a/b/'), 'a/b'); |
| 417 expect(builder.normalize(r'a/b\'), r'a/b\'); | |
| 391 expect(builder.normalize('a/b///'), 'a/b'); | 418 expect(builder.normalize('a/b///'), 'a/b'); |
| 392 }); | 419 }); |
| 393 }); | 420 }); |
| 394 | 421 |
| 395 group('relative', () { | 422 group('relative', () { |
| 396 group('from absolute root', () { | 423 group('from absolute root', () { |
| 397 test('given absolute path in root', () { | 424 test('given absolute path in root', () { |
| 398 expect(builder.relative('http://dartlang.org'), '../..'); | 425 expect(builder.relative('http://dartlang.org'), '../..'); |
| 399 expect(builder.relative('http://dartlang.org/'), '../..'); | 426 expect(builder.relative('http://dartlang.org/'), '../..'); |
| 400 expect(builder.relative('/'), '../..'); | 427 expect(builder.relative('/'), '../..'); |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 651 expect(builder.toUri('http://dartlang.org/path/to/foo'), | 678 expect(builder.toUri('http://dartlang.org/path/to/foo'), |
| 652 Uri.parse('http://dartlang.org/path/to/foo')); | 679 Uri.parse('http://dartlang.org/path/to/foo')); |
| 653 expect(builder.toUri('http://dartlang.org/path/to/foo/'), | 680 expect(builder.toUri('http://dartlang.org/path/to/foo/'), |
| 654 Uri.parse('http://dartlang.org/path/to/foo/')); | 681 Uri.parse('http://dartlang.org/path/to/foo/')); |
| 655 expect(builder.toUri('file:///path/to/foo'), | 682 expect(builder.toUri('file:///path/to/foo'), |
| 656 Uri.parse('file:///path/to/foo')); | 683 Uri.parse('file:///path/to/foo')); |
| 657 expect(builder.toUri('foo/bar'), Uri.parse('foo/bar')); | 684 expect(builder.toUri('foo/bar'), Uri.parse('foo/bar')); |
| 658 expect(builder.toUri('http://dartlang.org/path/to/foo%23bar'), | 685 expect(builder.toUri('http://dartlang.org/path/to/foo%23bar'), |
| 659 Uri.parse('http://dartlang.org/path/to/foo%23bar')); | 686 Uri.parse('http://dartlang.org/path/to/foo%23bar')); |
| 660 }); | 687 }); |
| 688 | |
| 689 | |
| 690 void testGetters(String p, List components, String properties) { | |
| 691 final int DIRNAME = 0; | |
| 692 final int FILENAME = 1; | |
| 693 final int FILENAME_NO_EXTENSION = 2; | |
| 694 final int WITHOUT_EXTENSION = 3; | |
| 695 final int EXTENSION = 4; | |
| 696 final int ROOT_PREFIX = 5; | |
| 697 expect(components[DIRNAME], builder.dirname(p)); | |
| 698 expect(components[FILENAME], builder.basename(p)); | |
| 699 expect(components[FILENAME_NO_EXTENSION], | |
| 700 builder.basenameWithoutExtension(p)); | |
| 701 expect(components[WITHOUT_EXTENSION], | |
| 702 builder.withoutExtension(p)); | |
| 703 expect(components[EXTENSION], builder.extension(p)); | |
| 704 expect(builder.isAbsolute(p), properties.contains('absolute')); | |
| 705 expect(components[ROOT_PREFIX], builder.rootPrefix(p)); | |
| 706 if (properties.contains('normalized')) { | |
| 707 expect(p, builder.normalize(p)); | |
| 708 } else { | |
| 709 expect(p, isNot(equals(builder.normalize(p)))); | |
| 710 } | |
| 711 } | |
| 712 | |
| 713 test('getters', () { | |
| 714 testGetters("/foo/bar/fisk.hest", | |
| 715 ['/foo/bar', 'fisk.hest', 'fisk', '/foo/bar/fisk', | |
| 716 '.hest', '/'], | |
| 717 'absolute normalized'); | |
| 718 testGetters('', | |
| 719 ['.', '', '', '', '', ''], | |
| 720 ''); // Issue with normalize('') == '.' change. | |
| 721 // This corner case leaves a trailing slash for the root. | |
| 722 testGetters('/', | |
| 723 ['/', '/', '/', '/', '', '/'], | |
| 724 'absolute normalized'); | |
| 725 testGetters('.', | |
| 726 ['.', '.', '.', '.', '', ''], | |
| 727 'normalized'); | |
| 728 testGetters('..', | |
| 729 ['.', '..', '..', '..', '', ''], | |
| 730 'normalized'); | |
| 731 testGetters('/ab,;- .c.d', | |
| 732 ['/', 'ab,;- .c.d', 'ab,;- .c', '/ab,;- .c', '.d', '/'], | |
| 733 'absolute normalized'); | |
| 734 | |
| 735 // Normalized and non-normalized cases | |
| 736 testGetters("a/b/../c/./d/e", | |
| 737 ['a/b/../c/./d', 'e', 'e', 'a/b/../c/./d/e', '', ''], | |
| 738 ''); | |
| 739 testGetters("a./b../..c/.d/e", | |
| 740 ['a./b../..c/.d', 'e', 'e', 'a./b../..c/.d/e', '', ''], | |
| 741 'normalized'); | |
| 742 testGetters("a/b/", | |
| 743 ['a', 'b', 'b', 'a/b/', '', ''], | |
| 744 ''); | |
| 745 // .. is allowed at the beginning of a normalized relative path. | |
| 746 testGetters("../../a/b/c/d", | |
| 747 ['../../a/b/c', 'd', 'd', '../../a/b/c/d', '', ''], | |
| 748 'normalized'); | |
| 749 | |
| 750 // '.' at the end of a path is not considered an extension. | |
| 751 testGetters("a/b.c/.", | |
| 752 ['a/b.c', '.', '.', 'a/b.c/.', '', ''], | |
| 753 ''); | |
| 754 // '..' at the end of a path is not considered an extension. | |
| 755 testGetters("a/bc/../..", | |
| 756 ['a/bc/..', '..', '..', 'a/bc/../..', '', ''], | |
| 757 ''); | |
| 758 // '.foo' at the end of a path is not considered an extension. | |
| 759 testGetters("a/bc/../.foo", | |
| 760 ['a/bc/..', '.foo', '.foo', 'a/bc/../.foo', '', ''], | |
| 761 ''); | |
| 762 // '.foo.bar' at the end of a path is has extension '.bar'. | |
| 763 testGetters("a/bc/../.foo.bar", | |
| 764 ['a/bc/..', '.foo.bar', '.foo', 'a/bc/../.foo', '.bar', ''], | |
| 765 ''); | |
| 766 | |
| 767 // Make sure that backslashes are uninterpreted on other platforms. | |
| 768 testGetters(r"c:\foo\bar\fisk.hest", | |
| 769 ['.', r'c:\foo\bar\fisk.hest', r'c:\foo\bar\fisk', | |
| 770 r'c:\foo\bar\fisk', '.hest', ''], | |
| 771 'normalized'); | |
| 772 testGetters(r"/foo\bar/bif/fisk.hest", | |
| 773 [r'/foo\bar/bif', 'fisk.hest', 'fisk', r'/foo\bar/bif/fisk', | |
| 774 '.hest', '/'], | |
| 775 'absolute normalized'); | |
| 776 testGetters(r"//foo\bar///bif////fisk.hest", | |
| 777 [r'//foo\bar///bif', 'fisk.hest', 'fisk', | |
| 778 r'//foo\bar///bif////fisk', '.hest', '/'], | |
| 779 'absolute'); | |
| 780 testGetters(r"/foo\ bar/bif/gule\ fisk.hest", | |
| 781 [r'/foo\ bar/bif', r'gule\ fisk.hest', r'gule\ fisk', | |
| 782 r'/foo\ bar/bif/gule\ fisk', '.hest', '/'], | |
| 783 'absolute normalized'); | |
| 784 }); | |
| 661 } | 785 } |
| OLD | NEW |