Fix diff edge case for trailing diffs

This commit is contained in:
Justin Santa Barbara 2017-02-21 00:15:46 -05:00
parent 58a8e9448a
commit 792bf5201e
2 changed files with 42 additions and 4 deletions

View File

@ -158,11 +158,13 @@ func buildDiffLines(lString, rString string) []lineRecord {
} }
} }
if l != "" && r != "" { if l != "" && l == r {
if l == r { results = append(results, lineRecord{Type: diffmatchpatch.DiffEqual, Line: l})
results = append(results, lineRecord{Type: diffmatchpatch.DiffEqual, Line: l}) } else {
} else { if l != "" {
results = append(results, lineRecord{Type: diffmatchpatch.DiffDelete, Line: l}) results = append(results, lineRecord{Type: diffmatchpatch.DiffDelete, Line: l})
}
if r != "" {
results = append(results, lineRecord{Type: diffmatchpatch.DiffInsert, Line: r}) results = append(results, lineRecord{Type: diffmatchpatch.DiffInsert, Line: r})
} }
} }

View File

@ -135,3 +135,39 @@ Line3`
t.Fatalf("unexpected diff. expected=%v, actual=%v", expectedDiff, actual) t.Fatalf("unexpected diff. expected=%v, actual=%v", expectedDiff, actual)
} }
} }
func Test_Diff_4(t *testing.T) {
l := `A
B
C
D
E
F
`
r := `A
B
C
D
E
F`
expectedDiff := `...
D
E
- F
+ F
`
{
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(l, r, false)
// We do need to cleanup, as otherwise we get some spurious changes on complex diffs
diffs = dmp.DiffCleanupSemantic(diffs)
t.Logf("diffs %v", diffs)
}
actual := FormatDiff(l, r)
if actual != expectedDiff {
t.Fatalf("unexpected diff. expected=%s, actual=%s", expectedDiff, actual)
}
}