2015-01-12 03:56:17 +00:00
|
|
|
package algo
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-04-16 05:02:43 +00:00
|
|
|
func assertMatch(t *testing.T, fun func(bool, bool, []rune, []rune) Result, caseSensitive, forward bool, input, pattern string, sidx int32, eidx int32, bonus int32) {
|
2015-01-01 19:49:30 +00:00
|
|
|
if !caseSensitive {
|
|
|
|
pattern = strings.ToLower(pattern)
|
|
|
|
}
|
2016-04-16 05:02:43 +00:00
|
|
|
res := fun(caseSensitive, forward, []rune(input), []rune(pattern))
|
|
|
|
if res.Start != sidx {
|
|
|
|
t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", res.Start, sidx, input, pattern)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2016-04-16 05:02:43 +00:00
|
|
|
if res.End != eidx {
|
|
|
|
t.Errorf("Invalid end index: %d (expected: %d, %s / %s)", res.End, eidx, input, pattern)
|
|
|
|
}
|
|
|
|
if res.Bonus != bonus {
|
|
|
|
t.Errorf("Invalid bonus: %d (expected: %d, %s / %s)", res.Bonus, bonus, input, pattern)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFuzzyMatch(t *testing.T) {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, FuzzyMatch, false, true, "fooBarbaz", "oBZ", 2, 9, 2)
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "foo bar baz", "fbb", 0, 9, 8)
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "/AutomatorDocument.icns", "rdoc", 9, 13, 4)
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "/man1/zshcompctl.1", "zshc", 6, 10, 7)
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "/.oh-my-zsh/cache", "zshc", 8, 13, 8)
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "ab0123 456", "12356", 3, 10, 3)
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "abc123 456", "12356", 3, 10, 5)
|
|
|
|
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "foo/bar/baz", "fbb", 0, 9, 8)
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "fooBarBaz", "fbb", 0, 7, 6)
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "foo barbaz", "fbb", 0, 8, 6)
|
|
|
|
assertMatch(t, FuzzyMatch, false, true, "fooBar Baz", "foob", 0, 4, 8)
|
|
|
|
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBZ", -1, -1, 0)
|
|
|
|
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBz", 2, 9, 2)
|
|
|
|
assertMatch(t, FuzzyMatch, true, true, "Foo Bar Baz", "fbb", -1, -1, 0)
|
|
|
|
assertMatch(t, FuzzyMatch, true, true, "Foo/Bar/Baz", "FBB", 0, 9, 8)
|
|
|
|
assertMatch(t, FuzzyMatch, true, true, "FooBarBaz", "FBB", 0, 7, 6)
|
|
|
|
assertMatch(t, FuzzyMatch, true, true, "foo BarBaz", "fBB", 0, 8, 7)
|
|
|
|
assertMatch(t, FuzzyMatch, true, true, "FooBar Baz", "FooB", 0, 4, 8)
|
|
|
|
assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "fooBarbazz", -1, -1, 0)
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFuzzyMatchBackward(t *testing.T) {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, FuzzyMatch, false, true, "foobar fb", "fb", 0, 4, 4)
|
|
|
|
assertMatch(t, FuzzyMatch, false, false, "foobar fb", "fb", 7, 9, 5)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactMatchNaive(t *testing.T) {
|
2015-09-12 02:37:55 +00:00
|
|
|
for _, dir := range []bool{true, false} {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5, 0)
|
|
|
|
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "oBA", -1, -1, 0)
|
|
|
|
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "fooBarbazz", -1, -1, 0)
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactMatchNaiveBackward(t *testing.T) {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, ExactMatchNaive, false, true, "foobar foob", "oo", 1, 3, 0)
|
|
|
|
assertMatch(t, ExactMatchNaive, false, false, "foobar foob", "oo", 8, 10, 0)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrefixMatch(t *testing.T) {
|
2015-09-12 02:37:55 +00:00
|
|
|
for _, dir := range []bool{true, false} {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3, 0)
|
|
|
|
assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1, 0)
|
|
|
|
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "baz", -1, -1, 0)
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSuffixMatch(t *testing.T) {
|
2015-09-12 02:37:55 +00:00
|
|
|
for _, dir := range []bool{true, false} {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "Foo", -1, -1, 0)
|
|
|
|
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9, 0)
|
|
|
|
assertMatch(t, SuffixMatch, true, dir, "fooBarbaz", "Baz", -1, -1, 0)
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-03-19 04:06:20 +00:00
|
|
|
|
|
|
|
func TestEmptyPattern(t *testing.T) {
|
2015-09-12 02:37:55 +00:00
|
|
|
for _, dir := range []bool{true, false} {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, FuzzyMatch, true, dir, "foobar", "", 0, 0, 0)
|
|
|
|
assertMatch(t, ExactMatchNaive, true, dir, "foobar", "", 0, 0, 0)
|
|
|
|
assertMatch(t, PrefixMatch, true, dir, "foobar", "", 0, 0, 0)
|
|
|
|
assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6, 0)
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
2015-03-19 04:06:20 +00:00
|
|
|
}
|