Rob Findley | 6da3d7a | 2021-05-17 16:24:09 -0400 | [diff] [blame] | 1 | // Copyright 2021 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. |
| 4 | |
| 5 | package misc |
| 6 | |
| 7 | import ( |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | . "golang.org/x/tools/internal/lsp/regtest" |
danishprakash | 0c506a2 | 2021-06-20 12:32:55 +0530 | [diff] [blame^] | 12 | "golang.org/x/tools/internal/testenv" |
Rob Findley | 6da3d7a | 2021-05-17 16:24:09 -0400 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | func TestHoverUnexported(t *testing.T) { |
| 16 | const proxy = ` |
| 17 | -- golang.org/x/structs@v1.0.0/go.mod -- |
| 18 | module golang.org/x/structs |
| 19 | |
| 20 | go 1.12 |
| 21 | |
| 22 | -- golang.org/x/structs@v1.0.0/types.go -- |
| 23 | package structs |
| 24 | |
| 25 | type Mixed struct { |
| 26 | Exported int |
| 27 | unexported string |
| 28 | } |
| 29 | ` |
| 30 | const mod = ` |
| 31 | -- go.mod -- |
| 32 | module mod.com |
| 33 | |
| 34 | go 1.12 |
| 35 | |
| 36 | require golang.org/x/structs v1.0.0 |
| 37 | -- go.sum -- |
| 38 | golang.org/x/structs v1.0.0 h1:oxD5q25qV458xBbXf5+QX+Johgg71KFtwuJzt145c9A= |
| 39 | golang.org/x/structs v1.0.0/go.mod h1:47gkSIdo5AaQaWJS0upVORsxfEr1LL1MWv9dmYF3iq4= |
| 40 | -- main.go -- |
| 41 | package main |
| 42 | |
| 43 | import "golang.org/x/structs" |
| 44 | |
| 45 | func main() { |
| 46 | var _ structs.Mixed |
| 47 | } |
| 48 | ` |
| 49 | // TODO: use a nested workspace folder here. |
| 50 | WithOptions( |
| 51 | ProxyFiles(proxy), |
| 52 | ).Run(t, mod, func(t *testing.T, env *Env) { |
| 53 | env.OpenFile("main.go") |
| 54 | got, _ := env.Hover("main.go", env.RegexpSearch("main.go", "Mixed")) |
| 55 | if !strings.Contains(got.Value, "unexported") { |
| 56 | t.Errorf("Hover: missing expected field 'unexported'. Got:\n%q", got.Value) |
| 57 | } |
| 58 | }) |
| 59 | } |
danishprakash | 0c506a2 | 2021-06-20 12:32:55 +0530 | [diff] [blame^] | 60 | |
| 61 | func TestHoverIntLiteral(t *testing.T) { |
| 62 | testenv.NeedsGo1Point(t, 13) |
| 63 | const source = ` |
| 64 | -- main.go -- |
| 65 | package main |
| 66 | |
| 67 | var ( |
| 68 | bigBin = 0b1001001 |
| 69 | ) |
| 70 | |
| 71 | var hex = 0xe34e |
| 72 | |
| 73 | func main() { |
| 74 | } |
| 75 | ` |
| 76 | Run(t, source, func(t *testing.T, env *Env) { |
| 77 | env.OpenFile("main.go") |
| 78 | hexExpected := "58190" |
| 79 | got, _ := env.Hover("main.go", env.RegexpSearch("main.go", "hex")) |
| 80 | if got != nil && !strings.Contains(got.Value, hexExpected) { |
| 81 | t.Errorf("Hover: missing expected field '%s'. Got:\n%q", hexExpected, got.Value) |
| 82 | } |
| 83 | |
| 84 | binExpected := "73" |
| 85 | got, _ = env.Hover("main.go", env.RegexpSearch("main.go", "bigBin")) |
| 86 | if got != nil && !strings.Contains(got.Value, binExpected) { |
| 87 | t.Errorf("Hover: missing expected field '%s'. Got:\n%q", binExpected, got.Value) |
| 88 | } |
| 89 | }) |
| 90 | } |