diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2012-11-17 20:09:51 -0800 |
|---|---|---|
| committer | Graydon Hoare <graydon@mozilla.com> | 2012-11-20 16:39:30 -0800 |
| commit | 28745ce7c8d23444130b10cf101875ddbb4e174d (patch) | |
| tree | a6c07f47d24944dc6bc31b5534e5795a74a05ea3 /src | |
| parent | ab5d84258e25eb74d0293df444e200561010b5df (diff) | |
| download | rust-28745ce7c8d23444130b10cf101875ddbb4e174d.tar.gz rust-28745ce7c8d23444130b10cf101875ddbb4e174d.zip | |
libcore: Clean up the path tests
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/path.rs | 248 |
1 files changed, 121 insertions, 127 deletions
diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 12056d3571e..187eda37324 100644 --- a/src/libcore/path.rs +++ b/src/libcore/path.rs @@ -498,90 +498,132 @@ pub pure fn normalize(components: &[~str]) -> ~[~str] { 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 @@ mod posix { #[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 @@ mod windows { } } - 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; - } - } |
