251 lines
7.1 KiB
Go
251 lines
7.1 KiB
Go
package keybind
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSplit(t *testing.T) {
|
|
cases := map[string][2]string{
|
|
"ctrl+a=new_tab": {"ctrl+a", "new_tab"},
|
|
"==text:=": {"=", "text:="},
|
|
"ctrl+==text:=hello": {"ctrl+=", "text:=hello"},
|
|
"=+ctrl=text:=hello": {"=+ctrl", "text:=hello"},
|
|
"super+shift+,=reload": {"super+shift+,", "reload"},
|
|
"ctrl+a>n=new_window": {"ctrl+a>n", "new_window"},
|
|
"§=goto_split:next": {"§", "goto_split:next"},
|
|
"super++=increase": {"super++", "increase"},
|
|
}
|
|
for in, want := range cases {
|
|
tr, ac := Split(in)
|
|
if tr != want[0] || ac != want[1] {
|
|
t.Errorf("Split(%q) = %q, %q", in, tr, ac)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNormalize(t *testing.T) {
|
|
same := [][]string{
|
|
{"cmd+shift+t", "shift+super+T", "command+shift+t", "global:super+shift+t"},
|
|
{"ctrl+up", "control+arrow_up", "ctrl+ArrowUp"},
|
|
{"super++", "super+plus"},
|
|
{"opt+grave_accent", "alt+backquote"},
|
|
{"ctrl+a>n", "control+a>N"},
|
|
{"super+=", "cmd+="},
|
|
}
|
|
for _, group := range same {
|
|
want, err := Normalize(group[0])
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", group[0], err)
|
|
}
|
|
for _, g := range group[1:] {
|
|
got, err := Normalize(g)
|
|
if err != nil || got != want {
|
|
t.Errorf("Normalize(%q) = %q, %v; want %q", g, got, err, want)
|
|
}
|
|
}
|
|
}
|
|
for _, bad := range []string{"ctrl+ctrl+a", "ctrl+", "ctrl+a+b", "ctrl+bogus"} {
|
|
if _, err := Normalize(bad); err == nil && bad != "ctrl+" {
|
|
t.Errorf("Normalize(%q) should fail", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
var defaults = []string{
|
|
"super+t=new_tab",
|
|
"super+w=close_surface",
|
|
"super+d=new_split:right",
|
|
"super+arrow_right=text:\\x05",
|
|
}
|
|
|
|
func values(s *Set) string { return strings.Join(s.Values(), " | ") }
|
|
|
|
func find(t *testing.T, s *Set, trigger string) Binding {
|
|
t.Helper()
|
|
n, _ := Normalize(trigger)
|
|
for _, b := range s.Bindings() {
|
|
if b.Norm == n {
|
|
return b
|
|
}
|
|
}
|
|
t.Fatalf("no binding for %s", trigger)
|
|
return Binding{}
|
|
}
|
|
|
|
func TestEffective(t *testing.T) {
|
|
s := NewSet(defaults, []string{
|
|
"super+w=unbind",
|
|
"cmd+t=new_window",
|
|
"ctrl+a>n=new_tab",
|
|
"chain=toggle_fullscreen",
|
|
})
|
|
if b := find(t, s, "super+t"); b.Source != Changed || b.Action != "new_window" || b.Default != "new_tab" {
|
|
t.Errorf("super+t = %+v", b)
|
|
}
|
|
if b := find(t, s, "super+w"); b.Source != Unbound {
|
|
t.Errorf("super+w = %+v", b)
|
|
}
|
|
if b := find(t, s, "ctrl+a>n"); b.Source != Custom || len(b.Chain) != 1 || b.Chain[0] != "toggle_fullscreen" {
|
|
t.Errorf("ctrl+a>n = %+v", b)
|
|
}
|
|
if b := find(t, s, "super+d"); b.Source != Default || b.InConfig {
|
|
t.Errorf("super+d = %+v", b)
|
|
}
|
|
}
|
|
|
|
func TestClear(t *testing.T) {
|
|
s := NewSet(defaults, []string{"clear", "super+t=new_tab"})
|
|
if b := find(t, s, "super+t"); b.Source != Default || !b.InConfig {
|
|
t.Errorf("super+t = %+v", b)
|
|
}
|
|
if b := find(t, s, "super+d"); b.Source != Unbound {
|
|
t.Errorf("super+d should be unbound after clear: %+v", b)
|
|
}
|
|
s.Restore(find(t, s, "super+d"))
|
|
if got := values(s); got != "clear | super+t=new_tab | super+d=new_split:right" {
|
|
t.Errorf("restore after clear: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestChangeDefaultAction(t *testing.T) {
|
|
s := NewSet(defaults, nil)
|
|
b := find(t, s, "super+d")
|
|
if err := s.Change(&b, "super+d=new_split:down"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := values(s); got != "super+d=new_split:down" {
|
|
t.Errorf("got %s", got)
|
|
}
|
|
}
|
|
|
|
func TestMoveDefaultBinding(t *testing.T) {
|
|
s := NewSet(defaults, nil)
|
|
b := find(t, s, "super+d")
|
|
if err := s.Change(&b, "super+shift+d=new_split:right"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := values(s); got != "super+d=unbind | super+shift+d=new_split:right" {
|
|
t.Errorf("got %s", got)
|
|
}
|
|
}
|
|
|
|
func TestMoveUserBindingRevertsToDefault(t *testing.T) {
|
|
s := NewSet(defaults, []string{"super+arrow_right=goto_window:next"})
|
|
b := find(t, s, "super+arrow_right")
|
|
if err := s.Change(&b, "super+alt+arrow_right=goto_window:next"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := values(s); got != "super+alt+arrow_right=goto_window:next" {
|
|
t.Errorf("got %s", got)
|
|
}
|
|
if b := find(t, s, "super+arrow_right"); b.Source != Default {
|
|
t.Errorf("old trigger should fall back to the default: %+v", b)
|
|
}
|
|
}
|
|
|
|
func TestMoveUserBindingKeepsPosition(t *testing.T) {
|
|
s := NewSet(defaults, []string{"a=new_tab", "super+arrow_right=goto_window:next", "b=new_window"})
|
|
b := find(t, s, "super+arrow_right")
|
|
if err := s.Change(&b, "super+shift+k=goto_window:next"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := values(s); got != "a=new_tab | super+shift+k=goto_window:next | b=new_window" {
|
|
t.Errorf("got %s", got)
|
|
}
|
|
}
|
|
|
|
func TestChangeReplacesConflictInPlace(t *testing.T) {
|
|
s := NewSet(defaults, []string{"ctrl+x=copy_to_clipboard", "ctrl+y=paste_from_clipboard"})
|
|
b := find(t, s, "ctrl+y")
|
|
if err := s.Change(&b, "ctrl+x=paste_from_clipboard"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := values(s); got != "ctrl+x=paste_from_clipboard" {
|
|
t.Errorf("got %s", got)
|
|
}
|
|
}
|
|
|
|
func TestChangeBackToDefaultRemovesEntry(t *testing.T) {
|
|
s := NewSet(defaults, []string{"# not a keybind", "super+t=new_window"})
|
|
b := find(t, s, "super+t")
|
|
if err := s.Change(&b, "super+t=new_tab"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := values(s); got != "# not a keybind" {
|
|
t.Errorf("got %s", got)
|
|
}
|
|
}
|
|
|
|
func TestUnbindAndRestore(t *testing.T) {
|
|
s := NewSet(defaults, []string{"super+t=new_window", "ctrl+q=quit"})
|
|
s.Unbind(find(t, s, "super+t"))
|
|
s.Unbind(find(t, s, "ctrl+q"))
|
|
if got := values(s); got != "super+t=unbind" {
|
|
t.Errorf("after unbind: %s", got)
|
|
}
|
|
s.Restore(find(t, s, "super+t"))
|
|
if got := values(s); got != "" {
|
|
t.Errorf("after restore: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestChainMovesWithBinding(t *testing.T) {
|
|
s := NewSet(nil, []string{"ctrl+a=new_tab", "chain=toggle_fullscreen"})
|
|
b := find(t, s, "ctrl+a")
|
|
if err := s.Change(&b, "ctrl+b=new_tab"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := values(s); got != "ctrl+b=new_tab | chain=toggle_fullscreen" {
|
|
t.Errorf("got %s", got)
|
|
}
|
|
}
|
|
|
|
func TestDecodeCSI(t *testing.T) {
|
|
cases := map[string]string{
|
|
"\x1b[116;9u": "super+t",
|
|
"\x1b[116;10u": "super+shift+t",
|
|
"\x1b[44;10u": "super+shift+,",
|
|
"\x1b[97;5u": "ctrl+a",
|
|
"\x1b[167;9u": "super+§",
|
|
"\x1b[1;9C": "super+arrow_right",
|
|
"\x1b[5;3~": "alt+page_up",
|
|
"\x1b[13;2u": "shift+enter",
|
|
"\x1b[57399;5u": "ctrl+numpad_0",
|
|
"\x1b[57376u": "f13",
|
|
"\x1b[97:65;2u": "shift+a",
|
|
}
|
|
for seq, want := range cases {
|
|
c, ok := DecodeCSI([]byte(seq))
|
|
if !ok || c.String() != want {
|
|
t.Errorf("DecodeCSI(%q) = %q, %v; want %q", seq, c.String(), ok, want)
|
|
}
|
|
}
|
|
if c, ok := DecodeCSI([]byte("\x1b[57441;2u")); !ok || !c.Modifier {
|
|
t.Error("left shift should be reported as a modifier key")
|
|
}
|
|
if _, ok := DecodeCSI([]byte("\x1b[?9u")); ok {
|
|
t.Error("kitty reply is not a key")
|
|
}
|
|
}
|
|
|
|
func TestDecodeKeyName(t *testing.T) {
|
|
cases := map[string]string{
|
|
"ctrl+a": "ctrl+a",
|
|
"alt+shift+up": "alt+shift+arrow_up",
|
|
"ctrl+shift+up": "ctrl+shift+arrow_up",
|
|
"f5": "f5",
|
|
"A": "shift+a",
|
|
"pgdown": "page_down",
|
|
"shift+tab": "shift+tab",
|
|
"alt+x": "alt+x",
|
|
"+": "+",
|
|
}
|
|
for in, want := range cases {
|
|
c, ok := DecodeKeyName(in)
|
|
if !ok || c.String() != want {
|
|
t.Errorf("DecodeKeyName(%q) = %q, %v; want %q", in, c.String(), ok, want)
|
|
}
|
|
}
|
|
}
|