pattern_test.go
This file
Path
src/pattern_test.go
206 lines · 7.7 KiB · Go
Last changed by
Make fzf pass go vetRyan Boehningbefore adoption21b94d2
History of this fileTake it
1package fzf23import (4 "reflect"5 "testing"67 "github.com/junegunn/fzf/src/algo"8 "github.com/junegunn/fzf/src/util"9)1011var slab *util.Slab1213func init() {14 slab = util.MakeSlab(slab16Size, slab32Size)15}1617func TestParseTermsExtended(t *testing.T) {18 terms := parseTerms(true, CaseSmart, false,19 "aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ | ^iii$ ^xxx | 'yyy | zzz$ | !ZZZ |")20 if len(terms) != 9 ||21 terms[0][0].typ != termFuzzy || terms[0][0].inv ||22 terms[1][0].typ != termExact || terms[1][0].inv ||23 terms[2][0].typ != termPrefix || terms[2][0].inv ||24 terms[3][0].typ != termSuffix || terms[3][0].inv ||25 terms[4][0].typ != termExact || !terms[4][0].inv ||26 terms[5][0].typ != termFuzzy || !terms[5][0].inv ||27 terms[6][0].typ != termPrefix || !terms[6][0].inv ||28 terms[7][0].typ != termSuffix || !terms[7][0].inv ||29 terms[7][1].typ != termEqual || terms[7][1].inv ||30 terms[8][0].typ != termPrefix || terms[8][0].inv ||31 terms[8][1].typ != termExact || terms[8][1].inv ||32 terms[8][2].typ != termSuffix || terms[8][2].inv ||33 terms[8][3].typ != termExact || !terms[8][3].inv {34 t.Errorf("%v", terms)35 }36 for _, termSet := range terms[:8] {37 term := termSet[0]38 if len(term.text) != 3 {39 t.Errorf("%v", term)40 }41 }42}4344func TestParseTermsExtendedExact(t *testing.T) {45 terms := parseTerms(false, CaseSmart, false,46 "aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")47 if len(terms) != 8 ||48 terms[0][0].typ != termExact || terms[0][0].inv || len(terms[0][0].text) != 3 ||49 terms[1][0].typ != termFuzzy || terms[1][0].inv || len(terms[1][0].text) != 3 ||50 terms[2][0].typ != termPrefix || terms[2][0].inv || len(terms[2][0].text) != 3 ||51 terms[3][0].typ != termSuffix || terms[3][0].inv || len(terms[3][0].text) != 3 ||52 terms[4][0].typ != termExact || !terms[4][0].inv || len(terms[4][0].text) != 3 ||53 terms[5][0].typ != termFuzzy || !terms[5][0].inv || len(terms[5][0].text) != 3 ||54 terms[6][0].typ != termPrefix || !terms[6][0].inv || len(terms[6][0].text) != 3 ||55 terms[7][0].typ != termSuffix || !terms[7][0].inv || len(terms[7][0].text) != 3 {56 t.Errorf("%v", terms)57 }58}5960func TestParseTermsEmpty(t *testing.T) {61 terms := parseTerms(true, CaseSmart, false, "' ^ !' !^")62 if len(terms) != 0 {63 t.Errorf("%v", terms)64 }65}6667func TestExact(t *testing.T) {68 defer clearPatternCache()69 clearPatternCache()70 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true,71 []Range{}, Delimiter{}, []rune("'abc"))72 chars := util.ToChars([]byte("aabbcc abc"))73 res, pos := algo.ExactMatchNaive(74 pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)75 if res.Start != 7 || res.End != 10 {76 t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)77 }78 if pos != nil {79 t.Errorf("pos is expected to be nil")80 }81}8283func TestEqual(t *testing.T) {84 defer clearPatternCache()85 clearPatternCache()86 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))8788 match := func(str string, sidxExpected int, eidxExpected int) {89 chars := util.ToChars([]byte(str))90 res, pos := algo.EqualMatch(91 pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)92 if res.Start != sidxExpected || res.End != eidxExpected {93 t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)94 }95 if pos != nil {96 t.Errorf("pos is expected to be nil")97 }98 }99 match("ABC", -1, -1)100 match("AbC", 0, 3)101}102103func TestCaseSensitivity(t *testing.T) {104 defer clearPatternCache()105 clearPatternCache()106 pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("abc"))107 clearPatternCache()108 pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))109 clearPatternCache()110 pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("abc"))111 clearPatternCache()112 pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))113 clearPatternCache()114 pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("abc"))115 clearPatternCache()116 pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))117118 if string(pat1.text) != "abc" || pat1.caseSensitive != false ||119 string(pat2.text) != "Abc" || pat2.caseSensitive != true ||120 string(pat3.text) != "abc" || pat3.caseSensitive != false ||121 string(pat4.text) != "abc" || pat4.caseSensitive != false ||122 string(pat5.text) != "abc" || pat5.caseSensitive != true ||123 string(pat6.text) != "Abc" || pat6.caseSensitive != true {124 t.Error("Invalid case conversion")125 }126}127128func TestOrigTextAndTransformed(t *testing.T) {129 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("jg"))130 tokens := Tokenize("junegunn", Delimiter{})131 trans := Transform(tokens, []Range{Range{1, 1}})132133 origBytes := []byte("junegunn.choi")134 for _, extended := range []bool{false, true} {135 chunk := Chunk{count: 1}136 chunk.items[0] = Item{137 text: util.ToChars([]byte("junegunn")),138 origText: &origBytes,139 transformed: &trans}140 pattern.extended = extended141 matches := pattern.matchChunk(&chunk, nil, slab) // No cache142 if !(matches[0].item.text.ToString() == "junegunn" &&143 string(*matches[0].item.origText) == "junegunn.choi" &&144 reflect.DeepEqual(*matches[0].item.transformed, trans)) {145 t.Error("Invalid match result", matches)146 }147148 match, offsets, pos := pattern.MatchItem(&chunk.items[0], true, slab)149 if !(match.item.text.ToString() == "junegunn" &&150 string(*match.item.origText) == "junegunn.choi" &&151 offsets[0][0] == 0 && offsets[0][1] == 5 &&152 reflect.DeepEqual(*match.item.transformed, trans)) {153 t.Error("Invalid match result", match, offsets, extended)154 }155 if !((*pos)[0] == 4 && (*pos)[1] == 0) {156 t.Error("Invalid pos array", *pos)157 }158 }159}160161func TestCacheKey(t *testing.T) {162 test := func(extended bool, patStr string, expected string, cacheable bool) {163 clearPatternCache()164 pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune(patStr))165 if pat.CacheKey() != expected {166 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())167 }168 if pat.cacheable != cacheable {169 t.Errorf("Expected: %t, actual: %t (%s)", cacheable, pat.cacheable, patStr)170 }171 clearPatternCache()172 }173 test(false, "foo !bar", "foo !bar", true)174 test(false, "foo | bar !baz", "foo | bar !baz", true)175 test(true, "foo bar baz", "foo\tbar\tbaz", true)176 test(true, "foo !bar", "foo", false)177 test(true, "foo !bar baz", "foo\tbaz", false)178 test(true, "foo | bar baz", "baz", false)179 test(true, "foo | bar | baz", "", false)180 test(true, "foo | bar !baz", "", false)181 test(true, "| | foo", "", false)182 test(true, "| | | foo", "foo", false)183}184185func TestCacheable(t *testing.T) {186 test := func(fuzzy bool, str string, expected string, cacheable bool) {187 clearPatternCache()188 pat := BuildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, true, []Range{}, Delimiter{}, []rune(str))189 if pat.CacheKey() != expected {190 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())191 }192 if cacheable != pat.cacheable {193 t.Errorf("Invalid Pattern.cacheable for \"%s\": %v (expected: %v)", str, pat.cacheable, cacheable)194 }195 clearPatternCache()196 }197 test(true, "foo bar", "foo\tbar", true)198 test(true, "foo 'bar", "foo\tbar", false)199 test(true, "foo !bar", "foo", false)200201 test(false, "foo bar", "foo\tbar", true)202 test(false, "foo 'bar", "foo", false)203 test(false, "foo '", "foo", true)204 test(false, "foo 'bar", "foo", false)205 test(false, "foo !bar", "foo", false)206}