my fork of dmp
Diffstat (limited to 'tests/test.rs')
-rw-r--r--tests/test.rs100
1 files changed, 91 insertions, 9 deletions
diff --git a/tests/test.rs b/tests/test.rs
index 0f0eca2..e4dc6a6 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -6,10 +6,6 @@ use diff_match_patch_rs::dmp::Diff;
use diff_match_patch_rs::{Compat, DiffMatchPatch, Efficient, Error, Ops, PatchInput};
-// const tests = [
-// 'testDiffIsDestructurable',
-// ];
-
#[test]
fn test_diff_levenshtein() {
let dmp = DiffMatchPatch::new();
@@ -86,7 +82,7 @@ fn test_diff_pretty_html() -> Result<(), Error> {
let new = "πŸ€”"; // [240, 159, 164, 148]
let diffs = dmp.diff_main::<Efficient>(old, new)?;
assert_eq!(
- "<span></span><del style=\"background:#ffe6e6;\">πŸ€ͺ</del><ins style=\"background:#e6ffe6;\">πŸ€”</ins>",
+ "<del style=\"background:#ffe6e6;\">πŸ€ͺ</del><ins style=\"background:#e6ffe6;\">πŸ€”</ins>",
dmp.diff_pretty_html(&diffs)?
);
@@ -104,7 +100,7 @@ fn test_diff_pretty_html() -> Result<(), Error> {
let new = "🌊"; // [240, 159, 140, 138]
let diffs = dmp.diff_main::<Efficient>(old, new)?;
assert_eq!(
- "<span></span><del style=\"background:#ffe6e6;\">🍊</del><ins style=\"background:#e6ffe6;\">🌊</ins>",
+ "<del style=\"background:#ffe6e6;\">🍊</del><ins style=\"background:#e6ffe6;\">🌊</ins>",
dmp.diff_pretty_html(&diffs)?
);
@@ -122,7 +118,7 @@ fn test_diff_pretty_html() -> Result<(), Error> {
let new = "π– Š"; // [240, 150, 160, 138]
let diffs = dmp.diff_main::<Efficient>(old, new)?;
assert_eq!(
- "<span></span><ins style=\"background:#e6ffe6;\">π– Š</ins><del style=\"background:#ffe6e6;\">𠌊</del>",
+ "<ins style=\"background:#e6ffe6;\">π– Š</ins><del style=\"background:#ffe6e6;\">𠌊</del>",
dmp.diff_pretty_html(&diffs)?
);
@@ -131,7 +127,7 @@ fn test_diff_pretty_html() -> Result<(), Error> {
let new = std::str::from_utf8(&[240, 160, 158, 132]).unwrap(); // basically an undefined element `π ž„`. Should still work
let diffs = dmp.diff_main::<Efficient>(old, new)?;
assert_eq!(
- "<span></span><del style=\"background:#ffe6e6;\">πž „</del><ins style=\"background:#e6ffe6;\">π ž„</ins>",
+ "<del style=\"background:#ffe6e6;\">πž „</del><ins style=\"background:#e6ffe6;\">π ž„</ins>",
dmp.diff_pretty_html(&diffs)?
);
@@ -140,7 +136,7 @@ fn test_diff_pretty_html() -> Result<(), Error> {
let new = "🌍"; // [240, 159, 140, 141] -- interesting revelation - last 2 bytes swapped and 🍌 becomes 🌍. Guess the world is going `Bananas!!`
let diffs = dmp.diff_main::<Efficient>(old, new)?;
assert_eq!(
- "<span></span><del style=\"background:#ffe6e6;\">🍌</del><ins style=\"background:#e6ffe6;\">🌍</ins>",
+ "<del style=\"background:#ffe6e6;\">🍌</del><ins style=\"background:#e6ffe6;\">🌍</ins>",
dmp.diff_pretty_html(&diffs)?
);
@@ -154,6 +150,92 @@ fn test_diff_pretty_html() -> Result<(), Error> {
dmp.diff_pretty_html(&diffs)?
);
+ // Compat mode
+ // Basic
+ let diffs = [
+ Diff::equal(&['a', '\n']),
+ Diff::delete(&"<B>b</B>".chars().collect::<Vec<_>>()[..]),
+ Diff::insert(&"c&d".chars().collect::<Vec<_>>()[..]),
+ ];
+ assert_eq!("<span>a&para;<br></span><del style=\"background:#ffe6e6;\">&lt;B&gt;b&lt;/B&gt;</del><ins style=\"background:#e6ffe6;\">c&amp;d</ins>", dmp.diff_pretty_html(&diffs)?);
+
+ // `Compat` mode shouldn't require monkey business atall
+
+ // Case 1. Two similar emoticons
+ // In bytes representation, these would have the last u8 different
+ // Which means the the diff should an equality block of 3 bytes folloed by insert and delete
+ let old = "πŸ€ͺ"; // [240, 159, 164, 170]
+ let new = "πŸ€”"; // [240, 159, 164, 148]
+ let diffs = dmp.diff_main::<Compat>(old, new)?;
+ assert_eq!(
+ "<del style=\"background:#ffe6e6;\">πŸ€ͺ</del><ins style=\"background:#e6ffe6;\">πŸ€”</ins>",
+ dmp.diff_pretty_html(&diffs)?
+ );
+
+ // Now Case 1. but with some text before and after
+ let old = "I'm puzzledπŸ€ͺ or am I?";
+ let new = "I'm puzzledπŸ€” or thinking I guess!";
+ let diffs = dmp.diff_main::<Compat>(old, new)?;
+ assert_eq!(
+ "<span>I'm puzzled</span><del style=\"background:#ffe6e6;\">πŸ€ͺ</del><ins style=\"background:#e6ffe6;\">πŸ€”</ins><span> or </span><del style=\"background:#ffe6e6;\">am I?</del><ins style=\"background:#e6ffe6;\">thinking I guess!</ins>",
+ dmp.diff_pretty_html(&diffs)?
+ );
+
+ // Case 2. Emoticons with the third position different
+ let old = "🍊"; // [240, 159, 141, 138]
+ let new = "🌊"; // [240, 159, 140, 138]
+ let diffs = dmp.diff_main::<Compat>(old, new)?;
+ assert_eq!(
+ "<del style=\"background:#ffe6e6;\">🍊</del><ins style=\"background:#e6ffe6;\">🌊</ins>",
+ dmp.diff_pretty_html(&diffs)?
+ );
+
+ // Now Case 2. but with some text, lets complicate this
+ let old = "🍊, aah orange is the new black!"; // [240, 159, 141, 138]
+ let new = "Aah orange!🌊is the new 🌊"; // [240, 159, 140, 138]
+ let diffs = dmp.diff_main::<Compat>(old, new)?;
+ assert_eq!(
+ "<del style=\"background:#ffe6e6;\">🍊, a</del><ins style=\"background:#e6ffe6;\">A</ins><span>ah orange</span><del style=\"background:#ffe6e6;\"> </del><ins style=\"background:#e6ffe6;\">!🌊</ins><span>is the new </span><del style=\"background:#ffe6e6;\">black!</del><ins style=\"background:#e6ffe6;\">🌊</ins>",
+ dmp.diff_pretty_html(&diffs)?
+ );
+
+ // Case 3. with second and third different, but lets complicate this with an equality
+ let old = "𠌊"; // [240, 160, 140, 138]
+ let new = "π– Š"; // [240, 150, 160, 138]
+ let diffs = dmp.diff_main::<Compat>(old, new)?;
+ assert_eq!(
+ "<del style=\"background:#ffe6e6;\">𠌊</del><ins style=\"background:#e6ffe6;\">π– Š</ins>",
+ dmp.diff_pretty_html(&diffs)?
+ );
+
+ // Case 3. but let there be a swap
+ let old = "πž „"; // [240, 158, 160, 132]
+ let new = std::str::from_utf8(&[240, 160, 158, 132]).unwrap(); // basically an undefined element `π ž„`. Should still work
+ let diffs = dmp.diff_main::<Compat>(old, new)?;
+ assert_eq!(
+ "<del style=\"background:#ffe6e6;\">πž „</del><ins style=\"background:#e6ffe6;\">π ž„</ins>",
+ dmp.diff_pretty_html(&diffs)?
+ );
+
+ // Case 4. swap at the last 2 positions
+ let old = "🍌"; // [240, 159, 141, 140] -- FINALLY A BANANA
+ let new = "🌍"; // [240, 159, 140, 141] -- interesting revelation - last 2 bytes swapped and 🍌 becomes 🌍. Guess the world is going `Bananas!!`
+ let diffs = dmp.diff_main::<Compat>(old, new)?;
+ assert_eq!(
+ "<del style=\"background:#ffe6e6;\">🍌</del><ins style=\"background:#e6ffe6;\">🌍</ins>",
+ dmp.diff_pretty_html(&diffs)?
+ );
+
+ // Let's do this with a slightly longish string
+ let old = "Now, let's explore some emotional extremes 🌊.\nWe've got your ecstatic face 🀩, your devastated face 😭, and your utterly confused face 🀯. But that's not all! πŸ€” We've also got some subtle emotions like 😐, πŸ™ƒ, and πŸ‘€.";
+ let new = "Let's start with some basics 😊.\nWe've got your standard smiley face πŸ™‚, your sad face ☹️, and your angry face 😠. But wait, there's more! 🀩 We've also got some more complex emotions like 😍, 🀀, and πŸš€. And let's not forget about the classics: πŸ˜‰, πŸ‘, and πŸ‘.";
+ let diffs = dmp.diff_main::<Compat>(old, new)?;
+
+ assert_eq!(
+ "<del style=\"background:#ffe6e6;\">Now, let's explore some emotional extreme</del><ins style=\"background:#e6ffe6;\">Let's start with some basic</ins><span>s </span><del style=\"background:#ffe6e6;\">🌊</del><ins style=\"background:#e6ffe6;\">😊</ins><span>.&para;<br>We've got your </span><del style=\"background:#ffe6e6;\">ec</del><span>sta</span><del style=\"background:#ffe6e6;\">tic</del><ins style=\"background:#e6ffe6;\">ndard smiley</ins><span> face </span><del style=\"background:#ffe6e6;\">🀩</del><ins style=\"background:#e6ffe6;\">πŸ™‚</ins><span>, your </span><del style=\"background:#ffe6e6;\">devastate</del><ins style=\"background:#e6ffe6;\">sa</ins><span>d face </span><del style=\"background:#ffe6e6;\">😭</del><ins style=\"background:#e6ffe6;\">☹️</ins><span>, and your </span><del style=\"background:#ffe6e6;\">utterly confused</del><ins style=\"background:#e6ffe6;\">angry</ins><span> face </span><del style=\"background:#ffe6e6;\">🀯</del><ins style=\"background:#e6ffe6;\">😠</ins><span>. But </span><del style=\"background:#ffe6e6;\">that's not all</del><ins style=\"background:#e6ffe6;\">wait, there's more</ins><span>! </span><del style=\"background:#ffe6e6;\">πŸ€”</del><ins style=\"background:#e6ffe6;\">🀩</ins><span> We've also got some </span><del style=\"background:#ffe6e6;\">subt</del><ins style=\"background:#e6ffe6;\">more comp</ins><span>le</span><ins style=\"background:#e6ffe6;\">x</ins><span> emotions like </span><del style=\"background:#ffe6e6;\">😐</del><ins style=\"background:#e6ffe6;\">😍, 🀀, and πŸš€. And let's not forget about the classics: πŸ˜‰</ins><span>, </span><del style=\"background:#ffe6e6;\">πŸ™ƒ</del><ins style=\"background:#e6ffe6;\">πŸ‘</ins><span>, and </span><del style=\"background:#ffe6e6;\">πŸ‘€</del><ins style=\"background:#e6ffe6;\">πŸ‘</ins><span>.</span>",
+ dmp.diff_pretty_html(&diffs)?
+ );
+
Ok(())
}