-
Notifications
You must be signed in to change notification settings - Fork 3
/
keymap_test.go
37 lines (30 loc) · 1.06 KB
/
keymap_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
package main_test
import (
"fmt"
"testing"
"github.com/dhamidi/leader"
"github.com/stretchr/testify/assert"
)
type mockCommand struct{ called int }
func newMockCommand() *mockCommand { return &mockCommand{called: 0} }
func (m *mockCommand) Execute() error {
m.called++
return nil
}
func TestKeyMap_DefineKey_creates_an_entry_at_the_current_map_level(t *testing.T) {
keymap := main.NewKeyMap("root")
keymap.DefineKey('a', main.DoNothing)
assert.NotEqual(t, main.UnboundKey, keymap.LookupKey('a'))
}
func TestKeyMap_DefineKey_associates_a_binding_with_the_provided_command(t *testing.T) {
keymap := main.NewKeyMap("root")
keymap.DefineKey('a', main.FailWhenExecuted(fmt.Errorf("done")))
assert.Equal(t, fmt.Errorf("done"), keymap.LookupKey('a').Execute())
}
func TestKeyMap_nested_bindings_are_supported(t *testing.T) {
keymap := main.NewKeyMap("root")
command := newMockCommand()
keymap.Bind('a').Children().Bind('b').Do(command.Execute)
keymap.LookupKey('a').Children().LookupKey('b').Execute()
assert.Equal(t, 1, command.called, "command has not been called")
}