1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/tools/internal/lsp/source"
14 "golang.org/x/tools/internal/span"
17 func (r *runner) Diagnostics(t *testing.T, uri span.URI, want []*source.Diagnostic) {
18 if len(want) == 1 && want[0].Message == "" {
21 fname := uri.Filename()
22 out, _ := r.runGoplsCmd(t, "check", fname)
23 // parse got into a collection of reports
24 got := map[string]struct{}{}
25 for _, l := range strings.Split(out, "\n") {
29 // parse and reprint to normalize the span
30 bits := strings.SplitN(l, ": ", 2)
32 spn := span.Parse(strings.TrimSpace(bits[0]))
33 spn = span.New(spn.URI(), spn.Start(), span.Point{})
34 data, err := ioutil.ReadFile(fname)
38 converter := span.NewContentConverter(fname, data)
39 s, err := spn.WithPosition(converter)
43 l = fmt.Sprintf("%s: %s", s, strings.TrimSpace(bits[1]))
45 got[r.NormalizePrefix(l)] = struct{}{}
47 for _, diag := range want {
48 expect := fmt.Sprintf("%v:%v:%v: %v", uri.Filename(), diag.Range.Start.Line+1, diag.Range.Start.Character+1, diag.Message)
49 if diag.Range.Start.Character == 0 {
50 expect = fmt.Sprintf("%v:%v: %v", uri.Filename(), diag.Range.Start.Line+1, diag.Message)
52 expect = r.NormalizePrefix(expect)
53 _, found := got[expect]
55 t.Errorf("missing diagnostic %q, %v", expect, got)
60 for extra := range got {
61 t.Errorf("extra diagnostic %q", extra)