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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2015-11-03 13:49:32 +00:00
|
|
|
for _, words := range [][]string{[]string{"--nth", "..,3", "+x"}, []string{"--nth", "3,1..", "+x"}, []string{"--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", "")
|
2015-06-18 15:31:48 +00:00
|
|
|
check := func(i int, s string) {
|
|
|
|
if pairs[i] != s {
|
|
|
|
t.Errorf("%s != %s", pairs[i], s)
|
2015-03-28 17:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
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.AltZ, "alt-z")
|
|
|
|
check(tui.F2, "f2")
|
|
|
|
check(tui.AltZ+'@', "@")
|
|
|
|
check(tui.AltA, "Alt-a")
|
|
|
|
check(tui.AltZ+'!', "!")
|
|
|
|
check(tui.CtrlA+'g'-'a', "ctrl-G")
|
|
|
|
check(tui.AltZ+'J', "J")
|
|
|
|
check(tui.AltZ+'g', "g")
|
2017-04-27 17:36:36 +00:00
|
|
|
check(tui.CtrlAltA, "ctrl-alt-a")
|
|
|
|
check(tui.CtrlAltM, "ALT-enter")
|
2016-10-24 00:44:56 +00:00
|
|
|
check(tui.AltSpace, "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")
|
|
|
|
check(tui.AltZ+' ', "space")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check := func(pairs map[int]string, i int, s string) {
|
|
|
|
if pairs[i] != s {
|
|
|
|
t.Errorf("%s != %s", pairs[i], s)
|
2015-03-31 13:05:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 15:31:48 +00:00
|
|
|
pairs := parseKeyChords(",", "")
|
|
|
|
checkN(len(pairs), 1)
|
2016-10-24 00:44:56 +00:00
|
|
|
check(pairs, tui.AltZ+',', ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords(",,a,b", "")
|
|
|
|
checkN(len(pairs), 3)
|
2016-10-24 00:44:56 +00:00
|
|
|
check(pairs, tui.AltZ+'a', "a")
|
|
|
|
check(pairs, tui.AltZ+'b', "b")
|
|
|
|
check(pairs, tui.AltZ+',', ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords("a,b,,", "")
|
|
|
|
checkN(len(pairs), 3)
|
2016-10-24 00:44:56 +00:00
|
|
|
check(pairs, tui.AltZ+'a', "a")
|
|
|
|
check(pairs, tui.AltZ+'b', "b")
|
|
|
|
check(pairs, tui.AltZ+',', ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords("a,,,b", "")
|
|
|
|
checkN(len(pairs), 3)
|
2016-10-24 00:44:56 +00:00
|
|
|
check(pairs, tui.AltZ+'a', "a")
|
|
|
|
check(pairs, tui.AltZ+'b', "b")
|
|
|
|
check(pairs, tui.AltZ+',', ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords("a,,,b,c", "")
|
|
|
|
checkN(len(pairs), 4)
|
2016-10-24 00:44:56 +00:00
|
|
|
check(pairs, tui.AltZ+'a', "a")
|
|
|
|
check(pairs, tui.AltZ+'b', "b")
|
|
|
|
check(pairs, tui.AltZ+'c', "c")
|
|
|
|
check(pairs, tui.AltZ+',', ",")
|
2015-06-18 15:31:48 +00:00
|
|
|
|
|
|
|
pairs = parseKeyChords(",,,", "")
|
|
|
|
checkN(len(pairs), 1)
|
2016-10-24 00:44:56 +00:00
|
|
|
check(pairs, tui.AltZ+',', ",")
|
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()
|
|
|
|
check := func(keyName int, arg1 string, types ...actionType) {
|
|
|
|
if len(keymap[keyName]) != len(types) {
|
|
|
|
t.Errorf("invalid number of actions (%d != %d)", len(types), len(keymap[keyName]))
|
|
|
|
return
|
2015-05-20 12:25:15 +00:00
|
|
|
}
|
2017-01-21 17:32:49 +00:00
|
|
|
for idx, action := range keymap[keyName] {
|
|
|
|
if types[idx] != action.t {
|
|
|
|
t.Errorf("invalid action type (%d != %d)", types[idx], action.t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(arg1) > 0 && keymap[keyName][0].a != arg1 {
|
|
|
|
t.Errorf("invalid action argument: (%s != %s)", arg1, keymap[keyName][0].a)
|
2015-06-14 03:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-21 17:32:49 +00:00
|
|
|
check(tui.CtrlA, "", actBeginningOfLine)
|
|
|
|
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/"+
|
2019-11-21 14:06:14 +00:00
|
|
|
",f1:+top,f1:+top"+
|
2017-01-21 17:32:49 +00:00
|
|
|
",,:abort,::accept,+:execute:++\nfoobar,Y:execute(baz)+up")
|
|
|
|
check(tui.CtrlA, "", actKillLine)
|
|
|
|
check(tui.CtrlB, "", actToggleSort, actUp, actDown)
|
|
|
|
check(tui.AltZ+'c', "", actPageUp)
|
|
|
|
check(tui.AltZ+',', "", actAbort)
|
|
|
|
check(tui.AltZ+':', "", actAccept)
|
|
|
|
check(tui.AltZ, "", actPageDown)
|
2019-11-21 14:06:14 +00:00
|
|
|
check(tui.F1, "ls {+}", actExecute, actAbort, actExecute, actSelectAll, actTop, actTop)
|
2017-01-21 17:32:49 +00:00
|
|
|
check(tui.F2, "echo {}, {}, {}", actExecute)
|
|
|
|
check(tui.F3, "echo '({})'", actExecute)
|
|
|
|
check(tui.F4, "less {}", actExecute)
|
|
|
|
check(tui.AltZ+'x', "foo+bar", actExecute)
|
|
|
|
check(tui.AltZ+'X', "bar+baz", actExecute)
|
|
|
|
check(tui.AltA, "echo (,),[,],/,:,;,%,{}", actExecuteMulti)
|
|
|
|
check(tui.AltB, "echo (,),[,],/,:,@,%,{}", actExecute)
|
|
|
|
check(tui.AltZ+'+', "++\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))
|
|
|
|
check(tui.AltZ+int([]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")
|
|
|
|
check(tui.F1, "", 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) {
|
|
|
|
check := func(words []string, key int, expected actionType) {
|
|
|
|
opts := defaultOptions()
|
|
|
|
parseOptions(opts, words)
|
|
|
|
postProcessOptions(opts)
|
2017-01-21 17:32:49 +00:00
|
|
|
if opts.Keymap[key][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()
|
|
|
|
}
|
2020-10-09 10:53:51 +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 &&
|
2020-10-09 10:53:51 +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)
|
|
|
|
}
|
2020-10-09 10:53:51 +00:00
|
|
|
opts = optsFor("--preview-window=up:15:wrap:hidden:+{1}-/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 &&
|
2020-10-09 12:56:16 +00:00
|
|
|
opts.Preview.scroll == "{1}-/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},
|
|
|
|
{"\n", false},
|
|
|
|
{"\t", 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|