2015-01-02 04:49:30 +09:00
|
|
|
package fzf
|
|
|
|
|
2015-01-12 12:56:17 +09:00
|
|
|
import (
|
2015-08-02 14:00:18 +09:00
|
|
|
"reflect"
|
2015-01-12 12:56:17 +09:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/algo"
|
|
|
|
)
|
2015-01-02 04:49:30 +09:00
|
|
|
|
|
|
|
func TestParseTermsExtended(t *testing.T) {
|
2015-04-21 22:36:40 +09:00
|
|
|
terms := parseTerms(ModeExtended, CaseSmart,
|
2015-06-08 23:16:31 +09:00
|
|
|
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ ^iii$")
|
|
|
|
if len(terms) != 9 ||
|
2015-01-12 03:01:24 +09:00
|
|
|
terms[0].typ != termFuzzy || terms[0].inv ||
|
|
|
|
terms[1].typ != termExact || terms[1].inv ||
|
|
|
|
terms[2].typ != termPrefix || terms[2].inv ||
|
|
|
|
terms[3].typ != termSuffix || terms[3].inv ||
|
|
|
|
terms[4].typ != termFuzzy || !terms[4].inv ||
|
|
|
|
terms[5].typ != termExact || !terms[5].inv ||
|
|
|
|
terms[6].typ != termPrefix || !terms[6].inv ||
|
2015-06-08 23:16:31 +09:00
|
|
|
terms[7].typ != termSuffix || !terms[7].inv ||
|
|
|
|
terms[8].typ != termEqual || terms[8].inv {
|
2015-01-02 04:49:30 +09:00
|
|
|
t.Errorf("%s", terms)
|
|
|
|
}
|
|
|
|
for idx, term := range terms {
|
|
|
|
if len(term.text) != 3 {
|
|
|
|
t.Errorf("%s", term)
|
|
|
|
}
|
|
|
|
if idx > 0 && len(term.origText) != 4+idx/5 {
|
|
|
|
t.Errorf("%s", term)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTermsExtendedExact(t *testing.T) {
|
2015-04-21 22:36:40 +09:00
|
|
|
terms := parseTerms(ModeExtendedExact, CaseSmart,
|
2015-01-02 04:49:30 +09:00
|
|
|
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")
|
|
|
|
if len(terms) != 8 ||
|
2015-01-12 03:01:24 +09:00
|
|
|
terms[0].typ != termExact || terms[0].inv || len(terms[0].text) != 3 ||
|
2015-09-12 11:00:30 +09:00
|
|
|
terms[1].typ != termFuzzy || terms[1].inv || len(terms[1].text) != 3 ||
|
2015-01-12 03:01:24 +09:00
|
|
|
terms[2].typ != termPrefix || terms[2].inv || len(terms[2].text) != 3 ||
|
|
|
|
terms[3].typ != termSuffix || terms[3].inv || len(terms[3].text) != 3 ||
|
|
|
|
terms[4].typ != termExact || !terms[4].inv || len(terms[4].text) != 3 ||
|
2015-09-12 11:00:30 +09:00
|
|
|
terms[5].typ != termFuzzy || !terms[5].inv || len(terms[5].text) != 3 ||
|
2015-01-12 03:01:24 +09:00
|
|
|
terms[6].typ != termPrefix || !terms[6].inv || len(terms[6].text) != 3 ||
|
|
|
|
terms[7].typ != termSuffix || !terms[7].inv || len(terms[7].text) != 3 {
|
2015-01-02 04:49:30 +09:00
|
|
|
t.Errorf("%s", terms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTermsEmpty(t *testing.T) {
|
2015-04-21 22:36:40 +09:00
|
|
|
terms := parseTerms(ModeExtended, CaseSmart, "' $ ^ !' !^ !$")
|
2015-01-02 04:49:30 +09:00
|
|
|
if len(terms) != 0 {
|
|
|
|
t.Errorf("%s", terms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExact(t *testing.T) {
|
|
|
|
defer clearPatternCache()
|
|
|
|
clearPatternCache()
|
2015-09-12 11:37:55 +09:00
|
|
|
pattern := BuildPattern(ModeExtended, CaseSmart, true,
|
2015-08-10 18:34:20 +09:00
|
|
|
[]Range{}, Delimiter{}, []rune("'abc"))
|
2015-08-02 14:00:18 +09:00
|
|
|
sidx, eidx := algo.ExactMatchNaive(
|
2015-09-12 11:37:55 +09:00
|
|
|
pattern.caseSensitive, pattern.forward, []rune("aabbcc abc"), pattern.terms[0].text)
|
2015-01-02 04:49:30 +09:00
|
|
|
if sidx != 7 || eidx != 10 {
|
|
|
|
t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 23:16:31 +09:00
|
|
|
func TestEqual(t *testing.T) {
|
|
|
|
defer clearPatternCache()
|
|
|
|
clearPatternCache()
|
2015-09-12 11:37:55 +09:00
|
|
|
pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("^AbC$"))
|
2015-06-08 23:16:31 +09:00
|
|
|
|
|
|
|
match := func(str string, sidxExpected int, eidxExpected int) {
|
2015-08-02 14:00:18 +09:00
|
|
|
sidx, eidx := algo.EqualMatch(
|
2015-09-12 11:37:55 +09:00
|
|
|
pattern.caseSensitive, pattern.forward, []rune(str), pattern.terms[0].text)
|
2015-06-08 23:16:31 +09:00
|
|
|
if sidx != sidxExpected || eidx != eidxExpected {
|
|
|
|
t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match("ABC", -1, -1)
|
|
|
|
match("AbC", 0, 3)
|
|
|
|
}
|
|
|
|
|
2015-01-02 04:49:30 +09:00
|
|
|
func TestCaseSensitivity(t *testing.T) {
|
|
|
|
defer clearPatternCache()
|
|
|
|
clearPatternCache()
|
2015-09-12 11:37:55 +09:00
|
|
|
pat1 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("abc"))
|
2015-01-02 04:49:30 +09:00
|
|
|
clearPatternCache()
|
2015-09-12 11:37:55 +09:00
|
|
|
pat2 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("Abc"))
|
2015-01-02 04:49:30 +09:00
|
|
|
clearPatternCache()
|
2015-09-12 11:37:55 +09:00
|
|
|
pat3 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("abc"))
|
2015-01-02 04:49:30 +09:00
|
|
|
clearPatternCache()
|
2015-09-12 11:37:55 +09:00
|
|
|
pat4 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("Abc"))
|
2015-01-02 04:49:30 +09:00
|
|
|
clearPatternCache()
|
2015-09-12 11:37:55 +09:00
|
|
|
pat5 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("abc"))
|
2015-01-02 04:49:30 +09:00
|
|
|
clearPatternCache()
|
2015-09-12 11:37:55 +09:00
|
|
|
pat6 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("Abc"))
|
2015-01-02 04:49:30 +09: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-11 01:15:44 +09:00
|
|
|
|
2015-01-11 01:47:46 +09:00
|
|
|
func TestOrigTextAndTransformed(t *testing.T) {
|
2015-09-12 11:37:55 +09:00
|
|
|
pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("jg"))
|
2015-08-10 18:34:20 +09:00
|
|
|
tokens := Tokenize([]rune("junegunn"), Delimiter{})
|
2015-01-11 01:47:46 +09:00
|
|
|
trans := Transform(tokens, []Range{Range{1, 1}})
|
|
|
|
|
2015-08-02 14:00:18 +09:00
|
|
|
origRunes := []rune("junegunn.choi")
|
2015-03-01 11:16:38 +09:00
|
|
|
for _, mode := range []Mode{ModeFuzzy, ModeExtended} {
|
2015-01-11 01:15:44 +09:00
|
|
|
chunk := Chunk{
|
2015-01-11 01:47:46 +09:00
|
|
|
&Item{
|
2015-08-02 14:00:18 +09:00
|
|
|
text: []rune("junegunn"),
|
|
|
|
origText: &origRunes,
|
2015-01-11 01:47:46 +09:00
|
|
|
transformed: trans},
|
2015-01-11 01:15:44 +09:00
|
|
|
}
|
2015-03-01 11:16:38 +09:00
|
|
|
pattern.mode = mode
|
|
|
|
matches := pattern.matchChunk(&chunk)
|
2015-08-02 14:00:18 +09:00
|
|
|
if string(matches[0].text) != "junegunn" || string(*matches[0].origText) != "junegunn.choi" ||
|
2015-01-11 01:47:46 +09:00
|
|
|
matches[0].offsets[0][0] != 0 || matches[0].offsets[0][1] != 5 ||
|
2015-08-02 14:00:18 +09:00
|
|
|
!reflect.DeepEqual(matches[0].transformed, trans) {
|
2015-01-11 01:15:44 +09:00
|
|
|
t.Error("Invalid match result", matches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|