|
| 1 | +use gix_merge::blob::builtin_driver::binary::{Pick, ResolveWith}; |
| 2 | +use gix_merge::blob::{builtin_driver, Resolution}; |
| 3 | + |
| 4 | +#[test] |
| 5 | +fn binary() { |
| 6 | + assert_eq!( |
| 7 | + builtin_driver::binary(None), |
| 8 | + (Pick::Ours, Resolution::Conflict), |
| 9 | + "by default it picks ours and marks it as conflict" |
| 10 | + ); |
| 11 | + assert_eq!( |
| 12 | + builtin_driver::binary(Some(ResolveWith::Ancestor)), |
| 13 | + (Pick::Ancestor, Resolution::Complete), |
| 14 | + "Otherwise we can pick anything and it will mark it as complete" |
| 15 | + ); |
| 16 | + assert_eq!( |
| 17 | + builtin_driver::binary(Some(ResolveWith::Ours)), |
| 18 | + (Pick::Ours, Resolution::Complete) |
| 19 | + ); |
| 20 | + assert_eq!( |
| 21 | + builtin_driver::binary(Some(ResolveWith::Theirs)), |
| 22 | + (Pick::Theirs, Resolution::Complete) |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +mod text { |
| 27 | + use bstr::ByteSlice; |
| 28 | + use gix_merge::blob::Resolution; |
| 29 | + use pretty_assertions::assert_str_eq; |
| 30 | + |
| 31 | + #[test] |
| 32 | + fn run_baseline() -> crate::Result { |
| 33 | + let root = gix_testtools::scripted_fixture_read_only("text-baseline.sh")?; |
| 34 | + let cases = std::fs::read_to_string(root.join("baseline.cases"))?; |
| 35 | + let mut out = Vec::new(); |
| 36 | + for case in baseline::Expectations::new(&root, &cases) { |
| 37 | + let mut input = imara_diff::intern::InternedInput::default(); |
| 38 | + dbg!(&case.name, case.options); |
| 39 | + let actual = gix_merge::blob::builtin_driver::text( |
| 40 | + &mut out, |
| 41 | + &mut input, |
| 42 | + &case.ours, |
| 43 | + Some(case.ours_marker.as_str().as_ref()), |
| 44 | + &case.base, |
| 45 | + Some(case.base_marker.as_str().as_ref()), |
| 46 | + &case.theirs, |
| 47 | + Some(case.theirs_marker.as_str().as_ref()), |
| 48 | + case.options, |
| 49 | + ); |
| 50 | + let expected_resolution = if case.expected.contains_str("<<<<<<<") { |
| 51 | + Resolution::Conflict |
| 52 | + } else { |
| 53 | + Resolution::Complete |
| 54 | + }; |
| 55 | + assert_eq!(actual, expected_resolution, "{}: resolution mismatch", case.name,); |
| 56 | + assert_str_eq!( |
| 57 | + out.as_bstr().to_str_lossy(), |
| 58 | + case.expected.to_str_lossy(), |
| 59 | + "{}: output mismatch\n{}", |
| 60 | + case.name, |
| 61 | + out.as_bstr() |
| 62 | + ); |
| 63 | + } |
| 64 | + Ok(()) |
| 65 | + } |
| 66 | + |
| 67 | + mod baseline { |
| 68 | + use bstr::BString; |
| 69 | + use gix_merge::blob::builtin_driver::text::{ConflictStyle, ResolveWith}; |
| 70 | + use std::path::Path; |
| 71 | + |
| 72 | + #[derive(Debug)] |
| 73 | + pub struct Expectation { |
| 74 | + pub ours: BString, |
| 75 | + pub ours_marker: String, |
| 76 | + pub theirs: BString, |
| 77 | + pub theirs_marker: String, |
| 78 | + pub base: BString, |
| 79 | + pub base_marker: String, |
| 80 | + pub name: BString, |
| 81 | + pub expected: BString, |
| 82 | + pub options: gix_merge::blob::builtin_driver::text::Options, |
| 83 | + } |
| 84 | + |
| 85 | + pub struct Expectations<'a> { |
| 86 | + root: &'a Path, |
| 87 | + lines: std::str::Lines<'a>, |
| 88 | + } |
| 89 | + |
| 90 | + impl<'a> Expectations<'a> { |
| 91 | + pub fn new(root: &'a Path, cases: &'a str) -> Self { |
| 92 | + Expectations { |
| 93 | + root, |
| 94 | + lines: cases.lines(), |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + impl Iterator for Expectations<'_> { |
| 100 | + type Item = Expectation; |
| 101 | + |
| 102 | + fn next(&mut self) -> Option<Self::Item> { |
| 103 | + let line = self.lines.next()?; |
| 104 | + let mut words = line.split(' '); |
| 105 | + let (Some(ours), Some(base), Some(theirs), Some(output)) = |
| 106 | + (words.next(), words.next(), words.next(), words.next()) |
| 107 | + else { |
| 108 | + panic!("need at least the input and output") |
| 109 | + }; |
| 110 | + |
| 111 | + let read = |rela_path: &str| read_blob(self.root, rela_path); |
| 112 | + |
| 113 | + let mut options = gix_merge::blob::builtin_driver::text::Options::default(); |
| 114 | + for arg in words { |
| 115 | + match arg { |
| 116 | + "--diff3" => options.conflict_style = ConflictStyle::Diff3, |
| 117 | + "--zdiff3" => options.conflict_style = ConflictStyle::ZealousDiff3, |
| 118 | + "--ours" => options.on_conflict = Some(ResolveWith::Ours), |
| 119 | + "--theirs" => options.on_conflict = Some(ResolveWith::Theirs), |
| 120 | + "--union" => options.on_conflict = Some(ResolveWith::Union), |
| 121 | + _ => panic!("Unknown argument to parse into options: '{arg}'"), |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + Some(Expectation { |
| 126 | + ours: read(ours), |
| 127 | + ours_marker: ours.into(), |
| 128 | + theirs: read(theirs), |
| 129 | + theirs_marker: theirs.into(), |
| 130 | + base: read(base), |
| 131 | + base_marker: base.into(), |
| 132 | + expected: read(output), |
| 133 | + name: output.into(), |
| 134 | + options, |
| 135 | + }) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + fn read_blob(root: &Path, rela_path: &str) -> BString { |
| 140 | + std::fs::read(root.join(rela_path)) |
| 141 | + .unwrap_or_else(|_| panic!("Failed to read '{rela_path}' in '{}'", root.display())) |
| 142 | + .into() |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments