From 8e62af32a865254fb847456e53592ad0f41f270b Mon Sep 17 00:00:00 2001 From: raziman Date: Wed, 17 Feb 2021 17:08:39 +0800 Subject: [PATCH] add anko keybinding test --- anko/anko.go | 17 ++++----- anko/anko_test.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 9 deletions(-) diff --git a/anko/anko.go b/anko/anko.go index 080cefc..2687b1b 100644 --- a/anko/anko.go +++ b/anko/anko.go @@ -12,7 +12,6 @@ import ( "github.com/mattn/anko/vm" ) - type Anko struct { env *env.Env } @@ -102,18 +101,18 @@ func (a *Anko) KeybindExists(panel string, eventKey *tcell.EventKey) bool { if strings.Contains(name, "Ctrl") { key := extractCtrlRune(name) - src = fmt.Sprintf("Keybinds.%s.ctrl_%s", + src = fmt.Sprintf("Keybinds.%s[\"ctrl_%s\"]", panel, strings.ToLower(string(key))) } else if strings.Contains(name, "Alt") { key := extractAltRune(name) - src = fmt.Sprintf("Keybinds.%s.alt_%c", panel, key) + src = fmt.Sprintf("Keybinds.%s[\"alt_%c\"]", panel, key) } else if strings.Contains(name, "Rune") { - src = fmt.Sprintf("Keybinds.%s.%c", panel, eventKey.Rune()) + src = fmt.Sprintf("Keybinds.%s[\"%c\"]", panel, eventKey.Rune()) } else { - src = fmt.Sprintf("Keybinds.%s.%s", panel, strings.ToLower(name)) + src = fmt.Sprintf("Keybinds.%s[\"%s\"]", panel, strings.ToLower(name)) } @@ -133,18 +132,18 @@ func (a *Anko) ExecKeybind(panel string, eventKey *tcell.EventKey) error { if strings.Contains(name, "Ctrl") { key := extractCtrlRune(name) - src = fmt.Sprintf("Keybinds.%s.ctrl_%s()", + src = fmt.Sprintf("Keybinds.%s[\"ctrl_%s\"]()", panel, strings.ToLower(string(key))) } else if strings.Contains(name, "Alt") { key := extractAltRune(name) - src = fmt.Sprintf("Keybinds.%s.alt_%c()", panel, key) + src = fmt.Sprintf("Keybinds.%s[\"alt_%c\"]()", panel, key) } else if strings.Contains(name, "Rune") { - src = fmt.Sprintf("Keybinds.%s.%c()", panel, eventKey.Rune()) + src = fmt.Sprintf("Keybinds.%s[\"%c\"]()", panel, eventKey.Rune()) } else { - src = fmt.Sprintf("Keybinds.%s.%s()", panel, strings.ToLower(name)) + src = fmt.Sprintf("Keybinds.%s[\"%s\"]()", panel, strings.ToLower(name)) } diff --git a/anko/anko_test.go b/anko/anko_test.go index ef7bc99..2b7b711 100644 --- a/anko/anko_test.go +++ b/anko/anko_test.go @@ -1,8 +1,10 @@ package anko import ( + "fmt" "testing" + "github.com/gdamore/tcell/v2" "github.com/stretchr/testify/assert" ) @@ -146,4 +148,96 @@ func TestExtractAltRune(t *testing.T) { func TestKeybindExists(t *testing.T) { + tests := []struct { + panel string + key *tcell.EventKey + exists bool + }{ + { + panel: "global", + key: tcell.NewEventKey(tcell.KeyRune, 'b', tcell.ModNone), + exists: true, + }, + { + panel: "global", + key: tcell.NewEventKey(tcell.KeyRune, 'x', tcell.ModNone), + exists: false, + }, + { + panel: "global", + key: tcell.NewEventKey(tcell.KeyRune, ']', tcell.ModNone), + exists: true, + }, + { + panel: "global", + key: tcell.NewEventKey(tcell.KeyRune, '[', tcell.ModNone), + exists: false, + }, + { + panel: "global", + key: tcell.NewEventKey(tcell.KeyCtrlB, 'b', tcell.ModCtrl), + exists: true, + }, + { + panel: "global", + key: tcell.NewEventKey(tcell.KeyCtrlC, 'c', tcell.ModCtrl), + exists: false, + }, + { + panel: "playlist", + key: tcell.NewEventKey(tcell.KeyRune, '!', tcell.ModAlt), + exists: true, + }, + { + panel: "playlist", + key: tcell.NewEventKey(tcell.KeyRune, '>', tcell.ModAlt), + exists: false, + }, + { + panel: "playlist", + key: tcell.NewEventKey(tcell.KeyCtrlCarat, '^', tcell.ModCtrl), + exists: true, + }, + { + panel: "queue", + key: tcell.NewEventKey(tcell.KeyRune, '>', tcell.ModAlt), + exists: true, + }, + { + panel: "queue", + key: tcell.NewEventKey(tcell.KeyEnter, 0, tcell.ModNone), + exists: true, + }, + } + + src := ` +module Keybinds { + global = {} + playlist = {} + queue = {} + + global["b"] = func() { return 0 } + global["]"] = func() { return 0 } + global["ctrl_b"] = func() { return 0 } + global["alt_b"] = func() { return 0 } + + playlist["alt_!"] = func() { return 0 } + playlist["ctrl_^"] = func() { return 0 } + + queue["alt_>"] = func() { return 0 } + queue["enter"] = func() { return 0 } +} +` + a := NewAnko() + + _, err := a.Execute(src) + if err != nil { + t.Error(err) + } + + for i, test := range tests { + got := a.KeybindExists(test.panel, test.key) + msg := fmt.Sprintf("error on test %d", i+1) + assert.Equal(t, test.exists, got, msg) + } }