Skip to content
Myr

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 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,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()78	pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, true, true,79		[]Range{}, Delimiter{}, []rune("'abc"))80	res, pos := algo.ExactMatchNaive(81		pattern.caseSensitive, pattern.forward, util.RunesToChars([]rune("aabbcc abc")), pattern.termSets[0][0].text, true, nil)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()93	pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))9495	match := func(str string, sidxExpected int, eidxExpected int) {96		res, pos := algo.EqualMatch(97			pattern.caseSensitive, pattern.forward, util.RunesToChars([]rune(str)), pattern.termSets[0][0].text, true, nil)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()112	pat1 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("abc"))113	clearPatternCache()114	pat2 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("Abc"))115	clearPatternCache()116	pat3 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, true, true, []Range{}, Delimiter{}, []rune("abc"))117	clearPatternCache()118	pat4 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, true, true, []Range{}, Delimiter{}, []rune("Abc"))119	clearPatternCache()120	pat5 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, true, true, []Range{}, Delimiter{}, []rune("abc"))121	clearPatternCache()122	pat6 := BuildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, true, true, []Range{}, Delimiter{}, []rune("Abc"))123124	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) {135	pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, true, true, []Range{}, Delimiter{}, []rune("jg"))136	tokens := Tokenize(util.RunesToChars([]rune("junegunn")), Delimiter{})137	trans := Transform(tokens, []Range{Range{1, 1}})138139	origBytes := []byte("junegunn.choi")140	for _, extended := range []bool{false, true} {141		chunk := Chunk{142			&Item{143				text:        util.RunesToChars([]rune("junegunn")),144				origText:    &origBytes,145				transformed: trans},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" &&151			reflect.DeepEqual(matches[0].item.transformed, trans)) {152			t.Error("Invalid match result", matches)153		}154155		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)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) {170		pat := BuildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, true, true, []Range{}, Delimiter{}, []rune(patStr))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}