1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-15 01:34:26 +02:00

Add tests for renderLines

These are very simple yet, but we'll extend them in the next commits.
This commit is contained in:
Stefan Haller
2023-08-17 21:43:18 +02:00
parent 198ead7c14
commit ec22570eac

View File

@ -0,0 +1,74 @@
package context
import (
"strings"
"testing"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
)
func TestListRenderer_renderLines(t *testing.T) {
scenarios := []struct {
name string
modelStrings []string
startIdx int
endIdx int
expectedOutput string
}{
{
name: "Render whole list",
modelStrings: []string{"a", "b", "c"},
startIdx: 0,
endIdx: 3,
expectedOutput: `
a
b
c`,
},
{
name: "Partial list, beginning",
modelStrings: []string{"a", "b", "c"},
startIdx: 0,
endIdx: 2,
expectedOutput: `
a
b`,
},
{
name: "Partial list, end",
modelStrings: []string{"a", "b", "c"},
startIdx: 1,
endIdx: 3,
expectedOutput: `
b
c`,
},
{
name: "Pass an endIdx greater than the model length",
modelStrings: []string{"a", "b", "c"},
startIdx: 2,
endIdx: 5,
expectedOutput: `
c`,
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
viewModel := NewListViewModel[string](func() []string { return s.modelStrings })
self := &ListRenderer{
list: viewModel,
getDisplayStrings: func(startIdx int, endIdx int) [][]string {
return lo.Map(s.modelStrings[startIdx:endIdx],
func(s string, _ int) []string { return []string{s} })
},
}
expectedOutput := strings.Join(lo.Map(
strings.Split(strings.TrimPrefix(s.expectedOutput, "\n"), "\n"),
func(line string, _ int) string { return strings.TrimSpace(line) }), "\n")
assert.Equal(t, expectedOutput, self.renderLines(s.startIdx, s.endIdx))
})
}
}