pattern_test.go
This file
Path
src/pattern_test.go
209 lines · 7.9 KiB · Go
Last changed by
Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
History of this fileTake it
The walk stopped after 200 commits: lines older than bb02819 point at it.
Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
lines 1–69why, 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) {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()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 70why, one step back70 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true,Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
lines 71–85why, one step back71 []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()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 86why, one step back86 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("^AbC$"))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
lines 87–108why, one step back8788 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 match("AbC ", 0, 3)102 match(" AbC ", 1, 4)103 match(" AbC", 2, 5)104}105106func TestCaseSensitivity(t *testing.T) {107 defer clearPatternCache()108 clearPatternCache()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 109why, one step back109 pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("abc"))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
line 110why, one step back110 clearPatternCache()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 111why, one step back111 pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc"))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
line 112why, one step back112 clearPatternCache()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 113why, one step back113 pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, false, true, []Range{}, Delimiter{}, []rune("abc"))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
line 114why, one step back114 clearPatternCache()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 115why, one step back115 pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc"))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
line 116why, one step back116 clearPatternCache()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 117why, one step back117 pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, false, true, []Range{}, Delimiter{}, []rune("abc"))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
line 118why, one step back118 clearPatternCache()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 119why, one step back119 pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc"))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
lines 120–131why, one step back120121 if string(pat1.text) != "abc" || pat1.caseSensitive != false ||122 string(pat2.text) != "Abc" || pat2.caseSensitive != true ||123 string(pat3.text) != "abc" || pat3.caseSensitive != false ||124 string(pat4.text) != "abc" || pat4.caseSensitive != false ||125 string(pat5.text) != "abc" || pat5.caseSensitive != true ||126 string(pat6.text) != "Abc" || pat6.caseSensitive != true {127 t.Error("Invalid case conversion")128 }129}130131func TestOrigTextAndTransformed(t *testing.T) {Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 132why, one step back132 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("jg"))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
lines 133–166why, one step back133 tokens := Tokenize("junegunn", Delimiter{})134 trans := Transform(tokens, []Range{{1, 1}})135136 origBytes := []byte("junegunn.choi")137 for _, extended := range []bool{false, true} {138 chunk := Chunk{count: 1}139 chunk.items[0] = Item{140 text: util.ToChars([]byte("junegunn")),141 origText: &origBytes,142 transformed: &trans}143 pattern.extended = extended144 matches := pattern.matchChunk(&chunk, nil, slab) // No cache145 if !(matches[0].item.text.ToString() == "junegunn" &&146 string(*matches[0].item.origText) == "junegunn.choi" &&147 reflect.DeepEqual(*matches[0].item.transformed, trans)) {148 t.Error("Invalid match result", matches)149 }150151 match, offsets, pos := pattern.MatchItem(&chunk.items[0], true, slab)152 if !(match.item.text.ToString() == "junegunn" &&153 string(*match.item.origText) == "junegunn.choi" &&154 offsets[0][0] == 0 && offsets[0][1] == 5 &&155 reflect.DeepEqual(*match.item.transformed, trans)) {156 t.Error("Invalid match result", match, offsets, extended)157 }158 if !((*pos)[0] == 4 && (*pos)[1] == 0) {159 t.Error("Invalid pos array", *pos)160 }161 }162}163164func TestCacheKey(t *testing.T) {165 test := func(extended bool, patStr string, expected string, cacheable bool) {166 clearPatternCache()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 167why, one step back167 pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune(patStr))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
lines 168–190why, one step back168 if pat.CacheKey() != expected {169 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())170 }171 if pat.cacheable != cacheable {172 t.Errorf("Expected: %t, actual: %t (%s)", cacheable, pat.cacheable, patStr)173 }174 clearPatternCache()175 }176 test(false, "foo !bar", "foo !bar", true)177 test(false, "foo | bar !baz", "foo | bar !baz", true)178 test(true, "foo bar baz", "foo\tbar\tbaz", true)179 test(true, "foo !bar", "foo", false)180 test(true, "foo !bar baz", "foo\tbaz", false)181 test(true, "foo | bar baz", "baz", false)182 test(true, "foo | bar | baz", "", false)183 test(true, "foo | bar !baz", "", false)184 test(true, "| | foo", "", false)185 test(true, "| | | foo", "foo", false)186}187188func TestCacheable(t *testing.T) {189 test := func(fuzzy bool, str string, expected string, cacheable bool) {190 clearPatternCache()Fix incorrect ordering of `--tiebreak=chunk`Junegunn Choibefore adoption38259d0
line 191why, one step back191 pat := BuildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, false, true, []Range{}, Delimiter{}, []rune(str))Set up dependabot for GitHub actions (#2764)Naveenbefore adoptionbb02819
lines 192–209why, one step back192 if pat.CacheKey() != expected {193 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())194 }195 if cacheable != pat.cacheable {196 t.Errorf("Invalid Pattern.cacheable for \"%s\": %v (expected: %v)", str, pat.cacheable, cacheable)197 }198 clearPatternCache()199 }200 test(true, "foo bar", "foo\tbar", true)201 test(true, "foo 'bar", "foo\tbar", false)202 test(true, "foo !bar", "foo", false)203204 test(false, "foo bar", "foo\tbar", true)205 test(false, "foo 'bar", "foo", false)206 test(false, "foo '", "foo", true)207 test(false, "foo 'bar", "foo", false)208 test(false, "foo !bar", "foo", false)209}