Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: tests/corelib/uri_file_test.dart

Issue 21039005: Add support for file URIs and extracting the file path from file URIs (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed first round of review comments Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import "package:expect/expect.dart";
6
7 testFileUri() {
8 final unsupported = new UnsupportedError("");
9
10 var tests = [
11 ["", "", ""],
12 ["relative", "relative", "relative"],
13 ["relative/", "relative/", "relative\\"],
14 ["a%20b", "a b", "a b"],
15 ["a%20b/", "a b/", "a b\\"],
16 ["a/b", "a/b", "a\\b"],
17 ["a/b/", "a/b/", "a\\b\\"],
18 ["a%20b/c%20d", "a b/c d", "a b\\c d"],
19 ["a%20b/c%20d/", "a b/c d/", "a b\\c d\\"],
20
21 ["file:///absolute", "/absolute", "\\absolute"],
22 ["file:///absolute", "/absolute", "\\absolute"],
23 ["file:///a/b", "/a/b", "\\a\\b"],
24 ["file:///a/b", "/a/b", "\\a\\b"],
25
26 ["file://server/a/b", unsupported, "\\\\server\\a\\b"],
27 ["file://server/a/b/", unsupported, "\\\\server\\a\\b\\"],
28
29 ["file:///C:/", "/C:/", "C:\\"],
30 ["file:///C:/a/b", "/C:/a/b", "C:\\a\\b"],
31 ["file:///C:/a/b/", "/C:/a/b/", "C:\\a\\b\\"],
32
33 ["http:/a/b", unsupported, unsupported],
34 ["https:/a/b", unsupported, unsupported],
35 ["urn:a:b", unsupported, unsupported],
36 ];
37
38 void check(String s, filePath, bool windowsPath) {
39 Uri uri = Uri.parse(s);
40 if (filePath is Error) {
41 if (filePath is UnsupportedError) {
42 Expect.throws(() => uri.toFilePath(windowsPath: windowsPath),
43 (e) => e is UnsupportedError);
44 } else {
45 Expect.throws(() => uri.toFilePath(windowsPath: windowsPath));
46 }
47 } else {
48 Expect.equals(filePath, uri.toFilePath(windowsPath: windowsPath));
49 Expect.equals(
50 s, new Uri.file(filePath, windowsPath: windowsPath).toString());
51 }
52 }
53
54 for (var test in tests) {
55 check(test[0], test[1], false);
56 check(test[0], test[2], true);
57 }
58
59 Uri uri;
60 uri = Uri.parse("file:a");
61 Expect.equals("/a", uri.toFilePath(windowsPath: false));
62 Expect.equals("\\a", uri.toFilePath(windowsPath: true));
63 uri = Uri.parse("file:a/");
64 Expect.equals("/a/", uri.toFilePath(windowsPath: false));
65 Expect.equals("\\a\\", uri.toFilePath(windowsPath: true));
66 }
67
68 testFileUriWindowsSlash() {
69 var tests = [
70 ["", "", ""],
71 ["relative", "relative", "relative"],
72 ["relative/", "relative/", "relative\\"],
73 ["a%20b", "a b", "a b"],
74 ["a%20b/", "a b/", "a b\\"],
75 ["a/b", "a/b", "a\\b"],
76 ["a/b/", "a/b/", "a\\b\\"],
77 ["a%20b/c%20d", "a b/c d", "a b\\c d"],
78 ["a%20b/c%20d/", "a b/c d/", "a b\\c d\\"],
79
80 ["file:///absolute", "/absolute", "\\absolute"],
81 ["file:///absolute", "/absolute", "\\absolute"],
82 ["file:///a/b", "/a/b", "\\a\\b"],
83 ["file:///a/b", "/a/b", "\\a\\b"],
84
85 ["file://server/a/b", "//server/a/b", "\\\\server\\a\\b"],
86 ["file://server/a/b/", "//server/a/b/", "\\\\server\\a\\b\\"],
87
88 ["file:///C:/", "C:/", "C:\\"],
89 ["file:///C:/a/b", "C:/a/b", "C:\\a\\b"],
90 ["file:///C:/a/b/", "C:/a/b/", "C:\\a\\b\\"],
91 ];
92
93 for (var test in tests) {
94 Uri uri;
95 uri = new Uri.file(test[1], windowsPath: true);
96 Expect.equals(test[0], uri.toString());
97 Expect.equals(test[2], uri.toFilePath(windowsPath: true));
98 }
99 }
100
101 testFileUriWindowsWin32Namespace() {
102 var tests = [
103 ["\\\\?\\C:\\", "file:///C:/", "C:\\"],
104 ["\\\\?\\C:\\", "file:///C:/", "C:\\"],
105 ["\\\\?\\UNC\\server\\share\\file",
106 "file://server/share/file",
107 "\\\\server\\share\\file"],
108 ];
109
110 for (var test in tests) {
111 Uri uri = new Uri.file(test[0], windowsPath: true);
112 Expect.equals(test[1], uri.toString());
113 Expect.equals(test[2], uri.toFilePath(windowsPath: true));
114 }
115
116 Expect.throws(
117 () => new Uri.file("\\\\?\\file", windowsPath: true),
118 (e) => e is ArgumentError);
119 Expect.throws(
120 () => new Uri.file("\\\\?\\UNX\\server\\share\\file", windowsPath: true),
121 (e) => e is ArgumentError);
122 }
123
124 testFileUriDriveLetter() {
125 check(String s, String nonWindows, String windows) {
126 Uri uri;
127 uri = Uri.parse(s);
128 Expect.equals(nonWindows, uri.toFilePath(windowsPath: false));
129 Expect.equals(windows, uri.toFilePath(windowsPath: true));
130 }
131
132 check("file:///C:", "/C:", "C:\\");
133 check("file:///C:/", "/C:/", "C:\\");
134 check("file:///C:a", "/C:a", "C:\\a");
135 check("file:///C:a/", "/C:a/", "C:\\a\\");
136 }
137
138 testFileUriResolve() {
139 var tests = [
140 ["file:///a", "/a", "", "\\a", ""],
141 ["file:///a/", "/a/", "", "\\a\\", ""],
142 ["file:///b", "/a", "b", "\\a", "b"],
143 ["file:///b/", "/a", "b/", "\\a", "b\\"],
144 ["file:///a/b", "/a/", "b", "\\a\\", "b"],
145 ["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"],
146 ["file:///a/c/d", "/a/b", "c/d", "\\a\\b", "c\\d"],
147 ["file:///a/c/d/", "/a/b", "c/d/", "\\a\\b", "c\\d\\"],
148 ["file:///a/b/c/d", "/a/b/", "c/d", "\\a\\b\\", "c\\d"],
149 ["file:///a/b/c/d/", "/a/b/", "c/d/", "\\a\\b\\", "c\\d\\"],
150 ];
151
152 check(String s, String absolute, String relative, bool windowsPath) {
153 Uri absoluteUri = new Uri.file(absolute, windowsPath: windowsPath);
154 Uri relativeUri = new Uri.file(relative, windowsPath: windowsPath);
155 String relativeString =
156 windowsPath ? relative.replaceAll("\\", "/") : relative;
157 Expect.equals(s, absoluteUri.resolve(relativeString).toString());
158 Expect.equals(s, absoluteUri.resolveUri(relativeUri).toString());
159 }
160
161 for (var test in tests) {
162 check(test[0], test[1], test[2], false);
163 check(test[0], test[1], test[2], true);
164 check(test[0], test[1], test[4], true);
165 check(test[0], test[3], test[2], true);
166 check(test[0], test[3], test[4], true);
167 }
168 }
169
170 testFileUriIllegalCharacters() {
171 // Slash is an invalid character in file names on both non-Windows and Windows .
ahe 2013/08/06 14:14:30 Long line.
Søren Gjesse 2013/08/07 12:08:00 Done.
172 Uri uri = Uri.parse("file:///a%2Fb");
173 Expect.throws(() => uri.toFilePath(windowsPath: false),
174 (e) => e is UnsupportedError);
175 Expect.throws(() => uri.toFilePath(windowsPath: true),
176 (e) => e is UnsupportedError);
177
178 // Illegal characters in windows file names. Note the test of colon
179 // cannot be in position 2 as it is then considered a drive letter
180 // separator and not illegal.
181 var illegalWindowsPaths =
182 ["a<b", "a>b", "aa:b", "a\"b", "a|b", "a?b", "a*b", "\\\\?\\c:\\a/b"];
183
184 for (var test in illegalWindowsPaths) {
185 Expect.throws(() => new Uri.file(test, windowsPath: true),
186 (e) => e is ArgumentError);
187 Expect.throws(() => new Uri.file("\\$test", windowsPath: true),
188 (e) => e is ArgumentError);
189
190 // It is possible to create non-Windows URIs, but not Windows URIs.
191 Uri uri = new Uri.file(test, windowsPath: false);
192 Uri absoluteUri = new Uri.file("/$test", windowsPath: false);
193 Expect.throws(() => new Uri.file(test, windowsPath: true));
194 Expect.throws(() => new Uri.file("\\$test", windowsPath: true));
195
196 // It is possible to extract non-Windows file path, but not
197 // Windows file path.
198 Expect.equals(test, uri.toFilePath(windowsPath: false));
199 Expect.equals("/$test", absoluteUri.toFilePath(windowsPath: false));
200 Expect.throws(() => uri.toFilePath(windowsPath: true));
201 Expect.throws(() => absoluteUri.toFilePath(windowsPath: true));
202 }
203
204 // Backslash
205 illegalWindowsPaths = ["a\\b", "a\\b\\"];
206 for (var test in illegalWindowsPaths) {
207 // It is possible to create both non-Windows URIs, and Windows URIs.
208 Uri uri = new Uri.file(test, windowsPath: false);
209 Uri absoluteUri = new Uri.file("/$test", windowsPath: false);
210 new Uri.file(test, windowsPath: true);
211 new Uri.file("\\$test", windowsPath: true);
212
213 // It is possible to extract non-Windows file path, but not
214 // Windows file path from the non-Windows URI (it has a backslash
215 // in a path segment).
216 Expect.equals(test, uri.toFilePath(windowsPath: false));
217 Expect.equals("/$test", absoluteUri.toFilePath(windowsPath: false));
218 Expect.throws(() => uri.toFilePath(windowsPath: true),
219 (e) => e is UnsupportedError);
220 Expect.throws(() => absoluteUri.toFilePath(windowsPath: true));
221 }
222 }
223
224 testFileUriIllegalDriveLetter() {
225 Expect.throws(() => new Uri.file("1:\\", windowsPath: true),
226 (e) => e is ArgumentError);
227 Uri uri = new Uri.file("1:\\", windowsPath: false);
228 Expect.equals("1:\\", uri.toFilePath(windowsPath: false));
229 Expect.throws(() => uri.toFilePath(windowsPath: true),
230 (e) => e is UnsupportedError);
231 }
232
233 main() {
234 testFileUri();
235 testFileUriWindowsSlash();
236 testFileUriDriveLetter();
237 testFileUriWindowsWin32Namespace();
238 testFileUriResolve();
239 testFileUriIllegalCharacters();
240 testFileUriIllegalDriveLetter();
241 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698