package keymap import ( "testing" "100gex/internal/rex" ) // key returns the event for a key: a character, with its unshifted form, or // a named physical key. func key(mods, k string, unshifted rune) rex.KeyEvent { return rex.KeyEvent{Action: "press", Key: k, Mods: mods, UnshiftedCodepoint: unshifted} } var ( ctrlB = key("ctrl", "key_b", 'b') plainC = key("", "key_c", 'c') plainZ = key("", "key_z", 'z') percent = key("shift", "digit_5", '5') escape = key("", "escape", 0) left = key("", "arrow_left", 0) shiftCtrlD = key("shift+ctrl", "key_d", 'd') ) func TestParseChord(t *testing.T) { for _, tt := range []struct { in, want string matches rex.KeyEvent }{ {"ctrl+shift+d", "shift+ctrl+d", shiftCtrlD}, {"Shift+Ctrl+D", "shift+ctrl+d", shiftCtrlD}, // case and order don't matter {"control+shift+d", "shift+ctrl+d", shiftCtrlD}, {"ctrl+b", "ctrl+b", ctrlB}, {"shift+5", "shift+5", percent}, {"left", "left", left}, {"esc", "escape", escape}, {"ctrl+[KeyB]", "ctrl+[key_b]", ctrlB}, {"shift+[Digit5]", "shift+[digit_5]", percent}, {"cmd+[ArrowLeft]", "cmd+[arrow_left]", key("super", "arrow_left", 0)}, {"ctrl++", "ctrl++", key("ctrl", "equal", '+')}, {"f5", "f5", key("", "f5", 0)}, {"[F5]", "[f5]", key("", "f5", 0)}, {"pageup", "pageup", key("", "page_up", 0)}, } { c, err := ParseChord(tt.in) if err != nil { t.Errorf("ParseChord(%q): %v", tt.in, err) continue } if c.String() != tt.want { t.Errorf("ParseChord(%q) = %s, want %s", tt.in, c, tt.want) } if !c.Matches(tt.matches) { t.Errorf("%q doesn't match %+v", tt.in, tt.matches) } } for _, bad := range []string{"", "ctrl+", "hyper+a", "ctrl+banana"} { if c, err := ParseChord(bad); err == nil { t.Errorf("ParseChord(%q) = %s, want an error", bad, c) } } // Modifiers must match exactly: shift is part of the key. d, _ := ParseChord("ctrl+d") if d.Matches(shiftCtrlD) { t.Error("ctrl+d matched ctrl+shift+d") } // Plus on a US layout is shift+=, not +. plus, _ := ParseChord("ctrl+shift+=") if !plus.Matches(key("shift+ctrl", "equal", '=')) { t.Error("ctrl+shift+= didn't match") } } func run(t *testing.T, m *Matcher, k rex.KeyEvent, wantAction string) { t.Helper() r := m.Key(k) if r.Outcome != Run || r.Binding.Action != wantAction { t.Fatalf("%+v: got %+v, want to run %s", k, r, wantAction) } } func outcome(t *testing.T, m *Matcher, k rex.KeyEvent, want Outcome) { t.Helper() if r := m.Key(k); r.Outcome != want { t.Fatalf("%+v: outcome %v (%+v), want %v", k, r.Outcome, r.Binding, want) } } func TestSequences(t *testing.T) { km := New([]Binding{ {Keys: []string{"ctrl+b", "c"}, Action: "client.tab.new"}, {Keys: []string{"ctrl+b", "shift+5"}, Action: "pane.split"}, {Keys: []string{"ctrl+b", "ctrl+b"}, Action: "pane.send_key"}, {Keys: []string{"ctrl+b", "x"}, Action: "client.key.forward"}, {Keys: []string{"ctrl+shift+d"}, Action: "pane.split"}, }, nil) m := NewMatcher(km, nil) outcome(t, m, ctrlB, Consumed) if p := m.Pending(); len(p) != 1 || p[0] != "ctrl+b" { t.Errorf("pending = %v", p) } run(t, m, plainC, "client.tab.new") outcome(t, m, ctrlB, Consumed) run(t, m, percent, "pane.split") outcome(t, m, ctrlB, Consumed) run(t, m, ctrlB, "pane.send_key") // The prefix comes back for a sequence, for client.key. m.Key(ctrlB) r := m.Key(key("", "key_x", 'x')) if r.Outcome != Run || len(r.Prefix) != 1 || r.Prefix[0] != "ctrl+b" { t.Errorf("forwarded sequence = %+v", r) } // Escape cancels a sequence. outcome(t, m, ctrlB, Consumed) outcome(t, m, escape, Consumed) outcome(t, m, plainC, Pass) // A key that doesn't continue the sequence goes to the terminal, and // isn't tried as a shortcut of its own. outcome(t, m, ctrlB, Consumed) outcome(t, m, shiftCtrlD, Pass) run(t, m, shiftCtrlD, "pane.split") // Unbound keys pass. outcome(t, m, plainZ, Pass) } func TestAvailability(t *testing.T) { km := New([]Binding{ {Keys: []string{"ctrl+shift+d"}, Action: "client.sidebar.toggle"}, // another client's action {Keys: []string{"ctrl+shift+d"}, Action: "pane.split"}, {Keys: []string{"ctrl+shift+e"}, Action: "client.sidebar.toggle"}, }, nil) m := NewMatcher(km, func(a string) bool { return a != "client.sidebar.toggle" }) run(t, m, shiftCtrlD, "pane.split") outcome(t, m, key("shift+ctrl", "key_e", 'e'), Pass) } func TestModes(t *testing.T) { km := New([]Binding{ {Keys: []string{"ctrl+r"}, Action: "client.mode.enter"}, {Keys: []string{"left"}, Action: "pane.resize", Mode: "resize"}, {Keys: []string{"enter"}, Action: "client.mode.exit", Mode: "resize"}, {Keys: []string{"g", "g"}, Action: "pane.scroll_top", Mode: "copy"}, {Keys: []string{"left"}, Action: "pane.focus"}, {Keys: []string{"x"}, Action: "pane.close"}, }, []Mode{ {Name: "resize"}, {Name: "copy", Exclusive: true}, {Name: "quiet", Blocked: [][]string{{"x"}}}, }) m := NewMatcher(km, nil) run(t, m, left, "pane.focus") // In a mode, its bindings come first; others still work. m.EnterMode("resize", false) run(t, m, left, "pane.resize") run(t, m, key("", "key_x", 'x'), "pane.close") outcome(t, m, plainZ, Pass) // Escape leaves a mode that doesn't bind it. outcome(t, m, escape, Consumed) run(t, m, left, "pane.focus") // An exclusive mode ignores everything else, and allows sequences. m.EnterMode("copy", false) outcome(t, m, plainZ, Consumed) outcome(t, m, left, Consumed) outcome(t, m, key("", "key_g", 'g'), Consumed) run(t, m, key("", "key_g", 'g'), "pane.scroll_top") m.ExitAllModes() // A blocked key does nothing in its mode, even though it's bound // outside it. m.EnterMode("quiet", false) outcome(t, m, key("", "key_x", 'x'), Consumed) m.ExitMode() // A mode entered once is left after one binding runs. m.EnterMode("resize", true) run(t, m, left, "pane.resize") run(t, m, left, "pane.focus") // Modes nest: the innermost is tried first and left first. m.EnterMode("resize", false) m.EnterMode("quiet", false) run(t, m, left, "pane.resize") m.ExitMode() if got := m.Modes(); len(got) != 1 || got[0] != "resize" { t.Errorf("modes = %v", got) } // A new keymap without the mode leaves it. m.SetKeymap(New(nil, nil)) if len(m.Modes()) != 0 { t.Errorf("modes after reload = %v", m.Modes()) } } func TestChordEvent(t *testing.T) { for _, tt := range []struct { in string want rex.KeyEvent }{ {"ctrl+l", rex.KeyEvent{Action: "press", Key: "key_l", Mods: "ctrl", Text: "l", UnshiftedCodepoint: 'l'}}, {"shift+a", rex.KeyEvent{Action: "press", Key: "key_a", Mods: "shift", ConsumedMods: "shift", Text: "A", UnshiftedCodepoint: 'a'}}, {"alt+left", rex.KeyEvent{Action: "press", Key: "arrow_left", Mods: "alt"}}, {"ctrl+space", rex.KeyEvent{Action: "press", Key: "space", Mods: "ctrl", Text: " ", UnshiftedCodepoint: ' '}}, } { c, err := ParseChord(tt.in) if err != nil { t.Fatal(err) } if got := c.Event(); got != tt.want { t.Errorf("%s.Event() = %+v, want %+v", tt.in, got, tt.want) } // The event matches the chord it came from. if !c.Matches(c.Event()) { t.Errorf("%s doesn't match its own event", tt.in) } } } func TestChordLabel(t *testing.T) { for in, want := range map[string]string{ "shift+ctrl+d": "Ctrl+Shift+D", "ctrl+alt+left": "Ctrl+Alt+←", "cmd+[Digit1]": "Super+1", "ctrl+pageup": "Ctrl+PgUp", "f5": "F5", "ctrl+=": "Ctrl+=", } { c, err := ParseChord(in) if err != nil { t.Fatal(err) } if got := c.Label(); got != want { t.Errorf("%s.Label() = %q, want %q", in, got, want) } } }