|
|
|
@ -12,6 +12,20 @@ import (
|
|
|
|
|
"github.com/junegunn/fzf/src/util"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func replacePlaceholderTest(template string, stripAnsi bool, delimiter Delimiter, printsep string, forcePlus bool, query string, allItems []*Item) string {
|
|
|
|
|
return replacePlaceholder(replacePlaceholderParams{
|
|
|
|
|
template: template,
|
|
|
|
|
stripAnsi: stripAnsi,
|
|
|
|
|
delimiter: delimiter,
|
|
|
|
|
printsep: printsep,
|
|
|
|
|
forcePlus: forcePlus,
|
|
|
|
|
query: query,
|
|
|
|
|
allItems: allItems,
|
|
|
|
|
lastAction: actBackwardDeleteCharEof,
|
|
|
|
|
prompt: "prompt",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReplacePlaceholder(t *testing.T) {
|
|
|
|
|
item1 := newItem(" foo'bar \x1b[31mbaz\x1b[m")
|
|
|
|
|
items1 := []*Item{item1, item1}
|
|
|
|
@ -52,90 +66,90 @@ func TestReplacePlaceholder(t *testing.T) {
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// {}, preserve ansi
|
|
|
|
|
result = replacePlaceholder("echo {}", false, Delimiter{}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {}", false, Delimiter{}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} foo{{.I}}bar \x1b[31mbaz\x1b[m{{.O}}")
|
|
|
|
|
|
|
|
|
|
// {}, strip ansi
|
|
|
|
|
result = replacePlaceholder("echo {}", true, Delimiter{}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {}", true, Delimiter{}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} foo{{.I}}bar baz{{.O}}")
|
|
|
|
|
|
|
|
|
|
// {}, with multiple items
|
|
|
|
|
result = replacePlaceholder("echo {}", true, Delimiter{}, printsep, false, "query", items2, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {}", true, Delimiter{}, printsep, false, "query", items2)
|
|
|
|
|
checkFormat("echo {{.O}}foo{{.I}}bar baz{{.O}}")
|
|
|
|
|
|
|
|
|
|
// {..}, strip leading whitespaces, preserve ansi
|
|
|
|
|
result = replacePlaceholder("echo {..}", false, Delimiter{}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {..}", false, Delimiter{}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}}foo{{.I}}bar \x1b[31mbaz\x1b[m{{.O}}")
|
|
|
|
|
|
|
|
|
|
// {..}, strip leading whitespaces, strip ansi
|
|
|
|
|
result = replacePlaceholder("echo {..}", true, Delimiter{}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {..}", true, Delimiter{}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}}foo{{.I}}bar baz{{.O}}")
|
|
|
|
|
|
|
|
|
|
// {q}
|
|
|
|
|
result = replacePlaceholder("echo {} {q}", true, Delimiter{}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {} {q}", true, Delimiter{}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} foo{{.I}}bar baz{{.O}} {{.O}}query{{.O}}")
|
|
|
|
|
|
|
|
|
|
// {q}, multiple items
|
|
|
|
|
result = replacePlaceholder("echo {+}{q}{+}", true, Delimiter{}, printsep, false, "query 'string'", items2, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {+}{q}{+}", true, Delimiter{}, printsep, false, "query 'string'", items2)
|
|
|
|
|
checkFormat("echo {{.O}}foo{{.I}}bar baz{{.O}} {{.O}}FOO{{.I}}BAR BAZ{{.O}}{{.O}}query {{.I}}string{{.I}}{{.O}}{{.O}}foo{{.I}}bar baz{{.O}} {{.O}}FOO{{.I}}BAR BAZ{{.O}}")
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {}{q}{}", true, Delimiter{}, printsep, false, "query 'string'", items2, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {}{q}{}", true, Delimiter{}, printsep, false, "query 'string'", items2)
|
|
|
|
|
checkFormat("echo {{.O}}foo{{.I}}bar baz{{.O}}{{.O}}query {{.I}}string{{.I}}{{.O}}{{.O}}foo{{.I}}bar baz{{.O}}")
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {1}/{2}/{2,1}/{-1}/{-2}/{}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {1}/{2}/{2,1}/{-1}/{-2}/{}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}}foo{{.I}}bar{{.O}}/{{.O}}baz{{.O}}/{{.O}}bazfoo{{.I}}bar{{.O}}/{{.O}}baz{{.O}}/{{.O}}foo{{.I}}bar{{.O}}/{{.O}} foo{{.I}}bar baz{{.O}}/{{.O}}foo{{.I}}bar baz{{.O}}/{n.t}/{}/{1}/{q}/{{.O}}{{.O}}")
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, printsep, false, "query", items2, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, printsep, false, "query", items2)
|
|
|
|
|
checkFormat("echo {{.O}}foo{{.I}}bar{{.O}}/{{.O}}baz{{.O}}/{{.O}}baz{{.O}}/{{.O}}foo{{.I}}bar{{.O}}/{{.O}}foo{{.I}}bar baz{{.O}}/{n.t}/{}/{1}/{q}/{{.O}}{{.O}}")
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {+1}/{+2}/{+-1}/{+-2}/{+..}/{n.t}/\\{}/\\{1}/\\{q}/{+3}", true, Delimiter{}, printsep, false, "query", items2, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {+1}/{+2}/{+-1}/{+-2}/{+..}/{n.t}/\\{}/\\{1}/\\{q}/{+3}", true, Delimiter{}, printsep, false, "query", items2)
|
|
|
|
|
checkFormat("echo {{.O}}foo{{.I}}bar{{.O}} {{.O}}FOO{{.I}}BAR{{.O}}/{{.O}}baz{{.O}} {{.O}}BAZ{{.O}}/{{.O}}baz{{.O}} {{.O}}BAZ{{.O}}/{{.O}}foo{{.I}}bar{{.O}} {{.O}}FOO{{.I}}BAR{{.O}}/{{.O}}foo{{.I}}bar baz{{.O}} {{.O}}FOO{{.I}}BAR BAZ{{.O}}/{n.t}/{}/{1}/{q}/{{.O}}{{.O}} {{.O}}{{.O}}")
|
|
|
|
|
|
|
|
|
|
// forcePlus
|
|
|
|
|
result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, printsep, true, "query", items2, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, printsep, true, "query", items2)
|
|
|
|
|
checkFormat("echo {{.O}}foo{{.I}}bar{{.O}} {{.O}}FOO{{.I}}BAR{{.O}}/{{.O}}baz{{.O}} {{.O}}BAZ{{.O}}/{{.O}}baz{{.O}} {{.O}}BAZ{{.O}}/{{.O}}foo{{.I}}bar{{.O}} {{.O}}FOO{{.I}}BAR{{.O}}/{{.O}}foo{{.I}}bar baz{{.O}} {{.O}}FOO{{.I}}BAR BAZ{{.O}}/{n.t}/{}/{1}/{q}/{{.O}}{{.O}} {{.O}}{{.O}}")
|
|
|
|
|
|
|
|
|
|
// Whitespace preserving flag with "'" delimiter
|
|
|
|
|
result = replacePlaceholder("echo {s1}", true, Delimiter{str: &delim}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {s1}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} foo{{.O}}")
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {s2}", true, Delimiter{str: &delim}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {s2}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}}bar baz{{.O}}")
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {s}", true, Delimiter{str: &delim}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {s}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} foo{{.I}}bar baz{{.O}}")
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {s..}", true, Delimiter{str: &delim}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {s..}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} foo{{.I}}bar baz{{.O}}")
|
|
|
|
|
|
|
|
|
|
// Whitespace preserving flag with regex delimiter
|
|
|
|
|
regex = regexp.MustCompile(`\w+`)
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {s1}", true, Delimiter{regex: regex}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {s1}", true, Delimiter{regex: regex}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} {{.O}}")
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {s2}", true, Delimiter{regex: regex}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {s2}", true, Delimiter{regex: regex}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}}{{.I}}{{.O}}")
|
|
|
|
|
|
|
|
|
|
result = replacePlaceholder("echo {s3}", true, Delimiter{regex: regex}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {s3}", true, Delimiter{regex: regex}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} {{.O}}")
|
|
|
|
|
|
|
|
|
|
// No match
|
|
|
|
|
result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, printsep, false, "query", []*Item{nil, nil}, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {}/{+}", true, Delimiter{}, printsep, false, "query", []*Item{nil, nil})
|
|
|
|
|
check("echo /")
|
|
|
|
|
|
|
|
|
|
// No match, but with selections
|
|
|
|
|
result = replacePlaceholder("echo {}/{+}", true, Delimiter{}, printsep, false, "query", []*Item{nil, item1}, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {}/{+}", true, Delimiter{}, printsep, false, "query", []*Item{nil, item1})
|
|
|
|
|
checkFormat("echo /{{.O}} foo{{.I}}bar baz{{.O}}")
|
|
|
|
|
|
|
|
|
|
// String delimiter
|
|
|
|
|
result = replacePlaceholder("echo {}/{1}/{2}", true, Delimiter{str: &delim}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {}/{1}/{2}", true, Delimiter{str: &delim}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} foo{{.I}}bar baz{{.O}}/{{.O}}foo{{.O}}/{{.O}}bar baz{{.O}}")
|
|
|
|
|
|
|
|
|
|
// Regex delimiter
|
|
|
|
|
regex = regexp.MustCompile("[oa]+")
|
|
|
|
|
// foo'bar baz
|
|
|
|
|
result = replacePlaceholder("echo {}/{1}/{3}/{2..3}", true, Delimiter{regex: regex}, printsep, false, "query", items1, actIgnore)
|
|
|
|
|
result = replacePlaceholderTest("echo {}/{1}/{3}/{2..3}", true, Delimiter{regex: regex}, printsep, false, "query", items1)
|
|
|
|
|
checkFormat("echo {{.O}} foo{{.I}}bar baz{{.O}}/{{.O}}f{{.O}}/{{.O}}r b{{.O}}/{{.O}}{{.I}}bar b{{.O}}")
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@ -155,7 +169,6 @@ func TestReplacePlaceholder(t *testing.T) {
|
|
|
|
|
newItem("7a 7b 7c 7d 7e 7f"),
|
|
|
|
|
}
|
|
|
|
|
stripAnsi := false
|
|
|
|
|
printsep = "\n"
|
|
|
|
|
forcePlus := false
|
|
|
|
|
query := "sample query"
|
|
|
|
|
|
|
|
|
@ -199,7 +212,7 @@ func TestReplacePlaceholder(t *testing.T) {
|
|
|
|
|
// while the double q is invalid, it is useful here for testing purposes
|
|
|
|
|
templateToOutput[`{q}`] = "{{.O}}" + query + "{{.O}}"
|
|
|
|
|
templateToOutput[`{fzf:query}`] = "{{.O}}" + query + "{{.O}}"
|
|
|
|
|
templateToOutput[`{fzf:action}`] = "backward-delete-char-eof"
|
|
|
|
|
templateToOutput[`{fzf:action} {fzf:prompt}`] = "backward-delete-char-eof 'prompt'"
|
|
|
|
|
|
|
|
|
|
// IV. escaping placeholder
|
|
|
|
|
templateToOutput[`\{}`] = `{}`
|
|
|
|
@ -210,11 +223,11 @@ func TestReplacePlaceholder(t *testing.T) {
|
|
|
|
|
templateToOutput[`{++}`] = templateToOutput[`{+}`]
|
|
|
|
|
|
|
|
|
|
for giveTemplate, wantOutput := range templateToOutput {
|
|
|
|
|
result = replacePlaceholder(giveTemplate, stripAnsi, Delimiter{}, printsep, forcePlus, query, items3, actBackwardDeleteCharEof)
|
|
|
|
|
result = replacePlaceholderTest(giveTemplate, stripAnsi, Delimiter{}, printsep, forcePlus, query, items3)
|
|
|
|
|
checkFormat(wantOutput)
|
|
|
|
|
}
|
|
|
|
|
for giveTemplate, wantOutput := range templateToFile {
|
|
|
|
|
path := replacePlaceholder(giveTemplate, stripAnsi, Delimiter{}, printsep, forcePlus, query, items3, actIgnore)
|
|
|
|
|
path := replacePlaceholderTest(giveTemplate, stripAnsi, Delimiter{}, printsep, forcePlus, query, items3)
|
|
|
|
|
|
|
|
|
|
data, err := readFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -568,10 +581,10 @@ func testCommands(t *testing.T, tests []testCase) {
|
|
|
|
|
|
|
|
|
|
// evaluate the test cases
|
|
|
|
|
for idx, test := range tests {
|
|
|
|
|
gotOutput := replacePlaceholder(
|
|
|
|
|
gotOutput := replacePlaceholderTest(
|
|
|
|
|
test.give.template, stripAnsi, delimiter, printsep, forcePlus,
|
|
|
|
|
test.give.query,
|
|
|
|
|
test.give.allItems, actIgnore)
|
|
|
|
|
test.give.allItems)
|
|
|
|
|
switch {
|
|
|
|
|
case test.want.output != "":
|
|
|
|
|
if gotOutput != test.want.output {
|
|
|
|
|