Fix GenericPath.parentPath.

This commit is contained in:
Sönke Ludwig 2017-07-07 22:29:12 +02:00
parent b501d419f8
commit 429d5dcb77

View file

@ -480,7 +480,7 @@ struct GenericPath(F) {
auto b = Format.getBackNode(m_path); auto b = Format.getBackNode(m_path);
static const Exception e = new Exception("Path has no parent path"); static const Exception e = new Exception("Path has no parent path");
if (b.length >= m_path.length) throw e; if (b.length >= m_path.length) throw e;
return GenericPath.fromTrustedString(m_path[0 .. b.length]); return GenericPath.fromTrustedString(m_path[0 .. $ - b.length]);
} }
/** Removes any redundant path segments and replaces all separators by the /** Removes any redundant path segments and replaces all separators by the
@ -722,6 +722,29 @@ unittest {
assert(p.toString() == "/foo%2fbar/baz%2Fbam", p.toString); assert(p.toString() == "/foo%2fbar/baz%2Fbam", p.toString);
} }
unittest {
assert(!PosixPath("").hasParentPath);
assert(!PosixPath("/").hasParentPath);
assert(!PosixPath("foo\\bar").hasParentPath);
assert(PosixPath("foo/bar").parentPath.toString() == "foo/");
assert(PosixPath("./foo").parentPath.toString() == "./");
assert(PosixPath("./foo").parentPath.toString() == "./");
assert(!WindowsPath("").hasParentPath);
assert(!WindowsPath("/").hasParentPath);
assert(WindowsPath("foo\\bar").parentPath.toString() == "foo\\");
assert(WindowsPath("foo/bar").parentPath.toString() == "foo/");
assert(WindowsPath("./foo").parentPath.toString() == "./");
assert(WindowsPath("./foo").parentPath.toString() == "./");
assert(!InetPath("").hasParentPath);
assert(!InetPath("/").hasParentPath);
assert(InetPath("foo/bar").parentPath.toString() == "foo/");
assert(InetPath("foo/bar%2Fbaz").parentPath.toString() == "foo/");
assert(InetPath("./foo").parentPath.toString() == "./");
assert(InetPath("./foo").parentPath.toString() == "./");
}
/// Thrown when an invalid string representation of a path is detected. /// Thrown when an invalid string representation of a path is detected.
class PathValidationException : Exception { class PathValidationException : Exception {
this(string text, string file = __FILE__, size_t line = cast(size_t)__LINE__, Throwable next = null) this(string text, string file = __FILE__, size_t line = cast(size_t)__LINE__, Throwable next = null)