munin/internal/entry/diff_test.go
Thomas Adamek 466f1e40af Workspaces, Änderungshistorie, Papierkorb, Nutzungsübersicht, Hilfe
Umsetzung der Ausbaustufen 2.1, 3.6, 3.8, 3.9, 4.1, 4.2, 4.4, 4.6,
5.1–5.4 (Details in AUSBAUSTUFEN.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 07:44:52 +02:00

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) {
alt := "Zeile 1\nZeile 2\nZeile 3"
neu := "Zeile 1\nGeändert\nZeile 3"
got := formatDiff(DiffLines(alt, neu))
erwartet := " Zeile 1\n- Zeile 2\n+ Geändert\n Zeile 3\n"
if got != erwartet {
t.Errorf("DiffLines =\n%s\nerwartet:\n%s", got, erwartet)
}
}
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.
altGross := make([]byte, 0, 2200*2)
neuGross := make([]byte, 0, 2200*2)
for i := 0; i < 2200; i++ {
altGross = append(altGross, 'a', '\n')
neuGross = append(neuGross, 'b', '\n')
}
lines := DiffLines(string(altGross), string(neuGross))
for _, l := range lines {
if l.Op == DiffEqual {
t.Fatal("grobDiff sollte keine DiffEqual-Zeilen liefern")
}
}
}