-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration_test.go
More file actions
108 lines (87 loc) · 2.16 KB
/
Copy pathconfiguration_test.go
File metadata and controls
108 lines (87 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gbind
import (
"fmt"
"log"
"sync"
"testing"
"time"
"github.com/gdamore/tcell/v3"
)
const pressTimes = 7
func TestConfiguration(t *testing.T) {
t.Parallel()
wg := make([]*sync.WaitGroup, len(testCases))
config := NewConfiguration()
for i, c := range testCases {
wg[i] = new(sync.WaitGroup)
wg[i].Add(pressTimes)
i := i // Capture
err := config.Set(c.encoded, func(ev *tcell.EventKey) *tcell.EventKey {
wg[i].Done()
return nil
})
if err != nil {
t.Fatalf("failed to set keybind for %s: %s", c.encoded, err)
}
}
done := make(chan struct{})
timeout := time.After(5 * time.Second)
go func() {
for i := range testCases {
wg[i].Wait()
}
done <- struct{}{}
}()
errs := make(chan error)
for j := 0; j < pressTimes; j++ {
for i, c := range testCases {
i, c := i, c // Capture
go func() {
ev := config.Capture(tcell.NewEventKey(c.key, c.str, c.mod))
if ev != nil {
errs <- fmt.Errorf("failed to test capturing keybinds: failed to register case %d event %d %d %s", i, c.mod, c.key, c.str)
}
}()
}
}
select {
case err := <-errs:
t.Fatal(err)
case <-timeout:
t.Fatal("timeout")
case <-done:
}
}
// Example of creating and using an input configuration.
func ExampleNewConfiguration() {
// Create a new input configuration to store the key bindings.
c := NewConfiguration()
handleSave := func(ev *tcell.EventKey) *tcell.EventKey {
// Save
return nil
}
handleOpen := func(ev *tcell.EventKey) *tcell.EventKey {
// Open
return nil
}
handleExit := func(ev *tcell.EventKey) *tcell.EventKey {
// Exit
return nil
}
// Bind Alt+s.
if err := c.Set("Alt+s", handleSave); err != nil {
log.Fatalf("failed to set keybind: %s", err)
}
// Bind Alt+o.
c.SetRune(tcell.ModAlt, 'o', handleOpen)
// Bind Escape.
c.SetKey(tcell.ModNone, tcell.KeyEscape, handleExit)
// Capture input. This will differ based on the framework in use (if any).
// When using tview or cview, call Application.SetInputCapture before calling
// Application.Run.
// app.SetInputCapture(c.Capture)
}
// Example of capturing key events.
func ExampleConfiguration_Capture() {
// See the end of the NewConfiguration example.
}