summaryrefslogtreecommitdiffstats
path: root/src/options_test.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2020-12-30 01:59:18 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2020-12-30 18:39:17 +0900
commit7f8e0dbc408eff786865d0e2d9e3c62ec3ed4776 (patch)
treedeeac2dc561a2ccd5bf444f4180abb43bff060be /src/options_test.go
parent0de7ab18f64db7838c8ca06d08188f976cbfdbb4 (diff)
Extend support for alt key chords
"alt-" with any case-sensitive character is allowed
Diffstat (limited to 'src/options_test.go')
-rw-r--r--src/options_test.go121
1 files changed, 63 insertions, 58 deletions
diff --git a/src/options_test.go b/src/options_test.go
index 5cc11c85..4311aa4d 100644
--- a/src/options_test.go
+++ b/src/options_test.go
@@ -125,26 +125,29 @@ func TestIrrelevantNth(t *testing.T) {
func TestParseKeys(t *testing.T) {
pairs := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g,ctrl-alt-a,ALT-enter,alt-SPACE", "")
- check := func(i int, s string) {
- if pairs[i] != s {
- t.Errorf("%s != %s", pairs[i], s)
+ checkEvent := func(e tui.Event, s string) {
+ if pairs[e] != s {
+ t.Errorf("%s != %s", pairs[e], s)
}
}
+ check := func(et tui.EventType, s string) {
+ checkEvent(et.AsEvent(), s)
+ }
if len(pairs) != 12 {
t.Error(12)
}
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")
- check(tui.CtrlAltA, "ctrl-alt-a")
- check(tui.CtrlAltM, "ALT-enter")
- check(tui.AltSpace, "alt-SPACE")
+ 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")
// Synonyms
pairs = parseKeyChords("enter,Return,space,tab,btab,esc,up,down,left,right", "")
@@ -152,7 +155,7 @@ func TestParseKeys(t *testing.T) {
t.Error(9)
}
check(tui.CtrlM, "Return")
- check(tui.AltZ+' ', "space")
+ checkEvent(tui.Key(' '), "space")
check(tui.Tab, "tab")
check(tui.BTab, "btab")
check(tui.ESC, "esc")
@@ -184,63 +187,64 @@ func TestParseKeysWithComma(t *testing.T) {
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)
+ check := func(pairs map[tui.Event]string, e tui.Event, s string) {
+ if pairs[e] != s {
+ t.Errorf("%s != %s", pairs[e], s)
}
}
pairs := parseKeyChords(",", "")
checkN(len(pairs), 1)
- check(pairs, tui.AltZ+',', ",")
+ check(pairs, tui.Key(','), ",")
pairs = parseKeyChords(",,a,b", "")
checkN(len(pairs), 3)
- check(pairs, tui.AltZ+'a', "a")
- check(pairs, tui.AltZ+'b', "b")
- check(pairs, tui.AltZ+',', ",")
+ check(pairs, tui.Key('a'), "a")
+ check(pairs, tui.Key('b'), "b")
+ check(pairs, tui.Key(','), ",")
pairs = parseKeyChords("a,b,,", "")
checkN(len(pairs), 3)
- check(pairs, tui.AltZ+'a', "a")
- check(pairs, tui.AltZ+'b', "b")
- check(pairs, tui.AltZ+',', ",")
+ check(pairs, tui.Key('a'), "a")
+ check(pairs, tui.Key('b'), "b")
+ check(pairs, tui.Key(','), ",")
pairs = parseKeyChords("a,,,b", "")
checkN(len(pairs), 3)
- check(pairs, tui.AltZ+'a', "a")
- check(pairs, tui.AltZ+'b', "b")
- check(pairs, tui.AltZ+',', ",")
+ check(pairs, tui.Key('a'), "a")
+ check(pairs, tui.Key('b'), "b")
+ check(pairs, tui.Key(','), ",")
pairs = parseKeyChords("a,,,b,c", "")
checkN(len(pairs), 4)
- check(pairs, tui.AltZ+'a', "a")
- check(pairs, tui.AltZ+'b', "b")
- check(pairs, tui.AltZ+'c', "c")
- check(pairs, tui.AltZ+',', ",")
+ check(pairs, tui.Key('a'), "a")
+ check(pairs, tui.Key('b'), "b")
+ check(pairs, tui.Key('c'), "c")
+ check(pairs, tui.Key(','), ",")
pairs = parseKeyChords(",,,", "")
checkN(len(pairs), 1)
- check(pairs, tui.AltZ+',', ",")
+ check(pairs, tui.Key(','), ",")
}
func TestBind(t *testing.T) {
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]))
+ 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]))
return
}
- for idx, action := range keymap[keyName] {
+ for idx, action := range keymap[event] {
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)
+ if len(arg1) > 0 && keymap[event][0].a != arg1 {
+ t.Errorf("invalid action argument: (%s != %s)", arg1, keymap[event][0].a)
}
}
- check(tui.CtrlA, "", actBeginningOfLine)
+ check(tui.CtrlA.AsEvent(), "", actBeginningOfLine)
parseKeymap(keymap,
"ctrl-a:kill-line,ctrl-b:toggle-sort+up+down,c:page-up,alt-z:page-down,"+
"f1:execute(ls {+})+abort+execute(echo {+})+select-all,f2:execute/echo {}, {}, {}/,f3:execute[echo '({})'],f4:execute;less {};,"+
@@ -248,29 +252,29 @@ func TestBind(t *testing.T) {
"x:Execute(foo+bar),X:execute/bar+baz/"+
",f1:+first,f1:+top"+
",,: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)
- check(tui.F1, "ls {+}", actExecute, actAbort, actExecute, actSelectAll, actFirst, actFirst)
- 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)
+ 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)
for idx, char := range []rune{'~', '!', '@', '#', '$', '%', '^', '&', '*', '|', ';', '/'} {
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)
+ check(tui.Key([]rune(fmt.Sprintf("%d", idx%10))[0]), "foobar", actExecute)
}
parseKeymap(keymap, "f1:abort")
- check(tui.F1, "", actAbort)
+ check(tui.F1.AsEvent(), "", actAbort)
}
func TestColorSpec(t *testing.T) {
@@ -314,11 +318,12 @@ func TestColorSpec(t *testing.T) {
}
func TestDefaultCtrlNP(t *testing.T) {
- check := func(words []string, key int, expected actionType) {
+ check := func(words []string, et tui.EventType, expected actionType) {
+ e := et.AsEvent()
opts := defaultOptions()
parseOptions(opts, words)
postProcessOptions(opts)
- if opts.Keymap[key][0].t != expected {
+ if opts.Keymap[e][0].t != expected {
t.Error()
}
}