go-prompt/filter_test.go

165 lines
3.3 KiB
Go
Raw Normal View History

2017-07-18 10:04:11 +00:00
package prompt
import (
"reflect"
2017-08-03 06:53:38 +00:00
"testing"
2017-07-18 10:04:11 +00:00
)
func TestFilter(t *testing.T) {
2017-08-03 06:53:38 +00:00
var scenarioTable = []struct {
2017-07-18 10:04:11 +00:00
scenario string
2017-08-09 16:03:43 +00:00
filter Filter
2017-08-04 11:30:50 +00:00
list []Suggest
2017-07-18 10:04:11 +00:00
substr string
ignoreCase bool
2017-08-04 11:30:50 +00:00
expected []Suggest
2017-08-03 06:53:38 +00:00
}{
2017-07-18 10:04:11 +00:00
{
2017-08-03 06:53:38 +00:00
scenario: "Contains don't ignore case",
filter: FilterContains,
2017-08-04 11:30:50 +00:00
list: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
{Text: "fghij"},
{Text: "ABCDE"},
},
2017-07-18 10:04:11 +00:00
substr: "cd",
ignoreCase: false,
2017-08-04 11:30:50 +00:00
expected: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
},
2017-07-18 10:04:11 +00:00
},
{
2017-08-03 06:53:38 +00:00
scenario: "Contains ignore case",
filter: FilterContains,
2017-08-04 11:30:50 +00:00
list: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
{Text: "fghij"},
{Text: "ABCDE"},
},
2017-07-18 10:04:11 +00:00
substr: "cd",
ignoreCase: true,
2017-08-04 11:30:50 +00:00
expected: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
{Text: "ABCDE"},
},
2017-07-18 10:04:11 +00:00
},
{
2017-08-03 06:53:38 +00:00
scenario: "HasPrefix don't ignore case",
filter: FilterHasPrefix,
2017-08-04 11:30:50 +00:00
list: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
{Text: "fghij"},
{Text: "ABCDE"},
},
2017-07-18 10:04:11 +00:00
substr: "abc",
ignoreCase: false,
2017-08-04 11:30:50 +00:00
expected: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
},
2017-07-18 10:04:11 +00:00
},
{
2017-08-03 06:53:38 +00:00
scenario: "HasPrefix ignore case",
filter: FilterHasPrefix,
2017-08-04 11:30:50 +00:00
list: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
{Text: "fabcj"},
{Text: "ABCDE"},
},
2017-07-18 10:04:11 +00:00
substr: "abc",
ignoreCase: true,
2017-08-04 11:30:50 +00:00
expected: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
{Text: "ABCDE"},
},
2017-07-18 10:04:11 +00:00
},
{
2017-08-03 06:53:38 +00:00
scenario: "HasSuffix don't ignore case",
filter: FilterHasSuffix,
2017-08-04 11:30:50 +00:00
list: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
{Text: "fcdej"},
{Text: "ABCDE"},
},
2017-07-18 10:04:11 +00:00
substr: "cde",
ignoreCase: false,
2017-08-04 11:30:50 +00:00
expected: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
},
2017-07-18 10:04:11 +00:00
},
{
2017-08-03 06:53:38 +00:00
scenario: "HasSuffix ignore case",
filter: FilterHasSuffix,
2017-08-04 11:30:50 +00:00
list: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
{Text: "fcdej"},
{Text: "ABCDE"},
},
2017-07-18 10:04:11 +00:00
substr: "cde",
ignoreCase: true,
2017-08-04 11:30:50 +00:00
expected: []Suggest{
2017-07-18 17:12:22 +00:00
{Text: "abcde"},
{Text: "ABCDE"},
},
2017-07-18 10:04:11 +00:00
},
2018-09-03 04:55:51 +00:00
{
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"},
},
},
2017-07-18 10:04:11 +00:00
}
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)
}
}
}
2018-09-03 04:55:51 +00:00
2018-10-20 06:50:53 +00:00
func TestFuzzyMatch(t *testing.T) {
2018-09-03 04:55:51 +00:00
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)
}
}
}