2019-09-17 09:10:48 -06:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DiffDiagnostics prints the diff between expected and actual diagnostics test
|
|
|
|
// results.
|
2019-11-21 13:14:48 -07:00
|
|
|
func DiffDiagnostics(uri span.URI, want, got []source.Diagnostic) string {
|
2019-11-20 23:24:43 -07:00
|
|
|
source.SortDiagnostics(want)
|
|
|
|
source.SortDiagnostics(got)
|
2019-09-17 09:10:48 -06:00
|
|
|
|
|
|
|
if len(got) != len(want) {
|
2019-11-21 13:14:48 -07:00
|
|
|
return summarizeDiagnostics(-1, uri, want, got, "different lengths got %v want %v", len(got), len(want))
|
2019-09-17 09:10:48 -06:00
|
|
|
}
|
|
|
|
for i, w := range want {
|
|
|
|
g := got[i]
|
|
|
|
if w.Message != g.Message {
|
2019-11-21 13:14:48 -07:00
|
|
|
return summarizeDiagnostics(i, uri, want, got, "incorrect Message got %v want %v", g.Message, w.Message)
|
2019-09-17 09:10:48 -06:00
|
|
|
}
|
2019-10-16 17:09:37 -06:00
|
|
|
if w.Severity != g.Severity {
|
2019-11-21 13:14:48 -07:00
|
|
|
return summarizeDiagnostics(i, uri, want, got, "incorrect Severity got %v want %v", g.Severity, w.Severity)
|
2019-10-16 17:09:37 -06:00
|
|
|
}
|
|
|
|
if w.Source != g.Source {
|
2019-11-21 13:14:48 -07:00
|
|
|
return summarizeDiagnostics(i, uri, want, got, "incorrect Source got %v want %v", g.Source, w.Source)
|
2019-10-16 17:09:37 -06:00
|
|
|
}
|
|
|
|
// Don't check the range on the badimport test.
|
2019-11-21 13:14:48 -07:00
|
|
|
if strings.Contains(uri.Filename(), "badimport") {
|
2019-10-16 17:09:37 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
if protocol.ComparePosition(w.Range.Start, g.Range.Start) != 0 {
|
2019-11-21 13:14:48 -07:00
|
|
|
return summarizeDiagnostics(i, uri, want, got, "incorrect Start got %v want %v", g.Range.Start, w.Range.Start)
|
2019-09-17 09:10:48 -06:00
|
|
|
}
|
2019-10-21 15:49:45 -06:00
|
|
|
if !protocol.IsPoint(g.Range) { // Accept any 'want' range if the diagnostic returns a zero-length range.
|
2019-09-17 09:10:48 -06:00
|
|
|
if protocol.ComparePosition(w.Range.End, g.Range.End) != 0 {
|
2019-11-21 13:14:48 -07:00
|
|
|
return summarizeDiagnostics(i, uri, want, got, "incorrect End got %v want %v", g.Range.End, w.Range.End)
|
2019-09-17 09:10:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:14:48 -07:00
|
|
|
func summarizeDiagnostics(i int, uri span.URI, want []source.Diagnostic, got []source.Diagnostic, reason string, args ...interface{}) string {
|
2019-09-17 09:10:48 -06:00
|
|
|
msg := &bytes.Buffer{}
|
|
|
|
fmt.Fprint(msg, "diagnostics failed")
|
|
|
|
if i >= 0 {
|
|
|
|
fmt.Fprintf(msg, " at %d", i)
|
|
|
|
}
|
|
|
|
fmt.Fprint(msg, " because of ")
|
|
|
|
fmt.Fprintf(msg, reason, args...)
|
|
|
|
fmt.Fprint(msg, ":\nexpected:\n")
|
|
|
|
for _, d := range want {
|
2019-11-21 13:14:48 -07:00
|
|
|
fmt.Fprintf(msg, " %s:%v: %s\n", uri, d.Range, d.Message)
|
2019-09-17 09:10:48 -06:00
|
|
|
}
|
|
|
|
fmt.Fprintf(msg, "got:\n")
|
|
|
|
for _, d := range got {
|
2019-11-21 13:14:48 -07:00
|
|
|
fmt.Fprintf(msg, " %s:%v: %s\n", uri, d.Range, d.Message)
|
2019-09-17 09:10:48 -06:00
|
|
|
}
|
|
|
|
return msg.String()
|
|
|
|
}
|