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 testRaw(); |
11 testCanonicalize(); | 12 testCanonicalize(); |
12 testJoinAppend(); | 13 testJoinAppend(); |
13 testRelativeTo(); | 14 testRelativeTo(); |
14 testWindowsShare(); | 15 testWindowsShare(); |
15 } | 16 } |
16 | 17 |
17 void testBaseFunctions() { | 18 void testBaseFunctions() { |
18 testGetters(new Path("/foo/bar/fisk.hest"), | 19 testGetters(new Path("/foo/bar/fisk.hest"), |
19 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'], | 20 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'], |
20 'absolute canonical'); | 21 'absolute canonical'); |
(...skipping 30 matching lines...) Expand all Loading... |
51 testGetters(new Path("a/b.c/."), | 52 testGetters(new Path("a/b.c/."), |
52 ['a/b.c', '.', '.', ''], | 53 ['a/b.c', '.', '.', ''], |
53 ''); | 54 ''); |
54 // '..' at the end of a path is not considered an extension. | 55 // '..' at the end of a path is not considered an extension. |
55 testGetters(new Path("a/bc/../.."), | 56 testGetters(new Path("a/bc/../.."), |
56 ['a/bc/..', '..', '..', ''], | 57 ['a/bc/..', '..', '..', ''], |
57 ''); | 58 ''); |
58 | 59 |
59 // Test the special path cleaning operations on the Windows platform. | 60 // Test the special path cleaning operations on the Windows platform. |
60 if (Platform.operatingSystem == 'windows') { | 61 if (Platform.operatingSystem == 'windows') { |
61 testGetters(new Path.fromNative(r"c:\foo\bar\fisk.hest"), | 62 testGetters(new Path(r"c:\foo\bar\fisk.hest"), |
62 ['/c:/foo/bar', 'fisk.hest', 'fisk', 'hest'], | 63 ['/c:/foo/bar', 'fisk.hest', 'fisk', 'hest'], |
63 'absolute canonical'); | 64 'absolute canonical'); |
64 testGetters(new Path.fromNative("\\foo\\bar\\"), | 65 testGetters(new Path("\\foo\\bar\\"), |
65 ['/foo/bar', '', '', ''], | 66 ['/foo/bar', '', '', ''], |
66 'absolute canonical trailing'); | 67 'absolute canonical trailing'); |
67 testGetters(new Path.fromNative("\\foo\\bar\\hest"), | 68 testGetters(new Path("\\foo\\bar\\hest"), |
68 ['/foo/bar', 'hest', 'hest', ''], | 69 ['/foo/bar', 'hest', 'hest', ''], |
69 'absolute canonical'); | 70 'absolute canonical'); |
70 testGetters(new Path.fromNative(r"foo/bar\hest/.fisk"), | 71 testGetters(new Path(r"foo/bar\hest/.fisk"), |
71 ['foo/bar/hest', '.fisk', '', 'fisk'], | 72 ['foo/bar/hest', '.fisk', '', 'fisk'], |
72 'canonical'); | 73 'canonical'); |
73 testGetters(new Path.fromNative(r"foo//bar\\hest/\/.fisk."), | 74 testGetters(new Path(r"foo//bar\\hest/\/.fisk."), |
74 ['foo//bar//hest', '.fisk.', '.fisk', ''], | 75 ['foo//bar//hest', '.fisk.', '.fisk', ''], |
75 ''); | 76 ''); |
76 } else { | 77 } else { |
77 // Make sure that backslashes are uninterpreted on other platforms. | 78 // Make sure that backslashes are uninterpreted on other platforms. |
78 testGetters(new Path.fromNative(r"/foo\bar/bif/fisk.hest"), | 79 testGetters(new Path(r"c:\foo\bar\fisk.hest"), |
| 80 ['', r'c:\foo\bar\fisk.hest', r'c:\foo\bar\fisk', 'hest'], |
| 81 'canonical'); |
| 82 testGetters(new Path(r"/foo\bar/bif/fisk.hest"), |
79 [r'/foo\bar/bif', 'fisk.hest', 'fisk', 'hest'], | 83 [r'/foo\bar/bif', 'fisk.hest', 'fisk', 'hest'], |
80 'absolute canonical'); | 84 'absolute canonical'); |
81 testGetters(new Path.fromNative(r"//foo\bar///bif////fisk.hest"), | 85 testGetters(new Path(r"//foo\bar///bif////fisk.hest"), |
82 [r'//foo\bar///bif', 'fisk.hest', 'fisk', 'hest'], | 86 [r'//foo\bar///bif', 'fisk.hest', 'fisk', 'hest'], |
83 'absolute'); | 87 'absolute'); |
84 testGetters(new Path.fromNative(r"/foo\ bar/bif/gule\ fisk.hest"), | 88 testGetters(new Path(r"/foo\ bar/bif/gule\ fisk.hest"), |
85 [r'/foo\ bar/bif', r'gule\ fisk.hest', r'gule\ fisk', 'hest'], | 89 [r'/foo\ bar/bif', r'gule\ fisk.hest', r'gule\ fisk', 'hest'], |
86 'absolute canonical'); | 90 'absolute canonical'); |
87 } | 91 } |
88 } | 92 } |
89 | 93 |
90 | 94 |
91 void testGetters(Path path, List components, String properties) { | 95 void testGetters(Path path, List components, String properties) { |
92 final int DIRNAME = 0; | 96 final int DIRNAME = 0; |
93 final int FILENAME = 1; | 97 final int FILENAME = 1; |
94 final int FILENAME_NO_EXTENSION = 2; | 98 final int FILENAME_NO_EXTENSION = 2; |
95 final int EXTENSION = 3; | 99 final int EXTENSION = 3; |
96 Expect.equals(components[DIRNAME], path.directoryPath.toString()); | 100 Expect.equals(components[DIRNAME], path.directoryPath.toString()); |
97 Expect.equals(components[FILENAME], path.filename); | 101 Expect.equals(components[FILENAME], path.filename); |
98 Expect.equals(components[FILENAME_NO_EXTENSION], | 102 Expect.equals(components[FILENAME_NO_EXTENSION], |
99 path.filenameWithoutExtension); | 103 path.filenameWithoutExtension); |
100 Expect.equals(components[EXTENSION], path.extension); | 104 Expect.equals(components[EXTENSION], path.extension); |
101 | 105 |
102 Expect.equals(path.isCanonical, properties.contains('canonical')); | 106 Expect.equals(path.isCanonical, properties.contains('canonical')); |
103 Expect.equals(path.isAbsolute, properties.contains('absolute')); | 107 Expect.equals(path.isAbsolute, properties.contains('absolute')); |
104 Expect.equals(path.hasTrailingSeparator, properties.contains('trailing')); | 108 Expect.equals(path.hasTrailingSeparator, properties.contains('trailing')); |
105 } | 109 } |
106 | 110 |
| 111 void testRaw() { |
| 112 Expect.equals(new Path.raw('c:\\foo/bar bad').toString(), 'c:\\foo/bar bad'); |
| 113 Expect.equals(new Path.raw('').toString(), ''); |
| 114 Expect.equals(new Path.raw('\\bar\u2603\n.').toString(), '\\bar\u2603\n.'); |
| 115 } |
| 116 |
107 void testCanonicalize() { | 117 void testCanonicalize() { |
108 Function t = (input, canonicalized) { | 118 Function t = (input, canonicalized) { |
109 Expect.equals(canonicalized, new Path(input).canonicalize().toString()); | 119 Expect.equals(canonicalized, new Path(input).canonicalize().toString()); |
110 }; | 120 }; |
111 | 121 |
112 t('.', '.'); | 122 t('.', '.'); |
113 t('./.', '.'); | 123 t('./.', '.'); |
114 t('foo/..', '.'); | 124 t('foo/..', '.'); |
115 t('../foo', '../foo'); | 125 t('../foo', '../foo'); |
116 t('/../foo', '/foo'); | 126 t('/../foo', '/foo'); |
117 t('/foo/..', '/'); | 127 t('/foo/..', '/'); |
118 t('/foo/../', '/'); | 128 t('/foo/../', '/'); |
119 t('/c:/../foo', '/c:/foo'); | 129 t('/c:/../foo', '/c:/foo'); |
120 t('/c:/foo/..', '/c:/'); | 130 t('/c:/foo/..', '/c:/'); |
121 t('/c:/foo/../', '/c:/'); | 131 t('/c:/foo/../', '/c:/'); |
122 t('/c:/foo/../..', '/c:/'); | 132 t('/c:/foo/../..', '/c:/'); |
123 t('/c:/foo/../../', '/c:/'); | 133 t('/c:/foo/../../', '/c:/'); |
124 t('..', '..'); | 134 t('..', '..'); |
125 t('', '.'); | 135 t('', '.'); |
126 t('/', '/'); | 136 t('/', '/'); |
127 t('../foo/bar/..', '../foo'); | 137 t('../foo/bar/..', '../foo'); |
128 t('/../foo/bar/..', '/foo'); | 138 t('/../foo/bar/..', '/foo'); |
129 t('foo/bar/../../../joe/../..', '../..'); | 139 t('foo/bar/../../../joe/../..', '../..'); |
130 t('a/b/c/../../..d/./.e/f././', 'a/..d/.e/f./'); | 140 t('a/b/c/../../..d/./.e/f././', 'a/..d/.e/f./'); |
131 t('/%:/foo/../..', '/%:/'); | 141 t('/%:/foo/../..', '/%:/'); |
132 t('c:/foo/../../..', '..'); | 142 if (Platform.operatingSystem == 'windows') { |
133 t('c:/foo/../../bad/dad/./..', 'bad'); | 143 t('c:/foo/../../..', '/c:/'); |
| 144 t('c:/foo/../../bad/dad/./..', '/c:/bad'); |
| 145 } else { |
| 146 t('c:/foo/../../..', '..'); |
| 147 t('c:/foo/../../bad/dad/./..', 'bad'); |
| 148 } |
134 } | 149 } |
135 | 150 |
136 void testJoinAppend() { | 151 void testJoinAppend() { |
137 void testJoin(String a, String b, String c) { | 152 void testJoin(String a, String b, String c) { |
138 Expect.equals(new Path(a).join(new Path(b)).toString(), c); | 153 Expect.equals(new Path(a).join(new Path(b)).toString(), c); |
139 } | 154 } |
140 | 155 |
141 testJoin('/a/b', 'c/d', '/a/b/c/d'); | 156 testJoin('/a/b', 'c/d', '/a/b/c/d'); |
142 testJoin('a/', 'b/c/', 'a/b/c/'); | 157 testJoin('a/', 'b/c/', 'a/b/c/'); |
143 testJoin('a/b/./c/..//', 'd/.././..//e/f//', 'a/e/f/'); | 158 testJoin('a/b/./c/..//', 'd/.././..//e/f//', 'a/e/f/'); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 // Should always throw - no relative path can be constructed. | 209 // Should always throw - no relative path can be constructed. |
195 Expect.throws(() => | 210 Expect.throws(() => |
196 new Path('a/b').relativeTo(new Path('../../d'))); | 211 new Path('a/b').relativeTo(new Path('../../d'))); |
197 } | 212 } |
198 | 213 |
199 // Test that Windows share information is maintain through | 214 // Test that Windows share information is maintain through |
200 // Path operations. | 215 // Path operations. |
201 void testWindowsShare() { | 216 void testWindowsShare() { |
202 // Windows share information only makes sense on Windows. | 217 // Windows share information only makes sense on Windows. |
203 if (Platform.operatingSystem != 'windows') return; | 218 if (Platform.operatingSystem != 'windows') return; |
204 var path = new Path.fromNative(r'\\share\a\b\..\c'); | 219 var path = new Path(r'\\share\a\b\..\c'); |
205 Expect.isTrue(path.isAbsolute); | 220 Expect.isTrue(path.isAbsolute); |
206 Expect.isTrue(path.isWindowsShare); | 221 Expect.isTrue(path.isWindowsShare); |
207 Expect.isFalse(path.hasTrailingSeparator); | 222 Expect.isFalse(path.hasTrailingSeparator); |
208 var canonical = path.canonicalize(); | 223 var canonical = path.canonicalize(); |
209 Expect.isTrue(canonical.isAbsolute); | 224 Expect.isTrue(canonical.isAbsolute); |
210 Expect.isTrue(canonical.isWindowsShare); | 225 Expect.isTrue(canonical.isWindowsShare); |
211 Expect.isFalse(path.isCanonical); | 226 Expect.isFalse(path.isCanonical); |
212 Expect.isTrue(canonical.isCanonical); | 227 Expect.isTrue(canonical.isCanonical); |
213 var joined = canonical.join(new Path('d/e/f')); | 228 var joined = canonical.join(new Path('d/e/f')); |
214 Expect.isTrue(joined.isAbsolute); | 229 Expect.isTrue(joined.isAbsolute); |
215 Expect.isTrue(joined.isWindowsShare); | 230 Expect.isTrue(joined.isWindowsShare); |
216 var relativeTo = joined.relativeTo(canonical); | 231 var relativeTo = joined.relativeTo(canonical); |
217 Expect.isFalse(relativeTo.isAbsolute); | 232 Expect.isFalse(relativeTo.isAbsolute); |
218 Expect.isFalse(relativeTo.isWindowsShare); | 233 Expect.isFalse(relativeTo.isWindowsShare); |
219 var nonShare = new Path('/share/a/c/d/e'); | 234 var nonShare = new Path('/share/a/c/d/e'); |
220 Expect.throws(() => nonShare.relativeTo(canonical)); | 235 Expect.throws(() => nonShare.relativeTo(canonical)); |
221 Expect.isTrue(canonical.toString().startsWith('/share/a')); | 236 Expect.isTrue(canonical.toString().startsWith('/share/a')); |
222 Expect.isTrue(canonical.toNativePath().startsWith(r'\\share\a')); | 237 Expect.isTrue(canonical.toNativePath().startsWith(r'\\share\a')); |
223 Expect.listEquals(['share', 'a', 'c'], canonical.segments()); | 238 Expect.listEquals(['share', 'a', 'c'], canonical.segments()); |
224 var appended = canonical.append('d'); | 239 var appended = canonical.append('d'); |
225 Expect.isTrue(appended.isAbsolute); | 240 Expect.isTrue(appended.isAbsolute); |
226 Expect.isTrue(appended.isWindowsShare); | 241 Expect.isTrue(appended.isWindowsShare); |
227 var directoryPath = canonical.directoryPath; | 242 var directoryPath = canonical.directoryPath; |
228 Expect.isTrue(directoryPath.isAbsolute); | 243 Expect.isTrue(directoryPath.isAbsolute); |
229 Expect.isTrue(directoryPath.isWindowsShare); | 244 Expect.isTrue(directoryPath.isWindowsShare); |
230 } | 245 } |
OLD | NEW |