summaryrefslogtreecommitdiffstats
path: root/src/terminal_test.go
blob: 5afafaa20a71e656e480dd6f8f6b0c9074c6774b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package fzf

import (
	"regexp"
	"testing"

	"github.com/junegunn/fzf/src/util"
)

func newItem(str string) *Item {
	bytes := []byte(str)
	trimmed, _, _ := extractColor(str, nil, nil)
	return &Item{origText: &bytes, text: util.RunesToChars([]rune(trimmed))}
}

func TestReplacePlaceholder(t *testing.T) {
	items1 := []*Item{newItem("  foo'bar \x1b[31mbaz\x1b[m")}
	items2 := []*Item{
		newItem("foo'bar \x1b[31mbaz\x1b[m"),
		newItem("FOO'BAR \x1b[31mBAZ\x1b[m")}

	var result string
	check := func(expected string) {
		if result != expected {
			t.Errorf("expected: %s, actual: %s", expected, result)
		}
	}

	// {}, preserve ansi
	result = replacePlaceholder("echo {}", false, Delimiter{}, "query", items1)
	check("echo '  foo'\\''bar \x1b[31mbaz\x1b[m'")

	// {}, strip ansi
	result = replacePlaceholder("echo {}", true, Delimiter{}, "query", items1)
	check("echo '  foo'\\''bar baz'")

	// {}, with multiple items
	result = replacePlaceholder("echo {}", true, Delimiter{}, "query", items2)
	check("echo 'foo'\\''bar baz' 'FOO'\\''BAR BAZ'")

	// {..}, strip leading whitespaces, preserve ansi
	result = replacePlaceholder("echo {..}", false, Delimiter{}, "query", items1)
	check("echo 'foo'\\''bar \x1b[31mbaz\x1b[m'")

	// {..}, strip leading whitespaces, strip ansi
	result = replacePlaceholder("echo {..}", true, Delimiter{}, "query", items1)
	check("echo 'foo'\\''bar baz'")

	// {q}
	result = replacePlaceholder("echo {} {q}", true, Delimiter{}, "query", items1)
	check("echo '  foo'\\''bar baz' 'query'")

	// {q}, multiple items
	result = replacePlaceholder("echo {}{q}{}", true, Delimiter{}, "query 'string'", items2)
	check("echo 'foo'\\''bar baz' 'FOO'\\''BAR BAZ''query '\\''string'\\''''foo'\\''bar baz' 'FOO'\\''BAR BAZ'")

	result = replacePlaceholder("echo {1}/{2}/{2,1}/{-1}/{-2}/{}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, "query", items1)
	check("echo 'foo'\\''bar'/'baz'/'bazfoo'\\''bar'/'baz'/'foo'\\''bar'/'  foo'\\''bar baz'/'foo'\\''bar baz'/{n.t}/{}/{1}/{q}/''")

	result = replacePlaceholder("echo {1}/{2}/{-1}/{-2}/{..}/{n.t}/\\{}/\\{1}/\\{q}/{3}", true, Delimiter{}, "query", items2)
	check("echo 'foo'\\''bar' 'FOO'\\''BAR'/'baz' 'BAZ'/'baz' 'BAZ'/'foo'\\''bar' 'FOO'\\''BAR'/'foo'\\''bar baz' 'FOO'\\''BAR BAZ'/{n.t}/{}/{1}/{q}/'' ''")

	// String delimiter
	delim := "'"
	result = replacePlaceholder("echo {}/{1}/{2}", true, Delimiter{str: &delim}, "query", items1)
	check("echo '  foo'\\''bar baz'/'foo'/'bar baz'")

	// Regex delimiter
	regex := regexp.MustCompile("[oa]+")
	// foo'bar baz
	result = replacePlaceholder("echo {}/{1}/{3}/{2..3}", true, Delimiter{regex: regex}, "query", items1)
	check("echo '  foo'\\''bar baz'/'f'/'r b'/''\\''bar b'")
}