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

Unified 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, 5 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 side-by-side diff with in-line comments
Download patch
Index: tests/corelib/uri_file_test.dart
diff --git a/tests/corelib/uri_file_test.dart b/tests/corelib/uri_file_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..7d8d766007736341c85431dc83a77b621b710fbe
--- /dev/null
+++ b/tests/corelib/uri_file_test.dart
@@ -0,0 +1,177 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "package:expect/expect.dart";
+
+testFileUri() {
+ final unsupported = new UnsupportedError("");
+
+ var tests = [
+ ["relative", "relative", "relative"],
+ ["a%20b", "a b", "a b"],
+
+ ["file:///absolute", "/absolute", "\\absolute"],
+ ["file:///absolute", "/absolute", "\\absolute"],
+ ["file:///a/b", "/a/b", "\\a\\b"],
+ ["file:///a/b", "/a/b", "\\a\\b"],
+
+ ["file://server/a/b", unsupported, "\\\\server\\a\\b"],
+ ["file://server/a/b", unsupported, "\\\\server\\a\\b"],
+
+ ["file:///C:/", "/C:/", "C:\\"],
+ ["file:///C:/a/b", "/C:/a/b", "C:\\a\\b"],
+ ["file:///C:/a/b/", "/C:/a/b/", "C:\\a\\b\\"],
+
+ ["http:/a/b", unsupported, unsupported],
+ ["https:/a/b", unsupported, unsupported],
+ ["urn:a:b", unsupported, unsupported],
+ ];
+
+ void check(String s, filePath, bool windowsPath) {
+ Uri uri = Uri.parse(s);
+ if (filePath is Error) {
+ if (filePath is UnsupportedError) {
+ Expect.throws(() => uri.toFilePath(windowsPath: windowsPath),
+ (e) => e is UnsupportedError);
+ } else {
+ Expect.throws(() => uri.toFilePath(windowsPath: windowsPath));
+ }
+ } else {
+ Expect.equals(filePath, uri.toFilePath(windowsPath: windowsPath));
+ Expect.equals(
+ s, new Uri.file(filePath, windowsPath: windowsPath).toString());
+ }
+ }
+
+ for (var test in tests) {
+ check(test[0], test[1], false);
+ check(test[0], test[2], true);
+ }
+}
+
+testFileUriDriveLetter() {
+ check(String s, String nonWindows, String windows) {
+ Uri uri;
+ uri = Uri.parse(s);
+ Expect.equals(nonWindows, uri.toFilePath(windowsPath: false));
+ Expect.equals(windows, uri.toFilePath(windowsPath: true));
+ }
+
+ check("file:///C:", "/C:", "C:\\");
+ check("file:///C:/", "/C:/", "C:\\");
+ check("file:///C:a", "/C:a", "C:\\a");
+ //["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.
+ //["file:///C:a", "/C:a/", "C:\\a"],
+}
+
+testFileUriResolve() {
+ var tests = [
+ ["file:///b", "/a", "b", "\\a", "b"],
+ ["file:///b/", "/a", "b/", "\\a", "b\\"],
+ ["file:///a/b", "/a/", "b", "\\a\\", "b"],
+ ["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"],
+ ["file:///a/c/d", "/a/b", "c/d", "\\a\\b", "c\\d"],
+ ["file:///a/c/d/", "/a/b", "c/d/", "\\a\\b", "c\\d\\"],
+ ["file:///a/b/c/d", "/a/b/", "c/d", "\\a\\b\\", "c\\d"],
+ ["file:///a/b/c/d/", "/a/b/", "c/d/", "\\a\\b\\", "c\\d\\"],
+ ];
+
+ check(String s, String absolute, String relative, bool windowsPath) {
+ Uri absoluteUri = new Uri.file(absolute, windowsPath: windowsPath);
+ Uri relativeUri = new Uri.file(relative, windowsPath: windowsPath);
+ String relativeString =
+ windowsPath ? relative.replaceAll("\\", "/") : relative;
+ Expect.equals(s, absoluteUri.resolve(relativeString).toString());
+ Expect.equals(s, absoluteUri.resolveUri(relativeUri).toString());
+ }
+
+ for (var test in tests) {
+ check(test[0], test[1], test[2], false);
+ check(test[0], test[3], test[4], true);
+ }
+}
+
+testFileUriIllegalCharacters() {
+ // 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.
+ // cannot be in position 2 as it is then considered a drive letter
+ // separator and not illegal.
+ var illegalWindowsPaths =
+ ["a<b", "a>b", "aa:b", "a\"b", "a|b", "a?b", "a*b"];
+
+ for (var test in illegalWindowsPaths) {
+ Expect.throws(() => new Uri.file(test, windowsPath: true),
+ (e) => e is ArgumentError);
+ Expect.throws(() => new Uri.file("\\$test", windowsPath: true),
+ (e) => e is ArgumentError);
+
+ // It is possible to create non-Windows URIs, but not Windows URIs.
+ Uri uri = new Uri.file(test, windowsPath: false);
+ Uri absoluteUri = new Uri.file("/$test", windowsPath: false);
+ Expect.throws(() => new Uri.file(test, windowsPath: true));
+ Expect.throws(() => new Uri.file("\\$test", windowsPath: true));
+
+ // It is possible to extract non-Windows file path, but not
+ // Windows file path.
+ Expect.equals(test, uri.toFilePath(windowsPath: false));
+ Expect.equals("/$test", absoluteUri.toFilePath(windowsPath: false));
+ Expect.throws(() => uri.toFilePath(windowsPath: true));
+ Expect.throws(() => absoluteUri.toFilePath(windowsPath: true));
+ }
+
+ // Slash is also an invalid character in Windows file names, but
+ // when creating a non-Windows file URI and extracting the Windows
+ // file path the slashes are separators.
+ illegalWindowsPaths = ["a/b", "a/b/"];
+ for (var test in illegalWindowsPaths) {
+ // It is possible to create non-Windows URIs, but not Windows URIs.
+ Uri uri = new Uri.file(test, windowsPath: false);
+ Uri absoluteUri = new Uri.file("/$test", windowsPath: false);
+ Expect.throws(() => new Uri.file(test, windowsPath: true));
+ Expect.throws(() => new Uri.file("\\$test", windowsPath: true));
+
+ // It is possible to extract both non-Windows file path, and
+ // Windows file path.
+ Expect.equals(test, uri.toFilePath(windowsPath: false));
+ Expect.equals("/$test", absoluteUri.toFilePath(windowsPath: false));
+ Expect.equals(test.replaceAll("/", "\\"),
+ uri.toFilePath(windowsPath: true));
+ Expect.equals("/$test".replaceAll("/", "\\"),
+ absoluteUri.toFilePath(windowsPath: true));
+ }
+
+ // Backslash
+ illegalWindowsPaths = ["a\\b", "a\\b\\"];
+ for (var test in illegalWindowsPaths) {
+ // It is possible to create both non-Windows URIs, and Windows URIs.
+ Uri uri = new Uri.file(test, windowsPath: false);
+ Uri absoluteUri = new Uri.file("/$test", windowsPath: false);
+ new Uri.file(test, windowsPath: true);
+ new Uri.file("\\$test", windowsPath: true);
+
+ // It is possible to extract non-Windows file path, but not
+ // 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.
+ Expect.equals(test, uri.toFilePath(windowsPath: false));
+ Expect.equals("/$test", absoluteUri.toFilePath(windowsPath: false));
+ Expect.throws(() => uri.toFilePath(windowsPath: true),
+ (e) => e is UnsupportedError);
+ Expect.throws(() => absoluteUri.toFilePath(windowsPath: true));
+ }
+}
+
+testFileUriIllegalDriveLetter() {
+ Expect.throws(() => new Uri.file("1:\\", windowsPath: true),
+ (e) => e is ArgumentError);
+ Uri uri = new Uri.file("1:\\", windowsPath: false);
+ Expect.equals("1:\\", uri.toFilePath(windowsPath: false));
+ Expect.throws(() => uri.toFilePath(windowsPath: true),
+ (e) => e is UnsupportedError);
+}
+
+main() {
+ testFileUri();
+ testFileUriDriveLetter();
+ testFileUriResolve();
+ testFileUriIllegalCharacters();
+ testFileUriIllegalDriveLetter();
+}

Powered by Google App Engine
This is Rietveld 408576698