From 5bb420e162b75af34a4262e74e97972ff901676f Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 5 Apr 2025 09:45:44 +0100 Subject: [PATCH] Add diff tests --- backend/reconcile/src/diffs/myers.rs | 54 ++++++++++++++++++ ...le__diffs__myers__tests__complex_diff.snap | 55 +++++++++++++++++++ ...ile__diffs__myers__tests__delete_only.snap | 19 +++++++ ...iffs__myers__tests__identical_content.snap | 23 ++++++++ ...ile__diffs__myers__tests__insert_only.snap | 19 +++++++ ...iffs__myers__tests__prefix_and_suffix.snap | 43 +++++++++++++++ .../reconcile/tests/examples/multiline.yml | 20 +++++++ 7 files changed, 233 insertions(+) create mode 100644 backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__complex_diff.snap create mode 100644 backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__delete_only.snap create mode 100644 backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__identical_content.snap create mode 100644 backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__insert_only.snap create mode 100644 backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__prefix_and_suffix.snap create mode 100644 backend/reconcile/tests/examples/multiline.yml diff --git a/backend/reconcile/src/diffs/myers.rs b/backend/reconcile/src/diffs/myers.rs index 9692c22..da75404 100644 --- a/backend/reconcile/src/diffs/myers.rs +++ b/backend/reconcile/src/diffs/myers.rs @@ -282,3 +282,57 @@ fn conquer( )); } } + +#[cfg(test)] +mod tests { + use insta::assert_debug_snapshot; + + use super::*; + + #[test] + fn test_empty_diff() { + let old: Vec> = vec![]; + let new: Vec> = vec![]; + let result = diff(&old, &new); + assert_eq!(result.len(), 0); + } + + #[test] + fn test_identical_content() { + let content = vec!["a".into(), "b".into(), "c".into()]; + let result = diff(&content, &content); + assert_debug_snapshot!(result); + } + + #[test] + fn test_insert_only() { + let old: Vec> = vec![]; + let new: Vec> = vec!["a".into(), "b".into()]; + let result = diff(&old, &new); + assert_debug_snapshot!(result); + } + + #[test] + fn test_delete_only() { + let old = vec!["a".into(), "b".into()]; + let new: Vec> = vec![]; + let result = diff(&old, &new); + assert_debug_snapshot!(result); + } + + #[test] + fn test_prefix_and_suffix() { + let old = vec!["a".into(), "b".into(), "c".into(), "d".into()]; + let new = vec!["a".into(), "x".into(), "d".into()]; + let result = diff(&old, &new); + assert_debug_snapshot!(result); + } + + #[test] + fn test_complex_diff() { + let old = vec!["a".into(), "b".into(), "c".into(), "d".into()]; + let new = vec!["a".into(), "x".into(), "c".into(), "y".into()]; + let result = diff(&old, &new); + assert_debug_snapshot!(result); + } +} diff --git a/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__complex_diff.snap b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__complex_diff.snap new file mode 100644 index 0000000..8c89ed3 --- /dev/null +++ b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__complex_diff.snap @@ -0,0 +1,55 @@ +--- +source: reconcile/src/diffs/myers.rs +expression: result +snapshot_kind: text +--- +[ + Equal( + [ + Token { + normalised: "a", + original: "a", + }, + ], + ), + Insert( + [ + Token { + normalised: "x", + original: "x", + }, + ], + ), + Delete( + [ + Token { + normalised: "b", + original: "b", + }, + ], + ), + Equal( + [ + Token { + normalised: "c", + original: "c", + }, + ], + ), + Insert( + [ + Token { + normalised: "y", + original: "y", + }, + ], + ), + Delete( + [ + Token { + normalised: "d", + original: "d", + }, + ], + ), +] diff --git a/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__delete_only.snap b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__delete_only.snap new file mode 100644 index 0000000..f07eb3d --- /dev/null +++ b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__delete_only.snap @@ -0,0 +1,19 @@ +--- +source: reconcile/src/diffs/myers.rs +expression: result +snapshot_kind: text +--- +[ + Delete( + [ + Token { + normalised: "a", + original: "a", + }, + Token { + normalised: "b", + original: "b", + }, + ], + ), +] diff --git a/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__identical_content.snap b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__identical_content.snap new file mode 100644 index 0000000..a99e276 --- /dev/null +++ b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__identical_content.snap @@ -0,0 +1,23 @@ +--- +source: reconcile/src/diffs/myers.rs +expression: result +snapshot_kind: text +--- +[ + Equal( + [ + Token { + normalised: "a", + original: "a", + }, + Token { + normalised: "b", + original: "b", + }, + Token { + normalised: "c", + original: "c", + }, + ], + ), +] diff --git a/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__insert_only.snap b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__insert_only.snap new file mode 100644 index 0000000..b32c8ce --- /dev/null +++ b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__insert_only.snap @@ -0,0 +1,19 @@ +--- +source: reconcile/src/diffs/myers.rs +expression: result +snapshot_kind: text +--- +[ + Insert( + [ + Token { + normalised: "a", + original: "a", + }, + Token { + normalised: "b", + original: "b", + }, + ], + ), +] diff --git a/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__prefix_and_suffix.snap b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__prefix_and_suffix.snap new file mode 100644 index 0000000..03c8fee --- /dev/null +++ b/backend/reconcile/src/diffs/snapshots/reconcile__diffs__myers__tests__prefix_and_suffix.snap @@ -0,0 +1,43 @@ +--- +source: reconcile/src/diffs/myers.rs +expression: result +snapshot_kind: text +--- +[ + Equal( + [ + Token { + normalised: "a", + original: "a", + }, + ], + ), + Delete( + [ + Token { + normalised: "b", + original: "b", + }, + Token { + normalised: "c", + original: "c", + }, + ], + ), + Insert( + [ + Token { + normalised: "x", + original: "x", + }, + ], + ), + Equal( + [ + Token { + normalised: "d", + original: "d", + }, + ], + ), +] diff --git a/backend/reconcile/tests/examples/multiline.yml b/backend/reconcile/tests/examples/multiline.yml new file mode 100644 index 0000000..c751feb --- /dev/null +++ b/backend/reconcile/tests/examples/multiline.yml @@ -0,0 +1,20 @@ +parent: Hello! +left: | + Hello there! + + How are you? + +right: | + Hello there! + + + Best, + Andras + +expected: | + Hello there! + + How are you? + + Best, + Andras