test: add test cases for cycle rename

This commit is contained in:
zhaoyang 2026-06-11 21:38:27 +08:00
parent 6f2b642d59
commit e4c5df99b5

View file

@ -409,44 +409,70 @@ mod tests {
#[test] #[test]
fn test_sort() { fn test_sort() {
fn cmp(input: &[(&str, &str)], expected: &[(&str, &str)]) { fn cmp(input: &[(&str, &str)], chain: &[(&str, &str)], cycles: &[&[(&str, &str)]]) {
let sorted = BulkRename::prioritized_paths( let (res_chain, res_cycles) = BulkRename::prioritized_paths(
input.iter().map(|&(o, _)| Tuple::new(0, o)).collect(), input.iter().map(|&(o, _)| Tuple::new(0, o)).collect(),
input.iter().map(|&(_, n)| Tuple::new(0, n)).collect(), input.iter().map(|&(_, n)| Tuple::new(0, n)).collect(),
); );
let sorted: Vec<_> = let flat = |v: &[(Tuple, Tuple)]| -> Vec<(String, String)> {
sorted.iter().map(|(o, n)| (o.to_str().unwrap(), n.to_str().unwrap())).collect(); v.iter().map(|(o, n)| (o.to_str().unwrap().into(), n.to_str().unwrap().into())).collect()
assert_eq!(sorted, expected); };
let want = |v: &[(&str, &str)]| -> Vec<(String, String)> {
v.iter().map(|&(o, n)| (o.into(), n.into())).collect()
};
assert_eq!(flat(&res_chain), want(chain));
assert_eq!(
res_cycles.iter().map(|c| flat(c)).collect::<Vec<_>>(),
cycles.iter().map(|c| want(c)).collect::<Vec<_>>()
);
} }
#[rustfmt::skip] #[rustfmt::skip]
cmp( cmp(
&[("2", "3"), ("1", "2"), ("3", "4")], &[("2", "3"), ("1", "2"), ("3", "4")],
&[("3", "4"), ("2", "3"), ("1", "2")] &[("3", "4"), ("2", "3"), ("1", "2")],
&[]
); );
#[rustfmt::skip] #[rustfmt::skip]
cmp( cmp(
&[("1", "3"), ("2", "3"), ("3", "4")], &[("1", "3"), ("2", "3"), ("3", "4")],
&[("3", "4"), ("1", "3"), ("2", "3")] &[("3", "4"), ("1", "3"), ("2", "3")],
&[]
);
#[rustfmt::skip]
cmp(
&[("b", "b_"), ("a", "a_"), ("c", "c_")],
&[("b", "b_"), ("a", "a_"), ("c", "c_")],
&[]
); );
#[rustfmt::skip] #[rustfmt::skip]
cmp( cmp(
&[("2", "1"), ("1", "2")], &[("1", "2"), ("2", "1")],
&[("2", "1"), ("1", "2")] &[],
&[&[("1", "2"), ("2", "1")]]
);
#[rustfmt::skip]
cmp(
&[("1", "2"), ("2", "3"), ("3", "1")],
&[],
&[&[("1", "2"), ("2", "3"), ("3", "1")]]
); );
#[rustfmt::skip] #[rustfmt::skip]
cmp( cmp(
&[("3", "2"), ("2", "1"), ("1", "3"), ("a", "b"), ("b", "c")], &[("3", "2"), ("2", "1"), ("1", "3"), ("a", "b"), ("b", "c")],
&[("b", "c"), ("a", "b"), ("3", "2"), ("2", "1"), ("1", "3")] &[("b", "c"), ("a", "b")],
&[&[("3", "2"), ("2", "1"), ("1", "3")]]
); );
#[rustfmt::skip] #[rustfmt::skip]
cmp( cmp(
&[("b", "b_"), ("a", "a_"), ("c", "c_")], &[("1", "2"), ("2", "1"), ("3", "4"), ("4", "3")],
&[("b", "b_"), ("a", "a_"), ("c", "c_")], &[],
&[&[("1", "2"), ("2", "1")], &[("3", "4"), ("4", "3")]]
); );
} }
} }