| 1 | // Copyright 2019 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 fuzzy_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "sort" |
| 10 | "testing" |
| 11 | |
| 12 | "golang.org/x/tools/internal/fuzzy" |
| 13 | ) |
| 14 | |
| 15 | var rolesTests = []struct { |
| 16 | str string |
| 17 | want string |
| 18 | }{ |
| 19 | {str: "abc::def::goo", want: "Ccc//Ccc//Ccc"}, |
| 20 | {str: "proto::Message", want: "Ccccc//Ccccccc"}, |
| 21 | {str: "AbstractSWTFactory", want: "CcccccccCuuCcccccc"}, |
| 22 | {str: "Abs012", want: "Cccccc"}, |
| 23 | {str: "/", want: " "}, |
| 24 | {str: "fOO", want: "CCu"}, |
| 25 | {str: "fo_oo.o_oo", want: "Cc Cc/C Cc"}, |
| 26 | } |
| 27 | |
| 28 | func rolesString(roles []fuzzy.RuneRole) string { |
| 29 | var buf bytes.Buffer |
| 30 | for _, r := range roles { |
| 31 | buf.WriteByte(" /cuC"[int(r)]) |
| 32 | } |
| 33 | return buf.String() |
| 34 | } |
| 35 | |
| 36 | func TestRoles(t *testing.T) { |
| 37 | for _, tc := range rolesTests { |
| 38 | gotRoles := make([]fuzzy.RuneRole, len(tc.str)) |
| 39 | fuzzy.RuneRoles([]byte(tc.str), gotRoles) |
| 40 | got := rolesString(gotRoles) |
| 41 | if got != tc.want { |
| 42 | t.Errorf("roles(%s) = %v; want %v", tc.str, got, tc.want) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | var wordSplitTests = []struct { |
| 48 | input string |
| 49 | want []string |
| 50 | }{ |
| 51 | { |
| 52 | input: "foo bar baz", |
| 53 | want: []string{"foo", "bar", "baz"}, |
| 54 | }, |
| 55 | { |
| 56 | input: "fooBarBaz", |
| 57 | want: []string{"foo", "Bar", "Baz"}, |
| 58 | }, |
| 59 | { |
| 60 | input: "FOOBarBAZ", |
| 61 | want: []string{"FOO", "Bar", "BAZ"}, |
| 62 | }, |
| 63 | { |
| 64 | input: "foo123_bar2Baz3", |
| 65 | want: []string{"foo123", "bar2", "Baz3"}, |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | func TestWordSplit(t *testing.T) { |
| 70 | for _, tc := range wordSplitTests { |
| 71 | roles := fuzzy.RuneRoles([]byte(tc.input), nil) |
| 72 | |
| 73 | var got []string |
| 74 | consumer := func(i, j int) { |
| 75 | got = append(got, tc.input[i:j]) |
| 76 | } |
| 77 | fuzzy.Words(roles, consumer) |
| 78 | |
| 79 | if eq := diffStringLists(tc.want, got); !eq { |
| 80 | t.Errorf("input %v: (want %v -> got %v)", tc.input, tc.want, got) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func diffStringLists(a, b []string) bool { |
| 86 | if len(a) != len(b) { |
| 87 | return false |
| 88 | } |
| 89 | sort.Strings(a) |
| 90 | sort.Strings(b) |
| 91 | for i := range a { |
| 92 | if a[i] != b[i] { |
| 93 | return false |
| 94 | } |
| 95 | } |
| 96 | return true |
| 97 | } |
| 98 | |
| 99 | var lastSegmentSplitTests = []struct { |
| 100 | str string |
| 101 | want string |
| 102 | }{ |
| 103 | { |
| 104 | str: "identifier", |
| 105 | want: "identifier", |
| 106 | }, |
| 107 | { |
| 108 | str: "two_words", |
| 109 | want: "two_words", |
| 110 | }, |
| 111 | { |
| 112 | str: "first::second", |
| 113 | want: "second", |
| 114 | }, |
| 115 | { |
| 116 | str: "foo.bar.FOOBar_buz123_test", |
| 117 | want: "FOOBar_buz123_test", |
| 118 | }, |
| 119 | } |
| 120 | |
| 121 | func TestLastSegment(t *testing.T) { |
| 122 | for _, tc := range lastSegmentSplitTests { |
| 123 | roles := fuzzy.RuneRoles([]byte(tc.str), nil) |
| 124 | |
| 125 | got := fuzzy.LastSegment(tc.str, roles) |
| 126 | |
| 127 | if got != tc.want { |
| 128 | t.Errorf("str %v: want %v; got %v", tc.str, tc.want, got) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func BenchmarkRoles(b *testing.B) { |
| 134 | str := "AbstractSWTFactory" |
| 135 | out := make([]fuzzy.RuneRole, len(str)) |
| 136 | |
| 137 | for i := 0; i < b.N; i++ { |
| 138 | fuzzy.RuneRoles([]byte(str), out) |
| 139 | } |
| 140 | b.SetBytes(int64(len(str))) |
| 141 | } |
| 142 |
Members