pattern_test.go
This file
Path
src/pattern_test.go
206 lines · 7.6 KiB · Go
Last changed by
Remove pointer indirection by changing Chunk definitionJunegunn Choibefore adoptiond4f3d5a
History of this fileTake it
The walk stopped after 200 commits: lines older than 37f2d8f point at it.
[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 1–17why, one step back1package 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) {Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 18why, one step back18 terms := parseTerms(true, CaseSmart, false,[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 19–52why, one step back19 "| 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("%s", terms)35 }36 for idx, termSet := range terms[:8] {37 term := termSet[0]38 if len(term.text) != 3 {39 t.Errorf("%s", term)40 }41 if idx > 0 && len(term.origText) != 4+idx/5 {42 t.Errorf("%s", term)43 }44 }45 for _, term := range terms[8] {46 if len(term.origText) != 4 {47 t.Errorf("%s", term)48 }49 }50}5152func TestParseTermsExtendedExact(t *testing.T) {Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 53why, one step back53 terms := parseTerms(false, CaseSmart, false,[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 54–68why, one step back54 "aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$")55 if len(terms) != 8 ||56 terms[0][0].typ != termExact || terms[0][0].inv || len(terms[0][0].text) != 3 ||57 terms[1][0].typ != termFuzzy || terms[1][0].inv || len(terms[1][0].text) != 3 ||58 terms[2][0].typ != termPrefix || terms[2][0].inv || len(terms[2][0].text) != 3 ||59 terms[3][0].typ != termSuffix || terms[3][0].inv || len(terms[3][0].text) != 3 ||60 terms[4][0].typ != termExact || !terms[4][0].inv || len(terms[4][0].text) != 3 ||61 terms[5][0].typ != termFuzzy || !terms[5][0].inv || len(terms[5][0].text) != 3 ||62 terms[6][0].typ != termPrefix || !terms[6][0].inv || len(terms[6][0].text) != 3 ||63 terms[7][0].typ != termSuffix || !terms[7][0].inv || len(terms[7][0].text) != 3 {64 t.Errorf("%s", terms)65 }66}6768func TestParseTermsEmpty(t *testing.T) {Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 69why, one step back69 terms := parseTerms(true, CaseSmart, false, "' $ ^ !' !^ !$")[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 70–77why, one step back70 if len(terms) != 0 {71 t.Errorf("%s", terms)72 }73}7475func TestExact(t *testing.T) {76 defer clearPatternCache()77 clearPatternCache()Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 78why, one step back78 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true,[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 79–80why, one step back79 []Range{}, Delimiter{}, []rune("'abc"))80 res, pos := algo.ExactMatchNaive(Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 81why, one step back81 pattern.caseSensitive, pattern.normalize, pattern.forward, util.RunesToChars([]rune("aabbcc abc")), pattern.termSets[0][0].text, true, nil)[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 82–92why, one step back82 if res.Start != 7 || res.End != 10 {83 t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)84 }85 if pos != nil {86 t.Errorf("pos is expected to be nil")87 }88}8990func TestEqual(t *testing.T) {91 defer clearPatternCache()92 clearPatternCache()Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 93why, one step back93 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 94–96why, one step back9495 match := func(str string, sidxExpected int, eidxExpected int) {96 res, pos := algo.EqualMatch(Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 97why, one step back97 pattern.caseSensitive, pattern.normalize, pattern.forward, util.RunesToChars([]rune(str)), pattern.termSets[0][0].text, true, nil)[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 98–111why, one step back98 if res.Start != sidxExpected || res.End != eidxExpected {99 t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)100 }101 if pos != nil {102 t.Errorf("pos is expected to be nil")103 }104 }105 match("ABC", -1, -1)106 match("AbC", 0, 3)107}108109func TestCaseSensitivity(t *testing.T) {110 defer clearPatternCache()111 clearPatternCache()Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 112why, one step back112 pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("abc"))[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
line 113why, one step back113 clearPatternCache()Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 114why, one step back114 pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
line 115why, one step back115 clearPatternCache()Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 116why, one step back116 pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("abc"))[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
line 117why, one step back117 clearPatternCache()Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 118why, one step back118 pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
line 119why, one step back119 clearPatternCache()Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 120why, one step back120 pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("abc"))[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
line 121why, one step back121 clearPatternCache()Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 122why, one step back122 pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 123–134why, one step back123124 if string(pat1.text) != "abc" || pat1.caseSensitive != false ||125 string(pat2.text) != "Abc" || pat2.caseSensitive != true ||126 string(pat3.text) != "abc" || pat3.caseSensitive != false ||127 string(pat4.text) != "abc" || pat4.caseSensitive != false ||128 string(pat5.text) != "abc" || pat5.caseSensitive != true ||129 string(pat6.text) != "Abc" || pat6.caseSensitive != true {130 t.Error("Invalid case conversion")131 }132}133134func TestOrigTextAndTransformed(t *testing.T) {Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 135why, one step back135 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("jg"))[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 136–141why, one step back136 tokens := Tokenize(util.RunesToChars([]rune("junegunn")), Delimiter{})137 trans := Transform(tokens, []Range{Range{1, 1}})138139 origBytes := []byte("junegunn.choi")140 for _, extended := range []bool{false, true} {141 chunk := Chunk{Remove pointer indirection by changing Chunk definitionJunegunn Choibefore adoptiond4f3d5a
line 142why, one step back142 Item{[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 143–154why, one step back143 text: util.RunesToChars([]rune("junegunn")),144 origText: &origBytes,145 transformed: trans},146 }147 pattern.extended = extended148 matches := pattern.matchChunk(&chunk, nil, slab) // No cache149 if !(matches[0].item.text.ToString() == "junegunn" &&150 string(*matches[0].item.origText) == "junegunn.choi" &&151 reflect.DeepEqual(matches[0].item.transformed, trans)) {152 t.Error("Invalid match result", matches)153 }154Remove pointer indirection by changing Chunk definitionJunegunn Choibefore adoptiond4f3d5a
line 155why, one step back155 match, offsets, pos := pattern.MatchItem(&chunk[0], true, slab)[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 156–169why, one step back156 if !(match.item.text.ToString() == "junegunn" &&157 string(*match.item.origText) == "junegunn.choi" &&158 offsets[0][0] == 0 && offsets[0][1] == 5 &&159 reflect.DeepEqual(match.item.transformed, trans)) {160 t.Error("Invalid match result", match, offsets, extended)161 }162 if !((*pos)[0] == 4 && (*pos)[1] == 0) {163 t.Error("Invalid pos array", *pos)164 }165 }166}167168func TestCacheKey(t *testing.T) {169 test := func(extended bool, patStr string, expected string, cacheable bool) {Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a
line 170why, one step back170 pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune(patStr))[vim] Respect g:fzf_colorsJunegunn Choibefore adoption37f2d8f
lines 171–188why, one step back171 if pat.CacheKey() != expected {172 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())173 }174 if pat.cacheable != cacheable {175 t.Errorf("Expected: %s, actual: %s (%s)", cacheable, pat.cacheable, patStr)176 }177 clearPatternCache()178 }179 test(false, "foo !bar", "foo !bar", true)180 test(false, "foo | bar !baz", "foo | bar !baz", true)181 test(true, "foo bar baz", "foo bar baz", true)182 test(true, "foo !bar", "foo", false)183 test(true, "foo !bar baz", "foo baz", false)184 test(true, "foo | bar baz", "baz", false)185 test(true, "foo | bar | baz", "", false)186 test(true, "foo | bar !baz", "", false)187 test(true, "| | | foo", "foo", true)188}Fix caching scheme when --exact is set and '-prefix is usedJunegunn Choibefore adoptiondd1f265
lines 189–206why, one step back189190func TestCacheable(t *testing.T) {191 test := func(fuzzy bool, str string, cacheable bool) {192 clearPatternCache()193 pat := BuildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, true, []Range{}, Delimiter{}, []rune(str))194 if cacheable != pat.cacheable {195 t.Errorf("Invalid Pattern.cacheable for \"%s\": %v (expected: %v)", str, pat.cacheable, cacheable)196 }197 }198 test(true, "foo bar", true)199 test(true, "foo 'bar", true)200 test(true, "foo !bar", false)201202 test(false, "foo bar", true)203 test(false, "foo '", true)204 test(false, "foo 'bar", false)205 test(false, "foo !bar", false)206}