1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/filetree/file_node_test.go

164 lines
3.8 KiB
Go
Raw Normal View History

2021-03-21 06:58:15 +02:00
package filetree
import (
"testing"
2021-03-21 06:58:15 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/stretchr/testify/assert"
)
func TestCompress(t *testing.T) {
scenarios := []struct {
name string
root *Node[models.File]
expected *Node[models.File]
}{
{
name: "nil node",
root: nil,
expected: nil,
},
{
name: "leaf node",
root: &Node[models.File]{
2021-03-21 01:46:43 +02:00
Path: "",
Children: []*Node[models.File]{
2021-03-21 06:58:15 +02:00
{File: &models.File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
},
},
expected: &Node[models.File]{
2021-03-21 01:46:43 +02:00
Path: "",
Children: []*Node[models.File]{
2021-03-21 06:58:15 +02:00
{File: &models.File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
},
},
},
{
name: "big example",
root: &Node[models.File]{
2021-03-21 01:46:43 +02:00
Path: "",
Children: []*Node[models.File]{
{
Path: "dir1",
Children: []*Node[models.File]{
{
2021-03-21 06:58:15 +02:00
File: &models.File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir1/file2",
},
},
},
{
Path: "dir2",
Children: []*Node[models.File]{
{
2021-03-21 06:58:15 +02:00
File: &models.File{Name: "file3", ShortStatus: " M", HasStagedChanges: true},
Path: "dir2/file3",
},
{
2021-03-21 06:58:15 +02:00
File: &models.File{Name: "file4", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir2/file4",
},
},
},
{
Path: "dir3",
Children: []*Node[models.File]{
{
Path: "dir3/dir3-1",
Children: []*Node[models.File]{
{
2021-03-21 06:58:15 +02:00
File: &models.File{Name: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir3/dir3-1/file5",
},
},
},
},
},
{
2021-03-21 06:58:15 +02:00
File: &models.File{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "file1",
},
},
},
expected: &Node[models.File]{
2021-03-21 01:46:43 +02:00
Path: "",
Children: []*Node[models.File]{
{
2021-07-27 13:00:33 +02:00
Path: "dir1",
Children: []*Node[models.File]{
2021-07-27 13:00:33 +02:00
{
File: &models.File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir1/file2",
},
},
},
{
Path: "dir2",
Children: []*Node[models.File]{
{
2021-03-21 06:58:15 +02:00
File: &models.File{Name: "file3", ShortStatus: " M", HasStagedChanges: true},
Path: "dir2/file3",
},
{
2021-03-21 06:58:15 +02:00
File: &models.File{Name: "file4", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir2/file4",
},
},
},
{
2021-07-27 13:00:33 +02:00
Path: "dir3/dir3-1",
CompressionLevel: 1,
Children: []*Node[models.File]{
2021-07-27 13:00:33 +02:00
{
File: &models.File{Name: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir3/dir3-1/file5",
},
},
},
{
2021-03-21 06:58:15 +02:00
File: &models.File{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "file1",
},
},
},
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
s.root.Compress()
assert.EqualValues(t, s.expected, s.root)
})
}
}
2022-01-25 16:20:19 +02:00
func TestGetFile(t *testing.T) {
scenarios := []struct {
name string
viewModel *FileTree
2022-01-25 16:20:19 +02:00
path string
expected *models.File
}{
{
name: "valid case",
viewModel: NewFileTree(func() []*models.File { return []*models.File{{Name: "blah/one"}, {Name: "blah/two"}} }, nil, false),
2022-01-25 16:20:19 +02:00
path: "blah/two",
expected: &models.File{Name: "blah/two"},
},
{
name: "not found",
viewModel: NewFileTree(func() []*models.File { return []*models.File{{Name: "blah/one"}, {Name: "blah/two"}} }, nil, false),
2022-01-25 16:20:19 +02:00
path: "blah/three",
expected: nil,
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
assert.EqualValues(t, s.expected, s.viewModel.GetFile(s.path))
})
}
}