-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffview_test.go
More file actions
159 lines (148 loc) · 3.21 KB
/
Copy pathdiffview_test.go
File metadata and controls
159 lines (148 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"strings"
"testing"
"time"
)
const sampleDiff = `diff --git a/foo.go b/foo.go
index 1234567..abcdefg 100644
--- a/foo.go
+++ b/foo.go
@@ -1,5 +1,7 @@
package main
-import "fmt"
+import (
+ "fmt"
+ "os"
+)
func main() {
- fmt.Println("hello")
+ fmt.Println("hello world")
+ os.Exit(0)
}
`
func TestParseUnifiedDiff(t *testing.T) {
files := parseUnifiedDiff(sampleDiff)
if len(files) != 1 {
t.Fatalf("expected 1 file, got %d", len(files))
}
fc := files[0]
if fc.newPath != "foo.go" {
t.Errorf("expected newPath=foo.go, got %s", fc.newPath)
}
if fc.oldPath != "foo.go" {
t.Errorf("expected oldPath=foo.go, got %s", fc.oldPath)
}
if len(fc.hunks) != 1 {
t.Fatalf("expected 1 hunk, got %d", len(fc.hunks))
}
h := fc.hunks[0]
if h.oldStart != 1 || h.oldLines != 5 {
t.Errorf("expected oldStart=1 oldLines=5, got %d %d", h.oldStart, h.oldLines)
}
if h.newStart != 1 || h.newLines != 7 {
t.Errorf("expected newStart=1 newLines=7, got %d %d", h.newStart, h.newLines)
}
adds := 0
dels := 0
ctxs := 0
for _, line := range h.lines {
switch line.kind {
case diffAdd:
adds++
case diffDelete:
dels++
case diffContext:
ctxs++
}
}
if adds != 6 {
t.Errorf("expected 6 add lines, got %d", adds)
}
if dels != 2 {
t.Errorf("expected 2 delete lines, got %d", dels)
}
if ctxs != 5 {
t.Errorf("expected 5 context lines, got %d", ctxs)
}
}
func TestRenderDiff(t *testing.T) {
files := parseUnifiedDiff(sampleDiff)
rendered := renderDiff(files, 80)
if rendered == "" {
t.Fatal("expected non-empty rendered diff")
}
if !strings.Contains(rendered, "foo.go") {
t.Error("expected rendered diff to contain filename")
}
if !strings.Contains(rendered, "@@ -1,5 +1,7 @@") {
t.Error("expected rendered diff to contain hunk header")
}
fmt.Printf("Rendered diff (%d chars):\n%s\n", len(rendered), rendered)
}
func TestHighlightCode(t *testing.T) {
code := `func main() { fmt.Println("hello") }`
hl := highlightCode(code, "go")
if hl == code {
t.Error("expected highlighted code to differ from input")
}
plain := highlightCode(code, "unknownlang")
if plain != code {
t.Error("expected unknown language to return plain code")
}
}
func TestParseMultipleHunks(t *testing.T) {
diff := `diff --git a/a.go b/a.go
--- a/a.go
+++ b/a.go
@@ -1,3 +1,3 @@
line1
-old
+new
line3
@@ -10,3 +10,3 @@
line10
-old2
+new2
line12
`
files := parseUnifiedDiff(diff)
if len(files) != 1 {
t.Fatalf("expected 1 file, got %d", len(files))
}
if len(files[0].hunks) != 2 {
t.Errorf("expected 2 hunks, got %d", len(files[0].hunks))
}
}
func TestParseMultipleFiles(t *testing.T) {
diff := `diff --git a/a.go b/a.go
--- a/a.go
+++ b/a.go
@@ -1,1 +1,1 @@
-a
+b
diff --git a/b.go b/b.go
--- a/b.go
+++ b/b.go
@@ -1,1 +1,1 @@
-c
+d
`
files := parseUnifiedDiff(diff)
if len(files) != 2 {
t.Fatalf("expected 2 files, got %d", len(files))
}
if files[0].newPath != "a.go" {
t.Errorf("expected first file a.go, got %s", files[0].newPath)
}
if files[1].newPath != "b.go" {
t.Errorf("expected second file b.go, got %s", files[1].newPath)
}
}
func TestFormatDate(t *testing.T) {
// zero time should return empty
if formatDate(time.Time{}) != "" {
t.Error("expected empty string for zero time")
}
}