|
| 1 | +// Copyright 2020 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 cmdtest |
| 6 | + |
| 7 | +import ( |
| 8 | + "path" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "golang.org/x/tools/internal/lsp/protocol" |
| 14 | +) |
| 15 | + |
| 16 | +func (r *runner) WorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) { |
| 17 | + r.runWorkspaceSymbols(t, "default", query, dirs) |
| 18 | +} |
| 19 | + |
| 20 | +func (r *runner) FuzzyWorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) { |
| 21 | + r.runWorkspaceSymbols(t, "fuzzy", query, dirs) |
| 22 | +} |
| 23 | + |
| 24 | +func (r *runner) CaseSensitiveWorkspaceSymbols(t *testing.T, query string, expectedSymbols []protocol.SymbolInformation, dirs map[string]struct{}) { |
| 25 | + r.runWorkspaceSymbols(t, "caseSensitive", query, dirs) |
| 26 | +} |
| 27 | + |
| 28 | +func (r *runner) runWorkspaceSymbols(t *testing.T, matcher, query string, dirs map[string]struct{}) { |
| 29 | + t.Helper() |
| 30 | + |
| 31 | + out, _ := r.runGoplsCmd(t, "workspace_symbol", "-matcher", matcher, query) |
| 32 | + var filtered []string |
| 33 | + for _, line := range strings.Split(out, "\n") { |
| 34 | + for dir := range dirs { |
| 35 | + if strings.HasPrefix(line, dir) { |
| 36 | + filtered = append(filtered, line) |
| 37 | + break |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + sort.Strings(filtered) |
| 42 | + got := r.Normalize(strings.Join(filtered, "\n")) |
| 43 | + |
| 44 | + expect := string(r.data.Golden("workspace_symbol", workspaceSymbolsGolden(matcher, query), func() ([]byte, error) { |
| 45 | + return []byte(got), nil |
| 46 | + })) |
| 47 | + |
| 48 | + if expect != got { |
| 49 | + t.Errorf("workspace_symbol failed for %s expected:\n%s\ngot:\n%s", query, expect, got) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +var workspaceSymbolsDir = map[string]string{ |
| 54 | + "default": "", |
| 55 | + "fuzzy": "fuzzy", |
| 56 | + "caseSensitive": "casesensitive", |
| 57 | +} |
| 58 | + |
| 59 | +func workspaceSymbolsGolden(matcher, query string) string { |
| 60 | + dir := []string{"workspacesymbol", workspaceSymbolsDir[matcher]} |
| 61 | + if query == "" { |
| 62 | + return path.Join(append(dir, "EmptyQuery")...) |
| 63 | + } |
| 64 | + |
| 65 | + var name []rune |
| 66 | + for _, r := range query { |
| 67 | + if 'A' <= r && r <= 'Z' { |
| 68 | + // Escape uppercase to '!' + lowercase for case insensitive file systems. |
| 69 | + name = append(name, '!', r+'a'-'A') |
| 70 | + } else { |
| 71 | + name = append(name, r) |
| 72 | + } |
| 73 | + } |
| 74 | + return path.Join(append(dir, string(name))...) |
| 75 | +} |
0 commit comments