-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_input_test.go
More file actions
192 lines (169 loc) · 4.1 KB
/
diff_input_test.go
File metadata and controls
192 lines (169 loc) · 4.1 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package tokendiff
import (
"strings"
"testing"
)
func TestProcessUnifiedDiff(t *testing.T) {
input := `--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
context line
-old word here
+new word here
another context
`
expected := `--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
context line
[-old-]{+new+} word here
another context
`
opts := DefaultOptions()
fmtOpts := FormatOptions{
StartDelete: "[-",
StopDelete: "-]",
StartInsert: "{+",
StopInsert: "+}",
}
var output strings.Builder
err := ProcessUnifiedDiff(strings.NewReader(input), &output, opts, fmtOpts)
if err != nil {
t.Fatalf("ProcessUnifiedDiff error: %v", err)
}
if output.String() != expected {
t.Errorf("ProcessUnifiedDiff:\ngot:\n%s\nwant:\n%s", output.String(), expected)
}
}
func TestProcessUnifiedDiffMultipleChanges(t *testing.T) {
input := `--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,2 @@
-hello world
+hello universe
@@ -10,2 +10,2 @@
-foo bar
+foo baz
`
opts := DefaultOptions()
fmtOpts := FormatOptions{
StartDelete: "[-",
StopDelete: "-]",
StartInsert: "{+",
StopInsert: "+}",
}
var output strings.Builder
err := ProcessUnifiedDiff(strings.NewReader(input), &output, opts, fmtOpts)
if err != nil {
t.Fatalf("ProcessUnifiedDiff error: %v", err)
}
result := output.String()
// Space between Delete and Insert because both "world" and "universe" are
// preceded by a space in their respective texts
if !strings.Contains(result, "hello [-world-] {+universe+}") {
t.Errorf("expected word diff for first hunk, got: %s", result)
}
if !strings.Contains(result, "foo [-bar-] {+baz+}") {
t.Errorf("expected word diff for second hunk, got: %s", result)
}
}
func TestProcessUnifiedDiffEmptyLines(t *testing.T) {
// Test handling of empty lines within hunks
input := `--- a/file.txt
+++ b/file.txt
@@ -1,4 +1,4 @@
first line
-old line
+new line
`
opts := DefaultOptions()
fmtOpts := FormatOptions{
StartDelete: "[-",
StopDelete: "-]",
StartInsert: "{+",
StopInsert: "+}",
}
var output strings.Builder
err := ProcessUnifiedDiff(strings.NewReader(input), &output, opts, fmtOpts)
if err != nil {
t.Fatalf("ProcessUnifiedDiff error: %v", err)
}
result := output.String()
if !strings.Contains(result, "first line") {
t.Errorf("expected 'first line' in output, got: %s", result)
}
if !strings.Contains(result, "[-old-]{+new+} line") {
t.Errorf("expected word diff, got: %s", result)
}
}
func TestProcessUnifiedDiffUnknownLineFormat(t *testing.T) {
// Test handling of lines with unknown format (no prefix)
input := `--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,2 @@
-old
+new
unprefixed line
`
opts := DefaultOptions()
fmtOpts := FormatOptions{
StartDelete: "[-",
StopDelete: "-]",
StartInsert: "{+",
StopInsert: "+}",
}
var output strings.Builder
err := ProcessUnifiedDiff(strings.NewReader(input), &output, opts, fmtOpts)
if err != nil {
t.Fatalf("ProcessUnifiedDiff error: %v", err)
}
result := output.String()
// The unknown line should be passed through
if !strings.Contains(result, "unprefixed line") {
t.Errorf("expected unknown line to be passed through, got: %s", result)
}
}
func TestProcessUnifiedDiffGitExtendedHeaders(t *testing.T) {
// Test handling of various git extended headers
input := `diff --git a/file.txt b/file.txt
index abc123..def456 100644
new file mode 100644
deleted file mode 100644
similarity index 95%
rename from old.txt
Binary files differ
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-old
+new
`
opts := DefaultOptions()
fmtOpts := FormatOptions{
StartDelete: "[-",
StopDelete: "-]",
StartInsert: "{+",
StopInsert: "+}",
}
var output strings.Builder
err := ProcessUnifiedDiff(strings.NewReader(input), &output, opts, fmtOpts)
if err != nil {
t.Fatalf("ProcessUnifiedDiff error: %v", err)
}
result := output.String()
// All git headers should be passed through
headers := []string{
"diff --git",
"index abc123",
"new file mode",
"deleted file mode",
"similarity index",
"rename from",
"Binary files",
}
for _, h := range headers {
if !strings.Contains(result, h) {
t.Errorf("expected header %q in output, got: %s", h, result)
}
}
}