1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:14:45 -07:00

internal/lsp/regtest: add a OnceMet combinator for Expectations

A common problem when writing regtests is that if you have an error in
your expectations, you must wait until the regtest times out to see what
went wrong.

With the integration of additional progress reporting in the LSP server,
we know when diagnostic work should have been completed, and we should
be able to fail tests early once we know that our diagnostic
expectations will never be met.

This CL adds a new OnceMet Expectation, which combines two expecations:
the first is a precondition that must be met before checking the second.
The second is an arbitrary expectation, but is translated as follows:
once the precondition is met, the second condition is checked and any
Unmet verdicts are translated into Unmeetable.

Change-Id: Ie8c677229a347c624e2659a3ef9104304b175243
Reviewed-on: https://go-review.googlesource.com/c/tools/+/229977
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Rob Findley 2020-04-24 14:22:13 -04:00 committed by Robert Findley
parent dbf5ce1eac
commit 0c9eba77bc
2 changed files with 32 additions and 1 deletions

View File

@ -28,13 +28,21 @@ func main() {
}`
func TestDiagnosticErrorInEditedFile(t *testing.T) {
// This test is very basic: start with a clean Go program, make an error, and
// get a diagnostic for that error. However, it also demonstrates how to
// combine Expectations to await more complex state in the editor.
runner.Run(t, exampleProgram, func(t *testing.T, env *Env) {
// Deleting the 'n' at the end of Println should generate a single error
// diagnostic.
env.OpenFile("main.go")
env.RegexpReplace("main.go", "Printl(n)", "")
env.Await(
env.DiagnosticAtRegexp("main.go", "Printl"),
// Once we have gotten diagnostics for the change above, we should
// satisfy the DiagnosticAtRegexp assertion.
OnceMet(
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChange), 1),
env.DiagnosticAtRegexp("main.go", "Printl"),
),
// Assert that this test has sent no error logs to the client. This is not
// strictly necessary for testing this regression, but is included here
// as an example of using the NoErrorLogs() expectation. Feel free to

View File

@ -259,6 +259,29 @@ const (
Unmeetable
)
// OnceMet returns an Expectation that, once the precondition is met, asserts
// that mustMeet is met.
func OnceMet(precondition Expectation, mustMeet Expectation) *SimpleExpectation {
check := func(s State) (Verdict, interface{}) {
switch pre, _ := precondition.Check(s); pre {
case Unmeetable:
return Unmeetable, nil
case Met:
verdict, metBy := mustMeet.Check(s)
if verdict != Met {
return Unmeetable, metBy
}
return Met, metBy
default:
return Unmet, nil
}
}
return &SimpleExpectation{
check: check,
description: fmt.Sprintf("once %q is met, must have %q", precondition.Description(), mustMeet.Description()),
}
}
func (v Verdict) String() string {
switch v {
case Met: