forked from charmbracelet/bubbletea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_test.go
77 lines (69 loc) · 1.75 KB
/
key_test.go
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
package tea
import (
"bytes"
"testing"
)
func TestKeyString(t *testing.T) {
t.Run("alt+space", func(t *testing.T) {
if got := KeyMsg(Key{
Type: keySP,
Alt: true,
}).String(); got != "alt+space" {
t.Fatalf(`expected a "alt+space", got %q`, got)
}
})
t.Run("runes", func(t *testing.T) {
if got := KeyMsg(Key{
Type: KeyRunes,
Runes: []rune{'a'},
}).String(); got != "a" {
t.Fatalf(`expected an "a", got %q`, got)
}
})
t.Run("invalid", func(t *testing.T) {
if got := KeyMsg(Key{
Type: KeyType(99999),
}).String(); got != "" {
t.Fatalf(`expected a "", got %q`, got)
}
})
}
func TestKeyTypeString(t *testing.T) {
t.Run("space", func(t *testing.T) {
if got := keySP.String(); got != "space" {
t.Fatalf(`expected a "space", got %q`, got)
}
})
t.Run("invalid", func(t *testing.T) {
if got := KeyType(99999).String(); got != "" {
t.Fatalf(`expected a "", got %q`, got)
}
})
}
func TestReadInput(t *testing.T) {
for out, in := range map[string][]byte{
"a": {'a'},
"ctrl+a": {byte(keySOH)},
"alt+a": {0x1b, 'a'},
"abcd": {'a', 'b', 'c', 'd'},
"up": []byte("\x1b[A"),
"wheel up": {'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)},
"shift+tab": {'\x1b', '[', 'Z'},
} {
t.Run(out, func(t *testing.T) {
msgs, err := readInputs(bytes.NewReader(in))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(msgs) == 0 {
t.Fatalf("unexpected empty message list")
}
if m, ok := msgs[0].(KeyMsg); ok && m.String() != out {
t.Fatalf(`expected a keymsg %q, got %q`, out, m)
}
if m, ok := msgs[0].(MouseMsg); ok && mouseEventTypes[m.Type] != out {
t.Fatalf(`expected a mousemsg %q, got %q`, out, mouseEventTypes[m.Type])
}
})
}
}