73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package entry
|
|
|
|
import "testing"
|
|
|
|
func formatDiff(lines []DiffLine) string {
|
|
out := ""
|
|
for _, l := range lines {
|
|
switch l.Op {
|
|
case DiffEqual:
|
|
out += " " + l.Text + "\n"
|
|
case DiffRemove:
|
|
out += "- " + l.Text + "\n"
|
|
case DiffAdd:
|
|
out += "+ " + l.Text + "\n"
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestDiffLinesIdentical(t *testing.T) {
|
|
lines := DiffLines("a\nb\nc", "a\nb\nc")
|
|
for _, l := range lines {
|
|
if l.Op != DiffEqual {
|
|
t.Errorf("Op = %v bei identischem Text, erwartet DiffEqual", l.Op)
|
|
}
|
|
}
|
|
if len(lines) != 3 {
|
|
t.Errorf("%d Zeilen, erwartet 3", len(lines))
|
|
}
|
|
}
|
|
|
|
func TestDiffLinesChangeInMiddle(t *testing.T) {
|
|
old := "Zeile 1\nZeile 2\nZeile 3"
|
|
newer := "Zeile 1\nGeändert\nZeile 3"
|
|
|
|
got := formatDiff(DiffLines(old, newer))
|
|
want := " Zeile 1\n- Zeile 2\n+ Geändert\n Zeile 3\n"
|
|
if got != want {
|
|
t.Errorf("DiffLines =\n%s\nerwartet:\n%s", got, want)
|
|
}
|
|
}
|
|
|
|
func TestDiffLinesPureAddition(t *testing.T) {
|
|
lines := DiffLines("a\nb", "a\nb\nc")
|
|
if len(lines) != 3 || lines[2].Op != DiffAdd || lines[2].Text != "c" {
|
|
t.Errorf("DiffLines = %+v, erwartet [gleich gleich +c]", lines)
|
|
}
|
|
}
|
|
|
|
func TestDiffLinesEmptyOldContent(t *testing.T) {
|
|
lines := DiffLines("", "neu")
|
|
if len(lines) != 1 || lines[0].Op != DiffAdd || lines[0].Text != "neu" {
|
|
t.Errorf("DiffLines(\"\", \"neu\") = %+v, erwartet [+neu]", lines)
|
|
}
|
|
}
|
|
|
|
func TestDiffLinesFallsBackForHugeInput(t *testing.T) {
|
|
// Erzwingt den grobDiff-Pfad ohne wirklich riesige Strings zu bauen: die
|
|
// Grenze fällt auf Zeilenanzahl, nicht Textlänge.
|
|
oldUpper := make([]byte, 0, 2200*2)
|
|
newUpper := make([]byte, 0, 2200*2)
|
|
for i := 0; i < 2200; i++ {
|
|
oldUpper = append(oldUpper, 'a', '\n')
|
|
newUpper = append(newUpper, 'b', '\n')
|
|
}
|
|
|
|
lines := DiffLines(string(oldUpper), string(newUpper))
|
|
for _, l := range lines {
|
|
if l.Op == DiffEqual {
|
|
t.Fatal("grobDiff sollte keine DiffEqual-Zeilen liefern")
|
|
}
|
|
}
|
|
}
|