Some path functions
Kramer
Kramer_member at pathlink.com
Sun Jul 30 21:06:42 PDT 2006
Here's the unittests. An excessive amount to be sure, but, hey,
sometimes overly verifing your code can be fun. :)
// isRoot
unittest
{
version(Windows)
{
// Valid roots
assert(isRoot(getDrive(getcwd()) ~ sep));
assert(isRoot(std.string.toupper(getDrive(getcwd()) ~ sep), true));
// Invalid roots
char[][] roots = getRoots();
char[] lastRoot = roots[($ - 1)];
char[] succRoot = std.string.succ(lastRoot[0..($ - 2)]) ~
lastRoot[($ - 2)..$];
assert(!isRoot(succRoot));
assert(!isRoot(std.string.tolower(getDrive(getcwd()) ~ sep), true));
}
else version(linux)
{
// Valid roots
assert(isRoot("/"));
assert(isRoot(getRoots()[0]));
// Invalid roots
assert(!isRoot("../"));
assert(!isRoot(r"\"));
}
else
{
pragma(msg, "Unsupported OS");
static assert(0);
}
}
// containsRoot
unittest
{
version(Windows)
{
// Valid roots
assert(containsRoot(getcwd()));
assert(containsRoot(getDrive(getcwd()) ~ sep));
assert(containsRoot(std.string.toupper(getcwd()), true));
// Invalid roots
int idx = std.string.find(getcwd(), getDrive(getcwd()));
assert(!containsRoot(getcwd()[idx..(getDrive(getcwd()).length)]));
assert(!containsRoot(std.string.tolower(getcwd()), true));
assert(!containsRoot(std.string.tolower(getDrive(getcwd()) ~ sep),
true));
assert(!containsRoot(r"\directory\file"));
}
else version(linux)
{
// Valid roots
assert(containsRoot("/usr/d/src"));
// Invalid roots
assert(!containsRoot("../d/src"));
assert(!containsRoot(r"\d/src"));
}
else
{
pragma(msg, "Unsupported OS");
static assert(0);
}
}
// isNormPath
unittest
{
version(Windows)
{
assert(!isNormPath(r" .\"));
assert(!isNormPath(r".\"));
assert(!isNormPath(r"\."));
assert(!isNormPath(r"\.\"));
assert(!isNormPath(r"..\"));
assert(!isNormPath(r"\.."));
assert(!isNormPath(r"\..\"));
assert(!isNormPath(r"\\"));
}
else version(linux)
{
assert(isNormPath(r" .\"));
assert(isNormPath(r".\"));
assert(isNormPath(r"\."));
assert(isNormPath(r"\.\"));
assert(isNormPath(r"..\"));
assert(isNormPath(r"\.."));
assert(isNormPath(r"\..\"));
assert(isNormPath(r"\\"));
}
else
{
pragma(msg, "Unsupported OS");
static assert(0);
}
// These patterns are not normalized paths
assert(!isNormPath("//"));
assert(!isNormPath(" ./"));
assert(!isNormPath("./"));
assert(!isNormPath("/."));
assert(!isNormPath("/./"));
assert(!isNormPath("../"));
assert(!isNormPath("/.."));
assert(!isNormPath("/../"));
assert(!isNormPath(r"\/"));
// These patterns are normalized paths
assert(isNormPath(""));
assert(isNormPath(" "));
assert(isNormPath(" "));
assert(isNormPath(" a b c"));
assert(isNormPath("b"));
assert(isNormPath("."));
assert(isNormPath(".."));
assert(isNormPath(r"\"));
assert(isNormPath("/"));
assert(isNormPath(r"..\.."));
assert(isNormPath("../.."));
// Now test the patterns that are part of the normPath unittest,
duh duh dun...
assert(isNormPath(normPath("")));
assert(isNormPath(normPath(" ")));
assert(isNormPath(normPath(" ")));
assert(isNormPath(normPath(".")));
assert(isNormPath(normPath("..")));
assert(isNormPath(normPath(r"\")));
assert(isNormPath(normPath("/")));
assert(isNormPath(normPath("/.")));
assert(isNormPath(normPath("./")));
assert(isNormPath(normPath(r"\.")));
assert(isNormPath(normPath(r".\")));
assert(isNormPath(normPath(r".\dir1\dir2\file")));
assert(isNormPath(normPath("./dir1/dir2/file")));
assert(isNormPath(normPath(r"dir1\dir2\file\.")));
assert(isNormPath(normPath("dir1/dir2/file/.")));
assert(isNormPath(normPath(r"\dir1\dir2\file\.")));
assert(isNormPath(normPath("/dir1/dir2/file/.")));
assert(isNormPath(normPath(r".\dir1\dir2\file\.")));
assert(isNormPath(normPath("./dir1/dir2/file/.")));
assert(isNormPath(normPath(r".\.\dir1\dir2\file\.")));
assert(isNormPath(normPath("././dir1/dir2/file/.")));
assert(isNormPath(normPath(r".\.\dir1\dir2\file\.\.")));
assert(isNormPath(normPath("././dir1/dir2/file/./.")));
assert(isNormPath(normPath(r"dir1\.\dir2")));
assert(isNormPath(normPath("dir1/./dir2")));
assert(isNormPath(normPath(r"dir1\.\.\.\dir2\.\file")));
assert(isNormPath(normPath("dir1/./././dir2/./file")));
assert(isNormPath(normPath(r".\.")));
assert(isNormPath(normPath("./.")));
assert(isNormPath(normPath(r"\.\")));
assert(isNormPath(normPath("/./")));
assert(isNormPath(normPath(r"\.\.\.")));
assert(isNormPath(normPath("/././.")));
assert(isNormPath(normPath(r".\.\.\")));
assert(isNormPath(normPath("./././")));
assert(isNormPath(normPath("dir")));
assert(isNormPath(normPath("c:")));
assert(isNormPath(normPath(r"c:\..")));
assert(isNormPath(normPath("c:/..")));
assert(isNormPath(normPath(r"\dir")));
assert(isNormPath(normPath("/dir")));
assert(isNormPath(normPath(r"\dir1\file")));
assert(isNormPath(normPath("/dir1/file")));
assert(isNormPath(normPath(r"\dir1\file\")));
assert(isNormPath(normPath("/dir1/file/")));
assert(isNormPath(normPath(r"\dir1\file\..")));
assert(isNormPath(normPath("/dir1/file/..")));
assert(isNormPath(normPath(r"\dir1\dir2\..\file")));
assert(isNormPath(normPath("/dir1/dir2/../file")));
assert(isNormPath(normPath(r"\dir1\file..")));
assert(isNormPath(normPath("/dir1/file..")));
assert(isNormPath(normPath(r"\dir1\file\..\..")));
assert(isNormPath(normPath("/dir1/file/../..")));
assert(isNormPath(normPath("c:..file")));
assert(isNormPath(normPath(r"c:\..\dir1\..\file")));
assert(isNormPath(normPath("c:/../dir1/../file")));
assert(isNormPath(normPath(r"c:..file\dir1")));
assert(isNormPath(normPath("c:..file/dir1")));
assert(isNormPath(normPath(r"c:..file\dir1\..")));
assert(isNormPath(normPath("c:..file/dir1/..")));
assert(isNormPath(normPath(".")));
assert(isNormPath(normPath(r"c:\")));
assert(isNormPath(normPath("c:/")));
assert(isNormPath(normPath(r"c:\dir1\.\")));
assert(isNormPath(normPath("c:/dir1/./")));
assert(isNormPath(normPath(r"c:\dir1\.\file")));
assert(isNormPath(normPath("c:/dir1/./file")));
assert(isNormPath(normPath(r"c:\dir1\.\file\")));
assert(isNormPath(normPath("c:/dir1/./file/")));
assert(isNormPath(normPath(r"c:\dir1\..\file")));
assert(isNormPath(normPath("c:/dir1/../file")));
assert(isNormPath(normPath(r"c:\dir1\..\file\")));
assert(isNormPath(normPath("c:/dir1/../file/")));
assert(isNormPath(normPath(r"c:\dir1\..\file\.")));
assert(isNormPath(normPath("c:/dir1/../file/.")));
assert(isNormPath(normPath(r"c:\dir1\..\file\.\")));
assert(isNormPath(normPath("c:/dir1/../file/./")));
assert(isNormPath(normPath(r"c:\dir1\..\file\..")));
assert(isNormPath(normPath("c:/dir1/../file/..")));
assert(isNormPath(normPath(r"\\\dir1\file")));
assert(isNormPath(normPath("///dir1/file")));
assert(isNormPath(normPath(r"\\\dir1\..\dir2\file")));
assert(isNormPath(normPath("///dir1/../dir2/file")));
assert(isNormPath(normPath(r"\\\file")));
assert(isNormPath(normPath("///file")));
assert(isNormPath(normPath(r"..\..")));
assert(isNormPath(normPath("../..")));
assert(isNormPath(normPath(r"..\\\..")));
assert(isNormPath(normPath("..///..")));
assert(isNormPath(normPath(r"..\")));
assert(isNormPath(normPath("../")));
assert(isNormPath(normPath(r"..\\\\")));
assert(isNormPath(normPath("..////")));
assert(isNormPath(normPath(r".\")));
assert(isNormPath(normPath("./")));
assert(isNormPath(normPath(r"\.\file")));
assert(isNormPath(normPath("/./file")));
assert(isNormPath(normPath("c:" ~ sep ~ "dir1" ~ sep ~ "." ~ sep)));
assert(isNormPath(normPath("c:" ~ sep ~ "dir1" ~ sep ~ "." ~ sep ~
"file")));
assert(isNormPath(normPath("c:" ~ sep ~ "dir1" ~ sep ~ "." ~ sep ~
"file" ~ sep)));
assert(isNormPath(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~
"file")));
assert(isNormPath(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~
"file" ~ sep)));
assert(isNormPath(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~
"file" ~ sep ~ ".")));
assert(isNormPath(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~
"file" ~ sep ~ "." ~ sep)));
assert(isNormPath(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~
"file" ~ sep ~ "..")));
assert(isNormPath(normPath(sep ~ sep ~ sep ~ "dir1" ~ sep ~ "file")));
assert(isNormPath(normPath(sep ~ sep ~ sep ~ "dir1" ~ sep ~ ".." ~
sep ~ "dir2" ~ sep ~ "file")));
assert(isNormPath(normPath(sep ~ sep ~ sep ~ "file")));
assert(isNormPath(normPath(".." ~ sep ~ "..")));
assert(isNormPath(normPath(".." ~ sep ~ sep ~ sep ~ "..")));
assert(isNormPath(normPath(".." ~ sep)));
assert(isNormPath(normPath(".." ~ sep ~ sep ~ sep ~ sep)));
assert(isNormPath(normPath("." ~ sep)));
assert(isNormPath(normPath(sep ~ "." ~ sep ~ "file")));
assert(isNormPath(normPath(r"~\dir")));
assert(isNormPath(normPath("~/dir")));
assert(isNormPath(normPath(sep ~ "..")));
assert(isNormPath(normPath(sep ~ ".." ~ sep)));
assert(isNormPath(normPath(sep ~ ".." ~ sep ~ "dir")));
assert(isNormPath(normPath(sep ~ "..file")));
assert(isNormPath(normPath(sep ~ ".file")));
assert(isNormPath(normPath(".file")));
assert(isNormPath(normPath("file.")));
assert(isNormPath(normPath("file.file" ~ sep ~ "." ~ sep ~ "dir1" ~
sep ~ "dir2" ~ sep ~ "..")));
assert(isNormPath(normPath("file$.")));
}
// normPath
unittest
{
version(Windows)
{
assert(normPath("c:") == "c:");
assert(normPath("c:" ~ sep ~ "..") == r"c:" ~ sep);
assert(normPath("c:" ~ sep ~ ".." ~ sep ~ "dir1" ~ sep ~ ".." ~ sep
~ "file") == "c:" ~ sep ~ "file");
assert(normPath("c:" ~ sep) == "c:" ~ sep);
assert(normPath("c:" ~ sep ~ "dir1" ~ sep ~ "." ~ sep) == "c:" ~
sep ~ "dir1");
assert(normPath("c:" ~ sep ~ "dir1" ~ sep ~ "." ~ sep ~ "file") ==
"c:" ~ sep ~ "dir1" ~ sep ~ "file");
assert(normPath("c:" ~ sep ~ "dir1" ~ sep ~ "." ~ sep ~ "file" ~
sep) == "c:" ~ sep ~ "dir1" ~ sep ~ "file");
assert(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~ "file") ==
"c:" ~ sep ~ "file");
assert(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~ "file" ~
sep) == "c:" ~ sep ~ "file");
assert(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~ "file" ~
sep ~ ".") == "c:" ~ sep ~ "file");
assert(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~ "file" ~
sep ~ "." ~ sep) == "c:" ~ sep ~ "file");
assert(normPath("c:" ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~ "file" ~
sep ~ "..") == "c:" ~ sep);
assert(normPath(r"\\/dir1////\\dir2//..\\\\file/./") == sep ~
"dir1" ~ sep ~ "file");
}
else version(linux)
{
assert(normPath("c:/../file") == "file");
assert(normPath(r"c:\..\file") == r"c:\..\file");
assert(normPath("~/dir") == "~/dir");
}
else
{
pragma(msg, "Unsupported OS");
static assert(0);
}
assert(normPath("") == ".");
assert(normPath(r" ./") == r" .");
assert(normPath(" ") == " ");
assert(normPath(" ") == " ");
assert(normPath(".") == ".");
assert(normPath("..") == "..");
assert(normPath(sep) == sep);
assert(normPath(sep ~ curdir) == sep);
assert(normPath(curdir ~ sep) == curdir);
assert(normPath(curdir ~ sep ~ "dir1" ~ sep ~ "dir2" ~ sep ~
"file") == "dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(normPath("dir1" ~ sep ~ "dir2" ~ sep ~ "file" ~ sep ~
curdir) == "dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(normPath(sep ~ "dir1" ~ sep ~ "dir2" ~ sep ~ "file" ~ sep ~
curdir) == sep ~ "dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(normPath(curdir ~ sep ~ "dir1" ~ sep ~ "dir2" ~ sep ~ "file"
~ sep ~ curdir) == "dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(normPath(curdir ~ sep ~ curdir ~ sep ~ "dir1" ~ sep ~ "dir2"
~ sep ~ "file" ~ sep ~ curdir) == "dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(normPath(curdir ~ sep ~ curdir ~ sep ~ "dir1" ~ sep ~ "dir2"
~ sep ~ "file" ~ sep ~ curdir ~ sep ~ curdir) == "dir1" ~ sep ~ "dir2" ~
sep ~ "file");
assert(normPath("dir1" ~ sep ~ curdir ~ sep ~ "dir2") == "dir1" ~
sep ~ "dir2");
assert(normPath("dir1" ~ sep ~ curdir ~ sep ~ curdir ~ sep ~ curdir
~ sep ~ "dir2" ~ sep ~ curdir ~ sep ~ "file") == "dir1" ~ sep ~ "dir2" ~
sep ~ "file");
assert(normPath(curdir ~ sep ~ curdir) == curdir);
assert(normPath(sep ~ curdir ~ sep) == sep);
assert(normPath(sep ~ curdir ~ sep ~ curdir ~ sep ~ curdir) == sep);
assert(normPath(curdir ~ sep ~ curdir ~ sep ~ curdir ~ sep) == curdir);
assert(normPath("dir") == "dir");
assert(normPath(sep ~ "dir") == sep ~ "dir");
assert(normPath(sep ~ "dir1" ~ sep ~ "file") == sep ~ "dir1" ~ sep
~ "file");
assert(normPath(sep ~ "dir1" ~ sep ~ "file" ~ sep) == sep ~ "dir1"
~ sep ~ "file");
assert(normPath(sep ~ "dir1" ~ sep ~ "file" ~ sep ~ "..") == sep ~
"dir1");
assert(normPath(sep ~ "dir1" ~ sep ~ "dir2" ~ sep ~ ".." ~ sep ~
"file") == sep ~ "dir1" ~ sep ~ "file");
assert(normPath(sep ~ "dir1" ~ sep ~ "file..") == sep ~ "dir1" ~
sep ~ "file..");
assert(normPath(sep ~ "dir1" ~ sep ~ "file" ~ sep ~ ".." ~ sep ~
"..") == sep);
assert(normPath("c:..file") == "c:..file");
assert(normPath("c:..file" ~ sep ~ "dir1") == "c:..file" ~ sep ~
"dir1");
assert(normPath("c:..file" ~ sep ~ "dir1" ~ sep ~ "..") == "c:..file");
assert(normPath(sep ~ sep ~ sep ~ "dir1" ~ sep ~ "file") == sep ~
"dir1" ~ sep ~ "file");
assert(normPath(sep ~ sep ~ sep ~ "dir1" ~ sep ~ ".." ~ sep ~
"dir2" ~ sep ~ "file") == sep ~ "dir2" ~ sep ~ "file");
assert(normPath(sep ~ sep ~ sep ~ "file") == sep ~ "file");
assert(normPath(".." ~ sep ~ "..") == ".." ~ sep ~ "..");
assert(normPath(".." ~ sep ~ sep ~ sep ~ "..") == ".." ~ sep ~ "..");
assert(normPath(".." ~ sep) == "..");
assert(normPath(".." ~ sep ~ sep ~ sep ~ sep) == "..");
assert(normPath("." ~ sep) == ".");
assert(normPath(sep ~ "." ~ sep ~ "file") == sep ~ "file");
assert(normPath(r"~\dir") == r"~\dir");
assert(normPath("~") == "~");
assert(normPath(sep ~ "..") == sep);
assert(normPath(sep ~ ".." ~ sep) == sep);
assert(normPath(sep ~ ".." ~ sep ~ "dir") == sep ~ "dir");
assert(normPath(sep ~ "..file") == sep ~ "..file");
assert(normPath(sep ~ ".file") == sep ~ ".file");
assert(normPath(".file") == ".file");
assert(normPath("file.") == "file.");
assert(normPath("file.file" ~ sep ~ "." ~ sep ~ "dir1" ~ sep ~
"dir2" ~ sep ~ "..") == "file.file" ~ sep ~ "dir1");
assert(normPath("file$.") == "file$.");
}
// normCase
unittest
{
version(Windows)
{
assert(normCase(r"C:/dir1\file1/dir2/File2") ==
r"c:\dir1\file1\dir2\file2");
}
else version(linux)
{
assert(normCase(r"/Dir1\file1\DIR2/file2") ==
r"/Dir1\file1\DIR2/file2");
}
else
{
pragma(msg, "Unsupported OS");
static assert(0);
}
}
// normSep
unittest
{
version(Windows)
{
assert(normSep(r"c:/dir\file") == r"c:\dir\file");
}
else version(linux)
{
assert(normSep(r"\dir1/dir2\file") == r"\dir1/dir2\file");
}
else
{
pragma(msg, "Unsupported OS");
static assert(0);
}
}
// join
unittest
{
assert(join(r"\dir/file", "file2", r"file3\file4") == r"\dir/file"
~ sep ~ "file2" ~ sep ~ r"file3\file4");
assert(join(r"\dir1\file") == r"\dir1\file");
assert(join(r"\dir1\file", r"dir2\file") == r"\dir1\file" ~ sep ~
r"dir2\file");
assert(join(r"\dir1\file", r"dir2\file", r"dir3\file") ==
r"\dir1\file" ~ sep ~ r"dir2\file" ~ sep ~ r"dir3\file");
assert(join("file1", "file2", "file3", "file4") == "file1" ~ sep ~
"file2" ~ sep ~ "file3" ~ sep ~ "file4");
assert(join("file1", "file2"w, "file3"d, "file4"c) == "file1" ~ sep
~ "file2" ~ sep ~ "file3" ~ sep ~ "file4");
assert(join("f", "i"w, "l"d, "e"c) == "f" ~ sep ~ "i" ~ sep ~ "l" ~
sep ~ "e");
char c = 'i'; wchar w = 'l'; dchar d = 'e';
assert(join("f", c, w, d) == "f" ~ sep ~ "i" ~ sep ~ "l" ~ sep ~ "e");
assert(join('f', 'i', 'l', 'e') == "f" ~ sep ~ "i" ~ sep ~ "l" ~
sep ~ "e");
version(Windows)
{
assert(join("file1", r"\dir1\file2") == r"\dir1\file2");
assert(join(r"\file1", r"\dir1") == r"\dir1");
assert(join(r"\file1", "dir1", r"c:\dir2\file2") == r"c:\dir2\file2");
assert(join("file1", r"\dir1", r"c:\dir2", r"d:\dir3", r"file3") ==
r"d:\dir3\file3");
}
version(linux)
{
assert(join("file1", r"/dir1\file2") == r"/dir1\file2");
assert(join(r"\file1", r"\dir1") == r"\file1/\dir1");
assert(join(r"\file1", "dir1", r"c:\dir2\file2") ==
r"\file1/dir1/c:\dir2\file2");
assert(join("file1", r"\dir1", r"c:\dir2", r"d:\dir3", r"file3") ==
r"file1/\dir1/c:\dir2/d:\dir3/file3");
}
}
// absPath
unittest
{
version(Windows)
{
assert(absPath(r"c:\dir1\file1\") == r"c:\dir1\file1");
}
else version(linux)
{
assert(absPath("file") == std.path.join(getcwd(), "file"));
}
else
{
pragma(msg, "Unsupported OS");
static assert(0);
}
assert(absPath("file") == std.path.join(getcwd(), "file"));
assert(absPath(r"\dir1\file") == std.path.join(getcwd(),
r"\dir1\file"));
assert(absPath(getcwd()));
assert(absPath(absPath(getcwd())));
assert(absPath(absPath(getDirName(getcwd()))));
}
// expandPath
unittest
{
version(Windows)
{
assert(expandPath("", "u:") == getcwd() ~ sep ~ "u:");
assert(expandPath("", r"u:\") == r"u:\");
assert(expandPath("", r"u:\dir1") == r"u:\dir1");
assert(expandPath("", r"u:\dir1\") == r"u:\dir1");
assert(expandPath("", r"u:\dir1\file\..") == r"u:\dir1");
assert(expandPath("", r"u:\dir1\..\file\.") == r"u:\file");
assert(expandPath("", r"\dir1\\:file") == getcwd() ~ r"\dir1\:file");
assert(expandPath("", r"\:dir1\\file") == getcwd() ~ r"\:dir1\file");
assert(expandPath("", "u:") == getcwd() ~ r"\u:");
assert(expandPath("", r"u:\") == r"u:\");
assert(expandPath("", r"u:\dir1") == r"u:\dir1");
assert(expandPath("", r"u:\dir1\") == r"u:\dir1");
assert(expandPath("", r"u:\dir1\file\..") == r"u:\dir1");
assert(expandPath("", r"u:\dir1\..\file\.") == r"u:\file");
assert(expandPath("file", r"p:\" ~ "dir1") == r"p:\" ~ "dir1" ~ sep
~ "file");
assert(expandPath("file", r"p:\" ~ "dir1" ~ sep) == r"p:\" ~ "dir1"
~ sep ~ "file");
assert(expandPath("file", r"p:\" ~ "dir1" ~ sep ~ sep) == r"p:\" ~
"dir1" ~ sep ~ "file");
assert(expandPath("file", "p:\\dir1\\\\dir2\\") == "p:" ~ sep ~
"dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(expandPath("file",
"p:\\dir1\\\\dir2\\\\\\dir3\\\\\\\\dir4\\\\") == "p:" ~ sep ~ "dir1" ~
sep ~ "dir2" ~ sep ~ "dir3" ~ sep ~ "dir4" ~ sep ~ "file");
assert(expandPath("file", "p:") == getcwd() ~ sep ~ "p:" ~ sep ~
"file");
assert(expandPath("file", "p:\\") == "p:" ~ sep ~ "file");
assert(expandPath("file" , "p:\\dir\\.") == "p:" ~ sep ~ "dir" ~
sep ~ "file");
assert(expandPath("file" , "p:\\dir1\\.\\dir2\\") == "p:" ~ sep ~
"dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(expandPath("file" , "p:\\dir1\\.\\dir2\\.") == "p:" ~ sep ~
"dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(expandPath("file" , "p:\\dir1\\.\\dir2\\.\\") == "p:" ~ sep
~ "dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(expandPath("", r"\") == getcwd());
assert(expandPath("", r"\dir1") == getcwd() ~ r"\dir1");
assert(expandPath("", r"\dir1\\file") == getcwd() ~ r"\dir1\file");
assert(expandPath("", r"\..\dir1\\file") == getDirName(getcwd()) ~
r"\dir1\file");
assert(expandPath("", r"\\\dir1\\file\\..\\dir2\\dir3\\..") ==
getcwd() ~ r"\dir1\dir2");
assert(expandPath("file") == getcwd() ~ r"\file");
assert(expandPath(r"\dir\file") == getcwd() ~ r"\dir\file");
assert(expandPath("file", r"\dir") == getcwd() ~ r"\dir\file");
assert(expandPath("file", r"....\") == getcwd() ~ sep ~ "...." ~
sep ~ "file");
assert(expandPath("file", "a.\\") == getcwd() ~ sep ~ "a." ~ sep ~
"file");
assert(expandPath("file", "..\\..") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath("file", "..\\..\\") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath("file", "..\\..\\..\\") ==
getDirName(getDirName(getDirName(getcwd()))) ~ sep ~ "file");
assert(expandPath("file", "..\\..\\..\\..\\") ==
getDirName(getDirName(getDirName(getDirName(getcwd())))) ~ sep ~ "file");
assert(expandPath("file" , ".\\dir") == getcwd() ~ sep ~ "dir" ~
sep ~ "file");
assert(expandPath("file" , "dir1\\.") == getcwd() ~ sep ~ "dir1" ~
sep ~ "file");
assert(expandPath("file" , "dir1\\.\\dir2") == getcwd() ~ sep ~
"dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(expandPath("file" , ".\\dir1\\.\\dir2") == getcwd() ~ sep ~
"dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(expandPath("file" , ".\\dir1\\.\\dir2\\.") == getcwd() ~ sep
~ "dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(expandPath("file" , ".\\..\\..\\.\\.") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath("file", ".\\..\\.\\.\\..\\") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath("file" , r"\\.\\dir1\\.\\dir2\\.") == getcwd() ~
sep ~ "dir1" ~ sep ~ "dir2" ~ sep ~ "file");
assert(expandPath("file", "\\.\\") == getcwd() ~ sep ~ "file");
assert(expandPath("file", "\\") == getcwd() ~ sep ~ "file");
assert(expandPath("file", "\\\\") == getcwd() ~ sep ~ "file");
assert(expandPath("file", "\\..\\..") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath("file", r"c:\..\..") == r"c:\file");
assert(expandPath("file", "\\..\\..\\") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath(r"\dir1\dir2/file", "/refdir") == getcwd() ~ sep
~ "refdir" ~ sep ~ "dir1" ~ sep ~ "dir2" ~ sep ~ "file");
}
else version(linux)
{
assert(expandPath("file") == getcwd() ~ "/file");
assert(expandPath(r"/dir/file") == "/dir/file");
assert(expandPath("file", "/dir") == "/dir/file");
assert(expandPath("", r"\") == getcwd() ~ r"/\");
assert(expandPath("", "/") == "/");
assert(expandPath("", r"\dir1") == getcwd() ~ r"/\dir1");
assert(expandPath("", r"\dir1\\file") == getcwd() ~ r"/\dir1\\file");
assert(expandPath("", r"/dir1\\file///file2") == r"/dir1\\file/file2");
assert(expandPath("", r"\..\dir1\\file") == getcwd() ~
r"/\..\dir1\\file");
assert(expandPath("", r"\\\dir1\\file\\..\\dir2\\dir3\\..") ==
getcwd() ~ r"/\\\dir1\\file\\..\\dir2\\dir3\\..");
assert(expandPath("", r"/dir1/file/../dir2/dir3/..") == "/dir1/dir2");
assert(expandPath("file") == getcwd() ~ "/file");
assert(expandPath(r"\dir\file") == getcwd() ~ r"/\dir\file");
assert(expandPath(r"/dir\file") == r"/dir\file");
assert(expandPath("file", r"\dir") == getcwd() ~ r"/\dir/file");
assert(expandPath("file", r"....\") == getcwd() ~ sep ~ r"....\" ~
sep ~ "file");
assert(expandPath("file", r"a.\") == getcwd() ~ sep ~ r"a.\" ~ sep
~ "file");
assert(expandPath("file", r"..\..") == getcwd() ~ r"/..\../file");
assert(expandPath("file", "../..") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath("file", r"..\..\") == getcwd() ~ r"/..\..\/file");
assert(expandPath("file", "../../") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath("file", r"..\..\..\") == getcwd() ~
r"/..\..\..\/file");
assert(expandPath("file", "../../../") ==
getDirName(getDirName(getDirName(getcwd()))) ~ sep ~ "file");
assert(expandPath("file", r"..\..\..\..\") == getcwd() ~
r"/..\..\..\..\/file");
assert(expandPath("file", "../../../../") ==
getDirName(getDirName(getDirName(getDirName(getcwd())))) ~ sep ~ "file");
assert(expandPath("file", r".\dir") == getcwd() ~ r"/.\dir/file");
assert(expandPath("file", r"dir1\.") == getcwd() ~ r"/dir1\./file");
assert(expandPath("file", r"dir1\.\dir2") == getcwd() ~
r"/dir1\.\dir2/file");
assert(expandPath("file", r".\dir1\.\dir2") == getcwd() ~
r"/.\dir1\.\dir2/file");
assert(expandPath("file", r".\dir1\.\dir2\.") == getcwd() ~
r"/.\dir1\.\dir2\./file");
assert(expandPath("file", r".\..\..\.\.") == getcwd() ~
r"/.\..\..\.\./file");
assert(expandPath("file", "./../.././.") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath("file", r".\..\.\.\..\") == getcwd() ~
r"/.\..\.\.\..\/file");
assert(expandPath("file", "./../././../") ==
getDirName(getDirName(getcwd())) ~ sep ~ "file");
assert(expandPath("file" , r"\\.\\dir1\\.\\dir2\\.") == getcwd() ~
r"/\\.\\dir1\\.\\dir2\\./file");
assert(expandPath("file" , "//.//dir1//.//dir2//.") == sep ~ "dir1"
~ sep ~ "dir2" ~ sep ~ "file");
assert(expandPath("file", r"\.\") == getcwd() ~ r"/\.\/file");
assert(expandPath("file", r"\") == getcwd() ~ r"/\/file");
assert(expandPath("file", r"\\") == getcwd() ~ r"/\\/file");
assert(expandPath("file", r"\..\..") == getcwd() ~ r"/\..\../file");
assert(expandPath("file", "/../..") == "/file");
assert(expandPath("file", r"\..\..\") == getcwd() ~ r"/\..\..\/file");
assert(expandPath(r"\dir1\dir2/file", "/refdir") ==
r"/refdir/\dir1\dir2/file");
}
else
{
pragma(msg, "Unsupported OS");
static assert(0);
}
assert(expandPath("") == "");
assert(expandPath("file") == getcwd() ~ sep ~ "file");
assert(expandPath("file", "") == getcwd() ~ sep ~ "file");
assert(expandPath("file", "..") == getDirName(getcwd()) ~ sep ~
"file");
assert(expandPath("..") == getDirName(getcwd()));
assert(expandPath(getcwd()));
assert(expandPath(getcwd(), ""));
assert(expandPath(getcwd(), getcwd()));
assert(expandPath(getcwd(), "refdir"));
}
More information about the Digitalmars-d-announce
mailing list