Skip to content
Myr

pattern_test.go

This file

Path

src/pattern_test.go

201 lines · 8.0 KiB · Go

Last changed by

Add 'exclude' action for excluding current/selected items from the result (#4231)Junegunn Choibefore adoption67dd7e1

History of this file

Take it

1package 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 buildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool,68	withPos bool, cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {69	return BuildPattern(NewChunkCache(), make(map[string]*Pattern),70		fuzzy, fuzzyAlgo, extended, caseMode, normalize, forward,71		withPos, cacheable, nth, delimiter, revision{}, runes, nil)72}7374func TestExact(t *testing.T) {75	pattern := buildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true,76		[]Range{}, Delimiter{}, []rune("'abc"))77	chars := util.ToChars([]byte("aabbcc abc"))78	res, pos := algo.ExactMatchNaive(79		pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)80	if res.Start != 7 || res.End != 10 {81		t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)82	}83	if pos != nil {84		t.Errorf("pos is expected to be nil")85	}86}8788func TestEqual(t *testing.T) {89	pattern := buildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("^AbC$"))9091	match := func(str string, sidxExpected int, eidxExpected int) {92		chars := util.ToChars([]byte(str))93		res, pos := algo.EqualMatch(94			pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)95		if res.Start != sidxExpected || res.End != eidxExpected {96			t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)97		}98		if pos != nil {99			t.Errorf("pos is expected to be nil")100		}101	}102	match("ABC", -1, -1)103	match("AbC", 0, 3)104	match("AbC  ", 0, 3)105	match(" AbC ", 1, 4)106	match("  AbC", 2, 5)107}108109func TestCaseSensitivity(t *testing.T) {110	pat1 := buildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("abc"))111	pat2 := buildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc"))112	pat3 := buildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, false, true, []Range{}, Delimiter{}, []rune("abc"))113	pat4 := buildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc"))114	pat5 := buildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, false, true, []Range{}, Delimiter{}, []rune("abc"))115	pat6 := buildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc"))116117	if string(pat1.text) != "abc" || pat1.caseSensitive != false ||118		string(pat2.text) != "Abc" || pat2.caseSensitive != true ||119		string(pat3.text) != "abc" || pat3.caseSensitive != false ||120		string(pat4.text) != "abc" || pat4.caseSensitive != false ||121		string(pat5.text) != "abc" || pat5.caseSensitive != true ||122		string(pat6.text) != "Abc" || pat6.caseSensitive != true {123		t.Error("Invalid case conversion")124	}125}126127func TestOrigTextAndTransformed(t *testing.T) {128	pattern := buildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("jg"))129	tokens := Tokenize("junegunn", Delimiter{})130	trans := Transform(tokens, []Range{{1, 1}})131132	origBytes := []byte("junegunn.choi")133	for _, extended := range []bool{false, true} {134		chunk := Chunk{count: 1}135		chunk.items[0] = Item{136			text:        util.ToChars([]byte("junegunn")),137			origText:    &origBytes,138			transformed: &transformed{pattern.revision, trans}}139		pattern.extended = extended140		matches := pattern.matchChunk(&chunk, nil, slab) // No cache141		if !(matches[0].item.text.ToString() == "junegunn" &&142			string(*matches[0].item.origText) == "junegunn.choi" &&143			reflect.DeepEqual((*matches[0].item.transformed).tokens, trans)) {144			t.Error("Invalid match result", matches)145		}146147		match, offsets, pos := pattern.MatchItem(&chunk.items[0], true, slab)148		if !(match.item.text.ToString() == "junegunn" &&149			string(*match.item.origText) == "junegunn.choi" &&150			offsets[0][0] == 0 && offsets[0][1] == 5 &&151			reflect.DeepEqual((*match.item.transformed).tokens, trans)) {152			t.Error("Invalid match result", match, offsets, extended)153		}154		if !((*pos)[0] == 4 && (*pos)[1] == 0) {155			t.Error("Invalid pos array", *pos)156		}157	}158}159160func TestCacheKey(t *testing.T) {161	test := func(extended bool, patStr string, expected string, cacheable bool) {162		pat := buildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune(patStr))163		if pat.CacheKey() != expected {164			t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())165		}166		if pat.cacheable != cacheable {167			t.Errorf("Expected: %t, actual: %t (%s)", cacheable, pat.cacheable, patStr)168		}169	}170	test(false, "foo !bar", "foo !bar", true)171	test(false, "foo | bar !baz", "foo | bar !baz", true)172	test(true, "foo  bar  baz", "foo\tbar\tbaz", true)173	test(true, "foo !bar", "foo", false)174	test(true, "foo !bar   baz", "foo\tbaz", false)175	test(true, "foo | bar baz", "baz", false)176	test(true, "foo | bar | baz", "", false)177	test(true, "foo | bar !baz", "", false)178	test(true, "| | foo", "", false)179	test(true, "| | | foo", "foo", false)180}181182func TestCacheable(t *testing.T) {183	test := func(fuzzy bool, str string, expected string, cacheable bool) {184		pat := buildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, false, true, []Range{}, Delimiter{}, []rune(str))185		if pat.CacheKey() != expected {186			t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())187		}188		if cacheable != pat.cacheable {189			t.Errorf("Invalid Pattern.cacheable for \"%s\": %v (expected: %v)", str, pat.cacheable, cacheable)190		}191	}192	test(true, "foo bar", "foo\tbar", true)193	test(true, "foo 'bar", "foo\tbar", false)194	test(true, "foo !bar", "foo", false)195196	test(false, "foo bar", "foo\tbar", true)197	test(false, "foo 'bar", "foo", false)198	test(false, "foo '", "foo", true)199	test(false, "foo 'bar", "foo", false)200	test(false, "foo !bar", "foo", false)201}