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 "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import "package:path/path.dart" as Path; | |
Bob Nystrom
2013/07/15 17:49:29
Lowercase "path" would be consistent with the othe
Bill Hesse
2013/07/16 12:42:26
I wanted to use "path" as a variable name, but I c
| |
8 import "dart:io"; | 9 import "dart:io"; |
9 | 10 |
10 void main() { | 11 void main() { |
11 testBaseFunctions(); | 12 testBaseFunctions(); |
12 testRaw(); | 13 testNormalize(); |
13 testToNativePath(); | 14 testJoin(); |
14 testCanonicalize(); | |
15 testJoinAppend(); | |
16 testRelativeTo(); | 15 testRelativeTo(); |
17 testWindowsShare(); | |
18 testWindowsDrive(); | |
19 } | 16 } |
20 | 17 |
21 void testBaseFunctions() { | 18 void testBaseFunctions() { |
22 testGetters(new Path("/foo/bar/fisk.hest"), | 19 testGetters("/foo/bar/fisk.hest", |
23 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'], | 20 ['/foo/bar', 'fisk.hest', 'fisk', '/foo/bar/fisk', '.hest', '/'], |
24 'absolute canonical'); | 21 'absolute normalized'); |
25 testGetters(new Path(''), | 22 testGetters('', |
26 ['', '', '', ''], | 23 ['.', '', '', '', '', ''], |
27 'empty'); | 24 'normalized'); // Issue with normalize('') == '.' change. |
28 // This corner case leaves a trailing slash for the root. | 25 // This corner case leaves a trailing slash for the root. |
29 testGetters(new Path('/'), | 26 testGetters('/', |
30 ['/', '', '', ''], | 27 ['/', '/', '/', '/', '', '/'], |
31 'absolute canonical trailing'); | 28 'absolute normalized'); |
32 testGetters(new Path('.'), | 29 testGetters('.', |
33 ['', '.', '.', ''], | 30 ['.', '.', '.', '.', '', ''], |
34 'canonical'); | 31 'normalized'); |
35 testGetters(new Path('..'), | 32 testGetters('..', |
36 ['', '..', '..', ''], | 33 ['.', '..', '..', '..', '', ''], |
37 'canonical'); | 34 'normalized'); |
38 testGetters(new Path('/ab,;- .c.d'), | 35 testGetters('/ab,;- .c.d', |
39 ['/', 'ab,;- .c.d', 'ab,;- .c', 'd'], | 36 ['/', 'ab,;- .c.d', 'ab,;- .c', '/ab,;- .c', '.d', '/'], |
40 'absolute canonical'); | 37 'absolute normalized'); |
41 | 38 |
42 // Canonical and non-canonical cases | 39 // Normalized and non-normalized cases |
43 testGetters(new Path("a/b/../c/./d/e"), | 40 testGetters("a/b/../c/./d/e", |
44 ['a/b/../c/./d', 'e', 'e', ''], | 41 ['a/b/../c/./d', 'e', 'e', 'a/b/../c/./d/e', '', ''], |
45 ''); | 42 ''); |
46 testGetters(new Path("a./b../..c/.d/e"), | 43 testGetters("a./b../..c/.d/e", |
47 ['a./b../..c/.d', 'e', 'e', ''], | 44 ['a./b../..c/.d', 'e', 'e', 'a./b../..c/.d/e', '', ''], |
48 'canonical'); | 45 'normalized'); |
49 // .. is allowed at the beginning of a canonical relative path. | 46 testGetters("a/b/", |
50 testGetters(new Path("../../a/b/c/d/"), | 47 ['a', 'b', 'b', 'a/b/', '', ''], |
51 ['../../a/b/c/d', '', '', ''], | 48 ''); |
52 'canonical trailing'); | 49 // .. is allowed at the beginning of a normalized relative path. |
50 testGetters("../../a/b/c/d", | |
51 ['../../a/b/c', 'd', 'd', '../../a/b/c/d', '', ''], | |
52 'normalized'); | |
53 | 53 |
54 // '.' at the end of a path is not considered an extension. | 54 // '.' at the end of a path is not considered an extension. |
55 testGetters(new Path("a/b.c/."), | 55 testGetters("a/b.c/.", |
56 ['a/b.c', '.', '.', ''], | 56 ['a/b.c', '.', '.', 'a/b.c/.', '', ''], |
57 ''); | 57 ''); |
58 // '..' at the end of a path is not considered an extension. | 58 // '..' at the end of a path is not considered an extension. |
59 testGetters(new Path("a/bc/../.."), | 59 testGetters("a/bc/../..", |
60 ['a/bc/..', '..', '..', ''], | 60 ['a/bc/..', '..', '..', 'a/bc/../..', '', ''], |
61 ''); | 61 ''); |
62 | 62 |
63 // Test the special path cleaning operations on the Windows platform. | 63 // Test the special path cleaning operations on the Windows platform. |
64 if (Platform.operatingSystem == 'windows') { | 64 if (Platform.operatingSystem == 'windows') { |
65 testGetters(new Path(r"c:\foo\bar\fisk.hest"), | 65 testGetters(r"c:\foo\bar\fisk.hest", |
66 ['/c:/foo/bar', 'fisk.hest', 'fisk', 'hest'], | 66 [r'c:\foo\bar', 'fisk.hest', 'fisk', r'c:\foo\bar\fisk', |
67 'absolute canonical'); | 67 '.hest', 'c:\\'], |
68 testGetters(new Path("\\foo\\bar\\"), | 68 'absolute normalized'); |
69 ['/foo/bar', '', '', ''], | 69 testGetters(r"\\sharename\foo\bar\", |
70 'absolute canonical trailing'); | 70 [r'\\sharename\foo\', 'bar', 'bar', r'\\sharename\foo\bar', |
71 testGetters(new Path("\\foo\\bar\\hest"), | 71 '', r'\\sharename\'], |
72 ['/foo/bar', 'hest', 'hest', ''], | 72 'absolute normalized'); |
73 'absolute canonical'); | 73 testGetters(r"\\sharename\..\bar.hest\", |
74 testGetters(new Path(r"foo/bar\hest/.fisk"), | 74 [r'\\sharename\..\', 'bar.hest', 'bar', r'\\sharename\..\bar', |
75 ['foo/bar/hest', '.fisk', '', 'fisk'], | 75 '.hest', r'\\sharename\'], |
76 'canonical'); | 76 'absolute normalized'); |
77 testGetters(new Path(r"foo//bar\\hest/\/.fisk."), | 77 testGetters(r"foo\bar\hest", |
78 ['foo//bar//hest', '.fisk.', '.fisk', ''], | 78 [r'foo\bar', 'hest', 'hest', r'foo\bar\hest', '', ''], |
79 'normalized'); | |
80 testGetters(r"foo/bar\hest/.fisk", | |
81 ['foo/bar\\hest', '.fisk', '.fisk', 'foo/bar\\hest/.fisk', | |
82 '', ''], | |
83 ''); | |
84 testGetters(r"foo//bar\\hest/\/.fisk.", | |
85 [r'foo//bar\\hest', '.fisk.', '.fisk', | |
86 r'foo//bar\\hest/\/.fisk', '', ''], | |
79 ''); | 87 ''); |
80 } else { | 88 } else { |
81 // Make sure that backslashes are uninterpreted on other platforms. | 89 // Make sure that backslashes are uninterpreted on other platforms. |
82 testGetters(new Path(r"c:\foo\bar\fisk.hest"), | 90 testGetters(r"c:\foo\bar\fisk.hest", |
83 ['', r'c:\foo\bar\fisk.hest', r'c:\foo\bar\fisk', 'hest'], | 91 ['.', r'c:\foo\bar\fisk.hest', r'c:\foo\bar\fisk', |
84 'canonical'); | 92 r'c:\foo\bar\fisk', '.hest', ''], |
85 testGetters(new Path(r"/foo\bar/bif/fisk.hest"), | 93 'normalized'); |
86 [r'/foo\bar/bif', 'fisk.hest', 'fisk', 'hest'], | 94 testGetters(r"/foo\bar/bif/fisk.hest", |
87 'absolute canonical'); | 95 [r'/foo\bar/bif', 'fisk.hest', 'fisk', r'/foo\bar/bif/fisk', |
88 testGetters(new Path(r"//foo\bar///bif////fisk.hest"), | 96 '.hest', '/'], |
89 [r'//foo\bar///bif', 'fisk.hest', 'fisk', 'hest'], | 97 'absolute normalized'); |
98 testGetters(r"//foo\bar///bif////fisk.hest", | |
99 [r'//foo\bar///bif', 'fisk.hest', 'fisk', | |
100 r'//foo\bar///bif////fisk', '.hest', '/'], | |
90 'absolute'); | 101 'absolute'); |
91 testGetters(new Path(r"/foo\ bar/bif/gule\ fisk.hest"), | 102 testGetters(r"/foo\ bar/bif/gule\ fisk.hest", |
92 [r'/foo\ bar/bif', r'gule\ fisk.hest', r'gule\ fisk', 'hest'], | 103 [r'/foo\ bar/bif', r'gule\ fisk.hest', r'gule\ fisk', |
93 'absolute canonical'); | 104 r'/foo\ bar/bif/gule\ fisk', '.hest', '/'], |
105 'absolute normalized'); | |
94 } | 106 } |
95 } | 107 } |
96 | 108 |
97 | 109 |
98 void testGetters(Path path, List components, String properties) { | 110 void testGetters(String path, List components, String properties) { |
99 final int DIRNAME = 0; | 111 final int DIRNAME = 0; |
100 final int FILENAME = 1; | 112 final int FILENAME = 1; |
101 final int FILENAME_NO_EXTENSION = 2; | 113 final int FILENAME_NO_EXTENSION = 2; |
102 final int EXTENSION = 3; | 114 final int WITHOUT_EXTENSION = 3; |
103 Expect.equals(components[DIRNAME], path.directoryPath.toString()); | 115 final int EXTENSION = 4; |
104 Expect.equals(components[FILENAME], path.filename); | 116 final int ROOT_PREFIX = 5; |
117 Expect.equals(components[DIRNAME], Path.dirname(path)); | |
118 Expect.equals(components[FILENAME], Path.basename(path)); | |
105 Expect.equals(components[FILENAME_NO_EXTENSION], | 119 Expect.equals(components[FILENAME_NO_EXTENSION], |
106 path.filenameWithoutExtension); | 120 Path.basenameWithoutExtension(path)); |
107 Expect.equals(components[EXTENSION], path.extension); | 121 Expect.equals(components[WITHOUT_EXTENSION], |
108 | 122 Path.withoutExtension(path)); |
109 Expect.equals(path.isCanonical, properties.contains('canonical')); | 123 Expect.equals(components[EXTENSION], Path.extension(path)); |
110 Expect.equals(path.isAbsolute, properties.contains('absolute')); | 124 Expect.equals(Path.isAbsolute(path), properties.contains('absolute')); |
111 Expect.equals(path.hasTrailingSeparator, properties.contains('trailing')); | 125 Expect.equals(components[ROOT_PREFIX], Path.rootPrefix(path)); |
126 if (properties.contains('normalized')) { | |
127 Expect.equals(path, Path.normalize(path)); | |
128 } else { | |
129 Expect.notEquals(path, Path.normalize(path)); | |
130 } | |
112 } | 131 } |
113 | 132 |
114 void testRaw() { | |
115 Expect.equals(new Path.raw('c:\\foo/bar bad').toString(), 'c:\\foo/bar bad'); | |
116 Expect.equals(new Path.raw('').toString(), ''); | |
117 Expect.equals(new Path.raw('\\bar\u2603\n.').toString(), '\\bar\u2603\n.'); | |
118 } | |
119 | 133 |
120 void testToNativePath() { | 134 void testNormalize() { |
121 Expect.equals('.', new Path('').toNativePath()); | 135 Function t = (input, normalized) { |
122 Expect.equals('.', new Path('.').toNativePath()); | 136 Expect.equals(normalized, Path.normalize(input)); |
123 Expect.equals('.', new Path('a_file').directoryPath.toNativePath()); | |
124 } | |
125 | |
126 void testCanonicalize() { | |
127 Function t = (input, canonicalized) { | |
128 Expect.equals(canonicalized, new Path(input).canonicalize().toString()); | |
129 }; | 137 }; |
130 | 138 |
131 t('.', '.'); | 139 t('.', '.'); |
132 t('./.', '.'); | 140 t('./.', '.'); |
133 t('foo/..', '.'); | 141 t('foo/..', '.'); |
134 t('../foo', '../foo'); | 142 t('../foo', '../foo'); |
135 t('/../foo', '/foo'); | 143 t('/../foo', '/foo'); |
136 t('/foo/..', '/'); | 144 t('/foo/..', '/'); |
137 t('/foo/../', '/'); | 145 t('/foo/../', '/'); |
138 t('/c:/../foo', '/c:/foo'); | |
139 t('/c:/foo/..', '/c:/'); | |
140 t('/c:/foo/../', '/c:/'); | |
141 t('/c:/foo/../..', '/c:/'); | |
142 t('/c:/foo/../../', '/c:/'); | |
143 t('..', '..'); | 146 t('..', '..'); |
144 t('', '.'); | 147 t('.', ''); |
145 t('/', '/'); | 148 t('/', '/'); |
146 t('../foo/bar/..', '../foo'); | 149 t('../foo/bar/..', '../foo'); |
147 t('/../foo/bar/..', '/foo'); | 150 t('/../foo/bar/..', '/foo'); |
148 t('foo/bar/../../../joe/../..', '../..'); | 151 t('foo/bar/../../../joe/../..', '../..'); |
149 t('a/b/c/../../..d/./.e/f././', 'a/..d/.e/f./'); | 152 t('a/b/c/../../..d/./.e/f././', 'a/..d/.e/f.'); |
150 t('/%:/foo/../..', '/%:/'); | |
151 if (Platform.operatingSystem == 'windows') { | 153 if (Platform.operatingSystem == 'windows') { |
152 t('c:/foo/../../..', '/c:/'); | 154 t('c:/foo/../../..', r'c:\'); |
153 t('c:/foo/../../bad/dad/./..', '/c:/bad'); | 155 t('c:/foo/../../bad/dad/./..', r'c:\bad'); |
154 } else { | 156 } else { |
155 t('c:/foo/../../..', '..'); | 157 t('c:/foo/../../..', '..'); |
156 t('c:/foo/../../bad/dad/./..', 'bad'); | 158 t('c:/foo/../../bad/dad/./..', 'bad'); |
157 } | 159 } |
158 } | 160 } |
159 | 161 |
160 void testJoinAppend() { | 162 void testJoin() { |
161 void testJoin(String a, String b, String c) { | 163 void test(String a, String b, String c) { |
162 Expect.equals(new Path(a).join(new Path(b)).toString(), c); | 164 Expect.equals(c, Path.join(a, b)); |
163 } | 165 } |
164 | 166 |
165 testJoin('/a/b', 'c/d', '/a/b/c/d'); | 167 test('/a/b', 'c/d', '/a/b/c/d'); |
166 testJoin('a/', 'b/c/', 'a/b/c/'); | 168 test('a/', 'b/c/', 'a/b/c/'); |
167 testJoin('a/b/./c/..//', 'd/.././..//e/f//', 'a/e/f/'); | 169 test('a/b/./c/..//', 'd/.././..//e/f//', 'a/b/./c/..//d/.././..//e/f//'); |
168 testJoin('a/b', 'c/../../../..', '..'); | 170 test('a/b', 'c/../../../..', 'a/b/c/../../../..'); |
169 testJoin('a/b', 'c/../../../', '.'); | 171 test('a/b/c/', '/d', '/d'); |
170 testJoin('/a/b', 'c/../../../..', '/'); | 172 test('', '', ''); |
171 testJoin('/a/b', 'c/../../../', '/'); | 173 test('a/b', '', 'a/b/'); |
172 testJoin('a/b', 'c/../../../../../', '../../'); | |
173 testJoin('a/b/c', '../../d', 'a/d'); | |
174 testJoin('/a/b/c', '../../d', '/a/d'); | |
175 testJoin('/a/b/c', '../../d/', '/a/d/'); | |
176 testJoin('a/b/c', '../../d/', 'a/d/'); | |
177 | |
178 void testAppend(String a, String b, String c) { | |
179 Expect.equals(new Path(a).append(b).toString(), c); | |
180 } | |
181 | |
182 testAppend('/a/b', 'c', '/a/b/c'); | |
183 testAppend('a/b/', 'cd', 'a/b/cd'); | |
184 testAppend('.', '..', './..'); | |
185 testAppend('a/b', '/c/d', 'a/b//c/d'); | |
186 testAppend('', 'foo/bar', 'foo/bar'); | |
187 testAppend('/foo', '', '/foo/'); | |
188 | |
189 // .join can only join a relative path to a path. | |
190 // It cannot join an absolute path to a path. | |
191 Expect.throws(() => new Path('/a/b/').join(new Path('/c/d'))); | |
192 } | 174 } |
193 | 175 |
194 void testRelativeTo() { | 176 void testRelativeTo() { |
177 relativeTest(new Path.Builder(style: Path.Style.posix, root: '.'), ''); | |
178 relativeTest(new Path.Builder(style: Path.Style.posix, root: '/'), ''); | |
179 relativeTest(new Path.Builder(style: Path.Style.windows, root: 'd:/'), 'c:'); | |
180 relativeTest(new Path.Builder(style: Path.Style.windows, root: '.'), 'c:'); | |
181 relativeTest(new Path.Builder(style: Path.Style.url, root: 'file:///'), | |
182 'http://myserver'); | |
183 relativeTest(new Path.Builder(style: Path.Style.url, root: '.'), | |
184 'http://myserver'); | |
185 relativeTest(new Path.Builder(style: Path.Style.url, root: 'file:///'), | |
186 ''); | |
187 relativeTest(new Path.Builder(style: Path.Style.url, root: '.'), | |
188 ''); | |
189 } | |
190 | |
191 | |
192 void relativeTest(Path.Builder P, String absolutePrefix) { | |
Bob Nystrom
2013/07/15 17:49:29
"P" is a weird variable name. "builder" instead? T
| |
193 var m = absolutePrefix; | |
195 // Cases where the arguments are absolute paths. | 194 // Cases where the arguments are absolute paths. |
196 Expect.equals('c/d', | 195 void test(result, path, from) { |
197 new Path('/a/b/c/d').relativeTo(new Path('/a/b')).toString()); | 196 Expect.equals(P.normalize(result), P.relative(path, from: from)); |
198 Expect.equals('c/d', | 197 } |
199 new Path('/a/b/c/d').relativeTo(new Path('/a/b/')).toString()); | |
200 Expect.equals('.', | |
201 new Path('/a').relativeTo(new Path('/a')).toString()); | |
202 | 198 |
203 // Trailing slash in the base path has no effect. This matches Path.join | 199 test('c/d', '$m/a/b/c/d', '$m/a/b'); |
204 // semantics, but not URL join semantics. | 200 test('c/d', '$m/a/b/c/d', '$m/a/b/'); |
205 Expect.equals('../../z/x/y', | 201 test('.', '$m/a', '$m/a'); |
206 new Path('/a/b/z/x/y').relativeTo(new Path('/a/b/c/d/')).toString()); | 202 // Trailing slashes in the inputs have no effect. |
207 Expect.equals('../../z/x/y', | 203 test('../../z/x/y', '$m/a/b/z/x/y', '$m/a/b/c/d/'); |
208 new Path('/a/b/z/x/y').relativeTo(new Path('/a/b/c/d')).toString()); | 204 test('../../z/x/y', '$m/a/b/z/x/y', '$m/a/b/c/d'); |
209 Expect.equals('../../z/x/y/', | 205 test('../../z/x/y', '$m/a/b/z/x/y/', '$m/a/b/c/d'); |
210 new Path('/a/b/z/x/y/').relativeTo(new Path('/a/b/c/d')).toString()); | 206 test('../../../z/x/y', '$m/z/x/y', '$m/a/b/c'); |
211 | 207 test('../../../z/x/y', '$m/z/x/y', '$m/a/b/c/'); |
212 Expect.equals('../../../z/x/y', | |
213 new Path('/z/x/y').relativeTo(new Path('/a/b/c')).toString()); | |
214 Expect.equals('../../../z/x/y', | |
215 new Path('/z/x/y').relativeTo(new Path('/a/b/c/')).toString()); | |
216 | |
217 // Cases where the arguments are relative paths. | 208 // Cases where the arguments are relative paths. |
218 Expect.equals('c/d', | 209 test('c/d', 'a/b/c/d', 'a/b'); |
219 new Path('a/b/c/d').relativeTo(new Path('a/b')).toString()); | 210 test('.', 'a/b/c', 'a/b/c'); |
220 Expect.equals('c/d', | 211 test('.', 'a/d/../b/c', 'a/b/c/'); |
221 new Path('/a/b/c/d').relativeTo(new Path('/a/b/')).toString()); | 212 test('.', '', ''); |
222 Expect.equals('.', | 213 test('.', '.', ''); |
223 new Path('a/b/c').relativeTo(new Path('a/b/c')).toString()); | 214 test('.', '', '.'); |
224 Expect.equals('.', | 215 test('.', '.', '.'); |
225 new Path('').relativeTo(new Path('')).toString()); | 216 test('.', '..', '..'); |
226 Expect.equals('.', | 217 if (P.root == '.') test('..', '..', '.'); |
227 new Path('.').relativeTo(new Path('.')).toString()); | 218 test('a', 'a', ''); |
228 Expect.equals('a', | 219 test('a', 'a', '.'); |
229 new Path('a').relativeTo(new Path('.')).toString()); | 220 test('..', '.', 'a'); |
230 Expect.equals('..', | 221 test('.', 'a/b/f/../c', 'a/e/../b/c'); |
231 new Path('..').relativeTo(new Path('.')).toString()); | 222 test('d', 'a/b/f/../c/d', 'a/e/../b/c'); |
232 Expect.equals('..', | 223 test('..', 'a/b/f/../c', 'a/e/../b/c/e/'); |
233 new Path('.').relativeTo(new Path('a')).toString()); | 224 test('../..', '', 'a/b/'); |
234 Expect.equals('.', | 225 if (P.root == '.') test('../../..', '..', 'a/b/'); |
235 new Path('..').relativeTo(new Path('..')).toString()); | 226 test('../b/c/d', 'b/c/d/', 'a/'); |
236 Expect.equals('./', | 227 test('../a/b/c', 'x/y/a//b/./f/../c', 'x//y/z'); |
237 new Path('a/b/f/../c/').relativeTo(new Path('a/e/../b/c')).toString()); | |
238 Expect.equals('d', | |
239 new Path('a/b/f/../c/d').relativeTo(new Path('a/e/../b/c')).toString()); | |
240 Expect.equals('..', | |
241 new Path('a/b/f/../c').relativeTo(new Path('a/e/../b/c/e/')).toString()); | |
242 Expect.equals('../..', | |
243 new Path('').relativeTo(new Path('a/b/')).toString()); | |
244 Expect.equals('../../..', | |
245 new Path('..').relativeTo(new Path('a/b')).toString()); | |
246 Expect.equals('../b/c/d/', | |
247 new Path('b/c/d/').relativeTo(new Path('a/')).toString()); | |
248 Expect.equals('../a/b/c', | |
249 new Path('x/y/a//b/./f/../c').relativeTo(new Path('x//y/z')).toString()); | |
250 | 228 |
251 // Case where base is a substring of relative: | 229 // Case where base is a substring of relative: |
252 Expect.equals('a/b', | 230 test('a/b', '$m/x/y//a/b', '$m/x/y/'); |
253 new Path('/x/y//a/b').relativeTo(new Path('/x/y/')).toString()); | 231 test('a/b', 'x/y//a/b', 'x/y/'); |
254 Expect.equals('a/b', | 232 test('../ya/b', '$m/x/ya/b', '$m/x/y'); |
255 new Path('x/y//a/b').relativeTo(new Path('x/y/')).toString()); | 233 test('../ya/b', 'x/ya/b', 'x/y'); |
256 Expect.equals('../ya/b', | 234 test('../b', 'x/y/../b', 'x/y/.'); |
257 new Path('/x/ya/b').relativeTo(new Path('/x/y')).toString()); | 235 test('a/b/c', 'x/y/a//b/./f/../c', 'x/y'); |
258 Expect.equals('../ya/b', | 236 test('.', '$m/x/y//', '$m/x/y/'); |
259 new Path('x/ya/b').relativeTo(new Path('x/y')).toString()); | 237 test('.', '$m/x/y/', '$m/x/y'); |
260 Expect.equals('../b', | |
261 new Path('x/y/../b').relativeTo(new Path('x/y/.')).toString()); | |
262 Expect.equals('a/b/c', | |
263 new Path('x/y/a//b/./f/../c').relativeTo(new Path('x/y')).toString()); | |
264 Expect.equals('.', | |
265 new Path('/x/y//').relativeTo(new Path('/x/y/')).toString()); | |
266 Expect.equals('.', | |
267 new Path('/x/y/').relativeTo(new Path('/x/y')).toString()); | |
268 | 238 |
269 // Should always throw - no relative path can be constructed. | 239 // Should always throw - no relative path can be constructed. |
270 Expect.throws(() => | 240 if (P.root == '.') { |
271 new Path('a/b').relativeTo(new Path('../../d'))); | 241 Expect.throws(() => P.relative('.', from: '..')); |
272 // Should always throw - relative and absolute paths are compared. | 242 Expect.throws(() => P.relative('a/b', from: '../../d')); |
273 Expect.throws(() => | 243 Expect.throws(() => print(P.relative('a/b', from: '$m/a/b'))); |
274 new Path('/a/b').relativeTo(new Path('c/d'))); | 244 // An absolute path relative from a relative path returns the absolute path. |
275 | 245 test('$m/a/b', '$m/a/b', 'c/d'); |
276 Expect.throws(() => | 246 } |
277 new Path('a/b').relativeTo(new Path('/a/b'))); | |
278 | |
279 } | 247 } |
280 | |
281 // Test that Windows share information is maintained through | |
282 // Path operations. | |
283 void testWindowsShare() { | |
284 // Windows share information only makes sense on Windows. | |
285 if (Platform.operatingSystem != 'windows') return; | |
286 var path = new Path(r'\\share\a\b\..\c'); | |
287 Expect.isTrue(path.isAbsolute); | |
288 Expect.isTrue(path.isWindowsShare); | |
289 Expect.isFalse(path.hasTrailingSeparator); | |
290 var canonical = path.canonicalize(); | |
291 Expect.isTrue(canonical.isAbsolute); | |
292 Expect.isTrue(canonical.isWindowsShare); | |
293 Expect.isFalse(path.isCanonical); | |
294 Expect.isTrue(canonical.isCanonical); | |
295 var joined = canonical.join(new Path('d/e/f')); | |
296 Expect.isTrue(joined.isAbsolute); | |
297 Expect.isTrue(joined.isWindowsShare); | |
298 var relativeTo = joined.relativeTo(canonical); | |
299 Expect.isFalse(relativeTo.isAbsolute); | |
300 Expect.isFalse(relativeTo.isWindowsShare); | |
301 var nonShare = new Path('/share/a/c/d/e'); | |
302 Expect.throws(() => nonShare.relativeTo(canonical)); | |
303 Expect.isTrue(canonical.toString().startsWith('/share/a')); | |
304 Expect.isTrue(canonical.toNativePath().startsWith(r'\\share\a')); | |
305 Expect.listEquals(['share', 'a', 'c'], canonical.segments()); | |
306 var appended = canonical.append('d'); | |
307 Expect.isTrue(appended.isAbsolute); | |
308 Expect.isTrue(appended.isWindowsShare); | |
309 var directoryPath = canonical.directoryPath; | |
310 Expect.isTrue(directoryPath.isAbsolute); | |
311 Expect.isTrue(directoryPath.isWindowsShare); | |
312 } | |
313 | |
314 // Test that Windows drive information is handled correctly in relative | |
315 // Path operations. | |
316 void testWindowsDrive() { | |
317 // Windows drive information only makes sense on Windows. | |
318 if (Platform.operatingSystem != 'windows') return; | |
319 // Test that case of drive letters is ignored, and that drive letters | |
320 // are treated specially. | |
321 var CPath = new Path(r'C:\a\b\..\c'); | |
322 var cPath = new Path(r'c:\a\b\d'); | |
323 var C2Path = new Path(r'C:\a\b\d'); | |
324 var C3Path = new Path(r'C:\a\b'); | |
325 var C4Path = new Path(r'C:\'); | |
326 var c4Path = new Path(r'c:\'); | |
327 var DPath = new Path(r'D:\a\b\d\e'); | |
328 var NoPath = new Path(r'\a\b\c\.'); | |
329 | |
330 Expect.throws(() => CPath.relativeTo(DPath)); | |
331 Expect.throws(() => CPath.relativeTo(NoPath)); | |
332 Expect.throws(() => NoPath.relativeTo(CPath)); | |
333 Expect.equals('../../c', CPath.relativeTo(cPath).toString()); | |
334 Expect.equals('../b/d', cPath.relativeTo(CPath).toString()); | |
335 Expect.equals('.', DPath.relativeTo(DPath).toString()); | |
336 Expect.equals('.', NoPath.relativeTo(NoPath).toString()); | |
337 Expect.equals('.', C2Path.relativeTo(cPath).toString()); | |
338 Expect.equals('..', C3Path.relativeTo(cPath).toString()); | |
339 Expect.equals('d', cPath.relativeTo(C3Path).toString()); | |
340 Expect.equals('a/b/d', cPath.relativeTo(C4Path).toString()); | |
341 Expect.equals('../../../', C4Path.relativeTo(cPath).toString()); | |
342 Expect.equals('a/b', C3Path.relativeTo(c4Path).toString()); | |
343 } | |
OLD | NEW |