| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package goversion_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"golang.org/x/tools/gopls/internal/util/goversion" |
| 13 |
) |
| 14 |
|
| 15 |
func TestMessage(t *testing.T) { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
type test struct { |
| 23 |
goVersion int |
| 24 |
fromBuild bool |
| 25 |
wantContains []string |
| 26 |
wantIsError bool |
| 27 |
} |
| 28 |
|
| 29 |
deprecated := func(goVersion int, lastVersion string) test { |
| 30 |
return test{ |
| 31 |
goVersion: goVersion, |
| 32 |
fromBuild: false, |
| 33 |
wantContains: []string{ |
| 34 |
fmt.Sprintf("Found Go version 1.%d", goVersion), |
| 35 |
"not supported", |
| 36 |
fmt.Sprintf("upgrade to Go 1.%d", goversion.OldestSupported()), |
| 37 |
fmt.Sprintf("install gopls %s", lastVersion), |
| 38 |
}, |
| 39 |
wantIsError: true, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
tests := []test{ |
| 44 |
{-1, false, nil, false}, |
| 45 |
deprecated(12, "v0.7.5"), |
| 46 |
deprecated(13, "v0.9.5"), |
| 47 |
deprecated(15, "v0.9.5"), |
| 48 |
deprecated(16, "v0.11.0"), |
| 49 |
deprecated(17, "v0.11.0"), |
| 50 |
deprecated(18, "v0.14.2"), |
| 51 |
{19, false, []string{"Found Go version 1.19", "unsupported by gopls v0.17.0", "upgrade to Go 1.21", "install gopls v0.15.3"}, false}, |
| 52 |
{19, true, []string{"Gopls was built with Go version 1.19", "unsupported by gopls v0.17.0", "upgrade to Go 1.21", "install gopls v0.15.3"}, false}, |
| 53 |
{20, false, []string{"Found Go version 1.20", "unsupported by gopls v0.17.0", "upgrade to Go 1.21", "install gopls v0.15.3"}, false}, |
| 54 |
{20, true, []string{"Gopls was built with Go version 1.20", "unsupported by gopls v0.17.0", "upgrade to Go 1.21", "install gopls v0.15.3"}, false}, |
| 55 |
} |
| 56 |
|
| 57 |
for _, test := range tests { |
| 58 |
gotMsg, gotIsError := goversion.Message(test.goVersion, test.fromBuild) |
| 59 |
|
| 60 |
if len(test.wantContains) == 0 && gotMsg != "" { |
| 61 |
t.Errorf("versionMessage(%d) = %q, want \"\"", test.goVersion, gotMsg) |
| 62 |
} |
| 63 |
|
| 64 |
for _, want := range test.wantContains { |
| 65 |
if !strings.Contains(gotMsg, want) { |
| 66 |
t.Errorf("versionMessage(%d) = %q, want containing %q", test.goVersion, gotMsg, want) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if gotIsError != test.wantIsError { |
| 71 |
t.Errorf("versionMessage(%d) isError = %v, want %v", test.goVersion, gotIsError, test.wantIsError) |
| 72 |
} |
| 73 |
} |
| 74 |
} |