diff --git a/src/context_diff.rs b/src/context_diff.rs index 2e2104d..37a9c4d 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -871,5 +871,30 @@ mod tests { .join("\n"); let output = diff(from.as_bytes(), to.as_bytes(), ¶ms); assert_diff_eq!(output, expected); + + // An insertion before a nearby change gives the two sides different + // line counts; the hunks must still be merged (issue #268). + let from = ["milk", "eggs", "bread", "butter", "apples", ""].join("\n"); + let to = ["milk", "eggs", "yogurt", "bread", "honey", "apples", ""].join("\n"); + let expected = [ + "*** foo\tTIMESTAMP", + "--- bar\tTIMESTAMP", + "***************", + "*** 2,5 ****", + " eggs", + " bread", + "! butter", + " apples", + "--- 2,6 ----", + " eggs", + "+ yogurt", + " bread", + "! honey", + " apples", + "", + ] + .join("\n"); + let output = diff(from.as_bytes(), to.as_bytes(), ¶ms); + assert_diff_eq!(output, expected); } } diff --git a/src/unified_diff.rs b/src/unified_diff.rs index 14b3e78..bd83afe 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -1008,5 +1008,25 @@ mod tests { .join("\n"); let output = diff(from.as_bytes(), to.as_bytes(), ¶ms); assert_diff_eq!(output, expected); + + // An insertion before a nearby change gives the two sides different + // line counts; the hunks must still be merged (issue #268). + let from = ["milk", "eggs", "bread", "butter", "apples", ""].join("\n"); + let to = ["milk", "eggs", "yogurt", "bread", "honey", "apples", ""].join("\n"); + let expected = [ + "--- foo\tTIMESTAMP", + "+++ bar\tTIMESTAMP", + "@@ -2,4 +2,5 @@", + " eggs", + "+yogurt", + " bread", + "-butter", + "+honey", + " apples", + "", + ] + .join("\n"); + let output = diff(from.as_bytes(), to.as_bytes(), ¶ms); + assert_diff_eq!(output, expected); } }