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:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 testFileUri() { | 7 testFileUri() { |
8 final unsupported = new UnsupportedError(""); | 8 final unsupported = new UnsupportedError(""); |
9 | 9 |
10 var tests = [ | 10 var tests = [ |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 84 |
85 ["file://server/a/b", "//server/a/b", "\\\\server\\a\\b"], | 85 ["file://server/a/b", "//server/a/b", "\\\\server\\a\\b"], |
86 ["file://server/a/b/", "//server/a/b/", "\\\\server\\a\\b\\"], | 86 ["file://server/a/b/", "//server/a/b/", "\\\\server\\a\\b\\"], |
87 | 87 |
88 ["file:///C:/", "C:/", "C:\\"], | 88 ["file:///C:/", "C:/", "C:\\"], |
89 ["file:///C:/a/b", "C:/a/b", "C:\\a\\b"], | 89 ["file:///C:/a/b", "C:/a/b", "C:\\a\\b"], |
90 ["file:///C:/a/b/", "C:/a/b/", "C:\\a\\b\\"], | 90 ["file:///C:/a/b/", "C:/a/b/", "C:\\a\\b\\"], |
91 ]; | 91 ]; |
92 | 92 |
93 for (var test in tests) { | 93 for (var test in tests) { |
94 Uri uri; | 94 Uri uri = new Uri.file(test[1], windows: true); |
95 uri = new Uri.file(test[1], windows: true); | |
96 Expect.equals(test[0], uri.toString()); | 95 Expect.equals(test[0], uri.toString()); |
97 Expect.equals(test[2], uri.toFilePath(windows: true)); | 96 Expect.equals(test[2], uri.toFilePath(windows: true)); |
| 97 bool couldBeDir = uri.path.isEmpty || uri.path.endsWith('\\'); |
| 98 Uri dirUri = new Uri.directory(test[1], windows: true); |
| 99 Expect.isTrue(dirUri.path.isEmpty || dirUri.path.endsWith('/')); |
| 100 if (couldBeDir) { |
| 101 Expect.equals(uri, dirUri); |
| 102 } |
98 } | 103 } |
99 } | 104 } |
100 | 105 |
101 testFileUriWindowsWin32Namespace() { | 106 testFileUriWindowsWin32Namespace() { |
102 var tests = [ | 107 var tests = [ |
103 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], | 108 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], |
104 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], | 109 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], |
105 ["\\\\?\\UNC\\server\\share\\file", | 110 ["\\\\?\\UNC\\server\\share\\file", |
106 "file://server/share/file", | 111 "file://server/share/file", |
107 "\\\\server\\share\\file"], | 112 "\\\\server\\share\\file"], |
108 ]; | 113 ]; |
109 | 114 |
110 for (var test in tests) { | 115 for (var test in tests) { |
111 Uri uri = new Uri.file(test[0], windows: true); | 116 Uri uri = new Uri.file(test[0], windows: true); |
112 Expect.equals(test[1], uri.toString()); | 117 Expect.equals(test[1], uri.toString()); |
113 Expect.equals(test[2], uri.toFilePath(windows: true)); | 118 Expect.equals(test[2], uri.toFilePath(windows: true)); |
114 } | 119 } |
115 | 120 |
116 Expect.throws( | 121 Expect.throws( |
117 () => new Uri.file("\\\\?\\file", windows: true), | 122 () => new Uri.file("\\\\?\\file", windows: true), |
118 (e) => e is ArgumentError); | 123 (e) => e is ArgumentError); |
119 Expect.throws( | 124 Expect.throws( |
120 () => new Uri.file("\\\\?\\UNX\\server\\share\\file", windows: true), | 125 () => new Uri.file("\\\\?\\UNX\\server\\share\\file", windows: true), |
121 (e) => e is ArgumentError); | 126 (e) => e is ArgumentError); |
| 127 Expect.throws( |
| 128 () => new Uri.directory("\\\\?\\file", windows: true), |
| 129 (e) => e is ArgumentError); |
| 130 Expect.throws( |
| 131 () => new Uri.directory("\\\\?\\UNX\\server\\share\\file", windows: true), |
| 132 (e) => e is ArgumentError); |
122 } | 133 } |
123 | 134 |
124 testFileUriDriveLetter() { | 135 testFileUriDriveLetter() { |
125 check(String s, String nonWindows, String windows) { | 136 check(String s, String nonWindows, String windows) { |
126 Uri uri; | 137 Uri uri; |
127 uri = Uri.parse(s); | 138 uri = Uri.parse(s); |
128 Expect.equals(nonWindows, uri.toFilePath(windows: false)); | 139 Expect.equals(nonWindows, uri.toFilePath(windows: false)); |
129 if (windows != null) { | 140 if (windows != null) { |
130 Expect.equals(windows, uri.toFilePath(windows: true)); | 141 Expect.equals(windows, uri.toFilePath(windows: true)); |
131 } else { | 142 } else { |
132 Expect.throws(() => uri.toFilePath(windows: true), | 143 Expect.throws(() => uri.toFilePath(windows: true), |
133 (e) => e is UnsupportedError); | 144 (e) => e is UnsupportedError); |
134 } | 145 } |
135 } | 146 } |
136 | 147 |
137 check("file:///C:", "/C:", "C:\\"); | 148 check("file:///C:", "/C:", "C:\\"); |
138 check("file:///C:/", "/C:/", "C:\\"); | 149 check("file:///C:/", "/C:/", "C:\\"); |
139 check("file:///C:a", "/C:a", null); | 150 check("file:///C:a", "/C:a", null); |
140 check("file:///C:a/", "/C:a/", null); | 151 check("file:///C:a/", "/C:a/", null); |
141 | 152 |
142 Expect.throws(() => new Uri.file("C:", windows: true), | 153 Expect.throws(() => new Uri.file("C:", windows: true), |
143 (e) => e is ArgumentError); | 154 (e) => e is ArgumentError); |
144 Expect.throws(() => new Uri.file("C:a", windows: true), | 155 Expect.throws(() => new Uri.file("C:a", windows: true), |
145 (e) => e is ArgumentError); | 156 (e) => e is ArgumentError); |
146 Expect.throws(() => new Uri.file("C:a\b", windows: true), | 157 Expect.throws(() => new Uri.file("C:a\b", windows: true), |
147 (e) => e is ArgumentError); | 158 (e) => e is ArgumentError); |
| 159 Expect.throws(() => new Uri.directory("C:", windows: true), |
| 160 (e) => e is ArgumentError); |
| 161 Expect.throws(() => new Uri.directory("C:a", windows: true), |
| 162 (e) => e is ArgumentError); |
| 163 Expect.throws(() => new Uri.directory("C:a\b", windows: true), |
| 164 (e) => e is ArgumentError); |
148 } | 165 } |
149 | 166 |
150 testFileUriResolve() { | 167 testFileUriResolve() { |
151 var tests = [ | 168 var tests = [ |
152 ["file:///a", "/a", "", "\\a", ""], | 169 ["file:///a", "/a", "", "\\a", ""], |
153 ["file:///a/", "/a/", "", "\\a\\", ""], | 170 ["file:///a/", "/a/", "", "\\a\\", ""], |
154 ["file:///b", "/a", "b", "\\a", "b"], | 171 ["file:///b", "/a", "b", "\\a", "b"], |
155 ["file:///b/", "/a", "b/", "\\a", "b\\"], | 172 ["file:///b/", "/a", "b/", "\\a", "b\\"], |
156 ["file:///a/b", "/a/", "b", "\\a\\", "b"], | 173 ["file:///a/b", "/a/", "b", "\\a\\", "b"], |
157 ["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"], | 174 ["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"], |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 | 207 |
191 // Illegal characters in windows file names. | 208 // Illegal characters in windows file names. |
192 var illegalWindowsPaths = | 209 var illegalWindowsPaths = |
193 ["a<b", "a>b", "a:b", "a\"b", "a|b", "a?b", "a*b", "\\\\?\\c:\\a/b"]; | 210 ["a<b", "a>b", "a:b", "a\"b", "a|b", "a?b", "a*b", "\\\\?\\c:\\a/b"]; |
194 | 211 |
195 for (var test in illegalWindowsPaths) { | 212 for (var test in illegalWindowsPaths) { |
196 Expect.throws(() => new Uri.file(test, windows: true), | 213 Expect.throws(() => new Uri.file(test, windows: true), |
197 (e) => e is ArgumentError); | 214 (e) => e is ArgumentError); |
198 Expect.throws(() => new Uri.file("\\$test", windows: true), | 215 Expect.throws(() => new Uri.file("\\$test", windows: true), |
199 (e) => e is ArgumentError); | 216 (e) => e is ArgumentError); |
| 217 Expect.throws(() => new Uri.directory(test, windows: true), |
| 218 (e) => e is ArgumentError); |
| 219 Expect.throws(() => new Uri.directory("\\$test", windows: true), |
| 220 (e) => e is ArgumentError); |
200 | 221 |
201 // It is possible to create non-Windows URIs, but not Windows URIs. | 222 // It is possible to create non-Windows URIs, but not Windows URIs. |
202 Uri uri = new Uri.file(test, windows: false); | 223 Uri uri = new Uri.file(test, windows: false); |
203 Uri absoluteUri = new Uri.file("/$test", windows: false); | 224 Uri absoluteUri = new Uri.file("/$test", windows: false); |
| 225 Uri dirUri = new Uri.directory(test, windows: false); |
| 226 Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false); |
204 Expect.throws(() => new Uri.file(test, windows: true)); | 227 Expect.throws(() => new Uri.file(test, windows: true)); |
205 Expect.throws(() => new Uri.file("\\$test", windows: true)); | 228 Expect.throws(() => new Uri.file("\\$test", windows: true)); |
| 229 Expect.throws(() => new Uri.direcory(test, windows: true)); |
| 230 Expect.throws(() => new Uri.directory("\\$test", windows: true)); |
206 | 231 |
207 // It is possible to extract non-Windows file path, but not | 232 // It is possible to extract non-Windows file path, but not |
208 // Windows file path. | 233 // Windows file path. |
209 Expect.equals(test, uri.toFilePath(windows: false)); | 234 Expect.equals(test, uri.toFilePath(windows: false)); |
210 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); | 235 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); |
| 236 Expect.equals("$test/", dirUri.toFilePath(windows: false)); |
| 237 Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false)); |
211 Expect.throws(() => uri.toFilePath(windows: true)); | 238 Expect.throws(() => uri.toFilePath(windows: true)); |
212 Expect.throws(() => absoluteUri.toFilePath(windows: true)); | 239 Expect.throws(() => absoluteUri.toFilePath(windows: true)); |
| 240 Expect.throws(() => dirUri.toFilePath(windows: true)); |
| 241 Expect.throws(() => dirAbsoluteUri.toFilePath(windows: true)); |
213 } | 242 } |
214 | 243 |
215 // Backslash | 244 // Backslash |
216 illegalWindowsPaths = ["a\\b", "a\\b\\"]; | 245 illegalWindowsPaths = ["a\\b", "a\\b\\"]; |
217 for (var test in illegalWindowsPaths) { | 246 for (var test in illegalWindowsPaths) { |
218 // It is possible to create both non-Windows URIs, and Windows URIs. | 247 // It is possible to create both non-Windows URIs, and Windows URIs. |
219 Uri uri = new Uri.file(test, windows: false); | 248 Uri uri = new Uri.file(test, windows: false); |
220 Uri absoluteUri = new Uri.file("/$test", windows: false); | 249 Uri absoluteUri = new Uri.file("/$test", windows: false); |
| 250 Uri dirUri = new Uri.directory(test, windows: false); |
| 251 Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false); |
221 new Uri.file(test, windows: true); | 252 new Uri.file(test, windows: true); |
222 new Uri.file("\\$test", windows: true); | 253 new Uri.file("\\$test", windows: true); |
223 | 254 |
224 // It is possible to extract non-Windows file path, but not | 255 // It is possible to extract non-Windows file path, but not |
225 // Windows file path from the non-Windows URI (it has a backslash | 256 // Windows file path from the non-Windows URI (it has a backslash |
226 // in a path segment). | 257 // in a path segment). |
227 Expect.equals(test, uri.toFilePath(windows: false)); | 258 Expect.equals(test, uri.toFilePath(windows: false)); |
228 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); | 259 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); |
| 260 Expect.equals("$test/", dirUri.toFilePath(windows: false)); |
| 261 Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false)); |
229 Expect.throws(() => uri.toFilePath(windows: true), | 262 Expect.throws(() => uri.toFilePath(windows: true), |
230 (e) => e is UnsupportedError); | 263 (e) => e is UnsupportedError); |
231 Expect.throws(() => absoluteUri.toFilePath(windows: true)); | 264 Expect.throws(() => absoluteUri.toFilePath(windows: true)); |
| 265 Expect.throws(() => dirUri.toFilePath(windows: true), |
| 266 (e) => e is UnsupportedError); |
| 267 Expect.throws(() => dirAbsoluteUri.toFilePath(windows: true)); |
232 } | 268 } |
233 } | 269 } |
234 | 270 |
235 testFileUriIllegalDriveLetter() { | 271 testFileUriIllegalDriveLetter() { |
236 Expect.throws(() => new Uri.file("1:\\", windows: true), | 272 Expect.throws(() => new Uri.file("1:\\", windows: true), |
237 (e) => e is ArgumentError); | 273 (e) => e is ArgumentError); |
| 274 Expect.throws(() => new Uri.directory("1:\\", windows: true), |
| 275 (e) => e is ArgumentError); |
238 Uri uri = new Uri.file("1:\\", windows: false); | 276 Uri uri = new Uri.file("1:\\", windows: false); |
| 277 Uri dirUri = new Uri.directory("1:\\", windows: false); |
239 Expect.equals("1:\\", uri.toFilePath(windows: false)); | 278 Expect.equals("1:\\", uri.toFilePath(windows: false)); |
| 279 Expect.equals("1:\\/", dirUri.toFilePath(windows: false)); |
240 Expect.throws(() => uri.toFilePath(windows: true), | 280 Expect.throws(() => uri.toFilePath(windows: true), |
241 (e) => e is UnsupportedError); | 281 (e) => e is UnsupportedError); |
| 282 Expect.throws(() => dirUri.toFilePath(windows: true), |
| 283 (e) => e is UnsupportedError); |
242 } | 284 } |
243 | 285 |
244 testAdditionalComponents() { | 286 testAdditionalComponents() { |
245 check(String s, {bool windowsOk: false}) { | 287 check(String s, {bool windowsOk: false}) { |
246 Uri uri = Uri.parse(s); | 288 Uri uri = Uri.parse(s); |
247 Expect.throws(() => uri.toFilePath(windows: false), | 289 Expect.throws(() => uri.toFilePath(windows: false), |
248 (e) => e is UnsupportedError); | 290 (e) => e is UnsupportedError); |
249 if (windowsOk) { | 291 if (windowsOk) { |
250 Expect.isTrue(uri.toFilePath(windows: true) is String); | 292 Expect.isTrue(uri.toFilePath(windows: true) is String); |
251 } else { | 293 } else { |
(...skipping 12 matching lines...) Expand all Loading... |
264 main() { | 306 main() { |
265 testFileUri(); | 307 testFileUri(); |
266 testFileUriWindowsSlash(); | 308 testFileUriWindowsSlash(); |
267 testFileUriDriveLetter(); | 309 testFileUriDriveLetter(); |
268 testFileUriWindowsWin32Namespace(); | 310 testFileUriWindowsWin32Namespace(); |
269 testFileUriResolve(); | 311 testFileUriResolve(); |
270 testFileUriIllegalCharacters(); | 312 testFileUriIllegalCharacters(); |
271 testFileUriIllegalDriveLetter(); | 313 testFileUriIllegalDriveLetter(); |
272 testAdditionalComponents(); | 314 testAdditionalComponents(); |
273 } | 315 } |
OLD | NEW |