1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:14:46 -07:00
go/internal/lsp/source/util_test.go
Rob Findley daa6538899 internal/lsp/source: fix panic in formatZeroValue for invalid type
formatZeroValue is currently only used when formatting return values for
statement completion. Per golang/go#40956, it must be possible to hit
this codepath with an invalid type.

In this case, the empty string seems like a reasonable value. Perhaps we
could do better, but fix the panic for now.

Fixes golang/go#40956

Change-Id: I45b559d41001c857cef34aea2a5ac4a9096fe950
Reviewed-on: https://go-review.googlesource.com/c/tools/+/249818
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-08-21 17:11:49 +00:00

29 lines
641 B
Go

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package source
import (
"go/types"
"testing"
)
func TestFormatZeroValue(t *testing.T) {
tests := []struct {
typ types.Type
want string
}{
{types.Typ[types.String], `""`},
{types.Typ[types.Byte], "0"},
{types.Typ[types.Invalid], ""},
{types.Universe.Lookup("error").Type(), "nil"},
}
for _, test := range tests {
if got := formatZeroValue(test.typ, nil); got != test.want {
t.Errorf("formatZeroValue(%v) = %q, want %q", test.typ, got, test.want)
}
}
}