internal/lsp: minor clean up of symbol tests

remove the goto and make it print more information about why the failed.

Change-Id: I2f5f3c7fe88eae6fe5de61c5d9b401797b6933b2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/172404
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Ian Cottrell 2019-04-16 16:47:01 -04:00
parent e528388d46
commit e2848a0e7d
1 changed files with 15 additions and 10 deletions

View File

@ -563,11 +563,8 @@ func (s symbols) test(t *testing.T, server *Server) {
continue
}
sort.Slice(symbols, func(i, j int) bool { return symbols[i].Name < symbols[j].Name })
sort.Slice(expectedSymbols, func(i, j int) bool { return expectedSymbols[i].Name < expectedSymbols[j].Name })
for i := range expectedSymbols {
children := s.children[expectedSymbols[i].Name]
sort.Slice(children, func(i, j int) bool { return children[i].Name < children[j].Name })
expectedSymbols[i].Children = children
}
if diff := diffSymbols(uri, expectedSymbols, symbols); diff != "" {
@ -577,30 +574,38 @@ func (s symbols) test(t *testing.T, server *Server) {
}
func diffSymbols(uri span.URI, want, got []protocol.DocumentSymbol) string {
sort.Slice(want, func(i, j int) bool { return want[i].Name < want[j].Name })
sort.Slice(got, func(i, j int) bool { return got[i].Name < got[j].Name })
if len(got) != len(want) {
goto Failed
return summarizeSymbols(-1, want, got, "different lengths got %v want %v", len(got), len(want))
}
for i, w := range want {
g := got[i]
if w.Name != g.Name {
goto Failed
return summarizeSymbols(i, want, got, "incorrect name got %v want %v", g.Name, w.Name)
}
if w.Kind != g.Kind {
goto Failed
return summarizeSymbols(i, want, got, "incorrect kind got %v want %v", g.Kind, w.Kind)
}
if w.SelectionRange != g.SelectionRange {
goto Failed
return summarizeSymbols(i, want, got, "incorrect span got %v want %v", g.SelectionRange, w.SelectionRange)
}
sort.Slice(g.Children, func(i, j int) bool { return g.Children[i].Name < g.Children[j].Name })
if msg := diffSymbols(uri, w.Children, g.Children); msg != "" {
return fmt.Sprintf("children of %s: %s", w.Name, msg)
}
}
return ""
}
Failed:
func summarizeSymbols(i int, want []protocol.DocumentSymbol, got []protocol.DocumentSymbol, reason string, args ...interface{}) string {
msg := &bytes.Buffer{}
fmt.Fprintf(msg, "document symbols failed for %s:\nexpected:\n", uri)
fmt.Fprint(msg, "document symbols 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 _, s := range want {
fmt.Fprintf(msg, " %v %v %v\n", s.Name, s.Kind, s.SelectionRange)
}