-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdiffparser_test.go
137 lines (125 loc) · 2.55 KB
/
diffparser_test.go
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
// Copyright (c) 2015 Jesse Meek <https://github.com/waigani>
// This program is Free Software see LICENSE file for details.
package diffparser
import (
"io/ioutil"
"testing"
"github.com/stretchr/testify/require"
)
// TODO(waigani) tests are missing more creative names (spaces, special
// chars), and diffed files that are not in the current directory.
func setup(t *testing.T) *Diff {
byt, err := ioutil.ReadFile("example.diff")
require.NoError(t, err)
diff, err := Parse(string(byt))
require.NoError(t, err)
require.Equal(t, len(diff.Files), 6)
return diff
}
func TestFileModeAndNaming(t *testing.T) {
diff := setup(t)
for i, expected := range []struct {
mode FileMode
origName string
newName string
}{
{
mode: MODIFIED,
origName: "file1",
newName: "file1",
},
{
mode: DELETED,
origName: "file2",
newName: "",
},
{
mode: DELETED,
origName: "file3",
newName: "",
},
{
mode: NEW,
origName: "",
newName: "file4",
},
{
mode: NEW,
origName: "",
newName: "newname",
},
{
mode: DELETED,
origName: "symlink",
newName: "",
},
} {
file := diff.Files[i]
t.Logf("testing file: %v", file)
require.Equal(t, expected.mode, file.Mode)
require.Equal(t, expected.origName, file.OrigName)
require.Equal(t, expected.newName, file.NewName)
}
}
func TestHunk(t *testing.T) {
diff := setup(t)
expectedOrigLines := []DiffLine{
{
Mode: UNCHANGED,
Number: 1,
Content: "some",
Position: 2,
}, {
Mode: UNCHANGED,
Number: 2,
Content: "lines",
Position: 3,
}, {
Mode: REMOVED,
Number: 3,
Content: "in",
Position: 4,
}, {
Mode: UNCHANGED,
Number: 4,
Content: "file1",
Position: 5,
},
}
expectedNewLines := []DiffLine{
{
Mode: ADDED,
Number: 1,
Content: "add a line",
Position: 1,
}, {
Mode: UNCHANGED,
Number: 2,
Content: "some",
Position: 2,
}, {
Mode: UNCHANGED,
Number: 3,
Content: "lines",
Position: 3,
}, {
Mode: UNCHANGED,
Number: 4,
Content: "file1",
Position: 5,
},
}
file := diff.Files[0]
origRange := file.Hunks[0].OrigRange
newRange := file.Hunks[0].NewRange
require.Equal(t, 1, origRange.Start)
require.Equal(t, 4, origRange.Length)
require.Equal(t, 1, newRange.Start)
require.Equal(t, 4, newRange.Length)
for i, line := range expectedOrigLines {
require.Equal(t, line, *origRange.Lines[i])
}
for i, line := range expectedNewLines {
require.Equal(t, line, *newRange.Lines[i])
}
}