github.com/v2fly/tools@v0.100.0/internal/lsp/cmd/test/workspace_symbol.go (about) 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 "fmt" 9 "path/filepath" 10 "sort" 11 "strings" 12 "testing" 13 14 "github.com/v2fly/tools/internal/lsp/source" 15 "github.com/v2fly/tools/internal/lsp/tests" 16 "github.com/v2fly/tools/internal/span" 17 ) 18 19 func (r *runner) WorkspaceSymbols(t *testing.T, uri span.URI, query string, typ tests.WorkspaceSymbolsTestType) { 20 var matcher string 21 switch typ { 22 case tests.WorkspaceSymbolsFuzzy: 23 matcher = "fuzzy" 24 case tests.WorkspaceSymbolsCaseSensitive: 25 matcher = "caseSensitive" 26 case tests.WorkspaceSymbolsDefault: 27 matcher = "caseInsensitive" 28 } 29 r.runWorkspaceSymbols(t, uri, matcher, query) 30 } 31 32 func (r *runner) runWorkspaceSymbols(t *testing.T, uri span.URI, matcher, query string) { 33 t.Helper() 34 35 out, _ := r.runGoplsCmd(t, "workspace_symbol", "-matcher", matcher, query) 36 var filtered []string 37 dir := filepath.Dir(uri.Filename()) 38 for _, line := range strings.Split(out, "\n") { 39 if source.InDir(dir, line) { 40 filtered = append(filtered, filepath.ToSlash(line)) 41 } 42 } 43 sort.Strings(filtered) 44 got := r.Normalize(strings.Join(filtered, "\n") + "\n") 45 46 expect := string(r.data.Golden(fmt.Sprintf("workspace_symbol-%s-%s", strings.ToLower(string(matcher)), query), uri.Filename(), func() ([]byte, error) { 47 return []byte(got), nil 48 })) 49 50 if expect != got { 51 t.Errorf("workspace_symbol failed for %s:\n%s", query, tests.Diff(t, expect, got)) 52 } 53 }