Skip to content
Myr

pattern_test.go

This file

Path

src/pattern_test.go

206 lines · 7.7 KiB · Go

Last changed by

Pass util.Chars by pointerJunegunn Choibefore adoptiona2a4df0

History of this file

Take it

The walk stopped after 200 commits: lines older than 2a669e9 point at it.

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 1–18why, 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) {18 terms := parseTerms(true, CaseSmart, false,

Treat | as proper query when it can't be an OR operatorJunegunn Choibefore adoptiona09e411

line 19why, one step back
19 "aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ | ^iii$ ^xxx | 'yyy | zzz$ | !ZZZ |")

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 20–35why, one step back
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 }

Fix invalid cache lookupsJunegunn Choibefore adoption999d374

line 36why, one step back
36 for _, termSet := range terms[:8] {

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 37–60why, one step back
37 term := termSet[0]
38 if len(term.text) != 3 {39 t.Errorf("%s", 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("%s", terms)57 }58}5960func TestParseTermsEmpty(t *testing.T) {

Treat $ as proper search queryJunegunn Choibefore adoption02a7b96

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

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 62–71why, one step back
62 if len(terms) != 0 {
63 t.Errorf("%s", 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"))

Pass util.Chars by pointerJunegunn Choibefore adoptiona2a4df0

line 72why, one step back
72 chars := util.ToChars([]byte("aabbcc abc"))

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

line 73why, one step back
73 res, pos := algo.ExactMatchNaive(

Pass util.Chars by pointerJunegunn Choibefore adoptiona2a4df0

line 74why, one step back
74 pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 75–88why, one step back
75 if res.Start != 7 || res.End != 10 {
76 t.Errorf("%s / %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) {

Pass util.Chars by pointerJunegunn Choibefore adoptiona2a4df0

line 89why, one step back
89 chars := util.ToChars([]byte(str))

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

line 90why, one step back
90 res, pos := algo.EqualMatch(

Pass util.Chars by pointerJunegunn Choibefore adoptiona2a4df0

line 91why, one step back
91 pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 92–129why, one step back
92 if res.Start != sidxExpected || res.End != eidxExpected {
93 t.Errorf("%s / %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"))

Avoid unconditionally storsing input as runesJunegunn Choibefore adoptionc9f16b6

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

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 131–134why, one step back
131 trans := Transform(tokens, []Range{Range{1, 1}})
132133 origBytes := []byte("junegunn.choi")134 for _, extended := range []bool{false, true} {

Remove special nilItemJunegunn Choibefore adoption0d171ba

lines 135–139why, one step back
135 chunk := Chunk{count: 1}
136 chunk.items[0] = Item{137 text: util.ToChars([]byte("junegunn")),138 origText: &origBytes,139 transformed: &trans}

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 140–143why, one step back
140 pattern.extended = extended
141 matches := pattern.matchChunk(&chunk, nil, slab) // No cache142 if !(matches[0].item.text.ToString() == "junegunn" &&143 string(*matches[0].item.origText) == "junegunn.choi" &&

Reduce memory footprint of Item structJunegunn Choibefore adoption9e85cba

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

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 145–147why, one step back
145 t.Error("Invalid match result", matches)
146 }147

Remove special nilItemJunegunn Choibefore adoption0d171ba

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

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

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

Reduce memory footprint of Item structJunegunn Choibefore adoption9e85cba

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

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

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

Fix invalid cache lookupsJunegunn Choibefore adoption999d374

line 163why, one step back
163 clearPatternCache()

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 164–168why, one step back
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 {

Allow escaping meta characters with backslashesJunegunn Choibefore adoptione85a8a6

line 169why, one step back
169 t.Errorf("Expected: %t, actual: %t (%s)", cacheable, pat.cacheable, patStr)

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 170–174why, one step back
170 }
171 clearPatternCache()172 }173 test(false, "foo !bar", "foo !bar", true)174 test(false, "foo | bar !baz", "foo | bar !baz", true)

Allow escaping meta characters with backslashesJunegunn Choibefore adoptione85a8a6

line 175why, one step back
175 test(true, "foo bar baz", "foo\tbar\tbaz", true)

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

line 176why, one step back
176 test(true, "foo !bar", "foo", false)

Allow escaping meta characters with backslashesJunegunn Choibefore adoptione85a8a6

line 177why, one step back
177 test(true, "foo !bar baz", "foo\tbaz", false)

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

lines 178–180why, one step back
178 test(true, "foo | bar baz", "baz", false)
179 test(true, "foo | bar | baz", "", false)180 test(true, "foo | bar !baz", "", false)

Treat | as proper query when it can't be an OR operatorJunegunn Choibefore adoptiona09e411

lines 181–182why, one step back
181 test(true, "| | foo", "", false)
182 test(true, "| | | foo", "foo", false)

Clear lines even when background color is not setJunegunn Choibefore adoption2a669e9

line 183why, one step back
183}

Fix caching scheme when --exact is set and '-prefix is usedJunegunn Choibefore adoptiondd1f265

lines 184–185why, one step back
184
185func TestCacheable(t *testing.T) {

Fix invalid cache lookupsJunegunn Choibefore adoption999d374

line 186why, one step back
186 test := func(fuzzy bool, str string, expected string, cacheable bool) {

Fix caching scheme when --exact is set and '-prefix is usedJunegunn Choibefore adoptiondd1f265

lines 187–188why, one step back
187 clearPatternCache()
188 pat := BuildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, true, []Range{}, Delimiter{}, []rune(str))

Fix invalid cache lookupsJunegunn Choibefore adoption999d374

lines 189–191why, one step back
189 if pat.CacheKey() != expected {
190 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())191 }

Fix caching scheme when --exact is set and '-prefix is usedJunegunn Choibefore adoptiondd1f265

lines 192–194why, one step back
192 if cacheable != pat.cacheable {
193 t.Errorf("Invalid Pattern.cacheable for \"%s\": %v (expected: %v)", str, pat.cacheable, cacheable)194 }

Fix invalid cache lookupsJunegunn Choibefore adoption999d374

line 195why, one step back
195 clearPatternCache()

Fix caching scheme when --exact is set and '-prefix is usedJunegunn Choibefore adoptiondd1f265

line 196why, one step back
196 }

Allow escaping meta characters with backslashesJunegunn Choibefore adoptione85a8a6

lines 197–198why, one step back
197 test(true, "foo bar", "foo\tbar", true)
198 test(true, "foo 'bar", "foo\tbar", false)

Fix invalid cache lookupsJunegunn Choibefore adoption999d374

line 199why, one step back
199 test(true, "foo !bar", "foo", false)

Fix caching scheme when --exact is set and '-prefix is usedJunegunn Choibefore adoptiondd1f265

line 200why, one step back
200

Allow escaping meta characters with backslashesJunegunn Choibefore adoptione85a8a6

line 201why, one step back
201 test(false, "foo bar", "foo\tbar", true)

Fix invalid cache lookupsJunegunn Choibefore adoption999d374

lines 202–205why, one step back
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)

Fix caching scheme when --exact is set and '-prefix is usedJunegunn Choibefore adoptiondd1f265

line 206why, one step back
206}