pattern_test.go
This file
Path
src/pattern_test.go
188 lines · 6.9 KiB · Go
Last changed by
Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
History of this fileTake it
The walk stopped after 200 commits: lines older than 1fd884b point at it.
Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
lines 1–7why, one step back1package fzf23import (4 "reflect"5 "testing"67 "github.com/junegunn/fzf/src/algo"[perf] Avoid allocating rune array for ascii stringJunegunn Choibefore adoption1d4057c
line 8why, one step back8 "github.com/junegunn/fzf/src/util"Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
lines 9–10why, one step back9)10Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
lines 11–16why, one step back11var slab *util.Slab1213func init() {14 slab = util.MakeSlab(slab16Size, slab32Size)15}16Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
lines 17–77why, one step back17func TestParseTermsExtended(t *testing.T) {18 terms := parseTerms(true, CaseSmart,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 != termFuzzy || !terms[4][0].inv ||26 terms[5][0].typ != termExact || !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 != termFuzzy || !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) {53 terms := parseTerms(false, CaseSmart,54 "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) {69 terms := parseTerms(true, CaseSmart, "' $ ^ !' !^ !$")70 if len(terms) != 0 {71 t.Errorf("%s", terms)72 }73}7475func TestExact(t *testing.T) {76 defer clearPatternCache()77 clearPatternCache()Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 78why, one step back78 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, true, true,Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 79why, one step back79 []Range{}, Delimiter{}, []rune("'abc"))Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
lines 80–81why, one step back80 res, pos := algo.ExactMatchNaive(81 pattern.caseSensitive, pattern.forward, util.RunesToChars([]rune("aabbcc abc")), pattern.termSets[0][0].text, true, nil)Enhanced ranking algorithmJunegunn Choibefore adoption2f6d23b
lines 82–83why, one step back82 if res.Start != 7 || res.End != 10 {83 t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 84why, one step back84 }Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
lines 85–87why, one step back85 if pos != nil {86 t.Errorf("pos is expected to be nil")87 }Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
lines 88–92why, one step back88}8990func TestEqual(t *testing.T) {91 defer clearPatternCache()92 clearPatternCache()Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 93why, one step back93 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 94why, one step back94Micro-optimizationsJunegunn Choibefore adoption37dc273
line 95why, one step back95 match := func(str string, sidxExpected int, eidxExpected int) {Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
lines 96–97why, one step back96 res, pos := algo.EqualMatch(97 pattern.caseSensitive, pattern.forward, util.RunesToChars([]rune(str)), pattern.termSets[0][0].text, true, nil)Enhanced ranking algorithmJunegunn Choibefore adoption2f6d23b
lines 98–99why, one step back98 if res.Start != sidxExpected || res.End != eidxExpected {99 t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 100why, one step back100 }Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
lines 101–103why, one step back101 if pos != nil {102 t.Errorf("pos is expected to be nil")103 }Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
lines 104–111why, one step back104 }105 match("ABC", -1, -1)106 match("AbC", 0, 3)107}108109func TestCaseSensitivity(t *testing.T) {110 defer clearPatternCache()111 clearPatternCache()Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 112why, one step back112 pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("abc"))Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 113why, one step back113 clearPatternCache()Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 114why, one step back114 pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("Abc"))Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 115why, one step back115 clearPatternCache()Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 116why, one step back116 pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, true, true, []Range{}, Delimiter{}, []rune("abc"))Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 117why, one step back117 clearPatternCache()Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 118why, one step back118 pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, true, true, []Range{}, Delimiter{}, []rune("Abc"))Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 119why, one step back119 clearPatternCache()Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 120why, one step back120 pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, true, true, []Range{}, Delimiter{}, []rune("abc"))Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 121why, one step back121 clearPatternCache()Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 122why, one step back122 pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, true, true, []Range{}, Delimiter{}, []rune("Abc"))Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
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) {Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 135why, one step back135 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("jg"))[perf] Avoid allocating rune array for ascii stringJunegunn Choibefore adoption1d4057c
line 136why, one step back136 tokens := Tokenize(util.RunesToChars([]rune("junegunn")), Delimiter{})Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
lines 137–138why, one step back137 trans := Transform(tokens, []Range{Range{1, 1}})138[perf] Avoid allocating rune array for ascii stringJunegunn Choibefore adoption1d4057c
line 139why, one step back139 origBytes := []byte("junegunn.choi")Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
lines 140–142why, one step back140 for _, extended := range []bool{false, true} {141 chunk := Chunk{142 &Item{[perf] Avoid allocating rune array for ascii stringJunegunn Choibefore adoption1d4057c
lines 143–144why, one step back143 text: util.RunesToChars([]rune("junegunn")),144 origText: &origBytes,Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
lines 145–147why, one step back145 transformed: trans},146 }147 pattern.extended = extendedRevise ranking algorithmJunegunn Choibefore adoption2fc7c18
lines 148–151why, one step back148 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)) {Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 152why, one step back152 t.Error("Invalid match result", matches)Remove Offset slice from Result structJunegunn Choibefore adoption827a83e
lines 153–154why, one step back153 }154Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
lines 155–160why, one step back155 match, offsets, pos := pattern.MatchItem(chunk[0], true, slab)156 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)Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
line 161why, one step back161 }Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
lines 162–164why, one step back162 if !((*pos)[0] == 4 && (*pos)[1] == 0) {163 t.Error("Invalid pos array", *pos)164 }Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
lines 165–169why, one step back165 }166}167168func TestCacheKey(t *testing.T) {169 test := func(extended bool, patStr string, expected string, cacheable bool) {Revise ranking algorithmJunegunn Choibefore adoption2fc7c18
line 170why, one step back170 pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, true, true, []Range{}, Delimiter{}, []rune(patStr))Merge pull request #423 from blueyed/zsh-fzf-completion-localoptionsJunegunn Choibefore adoption1fd884b
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}