Skip to content

Commit

Permalink
conflicts: add resolve_trivial() and test invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvonz committed Jun 19, 2023
1 parent 3f95daf commit 038b64d
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/src/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::hash::Hash;
use std::io::Write;

use itertools::Itertools;

use crate::backend::{BackendResult, FileId, ObjectId, TreeValue};
use crate::diff::{find_line_ranges, Diff, DiffHunk};
use crate::files::{ConflictHunk, MergeHunk, MergeResult};
use crate::merge::trivial_merge;
use crate::repo_path::RepoPath;
use crate::store::Store;
use crate::{backend, files};
Expand Down Expand Up @@ -86,6 +88,13 @@ impl<T> Conflict<T> {
}
self
}

pub fn resolve_trivial(&self) -> Option<&T>
where
T: Eq + Hash,
{
trivial_merge(&self.removes, &self.adds)
}
}

impl<T> Conflict<Option<T>> {
Expand Down Expand Up @@ -680,4 +689,37 @@ mod tests {
assert_eq!(c(&[0, 1], &[2, 3, 3]).simplify(), c(&[0, 1], &[2, 3, 3]));
assert_eq!(c(&[0, 1], &[2, 3, 4]).simplify(), c(&[0, 1], &[2, 3, 4]));
}

#[test]
fn test_conflict_invariants() {
fn check_invariants(removes: &[u32], adds: &[u32]) {
let conflict = Conflict::new(removes.to_vec(), adds.to_vec());
// `simplify()` is idempotent
assert_eq!(
conflict.clone().simplify().simplify(),
conflict.clone().simplify(),
"simplify() not idempotent for {conflict:?}"
);
// `resolve_trivial()` is unaffected by `simplify()`
assert_eq!(
conflict.clone().simplify().resolve_trivial(),
conflict.resolve_trivial(),
"simplify() changed result of resolve_trivial() for {conflict:?}"
);
}
// 1-way "conflict"
check_invariants(&[], &[0]);
for i in 0..=1 {
for j in 0..=i + 1 {
// 3-way conflict
check_invariants(&[0], &[i, j]);
for k in 0..=j + 1 {
for l in 0..=k + 1 {
// 5-way conflict
check_invariants(&[0, i], &[j, k, l]);
}
}
}
}
}
}

0 comments on commit 038b64d

Please sign in to comment.