2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
2015-01-12 03:56:17 +00:00
|
|
|
import (
|
2015-08-02 05:00:18 +00:00
|
|
|
"reflect"
|
2015-01-12 03:56:17 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/algo"
|
2016-08-13 15:39:44 +00:00
|
|
|
"github.com/junegunn/fzf/src/util"
|
2015-01-12 03:56:17 +00:00
|
|
|
)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2016-09-07 00:58:18 +00:00
|
|
|
var slab *util.Slab
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
slab = util.MakeSlab(slab16Size, slab32Size)
|
|
|
|
}
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
func TestParseTermsExtended(t *testing.T) {
|
2017-01-09 00:52:17 +00:00
|
|
|
terms := parseTerms(true, CaseSmart, false,
|
2017-08-10 15:07:18 +00:00
|
|
|
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ | ^iii$ ^xxx | 'yyy | zzz$ | !ZZZ |")
|
2015-06-08 14:16:31 +00:00
|
|
|
if len(terms) != 9 ||
|
2015-11-08 15:58:20 +00:00
|
|
|
terms[0][0].typ != termFuzzy || terms[0][0].inv ||
|
|
|
|
terms[1][0].typ != termExact || terms[1][0].inv ||
|
|
|
|
terms[2][0].typ != termPrefix || terms[2][0].inv ||
|
|
|
|
terms[3][0].typ != termSuffix || terms[3][0].inv ||
|
2016-10-03 17:09:03 +00:00
|
|
|
terms[4][0].typ != termExact || !terms[4][0].inv ||
|
|
|
|
terms[5][0].typ != termFuzzy || !terms[5][0].inv ||
|
2015-11-08 15:58:20 +00:00
|
|
|
terms[6][0].typ != termPrefix || !terms[6][0].inv ||
|
|
|
|
terms[7][0].typ != termSuffix || !terms[7][0].inv ||
|
|
|
|
terms[7][1].typ != termEqual || terms[7][1].inv ||
|
|
|
|
terms[8][0].typ != termPrefix || terms[8][0].inv ||
|
|
|
|
terms[8][1].typ != termExact || terms[8][1].inv ||
|
|
|
|
terms[8][2].typ != termSuffix || terms[8][2].inv ||
|
2016-10-03 17:09:03 +00:00
|
|
|
terms[8][3].typ != termExact || !terms[8][3].inv {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", terms)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2017-08-08 04:22:30 +00:00
|
|
|
for _, termSet := range terms[:8] {
|
2015-11-08 15:58:20 +00:00
|
|
|
term := termSet[0]
|
2015-01-01 19:49:30 +00:00
|
|
|
if len(term.text) != 3 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", term)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-11-08 15:58:20 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTermsExtendedExact(t *testing.T) {
|
2017-01-09 00:52:17 +00:00
|
|
|
terms := parseTerms(false, CaseSmart, false,
|
2015-01-01 19:49:30 +00:00
|
|
|
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")
|
|
|
|
if len(terms) != 8 ||
|
2015-11-08 15:58:20 +00:00
|
|
|
terms[0][0].typ != termExact || terms[0][0].inv || len(terms[0][0].text) != 3 ||
|
|
|
|
terms[1][0].typ != termFuzzy || terms[1][0].inv || len(terms[1][0].text) != 3 ||
|
|
|
|
terms[2][0].typ != termPrefix || terms[2][0].inv || len(terms[2][0].text) != 3 ||
|
|
|
|
terms[3][0].typ != termSuffix || terms[3][0].inv || len(terms[3][0].text) != 3 ||
|
|
|
|
terms[4][0].typ != termExact || !terms[4][0].inv || len(terms[4][0].text) != 3 ||
|
|
|
|
terms[5][0].typ != termFuzzy || !terms[5][0].inv || len(terms[5][0].text) != 3 ||
|
|
|
|
terms[6][0].typ != termPrefix || !terms[6][0].inv || len(terms[6][0].text) != 3 ||
|
|
|
|
terms[7][0].typ != termSuffix || !terms[7][0].inv || len(terms[7][0].text) != 3 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", terms)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTermsEmpty(t *testing.T) {
|
2017-08-10 14:59:40 +00:00
|
|
|
terms := parseTerms(true, CaseSmart, false, "' ^ !' !^")
|
2015-01-01 19:49:30 +00:00
|
|
|
if len(terms) != 0 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", terms)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExact(t *testing.T) {
|
|
|
|
defer clearPatternCache()
|
|
|
|
clearPatternCache()
|
2017-01-08 18:12:23 +00:00
|
|
|
pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true,
|
2015-08-10 09:34:20 +00:00
|
|
|
[]Range{}, Delimiter{}, []rune("'abc"))
|
2017-08-19 18:33:55 +00:00
|
|
|
chars := util.ToChars([]byte("aabbcc abc"))
|
2016-09-07 00:58:18 +00:00
|
|
|
res, pos := algo.ExactMatchNaive(
|
2017-08-19 18:33:55 +00:00
|
|
|
pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)
|
2016-04-16 05:02:43 +00:00
|
|
|
if res.Start != 7 || res.End != 10 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2016-09-07 00:58:18 +00:00
|
|
|
if pos != nil {
|
|
|
|
t.Errorf("pos is expected to be nil")
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 14:16:31 +00:00
|
|
|
func TestEqual(t *testing.T) {
|
|
|
|
defer clearPatternCache()
|
|
|
|
clearPatternCache()
|
2017-01-08 18:12:23 +00:00
|
|
|
pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))
|
2015-06-08 14:16:31 +00:00
|
|
|
|
2016-08-18 17:39:32 +00:00
|
|
|
match := func(str string, sidxExpected int, eidxExpected int) {
|
2017-08-19 18:33:55 +00:00
|
|
|
chars := util.ToChars([]byte(str))
|
2016-09-07 00:58:18 +00:00
|
|
|
res, pos := algo.EqualMatch(
|
2017-08-19 18:33:55 +00:00
|
|
|
pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)
|
2016-04-16 05:02:43 +00:00
|
|
|
if res.Start != sidxExpected || res.End != eidxExpected {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)
|
2015-06-08 14:16:31 +00:00
|
|
|
}
|
2016-09-07 00:58:18 +00:00
|
|
|
if pos != nil {
|
|
|
|
t.Errorf("pos is expected to be nil")
|
|
|
|
}
|
2015-06-08 14:16:31 +00:00
|
|
|
}
|
|
|
|
match("ABC", -1, -1)
|
|
|
|
match("AbC", 0, 3)
|
|
|
|
}
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
func TestCaseSensitivity(t *testing.T) {
|
|
|
|
defer clearPatternCache()
|
|
|
|
clearPatternCache()
|
2017-01-08 18:12:23 +00:00
|
|
|
pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("abc"))
|
2015-01-01 19:49:30 +00:00
|
|
|
clearPatternCache()
|
2017-01-08 18:12:23 +00:00
|
|
|
pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))
|
2015-01-01 19:49:30 +00:00
|
|
|
clearPatternCache()
|
2017-01-08 18:12:23 +00:00
|
|
|
pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("abc"))
|
2015-01-01 19:49:30 +00:00
|
|
|
clearPatternCache()
|
2017-01-08 18:12:23 +00:00
|
|
|
pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))
|
2015-01-01 19:49:30 +00:00
|
|
|
clearPatternCache()
|
2017-01-08 18:12:23 +00:00
|
|
|
pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("abc"))
|
2015-01-01 19:49:30 +00:00
|
|
|
clearPatternCache()
|
2017-01-08 18:12:23 +00:00
|
|
|
pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
if string(pat1.text) != "abc" || pat1.caseSensitive != false ||
|
|
|
|
string(pat2.text) != "Abc" || pat2.caseSensitive != true ||
|
|
|
|
string(pat3.text) != "abc" || pat3.caseSensitive != false ||
|
|
|
|
string(pat4.text) != "abc" || pat4.caseSensitive != false ||
|
|
|
|
string(pat5.text) != "abc" || pat5.caseSensitive != true ||
|
|
|
|
string(pat6.text) != "Abc" || pat6.caseSensitive != true {
|
|
|
|
t.Error("Invalid case conversion")
|
|
|
|
}
|
|
|
|
}
|
2015-01-10 16:15:44 +00:00
|
|
|
|
2015-01-10 16:47:46 +00:00
|
|
|
func TestOrigTextAndTransformed(t *testing.T) {
|
2017-01-08 18:12:23 +00:00
|
|
|
pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("jg"))
|
2017-07-19 17:44:30 +00:00
|
|
|
tokens := Tokenize("junegunn", Delimiter{})
|
2015-01-10 16:47:46 +00:00
|
|
|
trans := Transform(tokens, []Range{Range{1, 1}})
|
|
|
|
|
2016-08-13 15:39:44 +00:00
|
|
|
origBytes := []byte("junegunn.choi")
|
2015-11-03 13:49:32 +00:00
|
|
|
for _, extended := range []bool{false, true} {
|
2017-08-14 16:10:41 +00:00
|
|
|
chunk := Chunk{count: 1}
|
|
|
|
chunk.items[0] = Item{
|
|
|
|
text: util.ToChars([]byte("junegunn")),
|
|
|
|
origText: &origBytes,
|
|
|
|
transformed: &trans}
|
2015-11-03 13:49:32 +00:00
|
|
|
pattern.extended = extended
|
2016-09-07 00:58:18 +00:00
|
|
|
matches := pattern.matchChunk(&chunk, nil, slab) // No cache
|
|
|
|
if !(matches[0].item.text.ToString() == "junegunn" &&
|
|
|
|
string(*matches[0].item.origText) == "junegunn.choi" &&
|
2017-07-16 14:31:19 +00:00
|
|
|
reflect.DeepEqual(*matches[0].item.transformed, trans)) {
|
2015-01-10 16:15:44 +00:00
|
|
|
t.Error("Invalid match result", matches)
|
|
|
|
}
|
2016-08-19 16:46:54 +00:00
|
|
|
|
2017-08-14 16:10:41 +00:00
|
|
|
match, offsets, pos := pattern.MatchItem(&chunk.items[0], true, slab)
|
2016-09-07 00:58:18 +00:00
|
|
|
if !(match.item.text.ToString() == "junegunn" &&
|
|
|
|
string(*match.item.origText) == "junegunn.choi" &&
|
|
|
|
offsets[0][0] == 0 && offsets[0][1] == 5 &&
|
2017-07-16 14:31:19 +00:00
|
|
|
reflect.DeepEqual(*match.item.transformed, trans)) {
|
2016-09-07 00:58:18 +00:00
|
|
|
t.Error("Invalid match result", match, offsets, extended)
|
|
|
|
}
|
|
|
|
if !((*pos)[0] == 4 && (*pos)[1] == 0) {
|
|
|
|
t.Error("Invalid pos array", *pos)
|
2016-08-19 16:46:54 +00:00
|
|
|
}
|
2015-01-10 16:15:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-08 15:58:20 +00:00
|
|
|
|
|
|
|
func TestCacheKey(t *testing.T) {
|
|
|
|
test := func(extended bool, patStr string, expected string, cacheable bool) {
|
2017-08-08 04:22:30 +00:00
|
|
|
clearPatternCache()
|
2017-01-08 18:12:23 +00:00
|
|
|
pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune(patStr))
|
2015-11-08 15:58:20 +00:00
|
|
|
if pat.CacheKey() != expected {
|
|
|
|
t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())
|
|
|
|
}
|
|
|
|
if pat.cacheable != cacheable {
|
2017-08-09 14:25:32 +00:00
|
|
|
t.Errorf("Expected: %t, actual: %t (%s)", cacheable, pat.cacheable, patStr)
|
2015-11-08 15:58:20 +00:00
|
|
|
}
|
|
|
|
clearPatternCache()
|
|
|
|
}
|
|
|
|
test(false, "foo !bar", "foo !bar", true)
|
|
|
|
test(false, "foo | bar !baz", "foo | bar !baz", true)
|
2017-08-09 14:25:32 +00:00
|
|
|
test(true, "foo bar baz", "foo\tbar\tbaz", true)
|
2015-11-08 15:58:20 +00:00
|
|
|
test(true, "foo !bar", "foo", false)
|
2017-08-09 14:25:32 +00:00
|
|
|
test(true, "foo !bar baz", "foo\tbaz", false)
|
2015-11-08 15:58:20 +00:00
|
|
|
test(true, "foo | bar baz", "baz", false)
|
|
|
|
test(true, "foo | bar | baz", "", false)
|
|
|
|
test(true, "foo | bar !baz", "", false)
|
2017-08-10 15:07:18 +00:00
|
|
|
test(true, "| | foo", "", false)
|
|
|
|
test(true, "| | | foo", "foo", false)
|
2015-11-08 15:58:20 +00:00
|
|
|
}
|
2017-01-31 17:05:58 +00:00
|
|
|
|
|
|
|
func TestCacheable(t *testing.T) {
|
2017-08-08 04:22:30 +00:00
|
|
|
test := func(fuzzy bool, str string, expected string, cacheable bool) {
|
2017-01-31 17:05:58 +00:00
|
|
|
clearPatternCache()
|
|
|
|
pat := BuildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, true, []Range{}, Delimiter{}, []rune(str))
|
2017-08-08 04:22:30 +00:00
|
|
|
if pat.CacheKey() != expected {
|
|
|
|
t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())
|
|
|
|
}
|
2017-01-31 17:05:58 +00:00
|
|
|
if cacheable != pat.cacheable {
|
|
|
|
t.Errorf("Invalid Pattern.cacheable for \"%s\": %v (expected: %v)", str, pat.cacheable, cacheable)
|
|
|
|
}
|
2017-08-08 04:22:30 +00:00
|
|
|
clearPatternCache()
|
2017-01-31 17:05:58 +00:00
|
|
|
}
|
2017-08-09 14:25:32 +00:00
|
|
|
test(true, "foo bar", "foo\tbar", true)
|
|
|
|
test(true, "foo 'bar", "foo\tbar", false)
|
2017-08-08 04:22:30 +00:00
|
|
|
test(true, "foo !bar", "foo", false)
|
|
|
|
|
2017-08-09 14:25:32 +00:00
|
|
|
test(false, "foo bar", "foo\tbar", true)
|
2017-08-08 04:22:30 +00:00
|
|
|
test(false, "foo 'bar", "foo", false)
|
|
|
|
test(false, "foo '", "foo", true)
|
|
|
|
test(false, "foo 'bar", "foo", false)
|
|
|
|
test(false, "foo !bar", "foo", false)
|
2017-01-31 17:05:58 +00:00
|
|
|
}
|