use super::*;
fn text(value: &str) -> Side {
Side {
present: true,
size: value.len() as u64,
text: Some(value.to_string()),
}
}
fn missing() -> Side {
Side {
present: false,
size: 0,
text: None,
}
}
fn binary(size: u64) -> Side {
Side {
present: true,
size,
text: None,
}
}
fn compared(here: Side, there: Side) -> Versions {
compare(&RepositoryId::root(), "src/main.rs", here, there)
}
#[test]
fn test9998_two_versions_produce_a_diff_between_them() {
let versions = compared(
text("fn main() {\n one();\n}\n"),
text("fn main() {\n two();\n}\n"),
);
let diff = versions.diff.expect("a diff between two texts");
assert!(diff.contains("- one();"), "this machine's line is missing:\n{diff}");
assert!(diff.contains("+ two();"), "the peer's line is missing:\n{diff}");
assert!(diff.contains("a/src/main.rs"), "{diff}");
assert!(diff.contains("(this machine)"), "{diff}");
assert!(diff.contains("(the peer)"), "{diff}");
assert_eq!(versions.reason, None, "a diff needs no excuse");
assert!(versions.here.text.unwrap().contains("one();"));
assert!(versions.there.text.unwrap().contains("two();"));
}
#[test]
fn test9999_every_undiffable_conflict_says_why() {
let deleted_here = compared(missing(), text("still here\n"));
assert_eq!(deleted_here.reason.as_deref(), Some("deleted_here"));
assert!(deleted_here.diff.is_none());
assert!(!deleted_here.here.present);
let deleted_there = compared(text("still here\n"), missing());
assert_eq!(deleted_there.reason.as_deref(), Some("deleted_there"));
assert!(deleted_there.diff.is_none());
let binary_side = compared(binary(64), text("readable\n"));
assert_eq!(binary_side.reason.as_deref(), Some("binary"));
assert!(binary_side.diff.is_none());
let huge = compared(binary(MAX_BYTES + 1), text("small\n"));
assert_eq!(huge.reason.as_deref(), Some("too_large"));
let same = compared(text("agreed\n"), text("agreed\n"));
assert_eq!(same.reason.as_deref(), Some("identical"));
assert!(same.diff.is_none());
}
#[test]
fn test10000_relabelling_leaves_the_content_alone() {
let versions = compared(text("keep\n-- not a header\n"), text("keep\n-- changed\n"));
let diff = versions.diff.expect("a diff");
let (header, body) = diff
.split_once("@@")
.map(|(head, rest)| (head.to_string(), format!("@@{rest}")))
.expect("a diff has a hunk");
assert!(header.contains("--- a/src/main.rs"), "{header}");
assert!(header.contains("(this machine)"), "{header}");
assert_eq!(
header.lines().filter(|line| line.starts_with("--- ")).count(),
1,
"one header line, not one per line that looks like one:\n{header}"
);
assert!(
body.lines().any(|line| line == "--- not a header"),
"a removed line that looks like a header must survive as content:\n{body}"
);
assert!(
body.lines().any(|line| line == "+-- changed"),
"the peer's line must survive:\n{body}"
);
assert!(
!body.contains("this machine") && !body.contains("the peer"),
"nothing inside a hunk may be relabelled:\n{body}"
);
}