-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_help_test.go
More file actions
185 lines (175 loc) · 6.55 KB
/
Copy pathtool_help_test.go
File metadata and controls
185 lines (175 loc) · 6.55 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
package hpatch
import (
"fmt"
"regexp"
"strings"
"testing"
)
func TestHPatch2ToolGrammarQuotedOperands(t *testing.T) {
target := grammarTerminalRegexp(t, "TARGET_QUOTED")
for _, value := range []string{
`"text"`,
`"quote\"slash\\solidus\/"`,
`"tab\ttext"`,
`"tab\u0009text"`,
"\"literal\ttext\"",
} {
if !target.MatchString(value) {
t.Errorf("TARGET_QUOTED rejects valid value %q", value)
}
}
for control := range 0x20 {
encoded := fmt.Sprintf(`"before\u%04Xafter"`, control)
if got, want := target.MatchString(encoded), control == '\t'; got != want {
t.Errorf("TARGET_QUOTED matches encoded U+%04X = %v, want %v", control, got, want)
}
}
for _, value := range []string{`""`, `"newline\n"`, `"return\r"`, `"vertical\u000btab"`} {
if target.MatchString(value) {
t.Errorf("TARGET_QUOTED accepts invalid value %q", value)
}
}
value := grammarTerminalRegexp(t, "QUOTED")
for _, input := range []string{`""`, `"line\nvalue"`, `"return\rvalue"`, `"tab\tvalue"`} {
if !value.MatchString(input) {
t.Errorf("QUOTED rejects valid value %q", input)
}
}
}
func TestHPatch2ToolGrammarMatchesPublicCommands(t *testing.T) {
for _, rule := range []string{
`path_command: PATH_OP SP PATH`,
`inline_mutation: TYPE_OP SP target SP QUOTED`,
`heredoc_mutation: TYPE_OP SP target SP "<<PATCH" NL _patch_body "PATCH"`,
`inline_initializer: "type" SP QUOTED`,
`heredoc_initializer: "type" SP "<<PATCH" NL _patch_body "PATCH"`,
`ROW: /[1-9][0-9]*:[0-9a-f]{4}/`,
`TYPE_OP: "type" | "type-" | "type+"`,
} {
if !strings.Contains(toolGrammar, rule) {
t.Errorf("tool grammar omits %q", rule)
}
}
for _, removed := range []string{"tsel_command", "rsel_command", "delete_command", `"del"`, `"copy"`, `"cut"`, `"paste"`, `"commit"`, "<<TAG"} {
if strings.Contains(toolGrammar, removed) {
t.Errorf("tool grammar retains HPATCH/1 form %q", removed)
}
}
}
func TestHPatch2ToolGrammarLineTerminators(t *testing.T) {
newline := grammarTerminalRegexp(t, "NL")
for value, want := range map[string]bool{"\n": true, "\r\n": true, "\r": false} {
if got := newline.MatchString(value); got != want {
t.Errorf("NL matches %q = %v, want %v", value, got, want)
}
}
bodyLines := []*regexp.Regexp{
grammarTerminalRegexp(t, "PATCH_BODY_LINE"),
grammarTerminalRegexp(t, "PATCH_TYPE_PREFIX_LINE"),
grammarTerminalRegexp(t, "PATCH_TYPE_VALUE_LINE"),
}
matchesBodyLine := func(value string) bool {
for _, bodyLine := range bodyLines {
if bodyLine.MatchString(value) {
return true
}
}
return false
}
for value, want := range map[string]bool{
"body\n": true,
"body\r\n": true,
"body\r": false,
"PATCH\n": false,
"new index.html\n": true,
" type <<PATCH\n": true,
"type <<PATCH extra\n": true,
"type+not-an-opener <<PATCH\n": true,
"type <<PATCH\n": false,
"type 1:a2b3 <<PATCH\n": false,
"type- 1:a2b3..2:b3c4 <<PATCH\r\n": false,
"type+ 1:a2b3 \"literal\" 2 <<PATCH\n": false,
} {
if got := matchesBodyLine(value); got != want {
t.Errorf("patch body matches %q = %v, want %v", value, got, want)
}
}
}
func TestHPatch2ToolDescriptionCoversSafeCommandChoice(t *testing.T) {
normalized := strings.Join(strings.Fields(toolDescription), " ")
for _, guidance := range []string{
"HPATCH/2",
"Do not call this tool in parallel with other tools.",
"Nonempty line and range `type` replacements preserve",
"`type` replaces",
"`type-` inserts before",
"`type+` inserts after",
"An empty target-bearing `type` value deletes",
"fixed `<<PATCH`",
"reserved as a nested opener",
"immutable baseline",
"submit every known related edit in one atomic script",
"including related multiline declarations",
"Split only when a later edit depends on validation",
"Keep unrelated large `<<PATCH` values",
"Prefer the smallest mutation that expresses the semantic change",
"When a formatter owns formatting, alignment, or indentation",
"add one struct field with one insertion",
"indentation-sensitive languages such as Python",
"Successful final-state `LINE:HASH` rows are current references",
"not targetable in the same call",
"Multiple insertions at the same boundary render in script order.",
"Changed Go files are parsed and formatted before success",
"parents for `new` or `mv` must exist",
"reread stale rows instead of guessing",
} {
if !strings.Contains(normalized, guidance) {
t.Errorf("tool description omits %q", guidance)
}
}
for _, excluded := range []string{"HPATCH/1", "\ntsel ", "\nrsel ", "\ncopy", "\ncut", "\npaste", "\ncommit", "<<TAG", "Usage:", "--root", "hpatch gain", "prefer one range `type`", "at most one syntax-sensitive multiline Go", "discard its saved references", "Use `hgrep` as replacement of `rg` or `grep`", "Use `hread` as replacement of `cat` or `sed`"} {
if strings.Contains(toolDescription, excluded) {
t.Errorf("tool description retains excluded material %q", excluded)
}
}
}
func TestHPatch2ToolDescriptionExamplesExecute(t *testing.T) {
root := t.TempDir()
writeTestFile(t, root, "parser.go", "package parser\n\nfunc parse() {}\n", 0o644)
script := "in parser.go\n" +
"type- " + row(3, "func parse() {}") + ` "// parse converts one command.\n"`
_, stderr, exitCode := runForTest(root, nil, script)
if exitCode != 0 {
t.Fatalf("Run() = exit %d, stderr %q", exitCode, stderr)
}
want := "package parser\n\n// parse converts one command.\nfunc parse() {}\n"
if got := readTestFile(t, root, "parser.go"); got != want {
t.Fatalf("parser.go = %q, want %q", got, want)
}
}
func TestHPatch2ToolDescriptionUsesInlineForSingleLineInsertion(t *testing.T) {
if !strings.Contains(toolDescription, `type- 37:8c2f "// parseCommand parses one physical script line.\n"`) {
t.Fatal("tool description lacks the approved non-heredoc single-line insertion")
}
}
func grammarTerminalRegexp(t *testing.T, name string) *regexp.Regexp {
t.Helper()
prefix := name + ": /"
for line := range strings.SplitSeq(toolGrammar, "\n") {
if !strings.HasPrefix(line, prefix) {
continue
}
pattern, ok := strings.CutSuffix(strings.TrimPrefix(line, prefix), "/")
if !ok {
t.Fatalf("%s terminal does not end with /: %q", name, line)
}
pattern = strings.ReplaceAll(pattern, `\/`, "/")
compiled, err := regexp.Compile("^(?:" + pattern + ")$")
if err != nil {
t.Fatalf("compile %s terminal: %v", name, err)
}
return compiled
}
t.Fatalf("%s terminal not found", name)
return nil
}