std::path::absolute
This commit is contained in:
@@ -1665,6 +1665,64 @@ fn test_ord() {
|
||||
ord!(Equal, "foo/bar", "foo/bar//");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn test_unix_absolute() {
|
||||
use crate::path::absolute;
|
||||
|
||||
assert!(absolute("").is_err());
|
||||
|
||||
let relative = "a/b";
|
||||
let mut expected = crate::env::current_dir().unwrap();
|
||||
expected.push(relative);
|
||||
assert_eq!(absolute(relative).unwrap(), expected);
|
||||
|
||||
// Test how components are collected.
|
||||
assert_eq!(absolute("/a/b/c").unwrap(), Path::new("/a/b/c"));
|
||||
assert_eq!(absolute("/a//b/c").unwrap(), Path::new("/a/b/c"));
|
||||
assert_eq!(absolute("//a/b/c").unwrap(), Path::new("//a/b/c"));
|
||||
assert_eq!(absolute("///a/b/c").unwrap(), Path::new("/a/b/c"));
|
||||
assert_eq!(absolute("/a/b/c/").unwrap(), Path::new("/a/b/c/"));
|
||||
assert_eq!(absolute("/a/./b/../c/.././..").unwrap(), Path::new("/a/b/../c/../.."));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(windows)]
|
||||
fn test_windows_absolute() {
|
||||
use crate::path::absolute;
|
||||
// An empty path is an error.
|
||||
assert!(absolute("").is_err());
|
||||
|
||||
let relative = r"a\b";
|
||||
let mut expected = crate::env::current_dir().unwrap();
|
||||
expected.push(relative);
|
||||
assert_eq!(absolute(relative).unwrap(), expected);
|
||||
|
||||
macro_rules! unchanged(
|
||||
($path:expr) => {
|
||||
assert_eq!(absolute($path).unwrap(), Path::new($path));
|
||||
}
|
||||
);
|
||||
|
||||
unchanged!(r"C:\path\to\file");
|
||||
unchanged!(r"C:\path\to\file\");
|
||||
unchanged!(r"\\server\share\to\file");
|
||||
unchanged!(r"\\server.\share.\to\file");
|
||||
unchanged!(r"\\.\PIPE\name");
|
||||
unchanged!(r"\\.\C:\path\to\COM1");
|
||||
unchanged!(r"\\?\C:\path\to\file");
|
||||
unchanged!(r"\\?\UNC\server\share\to\file");
|
||||
unchanged!(r"\\?\PIPE\name");
|
||||
// Verbatim paths are always unchanged, no matter what.
|
||||
unchanged!(r"\\?\path.\to/file..");
|
||||
|
||||
assert_eq!(absolute(r"C:\path..\to.\file.").unwrap(), Path::new(r"C:\path..\to\file"));
|
||||
assert_eq!(absolute(r"C:\path\to\COM1").unwrap(), Path::new(r"\\.\COM1"));
|
||||
assert_eq!(absolute(r"C:\path\to\COM1.txt").unwrap(), Path::new(r"\\.\COM1"));
|
||||
assert_eq!(absolute(r"C:\path\to\COM1 .txt").unwrap(), Path::new(r"\\.\COM1"));
|
||||
assert_eq!(absolute(r"C:\path\to\cOnOuT$").unwrap(), Path::new(r"\\.\cOnOuT$"));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_path_cmp_fast_path_buf_sort(b: &mut test::Bencher) {
|
||||
let prefix = "my/home";
|
||||
|
||||
Reference in New Issue
Block a user