use pretty_assertions::assert_eq;
use super::*;
const PATCH: &str = "\
diff --git a/one b/one
--- a/one
+++ b/one
@@ -1 +1 @@
-see diff --git here
+b
diff --git a/two b/two
new file mode 100644
+x
diff --git a/three b/three
+y
";
fn split(patch: &[u8], limit: u64, chunk: usize) -> (Vec<Vec<u8>>, bool) {
let mut splitter = Splitter::new(limit);
let mut files = Vec::new();
for bytes in patch.chunks(chunk) {
let split = splitter.push(bytes);
files.extend(split.files);
if split.cut {
return (files, true);
}
}
let last = splitter.end();
files.extend(last.files);
(files, last.cut)
}
#[test]
fn a_patch_splits_into_the_same_whole_files_however_its_reads_are_cut() {
let patch = PATCH.as_bytes();
let two_at = PATCH.find("diff --git a/two").unwrap();
let three_at = PATCH.find("diff --git a/three").unwrap();
let (one, two, three) = (
&patch[..two_at],
&patch[two_at..three_at],
&patch[three_at..],
);
let whole = patch.len() as u64;
let cases: [(u64, &[&[u8]], bool); 6] = [
(whole, &[one, two, three], false),
(whole - 1, &[one, two], true),
((one.len() + two.len()) as u64, &[one, two], true),
(one.len() as u64, &[one], true),
(one.len() as u64 - 1, &[], true),
(0, &[], true),
];
for (limit, kept, cut) in cases {
let expected: (Vec<Vec<u8>>, bool) = (kept.iter().map(|file| file.to_vec()).collect(), cut);
let at_once = split(patch, limit, patch.len());
assert_eq!(at_once, expected, "whole at {limit}");
for chunk in [1, 5] {
assert_eq!(
split(patch, limit, chunk),
at_once,
"{chunk} bytes at a time at {limit}"
);
}
}
}