Debian Codegraph @

key_test.go

raw
1 package main
2
3 import (
4 "testing"
5
6 "github.com/gdamore/tcell/v3"
7 )
8
9 var gKeyTests = []struct {
10 ev *tcell.EventKey
11 s string
12 }{
13 {tcell.NewEventKey(tcell.KeyRune, "<", tcell.ModNone), "<lt>"},
14 {tcell.NewEventKey(tcell.KeyRune, ">", tcell.ModNone), "<gt>"},
15 {tcell.NewEventKey(tcell.KeyRune, " ", tcell.ModNone), "<space>"},
16 {tcell.NewEventKey(tcell.KeyRune, "a", tcell.ModNone), "a"},
17 {tcell.NewEventKey(tcell.KeyCtrlA, "", tcell.ModNone), "<c-a>"},
18 {tcell.NewEventKey(tcell.KeyRune, "A", tcell.ModNone), "A"},
19 {tcell.NewEventKey(tcell.KeyRune, "a", tcell.ModAlt), "<a-a>"},
20 {tcell.NewEventKey(tcell.KeyLeft, "", tcell.ModNone), "<left>"},
21 {tcell.NewEventKey(tcell.KeyLeft, "", tcell.ModCtrl), "<c-left>"},
22 {tcell.NewEventKey(tcell.KeyLeft, "", tcell.ModShift), "<s-left>"},
23 {tcell.NewEventKey(tcell.KeyLeft, "", tcell.ModAlt), "<a-left>"},
24 {tcell.NewEventKey(tcell.KeyEsc, "", tcell.ModNone), "<esc>"},
25 {tcell.NewEventKey(tcell.KeyF1, "", tcell.ModNone), "<f-1>"},
26 }
27
28 func TestReadKey(t *testing.T) {
29 for _, test := range gKeyTests {
30 if got := readKey(test.ev); got != test.s {
31 t.Errorf("at input '%#v' expected '%s' but got '%s'", test.ev, test.s, got)
32 }
33 }
34 }
35
36 func TestParseKey(t *testing.T) {
37 keyEqual := func(ev1, ev2 *tcell.EventKey) bool {
38 return ev1.Key() == ev2.Key() && ev1.Modifiers() == ev2.Modifiers() && ev1.Str() == ev2.Str()
39 }
40
41 for _, test := range gKeyTests {
42 if got := parseKey(test.s); !keyEqual(got, test.ev) {
43 t.Errorf("at input '%s' expected '%#v' but got '%#v'", test.s, test.ev, got)
44 }
45 }
46 }