Skip to content

Commit 63e03c3

Browse files
committed
gopls/internal/test/marker: generalize codeaction with named args
Address a TODO to consolidate the three codeaction marks into one, by using named arguments to provide an optional end location and one of edit, result, or err. Along the way, I accidentally nuked my golden files after doing quite a lot of manual editing of the test cases. To restore them, implement a heuristic I've wanted for a while: when running with -update, add new golden files immediately after the first reference to them among test files. This is the convention we've been using, but previously we'd have to position the golden files manually. Change-Id: Ie78f759045aa0c07817d4eb21672be63e51c5067 Reviewed-on: https://go-review.googlesource.com/c/tools/+/627136 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent acc2a74 commit 63e03c3

36 files changed

+840
-763
lines changed

gopls/internal/test/integration/workspace/quickfix_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func main() {}
341341
}
342342

343343
func TestStubMethods64087(t *testing.T) {
344-
// We can't use the @fix or @quickfixerr or @codeactionerr
344+
// We can't use the @fix or @quickfixerr or @codeaction
345345
// because the error now reported by the corrected logic
346346
// is internal and silently causes no fix to be offered.
347347
//
@@ -404,7 +404,7 @@ type myerror struct{any}
404404
}
405405

406406
func TestStubMethods64545(t *testing.T) {
407-
// We can't use the @fix or @quickfixerr or @codeactionerr
407+
// We can't use the @fix or @quickfixerr or @codeaction
408408
// because the error now reported by the corrected logic
409409
// is internal and silently causes no fix to be offered.
410410
//

gopls/internal/test/marker/doc.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
Package marker defines a framework for running "marker" tests, each
77
defined by a file in the testdata subdirectory.
88
9-
Use this command to run the tests:
9+
Use this command to run the tests, from the gopls module:
1010
11-
$ go test ./gopls/internal/test/marker [-update]
11+
$ go test ./internal/test/marker [-update]
1212
1313
A marker test uses the '//@' marker syntax of the x/tools/internal/expect package
1414
to annotate source code with various information such as locations and
@@ -100,21 +100,17 @@ The following markers are supported within marker tests:
100100
completion candidate produced at the given location with provided label
101101
results in the given golden state.
102102
103-
- codeaction(start, end, kind, golden): specifies a code action
104-
to request for the given range. To support multi-line ranges, the range
105-
is defined to be between start.Start and end.End. The golden directory
106-
contains changed file content after the code action is applied.
103+
- codeaction(start location, kind string, end=location, edit=golden, result=golden, err=stringMatcher)
107104
108-
TODO(rfindley): now that 'location' supports multi-line matches, replace
109-
uses of 'codeaction' with codeactionedit.
105+
Specifies a code action to request at the location, with given kind.
110106
111-
- codeactionedit(location, kind, golden): a shorter form of
112-
codeaction. Invokes a code action of the given kind for the given
113-
in-line range, and compares the resulting formatted unified *edits*
114-
(notably, not the full file content) with the golden directory.
107+
If end is set, the location is defined to be between start.Start and end.End.
115108
116-
- codeactionerr(start, end, kind, wantError): specifies a codeaction that
117-
fails with an error that matches the expectation.
109+
Exactly one of edit, result, or err must be set.
110+
If edit is set, it is a golden reference to the edits resulting from the code action.
111+
If result is set, it is a golden reference to the full set of changed files
112+
resulting from the code action.
113+
If err is set, it is the code action error.
118114
119115
- codelens(location, title): specifies that a codelens is expected at the
120116
given location, with given title. Must be used in conjunction with
@@ -136,7 +132,8 @@ The following markers are supported within marker tests:
136132
but end positions are ignored unless exact=true.
137133
138134
TODO(adonovan): in the older marker framework, the annotation asserted
139-
two additional fields (source="compiler", kind="error"). Restore them?
135+
two additional fields (source="compiler", kind="error"). Restore them using
136+
optional named arguments.
140137
141138
- def(src, dst location): performs a textDocument/definition request at
142139
the src location, and check the result points to the dst location.
@@ -208,17 +205,6 @@ The following markers are supported within marker tests:
208205
placeholder. If placeholder is "", this is treated as a negative
209206
assertion and prepareRename should return nil.
210207
211-
- rename(location, new, golden): specifies a renaming of the
212-
identifier at the specified location to the new name.
213-
The golden directory contains the transformed files.
214-
215-
- renameerr(location, new, wantError): specifies a renaming that
216-
fails with an error that matches the expectation.
217-
218-
- signature(location, label, active): specifies that
219-
signatureHelp at the given location should match the provided string, with
220-
the active parameter (an index) highlighted.
221-
222208
- quickfix(location, regexp, golden): like diag, the location and
223209
regexp identify an expected diagnostic, which must have exactly one
224210
associated "quickfix" code action.
@@ -244,6 +230,17 @@ The following markers are supported within marker tests:
244230
'want' locations. The first want location must be the declaration
245231
(assumedly unique).
246232
233+
- rename(location, new, golden): specifies a renaming of the
234+
identifier at the specified location to the new name.
235+
The golden directory contains the transformed files.
236+
237+
- renameerr(location, new, wantError): specifies a renaming that
238+
fails with an error that matches the expectation.
239+
240+
- signature(location, label, active): specifies that
241+
signatureHelp at the given location should match the provided string, with
242+
the active parameter (an index) highlighted.
243+
247244
- snippet(location, string OR completionItem, snippet): executes a
248245
textDocument/completion request at the location, and searches for a result
249246
with label matching that its second argument, which may be a string literal
@@ -288,20 +285,26 @@ the following tokens as defined by the Go spec:
288285
These values are passed as arguments to the corresponding parameter of the
289286
test function. Additional value conversions may occur for these argument ->
290287
parameter type pairs:
288+
291289
- string->regexp: the argument is parsed as a regular expressions.
290+
292291
- string->location: the argument is converted to the location of the first
293292
instance of the argument in the file content starting from the beginning of
294293
the line containing the note. Multi-line matches are permitted, but the
295294
match must begin before the note.
295+
296296
- regexp->location: the argument is converted to the location of the first
297297
match for the argument in the file content starting from the beginning of
298298
the line containing the note. Multi-line matches are permitted, but the
299299
match must begin before the note. If the regular expression contains
300300
exactly one subgroup, the position of the subgroup is used rather than the
301301
position of the submatch.
302+
302303
- name->location: the argument is replaced by the named location.
304+
303305
- name->Golden: the argument is used to look up golden content prefixed by
304306
@<argument>.
307+
305308
- {string,regexp,identifier}->stringMatcher: a stringMatcher type
306309
specifies an expected string, either in the form of a substring
307310
that must be present, a regular expression that it must match, or an
@@ -331,7 +334,7 @@ Here is a complete example:
331334
In this example, the @hover annotation tells the test runner to run the
332335
hoverMarker function, which has parameters:
333336
334-
(mark marker, src, dsc protocol.Location, g *Golden).
337+
(mark marker, src, dst protocol.Location, g *Golden).
335338
336339
The first argument holds the test context, including fake editor with open
337340
files, and sandboxed directory.
@@ -369,9 +372,6 @@ Note that -update does not cause missing @diag or @loc markers to be added.
369372
- Provide some means by which locations in the standard library
370373
(or builtin.go) can be named, so that, for example, we can we
371374
can assert that MyError implements the built-in error type.
372-
- If possible, improve handling for optional arguments. Rather than have
373-
multiple variations of a marker, it would be nice to support a more
374-
flexible signature: can codeaction, codeactionedit, codeactionerr, and
375-
quickfix be consolidated?
375+
- Eliminate all *err markers, preferring named arguments.
376376
*/
377377
package marker

0 commit comments

Comments
 (0)