use super::*;
fn landed_change(commit: &str, paths: &[&str]) -> LandedChange {
return LandedChange {
commit: String::from(commit),
paths: paths.iter().map(|path| { return String::from(*path); }).collect(),
};
}
fn capture(worktree: &str, sequence: i64, paths: &[&str]) -> CaptureView {
let owned: Vec<String> = paths.iter().map(|path| { return String::from(*path); }).collect();
return capture_view(worktree, sequence, &owned);
}
fn landed_records(entries: &[(&str, CaptureView)]) -> BTreeMap<String, CaptureView> {
return entries
.iter()
.map(|(commit, view)| { return (String::from(*commit), view.clone()); })
.collect();
}
#[test]
fn the_listing_attaches_records_to_their_commit() {
let parsed: Vec<LandedChange> = parse_commit_changes(
"abc\0:100644 100644 1 2 M\0a.txt\0:100644 100644 1 2 M\0b.txt\0def\0",
)
.expect("listing");
assert_eq!(parsed, vec![landed_change("abc", &["a.txt", "b.txt"]), landed_change("def", &[])]);
assert!(parse_commit_changes("").expect("empty").is_empty());
}
#[test]
fn a_malformed_listing_fails() {
assert!(parse_commit_changes(":100644 x\0a.txt\0").is_err());
assert!(parse_commit_changes("abc\0:100644 x").is_err());
}
#[test]
fn an_unread_path_is_unordered() {
let captured: CaptureView = capture("w", 3, &["other.txt"]);
let history: Vec<LandedChange> = vec![landed_change("c1", &["a.txt"])];
let landed: BTreeMap<String, CaptureView> = landed_records(&[("c1", capture("w", 1, &["a.txt"]))]);
assert_eq!(decide_capture_order("a.txt", &captured, &history, &landed), CaptureOrderDecision::Unordered);
}
#[test]
fn a_path_no_landed_commit_changed_is_unordered() {
let captured: CaptureView = capture("w", 3, &["a.txt"]);
assert_eq!(decide_capture_order("a.txt", &captured, &[], &BTreeMap::new()), CaptureOrderDecision::Unordered);
}
#[test]
fn a_landed_commit_without_a_matching_capture_is_unordered() {
let captured: CaptureView = capture("w", 3, &["a.txt"]);
let history: Vec<LandedChange> = vec![landed_change("c1", &["a.txt"])];
assert_eq!(
decide_capture_order("a.txt", &captured, &history, &BTreeMap::new()),
CaptureOrderDecision::Unordered
);
let other: BTreeMap<String, CaptureView> = landed_records(&[("c1", capture("elsewhere", 1, &["a.txt"]))]);
assert_eq!(decide_capture_order("a.txt", &captured, &history, &other), CaptureOrderDecision::Unordered);
let unread: BTreeMap<String, CaptureView> = landed_records(&[("c1", capture("w", 1, &["b.txt"]))]);
assert_eq!(decide_capture_order("a.txt", &captured, &history, &unread), CaptureOrderDecision::Unordered);
}
#[test]
fn the_later_capture_wins() {
let history: Vec<LandedChange> = vec![landed_change("c1", &["a.txt"]), landed_change("c2", &["a.txt"])];
let landed: BTreeMap<String, CaptureView> = landed_records(&[
("c1", capture("w", 2, &["a.txt"])),
("c2", capture("w", 5, &["a.txt"])),
]);
assert_eq!(
decide_capture_order("a.txt", &capture("w", 6, &["a.txt"]), &history, &landed),
CaptureOrderDecision::Prepared
);
assert_eq!(
decide_capture_order("a.txt", &capture("w", 4, &["a.txt"]), &history, &landed),
CaptureOrderDecision::Landed
);
}