xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / site / diff_test.go

1.1 KB
1package site
2
3import (
4	"fmt"
5	"strings"
6	"testing"
7)
8
9func TestParseDiff(t *testing.T) {
10	patch := "diff --git a/a.go b/a.go\nindex 1..2 100644\n--- a/a.go\n+++ b/a.go\n@@ -3,2 +3,2 @@ func f()\n x\n-y\n+z\n" +
11		"diff --git a/old.txt b/new.txt\nsimilarity index 90%\nrename from old.txt\nrename to new.txt\n" +
12		"diff --git a/i.png b/i.png\nnew file mode 100644\nBinary files /dev/null and b/i.png differ\n"
13	d := parseDiff(patch)
14	if len(d) != 3 || d[0].Path != "a.go" || d[1].Path != "new.txt" || d[2].Note == "" {
15		t.Fatalf("files: %+v", d)
16	}
17	want := []DiffLine{{"@", 0, 0, "@@ -3,2 +3,2 @@ func f()"}, {" ", 3, 3, "x"}, {"-", 4, 0, "y"}, {"+", 0, 4, "z"}}
18	if fmt.Sprint(d[0].Lines) != fmt.Sprint(want) || d[0].Adds != 1 || d[0].Dels != 1 {
19		t.Errorf("a.go: %+v", d[0])
20	}
21
22	big := "diff --git a/b b/b\n@@ -0,0 +1 @@\n" + strings.Repeat("+x\n", MaxDiffLines+10)
23	d = parseDiff(big)
24	if len(d[0].Lines) != MaxDiffLines || d[0].Adds != MaxDiffLines+10 || !strings.HasPrefix(d[0].Note, "Cut short") {
25		t.Errorf("cap: %d lines, %d adds, note %q", len(d[0].Lines), d[0].Adds, d[0].Note)
26	}
27}