File: configuration_test.go

package info (click to toggle)
golang-code.rocketnine-tslocum-cbind 0.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 112 kB
  • sloc: makefile: 4
file content (125 lines) | stat: -rw-r--r-- 2,870 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cbind

import (
	"fmt"
	"log"
	"sync"
	"testing"
	"time"

	"github.com/gdamore/tcell/v2"
)

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{}{}
	}()

	errs := make(chan error)
	for j := 0; j < pressTimes; j++ {
		for i, c := range testCases {
			i, c := i, c // Capture
			go func() {
				k := tcell.NewEventKey(c.key, c.ch, c.mod)
				if k.Key() != c.key {
					errs <- fmt.Errorf("failed to test capturing keybinds: tcell modified EventKey.Key: expected %d, got %d", c.key, k.Key())
					return
				} else if k.Rune() != c.ch {
					errs <- fmt.Errorf("failed to test capturing keybinds: tcell modified EventKey.Rune: expected %d, got %d", c.ch, k.Rune())
					return
				} else if k.Modifiers() != c.mod {
					errs <- fmt.Errorf("failed to test capturing keybinds: tcell modified EventKey.Modifiers: expected %d, got %d", c.mod, k.Modifiers())
					return
				}

				ev := config.Capture(tcell.NewEventKey(c.key, c.ch, c.mod))
				if ev != nil {
					errs <- fmt.Errorf("failed to test capturing keybinds: failed to register case %d event %d %d %d", i, c.mod, c.key, c.ch)
				}
			}()
		}
	}

	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.
}