cbind/configuration_test.go

126 lines
2.8 KiB
Go
Raw Normal View History

2020-01-22 00:01:30 +00:00
package cbind
import (
2021-03-09 08:49:42 +00:00
"fmt"
"log"
2020-01-22 00:01:30 +00:00
"sync"
"testing"
"time"
2020-08-26 21:45:15 +00:00
"github.com/gdamore/tcell/v2"
2020-01-22 00:01:30 +00:00
)
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
if c.key != tcell.KeyRune {
config.SetKey(c.mod, c.key, func(ev *tcell.EventKey) *tcell.EventKey {
wg[i].Done()
return nil
})
} else {
config.SetRune(c.mod, c.ch, func(ev *tcell.EventKey) *tcell.EventKey {
wg[i].Done()
return nil
})
}
}
done := make(chan struct{})
timeout := time.After(5 * time.Second)
go func() {
for i := range testCases {
wg[i].Wait()
}
done <- struct{}{}
}()
2021-03-09 08:49:42 +00:00
errs := make(chan error)
for j := 0; j < pressTimes; j++ {
for i, c := range testCases {
i, c := i, c // Capture
go func() {
2020-02-07 01:37:48 +00:00
k := tcell.NewEventKey(c.key, c.ch, c.mod)
if k.Key() != c.key {
2021-03-09 08:49:42 +00:00
errs <- fmt.Errorf("failed to test capturing keybinds: tcell modified EventKey.Key: expected %d, got %d", c.key, k.Key())
return
2020-02-07 01:37:48 +00:00
} else if k.Rune() != c.ch {
2021-03-09 08:49:42 +00:00
errs <- fmt.Errorf("failed to test capturing keybinds: tcell modified EventKey.Rune: expected %d, got %d", c.ch, k.Rune())
return
2020-02-07 01:37:48 +00:00
} else if k.Modifiers() != c.mod {
2021-03-09 08:49:42 +00:00
errs <- fmt.Errorf("failed to test capturing keybinds: tcell modified EventKey.Modifiers: expected %d, got %d", c.mod, k.Modifiers())
return
2020-02-07 01:37:48 +00:00
}
ev := config.Capture(tcell.NewEventKey(c.key, c.ch, c.mod))
if ev != nil {
2021-03-09 08:49:42 +00:00
errs <- fmt.Errorf("failed to test capturing keybinds: failed to register case %d event %d %d %d", i, c.mod, c.key, c.ch)
2020-02-07 01:37:48 +00:00
}
2021-03-09 08:49:42 +00:00
}()
2020-01-22 00:01:30 +00:00
}
2021-03-09 08:49:42 +00:00
}
2020-01-22 00:01:30 +00:00
select {
2021-03-09 08:49:42 +00:00
case err := <-errs:
t.Fatal(err)
2020-01-22 00:01:30 +00:00
case <-timeout:
2021-03-09 08:49:42 +00:00
t.Fatal("timeout")
2020-01-22 00:01:30 +00:00
case <-done:
}
}
// Example of creating and using an input configuration.
func ExampleNewConfiguration() {
// Create a new input configuration to store the key bindings.
2020-01-22 00:01:30 +00:00
c := NewConfiguration()
handleSave := func(ev *tcell.EventKey) *tcell.EventKey {
2020-01-22 00:01:30 +00:00
// Save
return nil
}
2020-01-22 00:01:30 +00:00
handleOpen := func(ev *tcell.EventKey) *tcell.EventKey {
2020-01-22 00:01:30 +00:00
// Open
return nil
}
2020-01-22 00:01:30 +00:00
handleExit := func(ev *tcell.EventKey) *tcell.EventKey {
2020-01-22 00:01:30 +00:00
// 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)
2020-01-22 00:01:30 +00:00
2020-10-13 19:41:54 +00:00
// 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.
2020-01-22 00:01:30 +00:00
// app.SetInputCapture(c.Capture)
}
2021-03-09 08:02:20 +00:00
// Example of capturing key events.
func ExampleConfiguration_Capture() {
// See the end of the NewConfiguration example.
}