github.com/v2fly/tools@v0.100.0/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 TestParseQuery(t *testing.T) {
    13  	tests := []struct {
    14  		query, s  string
    15  		wantMatch bool
    16  	}{
    17  		{"", "anything", false},
    18  		{"any", "anything", true},
    19  		{"any$", "anything", false},
    20  		{"ing$", "anything", true},
    21  		{"ing$", "anythinG", true},
    22  		{"inG$", "anything", false},
    23  		{"^any", "anything", true},
    24  		{"^any", "Anything", true},
    25  		{"^Any", "anything", false},
    26  		{"at", "anything", true},
    27  		// TODO: this appears to be a bug in the fuzzy matching algorithm. 'At'
    28  		// should cause a case-sensitive match.
    29  		// {"At", "anything", false},
    30  		{"At", "Anything", true},
    31  		{"'yth", "Anything", true},
    32  		{"'yti", "Anything", false},
    33  		{"'any 'thing", "Anything", true},
    34  		{"anythn nythg", "Anything", true},
    35  		{"ntx", "Anything", false},
    36  		{"anythn", "anything", true},
    37  		{"ing", "anything", true},
    38  		{"anythn nythgx", "anything", false},
    39  	}
    40  
    41  	for _, test := range tests {
    42  		matcher := parseQuery(test.query)
    43  		if score := matcher(test.s); score > 0 != test.wantMatch {
    44  			t.Errorf("parseQuery(%q) match for %q: %.2g, want match: %t", test.query, test.s, score, test.wantMatch)
    45  		}
    46  	}
    47  }
    48  
    49  func TestBestMatch(t *testing.T) {
    50  	tests := []struct {
    51  		desc      string
    52  		symbol    string
    53  		matcher   matcherFunc
    54  		wantMatch string
    55  		wantScore float64
    56  	}{
    57  		{
    58  			desc:      "shortest match",
    59  			symbol:    "foo/bar/baz.quux",
    60  			matcher:   func(string) float64 { return 1.0 },
    61  			wantMatch: "quux",
    62  			wantScore: 1.0,
    63  		},
    64  		{
    65  			desc:   "partial match",
    66  			symbol: "foo/bar/baz.quux",
    67  			matcher: func(s string) float64 {
    68  				if strings.HasPrefix(s, "bar") {
    69  					return 1.0
    70  				}
    71  				return 0.0
    72  			},
    73  			wantMatch: "bar/baz.quux",
    74  			wantScore: 1.0,
    75  		},
    76  		{
    77  			desc:   "longest match",
    78  			symbol: "foo/bar/baz.quux",
    79  			matcher: func(s string) float64 {
    80  				parts := strings.Split(s, "/")
    81  				return float64(len(parts))
    82  			},
    83  			wantMatch: "foo/bar/baz.quux",
    84  			wantScore: 3.0,
    85  		},
    86  	}
    87  
    88  	for _, test := range tests {
    89  		test := test
    90  		t.Run(test.desc, func(t *testing.T) {
    91  			gotMatch, gotScore := bestMatch(test.symbol, test.matcher)
    92  			if gotMatch != test.wantMatch || gotScore != test.wantScore {
    93  				t.Errorf("bestMatch(%q, matcher) = (%q, %.2g), want (%q, %.2g)", test.symbol, gotMatch, gotScore, test.wantMatch, test.wantScore)
    94  			}
    95  		})
    96  	}
    97  }