jesseduffield.lazygit/pkg/cheatsheet/generate_test.go
Stefan Haller 3d18ee8f91 Use a slice of keys for each binding
This is a pure refactor in preparation for letting users configure multiple
alternate bindings for a single command. Every Binding still has exactly one
key, so nothing changes visibly: the cheatsheet, the on-screen options bar,
and the keybindings menu all render identically.

When a Binding ends up with multiple keys, the on-screen options bar will
show only the first (to avoid clutter); the cheatsheet will show all of them (in
a later commit). For now both paths take Key[0].

MenuItem.Key is changed in the same way, it also has a slice of keys now.

In this commit we keep the name `Key` in Binding, KeybindingOpts and MenuItem,
instead of renaming them to `Keys` right away, in order to keep the diff a bit
more readable. We'll do the rename separately in the next commit.
2026-05-25 15:18:18 +02:00

271 lines
6 KiB
Go

package cheatsheet
import (
"testing"
"github.com/jesseduffield/lazygit/pkg/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/stretchr/testify/assert"
)
func TestGetBindingSections(t *testing.T) {
tr := i18n.EnglishTranslationSet()
tests := []struct {
testName string
bindings []*types.Binding
expected []*bindingSection
}{
{
testName: "no bindings",
bindings: []*types.Binding{},
expected: []*bindingSection{},
},
{
testName: "one binding",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "stage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
expected: []*bindingSection{
{
title: "Files",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "stage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
},
},
},
{
testName: "global binding",
bindings: []*types.Binding{
{
ViewName: "",
Description: "quit",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
expected: []*bindingSection{
{
title: "Global keybindings",
bindings: []*types.Binding{
{
ViewName: "",
Description: "quit",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
},
},
},
{
testName: "grouped bindings",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "stage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "files",
Description: "unstage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "submodules",
Description: "drop submodule",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
expected: []*bindingSection{
{
title: "Files",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "stage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "files",
Description: "unstage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
},
{
title: "Submodules",
bindings: []*types.Binding{
{
ViewName: "submodules",
Description: "drop submodule",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
},
},
},
{
testName: "with navigation bindings",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "stage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "files",
Description: "unstage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "files",
Description: "scroll",
Key: []gocui.Key{gocui.NewKeyRune('a')},
Tag: "navigation",
},
{
ViewName: "commits",
Description: "revert commit",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
expected: []*bindingSection{
{
title: "List panel navigation",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "scroll",
Key: []gocui.Key{gocui.NewKeyRune('a')},
Tag: "navigation",
},
},
},
{
title: "Commits",
bindings: []*types.Binding{
{
ViewName: "commits",
Description: "revert commit",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
},
{
title: "Files",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "stage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "files",
Description: "unstage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
},
},
},
{
testName: "with duplicate navigation bindings",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "stage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "files",
Description: "unstage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "files",
Description: "scroll",
Key: []gocui.Key{gocui.NewKeyRune('a')},
Tag: "navigation",
},
{
ViewName: "commits",
Description: "revert commit",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "commits",
Description: "scroll",
Key: []gocui.Key{gocui.NewKeyRune('a')},
Tag: "navigation",
},
{
ViewName: "commits",
Description: "page up",
Key: []gocui.Key{gocui.NewKeyRune('a')},
Tag: "navigation",
},
},
expected: []*bindingSection{
{
title: "List panel navigation",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "scroll",
Key: []gocui.Key{gocui.NewKeyRune('a')},
Tag: "navigation",
},
{
ViewName: "commits",
Description: "page up",
Key: []gocui.Key{gocui.NewKeyRune('a')},
Tag: "navigation",
},
},
},
{
title: "Commits",
bindings: []*types.Binding{
{
ViewName: "commits",
Description: "revert commit",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
},
{
title: "Files",
bindings: []*types.Binding{
{
ViewName: "files",
Description: "stage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
{
ViewName: "files",
Description: "unstage file",
Key: []gocui.Key{gocui.NewKeyRune('a')},
},
},
},
},
},
}
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
actual := getBindingSections(test.bindings, tr)
assert.EqualValues(t, test.expected, actual)
})
}
}