Skip to content
Myr

pattern_test.go

This file

Path

src/pattern_test.go

206 lines · 7.6 KiB · Go

Last changed by

Further reduce unnecessary rune array conversionJunegunn Choibefore adoptionbd98f98

History of this file

Take it

The walk stopped after 200 commits: lines older than 06a6ad8 point at it.

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 1–17why, one step back
1package fzf
23import (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 back
18 terms := parseTerms(true, CaseSmart, false,

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 19–52why, one step back
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("%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 back
53 terms := parseTerms(false, CaseSmart, false,

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 54–68why, one step back
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) {

Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a

line 69why, one step back
69 terms := parseTerms(true, CaseSmart, false, "' $ ^ !' !^ !$")

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 70–77why, one step back
70 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 back
78 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true,

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 79–80why, one step back
79 []Range{}, Delimiter{}, []rune("'abc"))
80 res, pos := algo.ExactMatchNaive(

Further reduce unnecessary rune array conversionJunegunn Choibefore adoptionbd98f98

line 81why, one step back
81 pattern.caseSensitive, pattern.normalize, pattern.forward, util.ToChars([]byte("aabbcc abc")), pattern.termSets[0][0].text, true, nil)

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 82–92why, one step back
82 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 back
93 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 94–96why, one step back
94
95 match := func(str string, sidxExpected int, eidxExpected int) {96 res, pos := algo.EqualMatch(

Further reduce unnecessary rune array conversionJunegunn Choibefore adoptionbd98f98

line 97why, one step back
97 pattern.caseSensitive, pattern.normalize, pattern.forward, util.ToChars([]byte(str)), pattern.termSets[0][0].text, true, nil)

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 98–111why, one step back
98 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 back
112 pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("abc"))

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

line 113why, one step back
113 clearPatternCache()

Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a

line 114why, one step back
114 pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

line 115why, one step back
115 clearPatternCache()

Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a

line 116why, one step back
116 pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("abc"))

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

line 117why, one step back
117 clearPatternCache()

Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a

line 118why, one step back
118 pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

line 119why, one step back
119 clearPatternCache()

Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a

line 120why, one step back
120 pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("abc"))

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

line 121why, one step back
121 clearPatternCache()

Merge pull request #794 from junegunn/develJunegunn Choibefore adoption05ed57a

line 122why, one step back
122 pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, true, []Range{}, Delimiter{}, []rune("Abc"))

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 123–134why, one step back
123
124 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 back
135 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("jg"))

Avoid unconditionally storsing input as runesJunegunn Choibefore adoptionc9f16b6

line 136why, one step back
136 tokens := Tokenize("junegunn", Delimiter{})

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 137–141why, one step back
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 back
142 Item{

Further reduce unnecessary rune array conversionJunegunn Choibefore adoptionbd98f98

line 143why, one step back
143 text: util.ToChars([]byte("junegunn")),

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

line 144why, one step back
144 origText: &origBytes,

Reduce memory footprint of Item structJunegunn Choibefore adoption9e85cba

line 145why, one step back
145 transformed: &trans},

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 146–150why, one step back
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" &&

Reduce memory footprint of Item structJunegunn Choibefore adoption9e85cba

line 151why, one step back
151 reflect.DeepEqual(*matches[0].item.transformed, trans)) {

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 152–154why, one step back
152 t.Error("Invalid match result", matches)
153 }154

Remove pointer indirection by changing Chunk definitionJunegunn Choibefore adoptiond4f3d5a

line 155why, one step back
155 match, offsets, pos := pattern.MatchItem(&chunk[0], true, slab)

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 156–158why, one step back
156 if !(match.item.text.ToString() == "junegunn" &&
157 string(*match.item.origText) == "junegunn.choi" &&158 offsets[0][0] == 0 && offsets[0][1] == 5 &&

Reduce memory footprint of Item structJunegunn Choibefore adoption9e85cba

line 159why, one step back
159 reflect.DeepEqual(*match.item.transformed, trans)) {

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 160–169why, one step back
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 back
170 pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune(patStr))

Update ANSI processor to ignore ^N and ^OJunegunn Choibefore adoption06a6ad8

lines 171–188why, one step back
171 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 back
189
190func 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}