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 // Test the Path class in dart:io. | 5 // Test the Path class in dart:io. |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 | 8 |
| 9 void main() { | 9 void main() { |
| 10 testBaseFunctions(); | 10 testBaseFunctions(); |
| 11 testCanonicalize(); | 11 testCanonicalize(); |
| 12 testJoinAppend(); | |
| 12 } | 13 } |
| 13 | 14 |
| 14 void testBaseFunctions() { | 15 void testBaseFunctions() { |
| 15 testGetters(new Path("/foo/bar/fisk.hest"), | 16 testGetters(new Path("/foo/bar/fisk.hest"), |
| 16 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'], | 17 ['/foo/bar', 'fisk.hest', 'fisk', 'hest', '/foo/bar/fisk'], |
| 17 'absolute canonical'); | |
| 18 testGetters(new Path("/foo/bar/fisk.hest"), | |
| 19 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'], | |
| 20 'absolute canonical'); | |
| 21 testGetters(new Path("/foo/bar/fisk.hest"), | |
| 22 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'], | |
| 23 'absolute canonical'); | 18 'absolute canonical'); |
| 24 testGetters(new Path(''), | 19 testGetters(new Path(''), |
| 25 ['', '', '', ''], | 20 ['', '', '', '', ''], |
| 26 'empty'); | 21 'empty'); |
| 27 // This corner case leaves a trailing slash for the root. | 22 // This corner case leaves a trailing slash for the root. |
| 28 testGetters(new Path('/'), | 23 testGetters(new Path('/'), |
| 29 ['/', '', '', ''], | 24 ['/', '', '', '', '/'], |
| 30 'absolute directory canonical trailing'); | 25 'absolute canonical trailing'); |
| 31 testGetters(new Path('.'), | 26 testGetters(new Path('.'), |
| 32 ['', '.', '', ''], | 27 ['', '.', '.', '', '.'], |
| 33 'canonical'); | 28 'canonical'); |
| 29 testGetters(new Path('..'), | |
| 30 ['', '..', '..', '', '..'], | |
| 31 'canonical'); | |
| 34 testGetters(new Path('/ab,;- .c.d'), | 32 testGetters(new Path('/ab,;- .c.d'), |
| 35 ['/', 'ab,;- .c.d', 'ab,;- .c', 'd'], | 33 ['/', 'ab,;- .c.d', 'ab,;- .c', 'd', '/ab,;- .c'], |
| 36 'absolute directory canonical'); | 34 'absolute canonical'); |
| 37 | 35 |
| 38 // Canonical and non-canonical cases | 36 // Canonical and non-canonical cases |
| 39 testGetters(new Path("a/b/../c/./d/e"), | 37 testGetters(new Path("a/b/../c/./d/e"), |
| 40 ['a/b/../c/./d', 'e', 'e', ''], | 38 ['a/b/../c/./d', 'e', 'e', '', 'a/b/../c/./d/e'], |
| 41 ''); | 39 ''); |
| 42 testGetters(new Path("a./b../..c/.d/e"), | 40 testGetters(new Path("a./b../..c/.d/e"), |
| 43 ['a./b../..c/.d', 'e', 'e', ''], | 41 ['a./b../..c/.d', 'e', 'e', '', 'a./b../..c/.d/e'], |
| 44 'canonical'); | 42 'canonical'); |
| 45 // .. is allowed at the beginning of a canonical relative path. | 43 // .. is allowed at the beginning of a canonical relative path. |
| 46 testGetters(new Path("../../a/b/c/d/"), | 44 testGetters(new Path("../../a/b/c/d/"), |
| 47 ['../../a/b/c/d', '', '', ''], | 45 ['../../a/b/c/d', '', '', '', '../../a/b/c/d/'], |
| 48 'canonical directory trailing'); | 46 'canonical directory trailing'); |
|
Bill Hesse
2012/06/21 15:07:33
Remove 'directory'
| |
| 47 | |
| 48 // '.' at the end of a path is not considered an extension. | |
| 49 testGetters(new Path("a/b.c/."), | |
| 50 ['a/b.c', '.', '.', '', 'a/b.c/.'], | |
| 51 ''); | |
| 52 // '..' at the end of a path is not considered an extension. | |
| 53 testGetters(new Path("a/bc/../.."), | |
| 54 ['a/bc/..', '..', '..', '', 'a/bc/../..'], | |
| 55 ''); | |
| 49 | 56 |
| 50 // Test the special path cleaning operations on the Windows platform. | 57 // Test the special path cleaning operations on the Windows platform. |
| 51 if (Platform.operatingSystem == 'windows') { | 58 if (Platform.operatingSystem == 'windows') { |
| 52 testGetters(new Path.fromNative(@"c:\foo\bar\fisk.hest"), | 59 testGetters(new Path.fromNative(@"c:\foo\bar\fisk.hest"), |
| 53 ['/c:/foo/bar', 'fisk.hest', 'fisk', 'hest'], | 60 ['/c:/foo/bar', 'fisk.hest', 'fisk', 'hest', |
| 61 '/c:/foo/bar/fisk'], | |
| 54 'absolute canonical'); | 62 'absolute canonical'); |
| 55 testGetters(new Path.fromNative("\\foo\\bar\\"), | 63 testGetters(new Path.fromNative("\\foo\\bar\\"), |
| 56 ['/foo/bar', '', '', ''], | 64 ['/foo/bar', '', '', '', '/foo/bar/'], |
| 57 'absolute directory canonical trailing'); | 65 'absolute directory canonical trailing'); |
|
Bill Hesse
2012/06/21 15:07:33
remove directory
| |
| 58 testGetters(new Path.fromNative("\\foo\\bar\\hest"), | 66 testGetters(new Path.fromNative("\\foo\\bar\\hest"), |
| 59 ['/foo/bar', 'hest', 'hest', ''], | 67 ['/foo/bar', 'hest', 'hest', '', '/foo/bar/hest'], |
| 60 'absolute canonical'); | 68 'absolute canonical'); |
| 61 testGetters(new Path.fromNative(@"foo/bar\hest/.fisk"), | 69 testGetters(new Path.fromNative(@"foo/bar\hest/.fisk"), |
| 62 ['foo/bar/hest', '.fisk', '', 'fisk'], | 70 ['foo/bar/hest', '.fisk', '', 'fisk', 'foo/bar/hest/'], |
| 63 'canonical'); | 71 'canonical'); |
| 64 testGetters(new Path.fromNative(@"foo//bar\\hest/\/.fisk"), | 72 testGetters(new Path.fromNative(@"foo//bar\\hest/\/.fisk."), |
| 65 ['foo//bar//hest', '.fisk', '', 'fisk'], | 73 ['foo//bar//hest', '.fisk.', '.fisk', '', |
| 74 'foo//bar//hest///.fisk'], | |
| 66 ''); | 75 ''); |
| 67 } else { | 76 } else { |
| 68 // Make sure that backslashes are uninterpreted on other platforms. | 77 // Make sure that backslashes are uninterpreted on other platforms. |
| 69 testGetters(new Path.fromNative(@"/foo\bar/bif/fisk.hest"), | 78 testGetters(new Path.fromNative(@"/foo\bar/bif/fisk.hest"), |
| 70 [@'/foo\bar/bif', 'fisk.hest', 'fisk', 'hest'], | 79 [@'/foo\bar/bif', 'fisk.hest', 'fisk', 'hest', |
| 80 @'/foo\bar/bif/fisk'], | |
| 71 'absolute canonical'); | 81 'absolute canonical'); |
| 72 testGetters(new Path.fromNative(@"//foo\bar///bif////fisk.hest"), | 82 testGetters(new Path.fromNative(@"//foo\bar///bif////fisk.hest"), |
| 73 [@'//foo\bar///bif', 'fisk.hest', 'fisk', 'hest'], | 83 [@'//foo\bar///bif', 'fisk.hest', 'fisk', 'hest', |
| 84 @'//foo\bar///bif////fisk'], | |
| 74 'absolute'); | 85 'absolute'); |
| 75 testGetters(new Path.fromNative(@"/foo\ bar/bif/gule\ fisk.hest"), | 86 testGetters(new Path.fromNative(@"/foo\ bar/bif/gule\ fisk.hest"), |
| 76 [@'/foo\ bar/bif', @'gule\ fisk.hest', @'gule\ fisk', 'hest'], | 87 [@'/foo\ bar/bif', @'gule\ fisk.hest', @'gule\ fisk', 'hest', |
| 88 @'/foo\ bar/bif/gule\ fisk'], | |
| 77 'absolute canonical'); | 89 'absolute canonical'); |
| 78 } | 90 } |
| 79 } | 91 } |
| 80 | 92 |
| 81 | 93 |
| 82 void testGetters(Path path, List components, String properties) { | 94 void testGetters(Path path, List components, String properties) { |
| 83 final int DIRNAME = 0; | 95 final int DIRNAME = 0; |
| 84 final int FILENAME = 1; | 96 final int FILENAME = 1; |
| 85 final int BASENAME = 2; | 97 final int FILENAME_NO_EXTENSION = 2; |
| 86 final int EXTENSION = 3; | 98 final int EXTENSION = 3; |
| 99 final int PATH_NO_EXTENSION = 4; | |
| 87 Expect.equals(components[DIRNAME], path.directoryPath.toString()); | 100 Expect.equals(components[DIRNAME], path.directoryPath.toString()); |
| 88 Expect.equals(components[FILENAME], path.filename); | 101 Expect.equals(components[FILENAME], path.filename); |
| 89 Expect.equals(components[BASENAME], path.filenameWithoutExtension); | 102 Expect.equals(components[FILENAME_NO_EXTENSION], |
| 103 path.filenameWithoutExtension); | |
| 90 Expect.equals(components[EXTENSION], path.extension); | 104 Expect.equals(components[EXTENSION], path.extension); |
| 105 Expect.equals(components[PATH_NO_EXTENSION], | |
| 106 path.pathWithoutExtension.toString()); | |
| 91 | 107 |
| 92 Expect.equals(path.isCanonical, properties.contains('canonical')); | 108 Expect.equals(path.isCanonical, properties.contains('canonical')); |
| 93 Expect.equals(path.isAbsolute, properties.contains('absolute')); | 109 Expect.equals(path.isAbsolute, properties.contains('absolute')); |
| 94 Expect.equals(path.hasTrailingSeparator, properties.contains('trailing')); | 110 Expect.equals(path.hasTrailingSeparator, properties.contains('trailing')); |
| 95 } | 111 } |
| 96 | 112 |
| 97 void testCanonicalize() { | 113 void testCanonicalize() { |
| 98 Function t = (input, canonicalized) { | 114 Function t = (input, canonicalized) { |
| 99 Expect.equals(canonicalized, new Path(input).canonicalize().toString()); | 115 Expect.equals(canonicalized, new Path(input).canonicalize().toString()); |
| 100 }; | 116 }; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 115 t('', '.'); | 131 t('', '.'); |
| 116 t('/', '/'); | 132 t('/', '/'); |
| 117 t('../foo/bar/..', '../foo'); | 133 t('../foo/bar/..', '../foo'); |
| 118 t('/../foo/bar/..', '/foo'); | 134 t('/../foo/bar/..', '/foo'); |
| 119 t('foo/bar/../../../joe/../..', '../..'); | 135 t('foo/bar/../../../joe/../..', '../..'); |
| 120 t('a/b/c/../../..d/./.e/f././', 'a/..d/.e/f./'); | 136 t('a/b/c/../../..d/./.e/f././', 'a/..d/.e/f./'); |
| 121 t('/%:/foo/../..', '/%:/'); | 137 t('/%:/foo/../..', '/%:/'); |
| 122 t('c:/foo/../../..', '..'); | 138 t('c:/foo/../../..', '..'); |
| 123 t('c:/foo/../../bad/dad/./..', 'bad'); | 139 t('c:/foo/../../bad/dad/./..', 'bad'); |
| 124 } | 140 } |
| 141 | |
| 142 void testJoinAppend() { | |
| 143 void testJoin(String a, String b, String c) { | |
| 144 Expect.equals(new Path(a).join(new Path(b)).toString(), c); | |
| 145 } | |
| 146 | |
| 147 testJoin('/a/b', 'c/d', '/a/b/c/d'); | |
| 148 testJoin('a/', 'b/c/', 'a/b/c/'); | |
| 149 testJoin('a/b/./c/..//', 'd/.././..//e/f//', 'a/e/f/'); | |
| 150 testJoin('a/b', 'c/../../../..', '..'); | |
| 151 testJoin('a/b', 'c/../../../', '.'); | |
| 152 testJoin('/a/b', 'c/../../../..', '/'); | |
| 153 testJoin('/a/b', 'c/../../../', '/'); | |
| 154 testJoin('a/b', 'c/../../../../../', '../../'); | |
| 155 testJoin('a/b/c', '../../d', 'a/d'); | |
| 156 testJoin('/a/b/c', '../../d', '/a/d'); | |
| 157 testJoin('/a/b/c', '../../d/', '/a/d/'); | |
| 158 testJoin('a/b/c', '../../d/', 'a/d/'); | |
| 159 | |
| 160 void testAppend(String a, String b, String c) { | |
| 161 Expect.equals(new Path(a).append(b).toString(), c); | |
| 162 } | |
| 163 | |
| 164 testAppendConcat('/a/b', 'c', '/a/b/c'); | |
|
Bill Hesse
2012/06/21 15:07:33
Remove "Concat"
| |
| 165 testAppendConcat('a/b/', 'cd', 'a/b/cd'); | |
| 166 testAppendConcat('.', '..', './..'); | |
| 167 testAppendConcat('a/b', '/c/d', 'a/b//c/d'); | |
| 168 | |
| 169 Expect.throws(() => new Path('/a/b/').join(new Path('/c/d'))); | |
| 170 } | |
| OLD | NEW |