use crate::diff_algorithm::common::{Change, DiffAlgorithm, DiffOp};
#[derive(Debug, Default)]
pub struct MyersDiff;
impl DiffAlgorithm for MyersDiff {
fn ops<'a>(&self, old: &'a str, new: &'a str) -> Vec<DiffOp> {
let similar_diff = crate::diff_algorithm::similar::SimilarDiff;
similar_diff.ops(old, new)
}
fn iter_inline_changes<'a>(&self, old: &'a str, new: &'a str, op: &DiffOp) -> Vec<Change<'a>> {
let similar_diff = crate::diff_algorithm::similar::SimilarDiff;
similar_diff.iter_inline_changes(old, new, op)
}
}
#[cfg(test)]
mod tests {
use crate::{diff_with_algorithm, Algorithm, ArrowsTheme};
use std::io::Cursor;
#[test]
fn test_myers_insertion() {
let old = "abc";
let new = "abxc";
let theme = ArrowsTheme::default();
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, old, new, &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("<abc"));
assert!(output.contains(">abxc"));
}
#[test]
fn test_myers_deletion() {
let old = "abxc";
let new = "abc";
let theme = ArrowsTheme::default();
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, old, new, &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("<abxc"));
assert!(output.contains(">abc"));
}
#[test]
fn test_myers_complex() {
let old = "abcd";
let new = "acbd";
let theme = ArrowsTheme::default();
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, old, new, &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("<abcd"));
assert!(output.contains(">acbd"));
}
#[test]
fn test_myers_empty_inputs() {
let theme = ArrowsTheme::default();
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, "", "", &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert_eq!(output, "< left / > right\n");
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, "", "abc", &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains(">abc"));
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, "abc", "", &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("<abc"));
}
#[test]
fn test_myers_identical_inputs() {
let old = "abc";
let new = "abc";
let theme = ArrowsTheme::default();
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, old, new, &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains(" abc"));
assert!(!output.contains("<abc"));
assert!(!output.contains(">abc"));
}
#[test]
fn test_myers_multiline() {
let old = "line1\nline2\nline3";
let new = "line1\nmodified line2\nline3";
let theme = ArrowsTheme::default();
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, old, new, &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains(" line1"));
assert!(output.contains("<line2"));
assert!(output.contains(">modified line2"));
assert!(output.contains(" line3"));
}
#[test]
fn test_myers_trailing_newline() {
let old = "line\n";
let new = "line";
let theme = ArrowsTheme::default();
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, old, new, &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("␊"));
let mut buffer = Cursor::new(Vec::new());
diff_with_algorithm(&mut buffer, new, old, &theme, Algorithm::Myers).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("␊"));
}
}