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

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: Style fix 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 ["relative", "relative", "relative"],
12 ["a%20b", "a b", "a b"],
13
14 ["file:///absolute", "/absolute", "\\absolute"],
15 ["file:///absolute", "/absolute", "\\absolute"],
16 ["file:///a/b", "/a/b", "\\a\\b"],
17 ["file:///a/b", "/a/b", "\\a\\b"],
18
19 ["file://server/a/b", unsupported, "\\\\server\\a\\b"],
20 ["file://server/a/b", unsupported, "\\\\server\\a\\b"],
21
22 ["file:///C:/", "/C:/", "C:\\"],
23 ["file:///C:/a/b", "/C:/a/b", "C:\\a\\b"],
24 ["file:///C:/a/b/", "/C:/a/b/", "C:\\a\\b\\"],
25
26 ["http:/a/b", unsupported, unsupported],
27 ["https:/a/b", unsupported, unsupported],
28 ["urn:a:b", unsupported, unsupported],
29 ];
30
31 void check(String s, filePath, bool windowsPath) {
32 Uri uri = Uri.parse(s);
33 if (filePath is Error) {
34 if (filePath is UnsupportedError) {
35 Expect.throws(() => uri.toFilePath(windowsPath: windowsPath),
36 (e) => e is UnsupportedError);
37 } else {
38 Expect.throws(() => uri.toFilePath(windowsPath: windowsPath));
39 }
40 } else {
41 Expect.equals(filePath, uri.toFilePath(windowsPath: windowsPath));
42 Expect.equals(
43 s, new Uri.file(filePath, windowsPath: windowsPath).toString());
44 }
45 }
46
47 for (var test in tests) {
48 check(test[0], test[1], false);
49 check(test[0], test[2], true);
50 }
51 }
52
53 testFileUriDriveLetter() {
54 check(String s, String nonWindows, String windows) {
55 Uri uri;
56 uri = Uri.parse(s);
57 Expect.equals(nonWindows, uri.toFilePath(windowsPath: false));
58 Expect.equals(windows, uri.toFilePath(windowsPath: true));
59 }
60
61 check("file:///C:", "/C:", "C:\\");
62 check("file:///C:/", "/C:/", "C:\\");
63 check("file:///C:a", "/C:a", "C:\\a");
64 //["file:///C:", "/C:", "C:\\"],
ahe 2013/07/29 15:30:16 What are these comments for?
Søren Gjesse 2013/07/30 13:41:26 Scratch-pad. Removed.
65 //["file:///C:a", "/C:a/", "C:\\a"],
66 }
67
68 testFileUriResolve() {
69 var tests = [
70 ["file:///b", "/a", "b", "\\a", "b"],
71 ["file:///b/", "/a", "b/", "\\a", "b\\"],
72 ["file:///a/b", "/a/", "b", "\\a\\", "b"],
73 ["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"],
74 ["file:///a/c/d", "/a/b", "c/d", "\\a\\b", "c\\d"],
75 ["file:///a/c/d/", "/a/b", "c/d/", "\\a\\b", "c\\d\\"],
76 ["file:///a/b/c/d", "/a/b/", "c/d", "\\a\\b\\", "c\\d"],
77 ["file:///a/b/c/d/", "/a/b/", "c/d/", "\\a\\b\\", "c\\d\\"],
78 ];
79
80 check(String s, String absolute, String relative, bool windowsPath) {
81 Uri absoluteUri = new Uri.file(absolute, windowsPath: windowsPath);
82 Uri relativeUri = new Uri.file(relative, windowsPath: windowsPath);
83 String relativeString =
84 windowsPath ? relative.replaceAll("\\", "/") : relative;
85 Expect.equals(s, absoluteUri.resolve(relativeString).toString());
86 Expect.equals(s, absoluteUri.resolveUri(relativeUri).toString());
87 }
88
89 for (var test in tests) {
90 check(test[0], test[1], test[2], false);
91 check(test[0], test[3], test[4], true);
92 }
93 }
94
95 testFileUriIllegalCharacters() {
96 // Illegal characters in windows file names. Not the test of colon
ahe 2013/07/29 15:30:16 Not -> Note.
Søren Gjesse 2013/07/30 13:41:26 Done.
97 // cannot be in position 2 as it is then considered a drive letter
98 // separator and not illegal.
99 var illegalWindowsPaths =
100 ["a<b", "a>b", "aa:b", "a\"b", "a|b", "a?b", "a*b"];
101
102 for (var test in illegalWindowsPaths) {
103 Expect.throws(() => new Uri.file(test, windowsPath: true),
104 (e) => e is ArgumentError);
105 Expect.throws(() => new Uri.file("\\$test", windowsPath: true),
106 (e) => e is ArgumentError);
107
108 // It is possible to create non-Windows URIs, but not Windows URIs.
109 Uri uri = new Uri.file(test, windowsPath: false);
110 Uri absoluteUri = new Uri.file("/$test", windowsPath: false);
111 Expect.throws(() => new Uri.file(test, windowsPath: true));
112 Expect.throws(() => new Uri.file("\\$test", windowsPath: true));
113
114 // It is possible to extract non-Windows file path, but not
115 // Windows file path.
116 Expect.equals(test, uri.toFilePath(windowsPath: false));
117 Expect.equals("/$test", absoluteUri.toFilePath(windowsPath: false));
118 Expect.throws(() => uri.toFilePath(windowsPath: true));
119 Expect.throws(() => absoluteUri.toFilePath(windowsPath: true));
120 }
121
122 // Slash is also an invalid character in Windows file names, but
123 // when creating a non-Windows file URI and extracting the Windows
124 // file path the slashes are separators.
125 illegalWindowsPaths = ["a/b", "a/b/"];
126 for (var test in illegalWindowsPaths) {
127 // It is possible to create non-Windows URIs, but not Windows URIs.
128 Uri uri = new Uri.file(test, windowsPath: false);
129 Uri absoluteUri = new Uri.file("/$test", windowsPath: false);
130 Expect.throws(() => new Uri.file(test, windowsPath: true));
131 Expect.throws(() => new Uri.file("\\$test", windowsPath: true));
132
133 // It is possible to extract both non-Windows file path, and
134 // Windows file path.
135 Expect.equals(test, uri.toFilePath(windowsPath: false));
136 Expect.equals("/$test", absoluteUri.toFilePath(windowsPath: false));
137 Expect.equals(test.replaceAll("/", "\\"),
138 uri.toFilePath(windowsPath: true));
139 Expect.equals("/$test".replaceAll("/", "\\"),
140 absoluteUri.toFilePath(windowsPath: true));
141 }
142
143 // Backslash
144 illegalWindowsPaths = ["a\\b", "a\\b\\"];
145 for (var test in illegalWindowsPaths) {
146 // It is possible to create both non-Windows URIs, and Windows URIs.
147 Uri uri = new Uri.file(test, windowsPath: false);
148 Uri absoluteUri = new Uri.file("/$test", windowsPath: false);
149 new Uri.file(test, windowsPath: true);
150 new Uri.file("\\$test", windowsPath: true);
151
152 // It is possible to extract non-Windows file path, but not
153 // Windows file path.
Bill Hesse 2013/07/30 11:29:32 from the non-Windows URI, not the Windows URI.
Søren Gjesse 2013/07/30 13:41:26 Done.
154 Expect.equals(test, uri.toFilePath(windowsPath: false));
155 Expect.equals("/$test", absoluteUri.toFilePath(windowsPath: false));
156 Expect.throws(() => uri.toFilePath(windowsPath: true),
157 (e) => e is UnsupportedError);
158 Expect.throws(() => absoluteUri.toFilePath(windowsPath: true));
159 }
160 }
161
162 testFileUriIllegalDriveLetter() {
163 Expect.throws(() => new Uri.file("1:\\", windowsPath: true),
164 (e) => e is ArgumentError);
165 Uri uri = new Uri.file("1:\\", windowsPath: false);
166 Expect.equals("1:\\", uri.toFilePath(windowsPath: false));
167 Expect.throws(() => uri.toFilePath(windowsPath: true),
168 (e) => e is UnsupportedError);
169 }
170
171 main() {
172 testFileUri();
173 testFileUriDriveLetter();
174 testFileUriResolve();
175 testFileUriIllegalCharacters();
176 testFileUriIllegalDriveLetter();
177 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698