go/gopls/internal/lsp/tests
Robert Findley b243e57ea8 gopls/internal/lsp/tests: simplify collectCompletionItems, remove Data.t
The marker function collectCompletionItems required at least three
arguments. Express this in its signature, leaving a final variadic
argument for documentation. I considered just making this final argument
mandatory, but opted for minimizing the diff given that there are 400+
existing @item annotations.

With this change the only use of tests.Data.t is in mustRange. Since
conversion to range should always succeed, I switched this usage to a
panic and removed the t field.

For golang/go#54845

Change-Id: I407f07cb85fa1356ceb6dba366007f69d1b6a068
Reviewed-on: https://go-review.googlesource.com/c/tools/+/432337
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-09-23 13:52:05 +00:00
..
compare gopls: migrate internal/lsp to gopls/internal/lsp 2022-09-07 16:44:44 +00:00
README.md gopls: migrate internal/lsp to gopls/internal/lsp 2022-09-07 16:44:44 +00:00
markdown_go118.go gopls: migrate internal/lsp to gopls/internal/lsp 2022-09-07 16:44:44 +00:00
markdown_go119.go gopls: migrate internal/lsp to gopls/internal/lsp 2022-09-07 16:44:44 +00:00
normalizer.go gopls: migrate internal/lsp to gopls/internal/lsp 2022-09-07 16:44:44 +00:00
tests.go gopls/internal/lsp/tests: simplify collectCompletionItems, remove Data.t 2022-09-23 13:52:05 +00:00
util.go internal/lsp/source: derive document symbols from syntax alone 2022-09-23 13:51:08 +00:00

README.md

Testing

LSP has "marker tests" defined in internal/lsp/testdata, as well as traditional tests.

Marker tests

Marker tests have a standard input file, like internal/lsp/testdata/foo/bar.go, and some may have a corresponding golden file, like internal/lsp/testdata/foo/bar.go.golden. The former is the "input" and the latter is the expected output.

Each input file contains annotations like //@suggestedfix("}", "refactor.rewrite", "Fill anonymous struct"). These annotations are interpreted by test runners to perform certain actions. The expected output after those actions is encoded in the golden file.

When tests are run, each annotation results in a new subtest, which is encoded in the golden file with a heading like,

-- suggestedfix_bar_11_21 --
// expected contents go here
-- suggestedfix_bar_13_20 --
// expected contents go here

The format of these headings vary: they are defined by the Golden function for each annotation. In the case above, the format is: annotation name, file name, annotation line location, annotation character location.

So, if internal/lsp/testdata/foo/bar.go has three suggestedfix annotations, the golden file should have three headers with suggestedfix_bar_xx_yy headings.

To see a list of all available annotations, see the exported "expectations" in tests.go.

To run marker tests,

cd /path/to/tools

# The marker tests are located in "internal/lsp", "internal/lsp/cmd, and
# "internal/lsp/source".
go test ./internal/lsp/...

There are quite a lot of marker tests, so to run one individually, pass the test path and heading into a -run argument:

cd /path/to/tools
go test ./internal/lsp/... -v -run TestLSP/Modules/SuggestedFix/bar_11_21

Resetting marker tests

Sometimes, a change is made to lsp that requires a change to multiple golden files. When this happens, you can run,

cd /path/to/tools
./internal/lsp/reset_golden.sh