github.com/april1989/origin-go-tools@v0.0.32/internal/lsp/source/workspace_symbol_test.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 source 6 7 import ( 8 "strings" 9 "testing" 10 ) 11 12 func TestBestMatch(t *testing.T) { 13 tests := []struct { 14 desc string 15 symbol string 16 matcher matcherFunc 17 wantMatch string 18 wantScore float64 19 }{ 20 { 21 desc: "shortest match", 22 symbol: "foo/bar/baz.quux", 23 matcher: func(string) float64 { return 1.0 }, 24 wantMatch: "quux", 25 wantScore: 1.0, 26 }, 27 { 28 desc: "partial match", 29 symbol: "foo/bar/baz.quux", 30 matcher: func(s string) float64 { 31 if strings.HasPrefix(s, "bar") { 32 return 1.0 33 } 34 return 0.0 35 }, 36 wantMatch: "bar/baz.quux", 37 wantScore: 1.0, 38 }, 39 { 40 desc: "longest match", 41 symbol: "foo/bar/baz.quux", 42 matcher: func(s string) float64 { 43 parts := strings.Split(s, "/") 44 return float64(len(parts)) 45 }, 46 wantMatch: "foo/bar/baz.quux", 47 wantScore: 3.0, 48 }, 49 } 50 51 for _, test := range tests { 52 test := test 53 t.Run(test.desc, func(t *testing.T) { 54 gotMatch, gotScore := bestMatch(test.symbol, test.matcher) 55 if gotMatch != test.wantMatch || gotScore != test.wantScore { 56 t.Errorf("bestMatch(%q, matcher) = (%q, %.2g), want (%q, %.2g)", test.symbol, gotMatch, gotScore, test.wantMatch, test.wantScore) 57 } 58 }) 59 } 60 }