|
| 1 | +use super::os_string_display::OsStringDisplay; |
| 2 | +use std::path::{Component::*, Path}; |
| 3 | + |
| 4 | +/// Get file name or directory name of a path. |
| 5 | +pub fn path_name(path: &Path) -> OsStringDisplay { |
| 6 | + match path.components().last() { |
| 7 | + None | Some(CurDir) => OsStringDisplay::os_string_from("."), |
| 8 | + Some(RootDir) => OsStringDisplay::os_string_from("/"), |
| 9 | + Some(Normal(name)) => OsStringDisplay::os_string_from(name), |
| 10 | + Some(Prefix(prefix)) => OsStringDisplay::os_string_from(prefix.as_os_str()), |
| 11 | + Some(ParentDir) => OsStringDisplay::os_string_from(path), |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +#[cfg(test)] |
| 16 | +use pretty_assertions::assert_eq; |
| 17 | +#[cfg(test)] |
| 18 | +use std::path::PathBuf; |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn empty() { |
| 22 | + let actual = path_name(&PathBuf::new()); |
| 23 | + let expected = OsStringDisplay::os_string_from("."); |
| 24 | + assert_eq!(actual, expected); |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn current_dir() { |
| 29 | + let actual = path_name(&PathBuf::from(".")); |
| 30 | + let expected = OsStringDisplay::os_string_from("."); |
| 31 | + assert_eq!(actual, expected); |
| 32 | +} |
| 33 | + |
| 34 | +#[cfg(unix)] |
| 35 | +#[test] |
| 36 | +fn root_dir() { |
| 37 | + let actual = path_name(&PathBuf::from("/")); |
| 38 | + let expected = OsStringDisplay::os_string_from("/"); |
| 39 | + assert_eq!(actual, expected); |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(windows)] |
| 43 | +#[test] |
| 44 | +fn root_dir() { |
| 45 | + let actual = path_name(&PathBuf::from("C:\\")); |
| 46 | + let expected = OsStringDisplay::os_string_from("c:"); |
| 47 | + assert_eq!(actual, expected); |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(unix)] |
| 51 | +#[test] |
| 52 | +fn normal_relative() { |
| 53 | + let actual = path_name(&PathBuf::from("abc/def/ghi")); |
| 54 | + let expected = OsStringDisplay::os_string_from("ghi"); |
| 55 | + assert_eq!(actual, expected); |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg(unix)] |
| 59 | +#[test] |
| 60 | +fn normal_absolute() { |
| 61 | + let actual = path_name(&PathBuf::from("/abc/def/ghi")); |
| 62 | + let expected = OsStringDisplay::os_string_from("ghi"); |
| 63 | + assert_eq!(actual, expected); |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(unix)] |
| 67 | +#[test] |
| 68 | +fn normal_trailing_separator() { |
| 69 | + let actual = path_name(&PathBuf::from("abc/def/ghi/")); |
| 70 | + let expected = OsStringDisplay::os_string_from("ghi"); |
| 71 | + assert_eq!(actual, expected); |
| 72 | +} |
| 73 | + |
| 74 | +#[cfg(unix)] |
| 75 | +#[test] |
| 76 | +fn parent_dir() { |
| 77 | + let actual = path_name(&PathBuf::from("..")); |
| 78 | + let expected = OsStringDisplay::os_string_from(".."); |
| 79 | + assert_eq!(actual, expected); |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(unix)] |
| 83 | +#[test] |
| 84 | +fn grandparent_dir() { |
| 85 | + let actual = path_name(&PathBuf::from("../..")); |
| 86 | + let expected = OsStringDisplay::os_string_from("../.."); |
| 87 | + assert_eq!(actual, expected); |
| 88 | +} |
0 commit comments