diff --git a/src/utils/mod.rs b/src/utils/mod.rs index cfe84ce..a404c25 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -37,7 +37,7 @@ impl PartialEq for Node { } pub fn is_a_parent_of(parent: &str, child: &str) -> bool { - child.starts_with(parent) && child.chars().nth(parent.chars().count()) == Some('/') + (child.starts_with(parent) && child.chars().nth(parent.chars().count()) == Some('/')) || parent == "/" } pub fn simplify_dir_names(filenames: Vec<&str>) -> HashSet { @@ -232,4 +232,18 @@ mod tests { correct.insert("src_v2".to_string()); assert_eq!(simplify_dir_names(vec!["src/", "src_v2"]), correct); } + + #[test] + fn test_is_a_parent_of() { + assert!(is_a_parent_of("/usr", "/usr/andy")); + assert!(is_a_parent_of("/usr", "/usr/andy/i/am/descendant")); + assert!(!is_a_parent_of("/usr/andy", "/usr")); + assert!(!is_a_parent_of("/usr/andy", "/usr/sibling")); + } + + #[test] + fn test_is_a_parent_of_root() { + assert!(is_a_parent_of("/", "/usr/andy")); + assert!(is_a_parent_of("/", "/usr")); + } }