-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapply_internal_test.go
More file actions
111 lines (98 loc) · 3.18 KB
/
apply_internal_test.go
File metadata and controls
111 lines (98 loc) · 3.18 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
package git_diff_parser
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFindPosRejectsAlreadyAppliedPostimage(t *testing.T) {
session := &applySession{
sourceLines: splitFileLines([]byte("a\nb\nx\nc\n")),
}
hunk := patchHunk{
oldStart: 1,
oldCount: 3,
newCount: 4,
lines: []patchLine{
{kind: ' ', text: "a", hasNewline: true},
{kind: ' ', text: "b", hasNewline: true},
{kind: '+', text: "x", hasNewline: true},
{kind: ' ', text: "c", hasNewline: true},
},
}
match, matched := session.findPos(hunk)
assert.Equal(t, matchedHunk{}, match)
assert.False(t, matched)
}
func TestMatchFragment_IgnoreWhitespace(t *testing.T) {
source := splitFileLines([]byte("alpha\n beta\ncharlie\n"))
fragment := []fileLine{
{text: "alpha", hasNewline: true},
{text: "beta", hasNewline: true},
{text: "charlie", hasNewline: true},
}
require.False(t, matchFragment(source, 0, fragment, false))
require.True(t, matchFragment(source, 0, fragment, true))
}
func TestFindPosForFragmentMatchesExactBlock(t *testing.T) {
session := &applySession{
sourceLines: splitFileLines([]byte("zero\nalpha\nbravo\ncharlie\n")),
}
match, matched := session.findPosForFragment(1, []fileLine{
{text: "alpha", hasNewline: true},
{text: "bravo", hasNewline: true},
{text: "charlie", hasNewline: true},
}, false, false)
require.True(t, matched)
assert.Equal(t, 1, match)
}
func TestFindPosWithMinContextReducesLeadingContext(t *testing.T) {
session := &applySession{
applier: &patchApply{options: applyOptions{MinContext: 1, MinContextSet: true}},
sourceLines: splitFileLines([]byte("a0\nA1\na2\na3\na4\na5\na6\n")),
patched: make([]bool, 7),
}
hunk := patchHunk{
oldStart: 2,
oldCount: 5,
newStart: 2,
newCount: 5,
lines: []patchLine{
{kind: ' ', text: "a1", hasNewline: true},
{kind: ' ', text: "a2", hasNewline: true},
{kind: '-', text: "a3", hasNewline: true},
{kind: '+', text: "A3", hasNewline: true},
{kind: ' ', text: "a4", hasNewline: true},
{kind: ' ', text: "a5", hasNewline: true},
},
}
match, matched := session.findPos(hunk)
require.True(t, matched)
assert.Equal(t, 2, match.sourceStart)
assert.Equal(t, 5, match.sourceEnd)
assert.Equal(t, 1, match.hunkStart)
assert.Equal(t, 5, match.hunkEnd)
}
func TestFindPosForFragmentRejectsPatchedRangesWithoutOverlap(t *testing.T) {
session := &applySession{
sourceLines: splitFileLines([]byte("zero\nalpha\nbravo\ncharlie\n")),
patched: []bool{false, true, true, false},
}
_, matched := session.findPosForFragment(1, []fileLine{
{text: "alpha", hasNewline: true},
{text: "bravo", hasNewline: true},
}, false, false)
assert.False(t, matched)
}
func TestFindPosForFragmentAllowsPatchedRangesWithOverlap(t *testing.T) {
session := &applySession{
applier: &patchApply{options: applyOptions{AllowOverlap: true}},
sourceLines: splitFileLines([]byte("zero\nalpha\nbravo\ncharlie\n")),
patched: []bool{false, true, true, false},
}
match, matched := session.findPosForFragment(1, []fileLine{
{text: "alpha", hasNewline: true},
{text: "bravo", hasNewline: true},
}, false, false)
require.True(t, matched)
assert.Equal(t, 1, match)
}