-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepair_test.go
More file actions
113 lines (103 loc) · 3.91 KB
/
Copy pathrepair_test.go
File metadata and controls
113 lines (103 loc) · 3.91 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
package hpatch
import (
"strings"
"testing"
)
func repairFor(t *testing.T, content, script string) string {
t.Helper()
root := t.TempDir()
writeTestFile(t, root, "file.txt", content, 0o644)
result, err := translateForHostAtTest(t, root, script, "")
if err == nil {
t.Fatalf("script unexpectedly succeeded: %q", script)
}
_, repair, _ := strings.Cut(result.Diagnostic, "\n")
return repair
}
func TestHPatch2RepairContextForIncompleteTextTarget(t *testing.T) {
content := "none\ntarget\nnone\n"
anchor := row(1, "none")
repair := repairFor(t, content, "in file.txt\ntype "+anchor+` "target" 2 "replacement"`)
for _, want := range []string{
"found 1 of 2 requested matches at or after line 1",
"apply that prerequisite, reread, and submit a later invocation",
"matching lines: 2",
"1:" + hashLine("none") + " none",
} {
if !strings.Contains(repair, want) {
t.Fatalf("repair lacks %q:\n%s", want, repair)
}
}
}
func TestHPatch2RepairContextForIncompleteUnanchoredTextTarget(t *testing.T) {
repair := repairFor(t, "none\ntarget\nnone\n", `in file.txt
type "target" 2 "replacement"`)
for _, want := range []string{
"found 1 of 2 requested matches in immutable baseline",
"matching lines: 2",
} {
if !strings.Contains(repair, want) {
t.Fatalf("repair lacks %q:\n%s", want, repair)
}
}
}
func TestHPatch2RepairContextForStaleRow(t *testing.T) {
repair := repairFor(t, "alpha\nbeta\n", "in file.txt\ntype 2:0000 \"B\"")
if !strings.Contains(repair, "2:"+hashLine("beta")+" beta") {
t.Fatalf("repair = %q", repair)
}
}
func TestHPatch2RepairContextForStaleRangeEnd(t *testing.T) {
content := "one\ntwo\nthree\nfour\nfive\nsix\nseven\n"
repair := repairFor(t, content, "in file.txt\ntype "+row(1, "one")+"..7:0000 \"\"")
if !strings.Contains(repair, "7:"+hashLine("seven")+" seven") {
t.Fatalf("repair = %q", repair)
}
}
func TestHPatch2MissingRowDoesNotGuessRepairContext(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "file.txt", "alpha\n", 0o644)
result, err := translateForHostAtTest(t, root, "in file.txt\ntype 9:0000 \"B\"", "")
if err == nil || !strings.Contains(result.Diagnostic, "row-missing") {
t.Fatalf("TranslateForHost() error = %v, diagnostic %q", err, result.Diagnostic)
}
if strings.Count(result.Diagnostic, "\n") != 1 {
t.Fatalf("missing row emitted guessed repair context: %q", result.Diagnostic)
}
}
func TestHPatch2RepairContextForEditConflict(t *testing.T) {
content := "alpha\nbeta\n"
script := "in file.txt\ntype " + row(1, "alpha") + ` "A"` + "\ntype " + row(1, "alpha") + ` ""`
repair := repairFor(t, content, script)
for _, want := range []string{
"baseline content conflicts with an earlier mutation",
"command 2 (type) line 1",
"1:" + hashLine("alpha") + " alpha",
} {
if !strings.Contains(repair, want) {
t.Fatalf("repair lacks %q:\n%s", want, repair)
}
}
}
func TestHPatch2RepairContextForReversedRange(t *testing.T) {
content := "alpha\nbeta\n"
script := "in file.txt\ntype " + row(2, "beta") + ".." + row(1, "alpha") + ` ""`
repair := repairFor(t, content, script)
if !strings.Contains(repair, "row range resolves to lines 2:1") ||
!strings.Contains(repair, "2:"+hashLine("beta")+" beta") {
t.Fatalf("repair = %q", repair)
}
}
func TestGeneratedSourceRepairIsBounded(t *testing.T) {
long := strings.Repeat("x", repairPreviewLimit+50)
content := strings.Join([]string{"one", "two", "three", "four", "five", long, "seven", "eight", "nine"}, "\n")
repair := generatedSourceRepair(content, 6, 201)
if strings.Count(repair, "\n") != 6 {
t.Fatalf("repair line count = %d, want 6:\n%s", strings.Count(repair, "\n"), repair)
}
if !strings.Contains(repair, "generated Go near 6:201\n") ||
!strings.Contains(repair, "> 6 | "+strings.Repeat("x", repairPreviewLimit)+"\n") ||
strings.Contains(repair, long) || strings.Contains(repair, " 3 |") || strings.Contains(repair, " 9 |") {
t.Fatalf("repair is not bounded around generated position:\n%s", repair)
}
}