use super::{
PathComponentDefect::{CurrentDirectory, Empty, Nul, ParentDirectory, Separator},
*,
};
#[test]
fn one_plain_component_is_accepted_and_every_other_spelling_names_its_defect() {
for accepted in [
"talk",
"MelSpectrogram",
"a.b",
"a b",
"文件",
"...",
".hidden",
"..a",
"a..",
"-",
"a-b_c.d",
"with:colon",
"with*star",
"a\tb",
"a\nb",
] {
assert_eq!(
single_path_component(accepted),
Ok(accepted),
"refused a plain component {accepted:?}"
);
}
for (refused, defect) in [
("", Empty),
("/", Separator),
("/abs", Separator),
("a/b", Separator),
("a/", Separator),
("./a", Separator),
("../escape", Separator),
("..//..", Separator),
("sub/dir", Separator),
("\\", Separator),
("back\\slash", Separator),
("..\\escape", Separator),
(".", CurrentDirectory),
("..", ParentDirectory),
("\0", Nul),
("nul\0byte", Nul),
] {
assert_eq!(
single_path_component(refused),
Err(defect),
"wrong verdict for {refused:?}"
);
}
}
#[test]
fn the_rule_is_only_about_which_directory_a_join_resolves_into() {
let long = "x".repeat(64 * 1024);
assert_eq!(single_path_component(&long), Ok(long.as_str()));
for name in ["CON", "NUL", "aux", "COM1", "$MFT", "lost+found"] {
assert_eq!(single_path_component(name), Ok(name));
}
let nfc = "caf\u{e9}";
let nfd = "cafe\u{301}";
assert_ne!(nfc, nfd);
assert_eq!(single_path_component(nfc), Ok(nfc));
assert_eq!(single_path_component(nfd), Ok(nfd));
}
#[test]
fn every_defect_renders_a_distinct_reason() {
let defects = [Empty, Separator, Nul, CurrentDirectory, ParentDirectory];
for (i, a) in defects.iter().enumerate() {
assert!(!a.reason().is_empty());
for b in &defects[i + 1..] {
assert_ne!(a.reason(), b.reason(), "{a:?} and {b:?} render the same");
}
}
}