1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
package prompt
import (
"reflect"
"testing"
)
func TestFilter(t *testing.T) {
var scenarioTable = []struct {
scenario string
filter Filter
list []Suggest
substr string
ignoreCase bool
expected []Suggest
}{
{
scenario: "Contains don't ignore case",
filter: FilterContains,
list: []Suggest{
{Text: "abcde"},
{Text: "fghij"},
{Text: "ABCDE"},
},
substr: "cd",
ignoreCase: false,
expected: []Suggest{
{Text: "abcde"},
},
},
{
scenario: "Contains ignore case",
filter: FilterContains,
list: []Suggest{
{Text: "abcde"},
{Text: "fghij"},
{Text: "ABCDE"},
},
substr: "cd",
ignoreCase: true,
expected: []Suggest{
{Text: "abcde"},
{Text: "ABCDE"},
},
},
{
scenario: "HasPrefix don't ignore case",
filter: FilterHasPrefix,
list: []Suggest{
{Text: "abcde"},
{Text: "fghij"},
{Text: "ABCDE"},
},
substr: "abc",
ignoreCase: false,
expected: []Suggest{
{Text: "abcde"},
},
},
{
scenario: "HasPrefix ignore case",
filter: FilterHasPrefix,
list: []Suggest{
{Text: "abcde"},
{Text: "fabcj"},
{Text: "ABCDE"},
},
substr: "abc",
ignoreCase: true,
expected: []Suggest{
{Text: "abcde"},
{Text: "ABCDE"},
},
},
{
scenario: "HasSuffix don't ignore case",
filter: FilterHasSuffix,
list: []Suggest{
{Text: "abcde"},
{Text: "fcdej"},
{Text: "ABCDE"},
},
substr: "cde",
ignoreCase: false,
expected: []Suggest{
{Text: "abcde"},
},
},
{
scenario: "HasSuffix ignore case",
filter: FilterHasSuffix,
list: []Suggest{
{Text: "abcde"},
{Text: "fcdej"},
{Text: "ABCDE"},
},
substr: "cde",
ignoreCase: true,
expected: []Suggest{
{Text: "abcde"},
{Text: "ABCDE"},
},
},
{
scenario: "Fuzzy don't ignore case",
filter: FilterFuzzy,
list: []Suggest{
{Text: "abcde"},
{Text: "fcdej"},
{Text: "ABCDE"},
},
substr: "ae",
ignoreCase: false,
expected: []Suggest{
{Text: "abcde"},
},
},
{
scenario: "Fuzzy ignore case",
filter: FilterFuzzy,
list: []Suggest{
{Text: "abcde"},
{Text: "fcdej"},
{Text: "ABCDE"},
},
substr: "ae",
ignoreCase: true,
expected: []Suggest{
{Text: "abcde"},
{Text: "ABCDE"},
},
},
}
for _, s := range scenarioTable {
if actual := s.filter(s.list, s.substr, s.ignoreCase); !reflect.DeepEqual(actual, s.expected) {
t.Errorf("%s: Should be %#v, but got %#v", s.scenario, s.expected, actual)
}
}
}
func TestFuzzyMatch(t *testing.T) {
tests := []struct {
s string
sub string
match bool
}{
{"dog house", "dog", true},
{"dog house", "", true},
{"", "", true},
{"this is much longer", "hhg", true},
{"this is much longer", "hhhg", false},
{"long", "longer", false},
{"can we do unicode 文字 with this 今日", "文字今日", true},
{"can we do unicode 文字 with this 今日", "d文字tt今日", true},
{"can we do unicode 文字 with this 今日", "d文字ttt今日", false},
}
for _, test := range tests {
if fuzzyMatch(test.s, test.sub) != test.match {
t.Errorf("fuzzymatch, %s in %s: expected %v, got %v", test.sub, test.s, test.match, !test.match)
}
}
}
|