2019-07-01 15:08:29 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package fuzzy_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/fuzzy"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rolesTests = []struct {
|
2019-10-21 22:07:21 -06:00
|
|
|
str string
|
|
|
|
want string
|
2019-07-01 15:08:29 -06:00
|
|
|
}{
|
2019-10-21 22:07:21 -06:00
|
|
|
{str: "abc::def::goo", want: "Ccc//Ccc//Ccc"},
|
|
|
|
{str: "proto::Message", want: "Ccccc//Ccccccc"},
|
|
|
|
{str: "AbstractSWTFactory", want: "CcccccccCuuCcccccc"},
|
|
|
|
{str: "Abs012", want: "Cccccc"},
|
|
|
|
{str: "/", want: " "},
|
|
|
|
{str: "fOO", want: "CCu"},
|
|
|
|
{str: "fo_oo.o_oo", want: "Cc Cc/C Cc"},
|
2019-07-01 15:08:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func rolesString(roles []fuzzy.RuneRole) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, r := range roles {
|
|
|
|
buf.WriteByte(" /cuC"[int(r)])
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoles(t *testing.T) {
|
|
|
|
for _, tc := range rolesTests {
|
|
|
|
gotRoles := make([]fuzzy.RuneRole, len(tc.str))
|
2019-10-21 22:07:21 -06:00
|
|
|
fuzzy.RuneRoles(tc.str, gotRoles)
|
2019-07-01 15:08:29 -06:00
|
|
|
got := rolesString(gotRoles)
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("roles(%s) = %v; want %v", tc.str, got, tc.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wordSplitTests = []struct {
|
|
|
|
input string
|
|
|
|
want []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "foo bar baz",
|
|
|
|
want: []string{"foo", "bar", "baz"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "fooBarBaz",
|
|
|
|
want: []string{"foo", "Bar", "Baz"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "FOOBarBAZ",
|
|
|
|
want: []string{"FOO", "Bar", "BAZ"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo123_bar2Baz3",
|
|
|
|
want: []string{"foo123", "bar2", "Baz3"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWordSplit(t *testing.T) {
|
|
|
|
for _, tc := range wordSplitTests {
|
2019-10-21 22:07:21 -06:00
|
|
|
roles := fuzzy.RuneRoles(tc.input, nil)
|
2019-07-01 15:08:29 -06:00
|
|
|
|
|
|
|
var got []string
|
|
|
|
consumer := func(i, j int) {
|
|
|
|
got = append(got, tc.input[i:j])
|
|
|
|
}
|
|
|
|
fuzzy.Words(roles, consumer)
|
|
|
|
|
|
|
|
if eq := diffStringLists(tc.want, got); !eq {
|
|
|
|
t.Errorf("input %v: (want %v -> got %v)", tc.input, tc.want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func diffStringLists(a, b []string) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sort.Strings(a)
|
|
|
|
sort.Strings(b)
|
|
|
|
for i := range a {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastSegmentSplitTests = []struct {
|
2019-10-21 22:07:21 -06:00
|
|
|
str string
|
|
|
|
want string
|
2019-07-01 15:08:29 -06:00
|
|
|
}{
|
|
|
|
{
|
2019-10-21 22:07:21 -06:00
|
|
|
str: "identifier",
|
|
|
|
want: "identifier",
|
2019-07-01 15:08:29 -06:00
|
|
|
},
|
|
|
|
{
|
2019-10-21 22:07:21 -06:00
|
|
|
str: "two_words",
|
|
|
|
want: "two_words",
|
2019-07-01 15:08:29 -06:00
|
|
|
},
|
|
|
|
{
|
2019-10-21 22:07:21 -06:00
|
|
|
str: "first::second",
|
|
|
|
want: "second",
|
2019-07-01 15:08:29 -06:00
|
|
|
},
|
|
|
|
{
|
2019-10-21 22:07:21 -06:00
|
|
|
str: "foo.bar.FOOBar_buz123_test",
|
|
|
|
want: "FOOBar_buz123_test",
|
2019-07-01 15:08:29 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLastSegment(t *testing.T) {
|
|
|
|
for _, tc := range lastSegmentSplitTests {
|
2019-10-21 22:07:21 -06:00
|
|
|
roles := fuzzy.RuneRoles(tc.str, nil)
|
2019-07-01 15:08:29 -06:00
|
|
|
|
|
|
|
got := fuzzy.LastSegment(tc.str, roles)
|
|
|
|
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("str %v: want %v; got %v", tc.str, tc.want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRoles(b *testing.B) {
|
|
|
|
str := "AbstractSWTFactory"
|
|
|
|
out := make([]fuzzy.RuneRole, len(str))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-10-21 22:07:21 -06:00
|
|
|
fuzzy.RuneRoles(str, out)
|
2019-07-01 15:08:29 -06:00
|
|
|
}
|
|
|
|
b.SetBytes(int64(len(str)))
|
|
|
|
}
|