2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
2015-03-28 17:59:32 +00:00
|
|
|
import (
|
2015-06-14 14:36:49 +00:00
|
|
|
"fmt"
|
2016-10-24 03:45:45 +00:00
|
|
|
"io/ioutil"
|
2015-03-28 17:59:32 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-10-24 00:44:56 +00:00
|
|
|
"github.com/junegunn/fzf/src/tui"
|
2015-03-28 17:59:32 +00:00
|
|
|
)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
func TestDelimiterRegex(t *testing.T) {
|
2015-08-10 09:34:20 +00:00
|
|
|
// Valid regex
|
|
|
|
delim := delimiterRegexp(".")
|
|
|
|
if delim.regex == nil || delim.str != nil {
|
|
|
|
t.Error(delim)
|
|
|
|
}
|
|
|
|
// Broken regex -> string
|
|
|
|
delim = delimiterRegexp("[0-9")
|
|
|
|
if delim.regex != nil || *delim.str != "[0-9" {
|
|
|
|
t.Error(delim)
|
|
|
|
}
|
|
|
|
// Valid regex
|
|
|
|
delim = delimiterRegexp("[0-9]")
|
2015-08-10 14:47:03 +00:00
|
|
|
if delim.regex.String() != "[0-9]" || delim.str != nil {
|
2015-08-10 09:34:20 +00:00
|
|
|
t.Error(delim)
|
|
|
|
}
|
|
|
|
// Tab character
|
|
|
|
delim = delimiterRegexp("\t")
|
|
|
|
if delim.regex != nil || *delim.str != "\t" {
|
|
|
|
t.Error(delim)
|
|
|
|
}
|
|
|
|
// Tab expression
|
|
|
|
delim = delimiterRegexp("\\t")
|
|
|
|
if delim.regex != nil || *delim.str != "\t" {
|
|
|
|
t.Error(delim)
|
|
|
|
}
|
|
|
|
// Tabs -> regex
|
|
|
|
delim = delimiterRegexp("\t+")
|
|
|
|
if delim.regex == nil || delim.str != nil {
|
|
|
|
t.Error(delim)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelimiterRegexString(t *testing.T) {
|
|
|
|
delim := delimiterRegexp("*")
|
2017-07-19 17:44:30 +00:00
|
|
|
tokens := Tokenize("-*--*---**---", delim)
|
2015-08-10 14:47:03 +00:00
|
|
|
if delim.regex != nil ||
|
2016-08-13 15:39:44 +00:00
|
|
|
tokens[0].text.ToString() != "-*" ||
|
|
|
|
tokens[1].text.ToString() != "--*" ||
|
|
|
|
tokens[2].text.ToString() != "---*" ||
|
|
|
|
tokens[3].text.ToString() != "*" ||
|
|
|
|
tokens[4].text.ToString() != "---" {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%s %v %d", delim, tokens, len(tokens))
|
2015-08-10 09:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelimiterRegexRegex(t *testing.T) {
|
|
|
|
delim := delimiterRegexp("--\\*")
|
2017-07-19 17:44:30 +00:00
|
|
|
tokens := Tokenize("-*--*---**---", delim)
|
2015-08-10 09:34:20 +00:00
|
|
|
if delim.str != nil ||
|
2016-08-13 15:39:44 +00:00
|
|
|
tokens[0].text.ToString() != "-*--*" ||
|
|
|
|
tokens[1].text.ToString() != "---*" ||
|
|
|
|
tokens[2].text.ToString() != "*---" {
|
2015-08-10 14:47:03 +00:00
|
|
|
t.Errorf("%s %d", tokens, len(tokens))
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 12:21:06 +00:00
|
|
|
func TestDelimiterRegexRegexCaret(t *testing.T) {
|
|
|
|
delim := delimiterRegexp(`(^\s*|\s+)`)
|
|
|
|
tokens := Tokenize("foo bar baz", delim)
|
|
|
|
if delim.str != nil ||
|
|
|
|
len(tokens) != 4 ||
|
|
|
|
tokens[0].text.ToString() != "" ||
|
|
|
|
tokens[1].text.ToString() != "foo " ||
|
|
|
|
tokens[2].text.ToString() != "bar " ||
|
|
|
|
tokens[3].text.ToString() != "baz" {
|
|
|
|
t.Errorf("%s %d", tokens, len(tokens))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
func TestSplitNth(t *testing.T) {
|
|
|
|
{
|
|
|
|
ranges := splitNth("..")
|
|
|
|
if len(ranges) != 1 ||
|
2015-01-11 18:01:24 +00:00
|
|
|
ranges[0].begin != rangeEllipsis ||
|
|
|
|
ranges[0].end != rangeEllipsis {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", ranges)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2015-01-22 21:26:00 +00:00
|
|
|
ranges := splitNth("..3,1..,2..3,4..-1,-3..-2,..,2,-2,2..-2,1..-1")
|
|
|
|
if len(ranges) != 10 ||
|
2015-01-11 18:01:24 +00:00
|
|
|
ranges[0].begin != rangeEllipsis || ranges[0].end != 3 ||
|
2015-01-22 21:26:00 +00:00
|
|
|
ranges[1].begin != rangeEllipsis || ranges[1].end != rangeEllipsis ||
|
2015-01-01 19:49:30 +00:00
|
|
|
ranges[2].begin != 2 || ranges[2].end != 3 ||
|
2015-01-22 21:26:00 +00:00
|
|
|
ranges[3].begin != 4 || ranges[3].end != rangeEllipsis ||
|
2015-01-01 19:49:30 +00:00
|
|
|
ranges[4].begin != -3 || ranges[4].end != -2 ||
|
2015-01-11 18:01:24 +00:00
|
|
|
ranges[5].begin != rangeEllipsis || ranges[5].end != rangeEllipsis ||
|
2015-01-01 19:49:30 +00:00
|
|
|
ranges[6].begin != 2 || ranges[6].end != 2 ||
|
2015-01-22 21:26:00 +00:00
|
|
|
ranges[7].begin != -2 || ranges[7].end != -2 ||
|
|
|
|
ranges[8].begin != 2 || ranges[8].end != -2 ||
|
|
|
|
ranges[9].begin != rangeEllipsis || ranges[9].end != rangeEllipsis {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("%v", ranges)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 21:26:00 +00:00
|
|
|
|
|
|
|
func TestIrrelevantNth(t *testing.T) {
|
|
|
|
{
|
|
|
|
opts := defaultOptions()
|
|
|
|
words := []string{"--nth", "..", "-x"}
|
|
|
|
parseOptions(opts, words)
|
2016-02-17 16:46:18 +00:00
|
|
|
postProcessOptions(opts)
|
2015-01-22 21:26:00 +00:00
|
|
|
if len(opts.Nth) != 0 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("nth should be empty: %v", opts.Nth)
|
2015-01-22 21:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-28 09:28:21 +00:00
|
|
|
for _, words := range [][]string{{"--nth", "..,3", "+x"}, {"--nth", "3,1..", "+x"}, {"--nth", "..-1,1", "+x"}} {
|
2015-01-22 21:26:00 +00:00
|
|
|
{
|
|
|
|
opts := defaultOptions()
|
|
|
|
parseOptions(opts, words)
|
2016-02-17 16:46:18 +00:00
|
|
|
postProcessOptions(opts)
|
2015-01-22 21:26:00 +00:00
|
|
|
if len(opts.Nth) != 0 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("nth should be empty: %v", opts.Nth)
|
2015-01-22 21:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
opts := defaultOptions()
|
|
|
|
words = append(words, "-x")
|
|
|
|
parseOptions(opts, words)
|
2016-02-17 16:46:18 +00:00
|
|
|
postProcessOptions(opts)
|
2015-01-22 21:26:00 +00:00
|
|
|
if len(opts.Nth) != 2 {
|
2018-02-17 22:01:06 +00:00
|
|
|
t.Errorf("nth should not be empty: %v", opts.Nth)
|
2015-01-22 21:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-28 17:59:32 +00:00
|
|
|
|
2015-03-31 13:05:02 +00:00
|
|
|
func TestParseKeys(t *testing.T) {
|
2017-04-27 17:36:36 +00:00
|
|
|
pairs := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g,ctrl-alt-a,ALT-enter,alt-SPACE", "")
|
2020-12-29 16:59:18 +00:00
|
|
|
checkEvent := func(e tui.Event, s string) {
|
|
|
|
if pairs[e] != s {
|
|
|
|
t.Errorf("%s != %s", pairs[e], s)
|
2015-03-28 17:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 16:59:18 +00:00
|
|
|
check := func(et tui.EventType, s string) {
|
|
|
|
checkEvent(et.AsEvent(), s)
|
|
|
|
}
|
2017-04-27 17:36:36 +00:00
|
|
|
if len(pairs) != 12 {
|
|
|
|
t.Error(12)
|
2015-06-18 15:31:48 +00:00
|
|
|
}
|
2016-10-24 00:44:56 +00:00
|
|
|
check(tui.CtrlZ, "ctrl-z")
|
|
|
|
check(tui.F2, "f2")
|
2020-12-29 16:59:18 +00:00
|
|
|
check(tui.CtrlG, "ctrl-G")
|
|
|
|
checkEvent(tui.AltKey('z'), "alt-z")
|
|
|
|
checkEvent(tui.Key('@'), "@")
|
|
|
|
checkEvent(tui.AltKey('a'), "Alt-a")
|
|
|
|
checkEvent(tui.Key('!'), "!")
|
|
|
|
checkEvent(tui.Key('J'), "J")
|
|
|
|
checkEvent(tui.Key('g'), "g")
|
|
|
|
checkEvent(tui.CtrlAltKey('a'), "ctrl-alt-a")
|
|
|
|
checkEvent(tui.CtrlAltKey('m'), "ALT-enter")
|
|
|
|
checkEvent(tui.AltKey(' '), "alt-SPACE")
|
2015-06-14 16:26:18 +00:00
|
|
|
|
|
|
|
// Synonyms
|
2015-06-18 15:31:48 +00:00
|
|
|
pairs = parseKeyChords("enter,Return,space,tab,btab,esc,up,down,left,right", "")
|
|
|
|
if len(pairs) != 9 {
|
|
|
|
t.Error(9)
|
|
|
|
}
|
2016-10-24 00:44:56 +00:00
|
|
|
check(tui.CtrlM, "Return")
|
2020-12-29 16:59:18 +00:00
|
|
|
checkEvent(tui.Key(' '), "space")
|
2016-10-24 00:44:56 +00:00
|
|
|
check(tui.Tab, "tab")
|
|
|
|
check(tui.BTab, "btab")
|
|
|
|
check(tui.ESC, "esc")
|
|
|
|
check(tui.Up, "up")
|
|
|
|
check(tui.Down, "down")
|
|
|
|
check(tui.Left, "left")
|
|
|
|
check(tui.Right, "right")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords("Tab,Ctrl-I,PgUp,page-up,pgdn,Page-Down,Home,End,Alt-BS,Alt-BSpace,shift-left,shift-right,btab,shift-tab,return,Enter,bspace", "")
|
|
|
|
if len(pairs) != 11 {
|
|
|
|
t.Error(11)
|
|
|
|
}
|
2016-10-24 00:44:56 +00:00
|
|
|
check(tui.Tab, "Ctrl-I")
|
|
|
|
check(tui.PgUp, "page-up")
|
|
|
|
check(tui.PgDn, "Page-Down")
|
|
|
|
check(tui.Home, "Home")
|
|
|
|
check(tui.End, "End")
|
|
|
|
check(tui.AltBS, "Alt-BSpace")
|
|
|
|
check(tui.SLeft, "shift-left")
|
|
|
|
check(tui.SRight, "shift-right")
|
|
|
|
check(tui.BTab, "shift-tab")
|
|
|
|
check(tui.CtrlM, "Enter")
|
|
|
|
check(tui.BSpace, "bspace")
|
2015-03-28 17:59:32 +00:00
|
|
|
}
|
2015-03-31 13:05:02 +00:00
|
|
|
|
|
|
|
func TestParseKeysWithComma(t *testing.T) {
|
2015-06-18 15:31:48 +00:00
|
|
|
checkN := func(a int, b int) {
|
|
|
|
if a != b {
|
|
|
|
t.Errorf("%d != %d", a, b)
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 16:59:18 +00:00
|
|
|
check := func(pairs map[tui.Event]string, e tui.Event, s string) {
|
|
|
|
if pairs[e] != s {
|
|
|
|
t.Errorf("%s != %s", pairs[e], s)
|
2015-03-31 13:05:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 15:31:48 +00:00
|
|
|
pairs := parseKeyChords(",", "")
|
|
|
|
checkN(len(pairs), 1)
|
2020-12-29 16:59:18 +00:00
|
|
|
check(pairs, tui.Key(','), ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords(",,a,b", "")
|
|
|
|
checkN(len(pairs), 3)
|
2020-12-29 16:59:18 +00:00
|
|
|
check(pairs, tui.Key('a'), "a")
|
|
|
|
check(pairs, tui.Key('b'), "b")
|
|
|
|
check(pairs, tui.Key(','), ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords("a,b,,", "")
|
|
|
|
checkN(len(pairs), 3)
|
2020-12-29 16:59:18 +00:00
|
|
|
check(pairs, tui.Key('a'), "a")
|
|
|
|
check(pairs, tui.Key('b'), "b")
|
|
|
|
check(pairs, tui.Key(','), ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords("a,,,b", "")
|
|
|
|
checkN(len(pairs), 3)
|
2020-12-29 16:59:18 +00:00
|
|
|
check(pairs, tui.Key('a'), "a")
|
|
|
|
check(pairs, tui.Key('b'), "b")
|
|
|
|
check(pairs, tui.Key(','), ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords("a,,,b,c", "")
|
|
|
|
checkN(len(pairs), 4)
|
2020-12-29 16:59:18 +00:00
|
|
|
check(pairs, tui.Key('a'), "a")
|
|
|
|
check(pairs, tui.Key('b'), "b")
|
|
|
|
check(pairs, tui.Key('c'), "c")
|
|
|
|
check(pairs, tui.Key(','), ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords(",,,", "")
|
|
|
|
checkN(len(pairs), 1)
|
2020-12-29 16:59:18 +00:00
|
|
|
check(pairs, tui.Key(','), ",")
|
2020-12-30 18:37:25 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords(",ALT-,,", "")
|
|
|
|
checkN(len(pairs), 1)
|
|
|
|
check(pairs, tui.AltKey(','), "ALT-,")
|
2015-03-31 13:05:02 +00:00
|
|
|
}
|
2015-05-20 12:25:15 +00:00
|
|
|
|
|
|
|
func TestBind(t *testing.T) {
|
2017-01-21 17:32:49 +00:00
|
|
|
keymap := defaultKeymap()
|
2020-12-29 16:59:18 +00:00
|
|
|
check := func(event tui.Event, arg1 string, types ...actionType) {
|
|
|
|
if len(keymap[event]) != len(types) {
|
|
|
|
t.Errorf("invalid number of actions for %v (%d != %d)",
|
|
|
|
event, len(types), len(keymap[event]))
|
2017-01-21 17:32:49 +00:00
|
|
|
return
|
2015-05-20 12:25:15 +00:00
|
|
|
}
|
2020-12-29 16:59:18 +00:00
|
|
|
for idx, action := range keymap[event] {
|
2017-01-21 17:32:49 +00:00
|
|
|
if types[idx] != action.t {
|
|
|
|
t.Errorf("invalid action type (%d != %d)", types[idx], action.t)
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 16:59:18 +00:00
|
|
|
if len(arg1) > 0 && keymap[event][0].a != arg1 {
|
|
|
|
t.Errorf("invalid action argument: (%s != %s)", arg1, keymap[event][0].a)
|
2015-06-14 03:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 16:59:18 +00:00
|
|
|
check(tui.CtrlA.AsEvent(), "", actBeginningOfLine)
|
2017-01-21 17:32:49 +00:00
|
|
|
parseKeymap(keymap,
|
|
|
|
"ctrl-a:kill-line,ctrl-b:toggle-sort+up+down,c:page-up,alt-z:page-down,"+
|
2019-11-21 14:06:13 +00:00
|
|
|
"f1:execute(ls {+})+abort+execute(echo {+})+select-all,f2:execute/echo {}, {}, {}/,f3:execute[echo '({})'],f4:execute;less {};,"+
|
2017-01-21 17:32:49 +00:00
|
|
|
"alt-a:execute-Multi@echo (,),[,],/,:,;,%,{}@,alt-b:execute;echo (,),[,],/,:,@,%,{};,"+
|
|
|
|
"x:Execute(foo+bar),X:execute/bar+baz/"+
|
2020-12-29 16:51:25 +00:00
|
|
|
",f1:+first,f1:+top"+
|
2017-01-21 17:32:49 +00:00
|
|
|
",,:abort,::accept,+:execute:++\nfoobar,Y:execute(baz)+up")
|
2020-12-29 16:59:18 +00:00
|
|
|
check(tui.CtrlA.AsEvent(), "", actKillLine)
|
|
|
|
check(tui.CtrlB.AsEvent(), "", actToggleSort, actUp, actDown)
|
|
|
|
check(tui.Key('c'), "", actPageUp)
|
|
|
|
check(tui.Key(','), "", actAbort)
|
|
|
|
check(tui.Key(':'), "", actAccept)
|
|
|
|
check(tui.AltKey('z'), "", actPageDown)
|
|
|
|
check(tui.F1.AsEvent(), "ls {+}", actExecute, actAbort, actExecute, actSelectAll, actFirst, actFirst)
|
|
|
|
check(tui.F2.AsEvent(), "echo {}, {}, {}", actExecute)
|
|
|
|
check(tui.F3.AsEvent(), "echo '({})'", actExecute)
|
|
|
|
check(tui.F4.AsEvent(), "less {}", actExecute)
|
|
|
|
check(tui.Key('x'), "foo+bar", actExecute)
|
|
|
|
check(tui.Key('X'), "bar+baz", actExecute)
|
|
|
|
check(tui.AltKey('a'), "echo (,),[,],/,:,;,%,{}", actExecuteMulti)
|
|
|
|
check(tui.AltKey('b'), "echo (,),[,],/,:,@,%,{}", actExecute)
|
|
|
|
check(tui.Key('+'), "++\nfoobar,Y:execute(baz)+up", actExecute)
|
2015-06-14 03:25:08 +00:00
|
|
|
|
2015-06-15 14:25:00 +00:00
|
|
|
for idx, char := range []rune{'~', '!', '@', '#', '$', '%', '^', '&', '*', '|', ';', '/'} {
|
2017-01-21 17:32:49 +00:00
|
|
|
parseKeymap(keymap, fmt.Sprintf("%d:execute%cfoobar%c", idx%10, char, char))
|
2020-12-29 16:59:18 +00:00
|
|
|
check(tui.Key([]rune(fmt.Sprintf("%d", idx%10))[0]), "foobar", actExecute)
|
2015-06-14 14:36:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 17:32:49 +00:00
|
|
|
parseKeymap(keymap, "f1:abort")
|
2020-12-29 16:59:18 +00:00
|
|
|
check(tui.F1.AsEvent(), "", actAbort)
|
2015-05-20 12:25:15 +00:00
|
|
|
}
|
2015-05-31 07:46:54 +00:00
|
|
|
|
|
|
|
func TestColorSpec(t *testing.T) {
|
2016-10-24 00:44:56 +00:00
|
|
|
theme := tui.Dark256
|
2015-05-31 07:46:54 +00:00
|
|
|
dark := parseTheme(theme, "dark")
|
|
|
|
if *dark != *theme {
|
|
|
|
t.Errorf("colors should be equivalent")
|
|
|
|
}
|
|
|
|
if dark == theme {
|
|
|
|
t.Errorf("point should not be equivalent")
|
|
|
|
}
|
|
|
|
|
|
|
|
light := parseTheme(theme, "dark,light")
|
|
|
|
if *light == *theme {
|
|
|
|
t.Errorf("should not be equivalent")
|
|
|
|
}
|
2016-10-24 00:44:56 +00:00
|
|
|
if *light != *tui.Light256 {
|
2015-05-31 07:46:54 +00:00
|
|
|
t.Errorf("colors should be equivalent")
|
|
|
|
}
|
|
|
|
if light == theme {
|
|
|
|
t.Errorf("point should not be equivalent")
|
|
|
|
}
|
|
|
|
|
|
|
|
customized := parseTheme(theme, "fg:231,bg:232")
|
2020-10-25 10:29:37 +00:00
|
|
|
if customized.Fg.Color != 231 || customized.Bg.Color != 232 {
|
2015-05-31 07:46:54 +00:00
|
|
|
t.Errorf("color not customized")
|
|
|
|
}
|
2016-10-24 00:44:56 +00:00
|
|
|
if *tui.Dark256 == *customized {
|
2015-05-31 07:46:54 +00:00
|
|
|
t.Errorf("colors should not be equivalent")
|
|
|
|
}
|
2016-10-24 00:44:56 +00:00
|
|
|
customized.Fg = tui.Dark256.Fg
|
|
|
|
customized.Bg = tui.Dark256.Bg
|
|
|
|
if *tui.Dark256 != *customized {
|
|
|
|
t.Errorf("colors should now be equivalent: %v, %v", tui.Dark256, customized)
|
2015-05-31 07:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
customized = parseTheme(theme, "fg:231,dark,bg:232")
|
2016-10-24 00:44:56 +00:00
|
|
|
if customized.Fg != tui.Dark256.Fg || customized.Bg == tui.Dark256.Bg {
|
2015-05-31 07:46:54 +00:00
|
|
|
t.Errorf("color not customized")
|
|
|
|
}
|
|
|
|
}
|
2015-10-09 03:16:47 +00:00
|
|
|
|
2016-02-17 16:46:18 +00:00
|
|
|
func TestDefaultCtrlNP(t *testing.T) {
|
2020-12-29 16:59:18 +00:00
|
|
|
check := func(words []string, et tui.EventType, expected actionType) {
|
|
|
|
e := et.AsEvent()
|
2016-02-17 16:46:18 +00:00
|
|
|
opts := defaultOptions()
|
|
|
|
parseOptions(opts, words)
|
|
|
|
postProcessOptions(opts)
|
2020-12-29 16:59:18 +00:00
|
|
|
if opts.Keymap[e][0].t != expected {
|
2016-02-17 16:46:18 +00:00
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
}
|
2016-10-24 00:44:56 +00:00
|
|
|
check([]string{}, tui.CtrlN, actDown)
|
|
|
|
check([]string{}, tui.CtrlP, actUp)
|
2016-02-17 16:46:18 +00:00
|
|
|
|
2016-10-24 00:44:56 +00:00
|
|
|
check([]string{"--bind=ctrl-n:accept"}, tui.CtrlN, actAccept)
|
|
|
|
check([]string{"--bind=ctrl-p:accept"}, tui.CtrlP, actAccept)
|
2016-02-17 16:46:18 +00:00
|
|
|
|
2016-10-24 03:45:45 +00:00
|
|
|
f, _ := ioutil.TempFile("", "fzf-history")
|
|
|
|
f.Close()
|
|
|
|
hist := "--history=" + f.Name()
|
2016-10-24 00:44:56 +00:00
|
|
|
check([]string{hist}, tui.CtrlN, actNextHistory)
|
|
|
|
check([]string{hist}, tui.CtrlP, actPreviousHistory)
|
2016-02-17 16:46:18 +00:00
|
|
|
|
2016-10-24 00:44:56 +00:00
|
|
|
check([]string{hist, "--bind=ctrl-n:accept"}, tui.CtrlN, actAccept)
|
|
|
|
check([]string{hist, "--bind=ctrl-n:accept"}, tui.CtrlP, actPreviousHistory)
|
2016-02-17 16:46:18 +00:00
|
|
|
|
2016-10-24 00:44:56 +00:00
|
|
|
check([]string{hist, "--bind=ctrl-p:accept"}, tui.CtrlN, actNextHistory)
|
|
|
|
check([]string{hist, "--bind=ctrl-p:accept"}, tui.CtrlP, actAccept)
|
2016-02-17 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:59 +00:00
|
|
|
func optsFor(words ...string) *Options {
|
|
|
|
opts := defaultOptions()
|
|
|
|
parseOptions(opts, words)
|
|
|
|
postProcessOptions(opts)
|
|
|
|
return opts
|
|
|
|
}
|
2016-02-17 16:46:18 +00:00
|
|
|
|
2016-08-11 16:16:59 +00:00
|
|
|
func TestToggle(t *testing.T) {
|
2016-02-17 16:46:18 +00:00
|
|
|
opts := optsFor()
|
|
|
|
if opts.ToggleSort {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = optsFor("--bind=a:toggle-sort")
|
|
|
|
if !opts.ToggleSort {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = optsFor("--bind=a:toggle-sort", "--bind=a:up")
|
|
|
|
if opts.ToggleSort {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
}
|
2016-08-11 16:16:59 +00:00
|
|
|
|
|
|
|
func TestPreviewOpts(t *testing.T) {
|
|
|
|
opts := optsFor()
|
|
|
|
if !(opts.Preview.command == "" &&
|
|
|
|
opts.Preview.hidden == false &&
|
2016-12-04 17:13:47 +00:00
|
|
|
opts.Preview.wrap == false &&
|
2016-08-11 16:16:59 +00:00
|
|
|
opts.Preview.position == posRight &&
|
|
|
|
opts.Preview.size.percent == true &&
|
|
|
|
opts.Preview.size.size == 50) {
|
|
|
|
t.Error()
|
|
|
|
}
|
2021-04-06 11:10:55 +00:00
|
|
|
opts = optsFor("--preview", "cat {}", "--preview-window=left:15,hidden,wrap:+{1}-/2")
|
2016-08-11 16:16:59 +00:00
|
|
|
if !(opts.Preview.command == "cat {}" &&
|
|
|
|
opts.Preview.hidden == true &&
|
2016-12-04 17:13:47 +00:00
|
|
|
opts.Preview.wrap == true &&
|
2016-08-11 16:16:59 +00:00
|
|
|
opts.Preview.position == posLeft &&
|
2021-03-12 17:24:37 +00:00
|
|
|
opts.Preview.scroll == "+{1}-/2" &&
|
2016-08-11 16:16:59 +00:00
|
|
|
opts.Preview.size.percent == false &&
|
2020-10-06 10:37:33 +00:00
|
|
|
opts.Preview.size.size == 15) {
|
2016-08-11 16:16:59 +00:00
|
|
|
t.Error(opts.Preview)
|
|
|
|
}
|
2021-04-06 11:10:55 +00:00
|
|
|
opts = optsFor("--preview-window=up,15,wrap,hidden,+{1}+3-1-2/2", "--preview-window=down", "--preview-window=cycle")
|
2016-08-11 16:16:59 +00:00
|
|
|
if !(opts.Preview.command == "" &&
|
2020-10-09 12:56:16 +00:00
|
|
|
opts.Preview.hidden == true &&
|
2020-10-06 10:37:33 +00:00
|
|
|
opts.Preview.wrap == true &&
|
|
|
|
opts.Preview.cycle == true &&
|
2016-08-11 16:16:59 +00:00
|
|
|
opts.Preview.position == posDown &&
|
2021-03-12 17:24:37 +00:00
|
|
|
opts.Preview.scroll == "+{1}+3-1-2/2" &&
|
2020-10-06 10:37:33 +00:00
|
|
|
opts.Preview.size.percent == false &&
|
|
|
|
opts.Preview.size.size == 15) {
|
|
|
|
t.Error(opts.Preview.size.size)
|
2016-08-11 16:16:59 +00:00
|
|
|
}
|
2016-12-04 17:13:47 +00:00
|
|
|
opts = optsFor("--preview-window=up:15:wrap:hidden")
|
|
|
|
if !(opts.Preview.command == "" &&
|
|
|
|
opts.Preview.hidden == true &&
|
|
|
|
opts.Preview.wrap == true &&
|
|
|
|
opts.Preview.position == posUp &&
|
|
|
|
opts.Preview.size.percent == false &&
|
2020-10-06 10:37:33 +00:00
|
|
|
opts.Preview.size.size == 15) {
|
2016-12-04 17:13:47 +00:00
|
|
|
t.Error(opts.Preview)
|
|
|
|
}
|
2020-10-10 16:51:39 +00:00
|
|
|
opts = optsFor("--preview=foo", "--preview-window=up", "--preview-window=default:70%")
|
|
|
|
if !(opts.Preview.command == "foo" &&
|
|
|
|
opts.Preview.position == posRight &&
|
|
|
|
opts.Preview.size.percent == true &&
|
|
|
|
opts.Preview.size.size == 70) {
|
|
|
|
t.Error(opts.Preview)
|
|
|
|
}
|
2016-08-11 16:16:59 +00:00
|
|
|
}
|
2017-08-26 17:19:39 +00:00
|
|
|
|
|
|
|
func TestAdditiveExpect(t *testing.T) {
|
|
|
|
opts := optsFor("--expect=a", "--expect", "b", "--expect=c")
|
|
|
|
if len(opts.Expect) != 3 {
|
|
|
|
t.Error(opts.Expect)
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 01:19:03 +00:00
|
|
|
|
|
|
|
func TestValidateSign(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
inputSign string
|
|
|
|
isValid bool
|
|
|
|
}{
|
|
|
|
{"> ", true},
|
|
|
|
{"아", true},
|
|
|
|
{"😀", true},
|
|
|
|
{"", false},
|
|
|
|
{">>>", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
err := validateSign(testCase.inputSign, "")
|
|
|
|
if testCase.isValid && err != nil {
|
|
|
|
t.Errorf("Input sign `%s` caused error", testCase.inputSign)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCase.isValid && err == nil {
|
|
|
|
t.Errorf("Input sign `%s` did not cause error", testCase.inputSign)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|