From 16aa8e8f1065ebe7a8a08cb9da6ae51df2168d8f Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 4 Nov 2023 15:51:39 -0700 Subject: [PATCH] test_merged_tree: nest each part of `test_diff_dir_file()` I'm about to add a few more checks for diffing with a matcher. I think it will help make it readable and reduce the risk of mixing up variables between each part of the test if we use some nested blocks. I also removed some unnecessary `.clone()` calls while at it. --- lib/tests/test_merged_tree.rs | 248 +++++++++++++++++----------------- 1 file changed, 124 insertions(+), 124 deletions(-) diff --git a/lib/tests/test_merged_tree.rs b/lib/tests/test_merged_tree.rs index 545dfdea7..1bf72b994 100644 --- a/lib/tests/test_merged_tree.rs +++ b/lib/tests/test_merged_tree.rs @@ -906,137 +906,137 @@ fn test_diff_dir_file() { (&path6.join(&file), "right"), ], ); - let left_merged = MergedTree::new(Merge::new( - vec![left_base.clone()], - vec![left_side1.clone(), left_side2.clone()], - )); - let right_merged = MergedTree::new(Merge::new( - vec![right_base.clone()], - vec![right_side1.clone(), right_side2.clone()], - )); + let left_merged = MergedTree::new(Merge::new(vec![left_base], vec![left_side1, left_side2])); + let right_merged = + MergedTree::new(Merge::new(vec![right_base], vec![right_side1, right_side2])); // Test the forwards diff - let actual_diff = left_merged - .diff(&right_merged, &EverythingMatcher) - .map(|(path, diff)| (path, diff.unwrap())) - .collect_vec(); - let expected_diff = vec![ - // path1: file1 -> directory1 - ( - path1.clone(), - (left_merged.path_value(&path1), Merge::absent()), - ), - ( - path1.join(&file), - (Merge::absent(), right_merged.path_value(&path1.join(&file))), - ), - // path2: file1 -> directory1+(directory2-absent) - ( - path2.clone(), - (left_merged.path_value(&path2), Merge::absent()), - ), - ( - path2.join(&file), - (Merge::absent(), right_merged.path_value(&path2.join(&file))), - ), - // path3: file1 -> directory1+(file1-absent) - ( - path3.clone(), + { + let actual_diff = left_merged + .diff(&right_merged, &EverythingMatcher) + .map(|(path, diff)| (path, diff.unwrap())) + .collect_vec(); + let expected_diff = vec![ + // path1: file1 -> directory1 ( - left_merged.path_value(&path3), - right_merged.path_value(&path3), + path1.clone(), + (left_merged.path_value(&path1), Merge::absent()), ), - ), - // path4: file1+(file2-file3) -> directory1+(directory2-directory3) - ( - path4.clone(), - (left_merged.path_value(&path4), Merge::absent()), - ), - ( - path4.join(&file), - (Merge::absent(), right_merged.path_value(&path4.join(&file))), - ), - // path5: directory1 -> file1+(file2-absent) - ( - path5.join(&file), - (left_merged.path_value(&path5.join(&file)), Merge::absent()), - ), - ( - path5.clone(), - (Merge::absent(), right_merged.path_value(&path5)), - ), - // path6: directory1 -> file1+(directory1-absent) - ( - path6.join(&file), - (left_merged.path_value(&path6.join(&file)), Merge::absent()), - ), - ( - path6.clone(), - (Merge::absent(), right_merged.path_value(&path6)), - ), - ]; - assert_eq!(actual_diff, expected_diff); + ( + path1.join(&file), + (Merge::absent(), right_merged.path_value(&path1.join(&file))), + ), + // path2: file1 -> directory1+(directory2-absent) + ( + path2.clone(), + (left_merged.path_value(&path2), Merge::absent()), + ), + ( + path2.join(&file), + (Merge::absent(), right_merged.path_value(&path2.join(&file))), + ), + // path3: file1 -> directory1+(file1-absent) + ( + path3.clone(), + ( + left_merged.path_value(&path3), + right_merged.path_value(&path3), + ), + ), + // path4: file1+(file2-file3) -> directory1+(directory2-directory3) + ( + path4.clone(), + (left_merged.path_value(&path4), Merge::absent()), + ), + ( + path4.join(&file), + (Merge::absent(), right_merged.path_value(&path4.join(&file))), + ), + // path5: directory1 -> file1+(file2-absent) + ( + path5.join(&file), + (left_merged.path_value(&path5.join(&file)), Merge::absent()), + ), + ( + path5.clone(), + (Merge::absent(), right_merged.path_value(&path5)), + ), + // path6: directory1 -> file1+(directory1-absent) + ( + path6.join(&file), + (left_merged.path_value(&path6.join(&file)), Merge::absent()), + ), + ( + path6.clone(), + (Merge::absent(), right_merged.path_value(&path6)), + ), + ]; + assert_eq!(actual_diff, expected_diff); + } + // Test the reverse diff - let actual_diff = right_merged - .diff(&left_merged, &EverythingMatcher) - .map(|(path, diff)| (path, diff.unwrap())) - .collect_vec(); - let expected_diff = vec![ - // path1: file1 -> directory1 - ( - path1.join(&file), - (right_merged.path_value(&path1.join(&file)), Merge::absent()), - ), - ( - path1.clone(), - (Merge::absent(), left_merged.path_value(&path1)), - ), - // path2: file1 -> directory1+(directory2-absent) - ( - path2.join(&file), - (right_merged.path_value(&path2.join(&file)), Merge::absent()), - ), - ( - path2.clone(), - (Merge::absent(), left_merged.path_value(&path2)), - ), - // path3: file1 -> directory1+(file1-absent) - ( - path3.clone(), + { + let actual_diff = right_merged + .diff(&left_merged, &EverythingMatcher) + .map(|(path, diff)| (path, diff.unwrap())) + .collect_vec(); + let expected_diff = vec![ + // path1: file1 -> directory1 ( - right_merged.path_value(&path3), - left_merged.path_value(&path3), + path1.join(&file), + (right_merged.path_value(&path1.join(&file)), Merge::absent()), ), - ), - // path4: file1+(file2-file3) -> directory1+(directory2-directory3) - ( - path4.join(&file), - (right_merged.path_value(&path4.join(&file)), Merge::absent()), - ), - ( - path4.clone(), - (Merge::absent(), left_merged.path_value(&path4)), - ), - // path5: directory1 -> file1+(file2-absent) - ( - path5.clone(), - (right_merged.path_value(&path5), Merge::absent()), - ), - ( - path5.join(&file), - (Merge::absent(), left_merged.path_value(&path5.join(&file))), - ), - // path6: directory1 -> file1+(directory1-absent) - ( - path6.clone(), - (right_merged.path_value(&path6), Merge::absent()), - ), - ( - path6.join(&file), - (Merge::absent(), left_merged.path_value(&path6.join(&file))), - ), - ]; - assert_eq!(actual_diff, expected_diff); + ( + path1.clone(), + (Merge::absent(), left_merged.path_value(&path1)), + ), + // path2: file1 -> directory1+(directory2-absent) + ( + path2.join(&file), + (right_merged.path_value(&path2.join(&file)), Merge::absent()), + ), + ( + path2.clone(), + (Merge::absent(), left_merged.path_value(&path2)), + ), + // path3: file1 -> directory1+(file1-absent) + ( + path3.clone(), + ( + right_merged.path_value(&path3), + left_merged.path_value(&path3), + ), + ), + // path4: file1+(file2-file3) -> directory1+(directory2-directory3) + ( + path4.join(&file), + (right_merged.path_value(&path4.join(&file)), Merge::absent()), + ), + ( + path4.clone(), + (Merge::absent(), left_merged.path_value(&path4)), + ), + // path5: directory1 -> file1+(file2-absent) + ( + path5.clone(), + (right_merged.path_value(&path5), Merge::absent()), + ), + ( + path5.join(&file), + (Merge::absent(), left_merged.path_value(&path5.join(&file))), + ), + // path6: directory1 -> file1+(directory1-absent) + ( + path6.clone(), + (right_merged.path_value(&path6), Merge::absent()), + ), + ( + path6.join(&file), + (Merge::absent(), left_merged.path_value(&path6.join(&file))), + ), + ]; + assert_eq!(actual_diff, expected_diff); + } } /// Merge 3 resolved trees that can be resolved