...
1 package cbind
2
3 import (
4 "fmt"
5 "log"
6 "sync"
7 "testing"
8 "time"
9
10 "github.com/gdamore/tcell/v2"
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 if c.key != tcell.KeyRune {
27 config.SetKey(c.mod, c.key, func(ev *tcell.EventKey) *tcell.EventKey {
28 wg[i].Done()
29 return nil
30 })
31 } else {
32 config.SetRune(c.mod, c.ch, func(ev *tcell.EventKey) *tcell.EventKey {
33 wg[i].Done()
34 return nil
35 })
36 }
37
38 }
39
40 done := make(chan struct{})
41 timeout := time.After(5 * time.Second)
42
43 go func() {
44 for i := range testCases {
45 wg[i].Wait()
46 }
47
48 done <- struct{}{}
49 }()
50
51 errs := make(chan error)
52 for j := 0; j < pressTimes; j++ {
53 for i, c := range testCases {
54 i, c := i, c
55 go func() {
56 k := tcell.NewEventKey(c.key, c.ch, c.mod)
57 if k.Key() != c.key {
58 errs <- fmt.Errorf("failed to test capturing keybinds: tcell modified EventKey.Key: expected %d, got %d", c.key, k.Key())
59 return
60 } else if k.Rune() != c.ch {
61 errs <- fmt.Errorf("failed to test capturing keybinds: tcell modified EventKey.Rune: expected %d, got %d", c.ch, k.Rune())
62 return
63 } else if k.Modifiers() != c.mod {
64 errs <- fmt.Errorf("failed to test capturing keybinds: tcell modified EventKey.Modifiers: expected %d, got %d", c.mod, k.Modifiers())
65 return
66 }
67
68 ev := config.Capture(tcell.NewEventKey(c.key, c.ch, c.mod))
69 if ev != nil {
70 errs <- fmt.Errorf("failed to test capturing keybinds: failed to register case %d event %d %d %d", i, c.mod, c.key, c.ch)
71 }
72 }()
73 }
74 }
75
76 select {
77 case err := <-errs:
78 t.Fatal(err)
79 case <-timeout:
80 t.Fatal("timeout")
81 case <-done:
82 }
83 }
84
85
86 func ExampleNewConfiguration() {
87
88 c := NewConfiguration()
89
90 handleSave := func(ev *tcell.EventKey) *tcell.EventKey {
91
92 return nil
93 }
94
95 handleOpen := func(ev *tcell.EventKey) *tcell.EventKey {
96
97 return nil
98 }
99
100 handleExit := func(ev *tcell.EventKey) *tcell.EventKey {
101
102 return nil
103 }
104
105
106 if err := c.Set("Alt+s", handleSave); err != nil {
107 log.Fatalf("failed to set keybind: %s", err)
108 }
109
110
111 c.SetRune(tcell.ModAlt, 'o', handleOpen)
112
113
114 c.SetKey(tcell.ModNone, tcell.KeyEscape, handleExit)
115
116
117
118
119
120 }
121
122
123 func ExampleConfiguration_Capture() {
124
125 }
126
View as plain text