test: key tests

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos A Becker 2021-10-12 10:38:08 -03:00 committed by Christian Rocha
parent cfb871c5ca
commit 350b13cba7
1 changed files with 73 additions and 0 deletions

73
key_test.go Normal file
View File

@ -0,0 +1,73 @@
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) {
msg, err := readInput(bytes.NewReader(in))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if m, ok := msg.(KeyMsg); ok && m.String() != out {
t.Fatalf(`expected a keymsg %q, got %q`, out, m)
}
if m, ok := msg.(MouseMsg); ok && mouseEventTypes[m.Type] != out {
t.Fatalf(`expected a mousemsg %q, got %q`, out, mouseEventTypes[m.Type])
}
})
}
}