提交 28745ce7 编写于 作者: E Erick Tryzelaar 提交者: Graydon Hoare

libcore: Clean up the path tests

上级 ab5d8425
......@@ -498,90 +498,132 @@ impl WindowsPath : GenericPath {
move cs
}
#[test]
fn test_double_slash_collapsing()
{
let path = PosixPath("tmp/");
let path = path.push("/hmm");
let path = path.normalize();
assert ~"tmp/hmm" == path.to_str();
let path = WindowsPath("tmp/");
let path = path.push("/hmm");
let path = path.normalize();
assert ~"tmp\\hmm" == path.to_str();
}
mod posix {
// Various windows helpers, and tests for the impl.
mod windows {
#[inline(always)]
pub pure fn is_sep(u: u8) -> bool {
u == '/' as u8 || u == '\\' as u8
}
#[cfg(test)]
fn mk(s: &str) -> PosixPath { PosixPath(s) }
pub pure fn extract_unc_prefix(s: &str) -> Option<(~str,~str)> {
if (s.len() > 1 &&
s[0] == '\\' as u8 &&
s[1] == '\\' as u8) {
let mut i = 2;
while i < s.len() {
if s[i] == '\\' as u8 {
let pre = s.slice(2, i);
let rest = s.slice(i, s.len());
return Some((move pre, move rest));
}
i += 1;
}
}
None
}
#[cfg(test)]
fn t(wp: &PosixPath, s: &str) {
let ss = wp.to_str();
let sss = str::from_slice(s);
if (ss != sss) {
debug!("got %s", ss);
debug!("expected %s", sss);
assert ss == sss;
pub pure fn extract_drive_prefix(s: &str) -> Option<(~str,~str)> {
unsafe {
if (s.len() > 1 &&
libc::isalpha(s[0] as libc::c_int) != 0 &&
s[1] == ':' as u8) {
let rest = if s.len() == 2 {
~""
} else {
s.slice(2, s.len())
};
return Some((s.slice(0,1), move rest));
}
None
}
}
}
#[cfg(tests)]
mod tests {
#[test]
fn test_double_slash_collapsing() {
let path = PosixPath("tmp/");
let path = path.push("/hmm");
let path = path.normalize();
assert ~"tmp/hmm" == path.to_str();
let path = WindowsPath("tmp/");
let path = path.push("/hmm");
let path = path.normalize();
assert ~"tmp\\hmm" == path.to_str();
}
#[test]
fn test_filetype_foo_bar() {
let wp = mk("foo.bar");
let wp = PosixPath("foo.bar");
assert wp.filetype() == Some(~".bar");
let wp = WindowsPath("foo.bar");
assert wp.filetype() == Some(~".bar");
}
#[test]
fn test_filetype_foo() {
let wp = mk("foo");
let wp = PosixPath("foo");
assert wp.filetype() == None;
let wp = WindowsPath("foo");
assert wp.filetype() == None;
}
#[test]
fn test_posix_paths() {
t(&(mk("hi")), "hi");
t(&(mk("/lib")), "/lib");
t(&(mk("hi/there")), "hi/there");
t(&(mk("hi/there.txt")), "hi/there.txt");
fn t(wp: &PosixPath, s: &str) {
let ss = wp.to_str();
let sss = str::from_slice(s);
if (ss != sss) {
debug!("got %s", ss);
debug!("expected %s", sss);
assert ss == sss;
}
}
t(&(PosixPath("hi")), "hi");
t(&(PosixPath("/lib")), "/lib");
t(&(PosixPath("hi/there")), "hi/there");
t(&(PosixPath("hi/there.txt")), "hi/there.txt");
t(&(mk("hi/there.txt")), "hi/there.txt");
t(&(mk("hi/there.txt")
t(&(PosixPath("hi/there.txt")), "hi/there.txt");
t(&(PosixPath("hi/there.txt")
.with_filetype("")), "hi/there");
t(&(mk("/a/b/c/there.txt")
t(&(PosixPath("/a/b/c/there.txt")
.with_dirname("hi")), "hi/there.txt");
t(&(mk("hi/there.txt")
t(&(PosixPath("hi/there.txt")
.with_dirname(".")), "./there.txt");
t(&(mk("a/b/c")
t(&(PosixPath("a/b/c")
.push("..")), "a/b/c/..");
t(&(mk("there.txt")
t(&(PosixPath("there.txt")
.with_filetype("o")), "there.o");
t(&(mk("hi/there.txt")
t(&(PosixPath("hi/there.txt")
.with_filetype("o")), "hi/there.o");
t(&(mk("hi/there.txt")
t(&(PosixPath("hi/there.txt")
.with_filetype("o")
.with_dirname("/usr/lib")),
"/usr/lib/there.o");
t(&(mk("hi/there.txt")
t(&(PosixPath("hi/there.txt")
.with_filetype("o")
.with_dirname("/usr/lib/")),
"/usr/lib/there.o");
t(&(mk("hi/there.txt")
t(&(PosixPath("hi/there.txt")
.with_filetype("o")
.with_dirname("/usr//lib//")),
"/usr/lib/there.o");
t(&(mk("/usr/bin/rust")
t(&(PosixPath("/usr/bin/rust")
.push_many([~"lib", ~"thingy.so"])
.with_filestem("librustc")),
"/usr/bin/rust/lib/librustc.so");
......@@ -590,86 +632,55 @@ fn test_posix_paths() {
#[test]
fn test_normalize() {
t(&(mk("hi/there.txt")
fn t(wp: &PosixPath, s: &str) {
let ss = wp.to_str();
let sss = str::from_slice(s);
if (ss != sss) {
debug!("got %s", ss);
debug!("expected %s", sss);
assert ss == sss;
}
}
t(&(PosixPath("hi/there.txt")
.with_dirname(".").normalize()), "there.txt");
t(&(mk("a/b/../c/././/../foo.txt/").normalize()),
t(&(PosixPath("a/b/../c/././/../foo.txt/").normalize()),
"a/foo.txt");
t(&(mk("a/b/c")
t(&(PosixPath("a/b/c")
.push("..").normalize()), "a/b");
}
}
// Various windows helpers, and tests for the impl.
mod windows {
#[inline(always)]
pub pure fn is_sep(u: u8) -> bool {
u == '/' as u8 || u == '\\' as u8
}
pub pure fn extract_unc_prefix(s: &str) -> Option<(~str,~str)> {
if (s.len() > 1 &&
s[0] == '\\' as u8 &&
s[1] == '\\' as u8) {
let mut i = 2;
while i < s.len() {
if s[i] == '\\' as u8 {
let pre = s.slice(2, i);
let rest = s.slice(i, s.len());
return Some((move pre, move rest));
}
i += 1;
}
}
None
}
pub pure fn extract_drive_prefix(s: &str) -> Option<(~str,~str)> {
unsafe {
if (s.len() > 1 &&
libc::isalpha(s[0] as libc::c_int) != 0 &&
s[1] == ':' as u8) {
let rest = if s.len() == 2 {
~""
} else {
s.slice(2, s.len())
};
return Some((s.slice(0,1), move rest));
}
None
}
}
#[test]
fn test_extract_unc_prefixes() {
assert extract_unc_prefix("\\\\").is_none();
assert extract_unc_prefix("\\\\hi").is_none();
assert extract_unc_prefix("\\\\hi\\") == Some((~"hi", ~"\\"));
assert extract_unc_prefix("\\\\hi\\there") ==
assert windows::extract_unc_prefix("\\\\").is_none();
assert windows::extract_unc_prefix("\\\\hi").is_none();
assert windows::extract_unc_prefix("\\\\hi\\") ==
Some((~"hi", ~"\\"));
assert windows::extract_unc_prefix("\\\\hi\\there") ==
Some((~"hi", ~"\\there"));
assert extract_unc_prefix("\\\\hi\\there\\friends.txt") ==
assert windows::extract_unc_prefix("\\\\hi\\there\\friends.txt") ==
Some((~"hi", ~"\\there\\friends.txt"));
}
#[test]
fn test_extract_drive_prefixes() {
assert extract_drive_prefix("c").is_none();
assert extract_drive_prefix("c:") == Some((~"c", ~""));
assert extract_drive_prefix("d:") == Some((~"d", ~""));
assert extract_drive_prefix("z:") == Some((~"z", ~""));
assert extract_drive_prefix("c:\\hi") == Some((~"c", ~"\\hi"));
assert extract_drive_prefix("d:hi") == Some((~"d", ~"hi"));
assert extract_drive_prefix("c:hi\\there.txt") ==
assert windows::extract_drive_prefix("c").is_none();
assert windows::extract_drive_prefix("c:") == Some((~"c", ~""));
assert windows::extract_drive_prefix("d:") == Some((~"d", ~""));
assert windows::extract_drive_prefix("z:") == Some((~"z", ~""));
assert windows::extract_drive_prefix("c:\\hi") ==
Some((~"c", ~"\\hi"));
assert windows::extract_drive_prefix("d:hi") == Some((~"d", ~"hi"));
assert windows::extract_drive_prefix("c:hi\\there.txt") ==
Some((~"c", ~"hi\\there.txt"));
assert extract_drive_prefix("c:\\hi\\there.txt") ==
assert windows::extract_drive_prefix("c:\\hi\\there.txt") ==
Some((~"c", ~"\\hi\\there.txt"));
}
#[test]
fn test_windows_paths() {
fn mk(s: &str) -> WindowsPath { WindowsPath(s) }
fn t(wp: &WindowsPath, s: &str) {
let ss = wp.to_str();
let sss = str::from_slice(s);
......@@ -680,51 +691,34 @@ fn t(wp: &WindowsPath, s: &str) {
}
}
t(&(mk("hi")), "hi");
t(&(mk("hi/there")), "hi\\there");
t(&(mk("hi/there.txt")), "hi\\there.txt");
t(&(WindowsPath("hi")), "hi");
t(&(WindowsPath("hi/there")), "hi\\there");
t(&(WindowsPath("hi/there.txt")), "hi\\there.txt");
t(&(mk("there.txt")
t(&(WindowsPath("there.txt")
.with_filetype("o")), "there.o");
t(&(mk("hi/there.txt")
t(&(WindowsPath("hi/there.txt")
.with_filetype("o")), "hi\\there.o");
t(&(mk("hi/there.txt")
t(&(WindowsPath("hi/there.txt")
.with_filetype("o")
.with_dirname("c:\\program files A")),
"c:\\program files A\\there.o");
t(&(mk("hi/there.txt")
t(&(WindowsPath("hi/there.txt")
.with_filetype("o")
.with_dirname("c:\\program files B\\")),
"c:\\program files B\\there.o");
t(&(mk("hi/there.txt")
t(&(WindowsPath("hi/there.txt")
.with_filetype("o")
.with_dirname("c:\\program files C\\/")),
"c:\\program files C\\there.o");
t(&(mk("c:\\program files (x86)\\rust")
t(&(WindowsPath("c:\\program files (x86)\\rust")
.push_many([~"lib", ~"thingy.dll"])
.with_filename("librustc.dll")),
"c:\\program files (x86)\\rust\\lib\\librustc.dll");
}
#[cfg(test)]
fn mk(s: &str) -> PosixPath { PosixPath(s) }
#[test]
fn test_filetype_foo_bar() {
let wp = mk("foo.bar");
assert wp.filetype() == Some(~".bar");
}
#[test]
fn test_filetype_foo() {
let wp = mk("foo");
assert wp.filetype() == None;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册