...
1 package cbind
2
3 import (
4 "fmt"
5 "log"
6 "sync"
7 "testing"
8 "time"
9
10 "github.com/gdamore/tcell/v3"
11 )
12
13 const pressTimes = 7
14
15 func TestConfiguration(t *testing.T) {
16 t.Parallel()
17
18 wg := make([]*sync.WaitGroup, len(testCases))
19
20 config := NewConfiguration()
21 for i, c := range testCases {
22 wg[i] = new(sync.WaitGroup)
23 wg[i].Add(pressTimes)
24
25 i := i
26 err := config.Set(c.encoded, func(ev *tcell.EventKey) *tcell.EventKey {
27 wg[i].Done()
28 return nil
29 })
30 if err != nil {
31 t.Fatalf("failed to set keybind for %s: %s", c.encoded, err)
32 }
33 }
34
35 done := make(chan struct{})
36 timeout := time.After(5 * time.Second)
37
38 go func() {
39 for i := range testCases {
40 wg[i].Wait()
41 }
42
43 done <- struct{}{}
44 }()
45
46 errs := make(chan error)
47 for j := 0; j < pressTimes; j++ {
48 for i, c := range testCases {
49 i, c := i, c
50 go func() {
51 ev := config.Capture(tcell.NewEventKey(c.key, c.str, c.mod))
52 if ev != nil {
53 errs <- fmt.Errorf("failed to test capturing keybinds: failed to register case %d event %d %d %s", i, c.mod, c.key, c.str)
54 }
55 }()
56 }
57 }
58
59 select {
60 case err := <-errs:
61 t.Fatal(err)
62 case <-timeout:
63 t.Fatal("timeout")
64 case <-done:
65 }
66 }
67
68
69 func ExampleNewConfiguration() {
70
71 c := NewConfiguration()
72
73 handleSave := func(ev *tcell.EventKey) *tcell.EventKey {
74
75 return nil
76 }
77
78 handleOpen := func(ev *tcell.EventKey) *tcell.EventKey {
79
80 return nil
81 }
82
83 handleExit := func(ev *tcell.EventKey) *tcell.EventKey {
84
85 return nil
86 }
87
88
89 if err := c.Set("Alt+s", handleSave); err != nil {
90 log.Fatalf("failed to set keybind: %s", err)
91 }
92
93
94 c.SetRune(tcell.ModAlt, 'o', handleOpen)
95
96
97 c.SetKey(tcell.ModNone, tcell.KeyEscape, handleExit)
98
99
100
101
102
103 }
104
105
106 func ExampleConfiguration_Capture() {
107
108 }
109
View as plain text