aboutsummaryrefslogtreecommitdiffstats
path: root/config/binds_test.go
blob: a92cf22fda77691c7ce967eeaeb1f9c76b13cb8e (plain) (blame)
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
package config

import (
	"fmt"
	"testing"

	"git.sr.ht/~rockorager/vaxis"
	"github.com/stretchr/testify/assert"
)

func TestGetBinding(t *testing.T) {
	assert := assert.New(t)

	bindings := NewKeyBindings()
	add := func(binding, cmd string) {
		b, _ := ParseBinding(binding, cmd, "")
		bindings.Add(b)
	}

	add("abc", ":abc")
	add("cba", ":cba")
	add("foo", ":foo")
	add("bar", ":bar")

	test := func(input []KeyStroke, result int, output string) {
		_output, _ := ParseKeyStrokes(output)
		r, out := bindings.GetBinding(input)
		assert.Equal(result, int(r), fmt.Sprintf(
			"%s: Expected result %d, got %d", output, result, r))
		assert.Equal(_output, out, fmt.Sprintf(
			"%s: Expected output %v, got %v", output, _output, out))
	}

	test([]KeyStroke{
		{vaxis.ModifierMask(0), 'a'},
	}, BINDING_INCOMPLETE, "")
	test([]KeyStroke{
		{vaxis.ModifierMask(0), 'a'},
		{vaxis.ModifierMask(0), 'b'},
		{vaxis.ModifierMask(0), 'c'},
	}, BINDING_FOUND, ":abc")
	test([]KeyStroke{
		{vaxis.ModifierMask(0), 'c'},
		{vaxis.ModifierMask(0), 'b'},
		{vaxis.ModifierMask(0), 'a'},
	}, BINDING_FOUND, ":cba")
	test([]KeyStroke{
		{vaxis.ModifierMask(0), 'f'},
		{vaxis.ModifierMask(0), 'o'},
	}, BINDING_INCOMPLETE, "")
	test([]KeyStroke{
		{vaxis.ModifierMask(0), '4'},
		{vaxis.ModifierMask(0), '0'},
		{vaxis.ModifierMask(0), '4'},
	}, BINDING_NOT_FOUND, "")

	add("<C-a>", "c-a")
	add("<C-Down>", ":next")
	add("<C-PgUp>", ":prev")
	add("<C-Enter>", ":open")
	test([]KeyStroke{
		{vaxis.ModCtrl, 'a'},
	}, BINDING_FOUND, "c-a")
	test([]KeyStroke{
		{vaxis.ModCtrl, vaxis.KeyDown},
	}, BINDING_FOUND, ":next")
	test([]KeyStroke{
		{vaxis.ModCtrl, vaxis.KeyPgUp},
	}, BINDING_FOUND, ":prev")
	test([]KeyStroke{
		{vaxis.ModCtrl, vaxis.KeyPgDown},
	}, BINDING_NOT_FOUND, "")
	test([]KeyStroke{
		{vaxis.ModCtrl, vaxis.KeyEnter},
	}, BINDING_FOUND, ":open")
}