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

248 lines
5.4 KiB
Go
Raw Normal View History

2021-05-30 07:22:04 +02:00
package utils
import (
"strings"
2021-05-30 07:22:04 +02:00
"testing"
"github.com/stretchr/testify/assert"
)
func TestWithPadding(t *testing.T) {
type scenario struct {
str string
padding int
alignment Alignment
expected string
2021-05-30 07:22:04 +02:00
}
scenarios := []scenario{
{
str: "hello world !",
padding: 1,
alignment: AlignLeft,
expected: "hello world !",
2021-05-30 07:22:04 +02:00
},
{
str: "hello world !",
padding: 14,
alignment: AlignLeft,
expected: "hello world ! ",
},
{
str: "hello world !",
padding: 14,
alignment: AlignRight,
expected: " hello world !",
2021-05-30 07:22:04 +02:00
},
2021-10-24 09:18:08 +02:00
{
str: "Güçlü",
padding: 7,
alignment: AlignLeft,
expected: "Güçlü ",
},
{
str: "Güçlü",
padding: 7,
alignment: AlignRight,
expected: " Güçlü",
2021-10-24 09:18:08 +02:00
},
2021-05-30 07:22:04 +02:00
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, WithPadding(s.str, s.padding, s.alignment))
2021-05-30 07:22:04 +02:00
}
}
func TestGetPadWidths(t *testing.T) {
type scenario struct {
2021-10-24 07:26:03 +02:00
input [][]string
expected []int
2021-05-30 07:22:04 +02:00
}
2021-10-24 07:26:03 +02:00
tests := []scenario{
2021-05-30 07:22:04 +02:00
{
[][]string{{""}, {""}},
[]int{},
},
{
[][]string{{"a"}, {""}},
[]int{},
},
{
[][]string{{"aa", "b", "ccc"}, {"c", "d", "e"}},
[]int{2, 1},
},
2021-10-24 07:26:03 +02:00
{
[][]string{{"AŁ", "b", "ccc"}, {"c", "d", "e"}},
[]int{2, 1},
},
2021-05-30 07:22:04 +02:00
}
2021-10-24 07:26:03 +02:00
for _, test := range tests {
output := getPadWidths(test.input)
2023-08-19 18:10:25 +02:00
assert.EqualValues(t, test.expected, output)
2021-05-30 07:22:04 +02:00
}
}
2021-10-30 11:15:50 +02:00
func TestTruncateWithEllipsis(t *testing.T) {
// will need to check chinese characters as well
// important that we have a three dot ellipsis within the limit
type scenario struct {
str string
limit int
expected string
}
scenarios := []scenario{
{
"hello world !",
1,
".",
},
{
"hello world !",
2,
"..",
},
{
"hello world !",
3,
"...",
},
{
"hello world !",
4,
"h...",
},
{
"hello world !",
5,
"he...",
},
{
"hello world !",
12,
"hello wor...",
},
{
"hello world !",
13,
"hello world !",
},
{
"hello world !",
14,
"hello world !",
},
{
"大大大大",
5,
"大...",
},
{
"大大大大",
2,
"..",
},
{
"大大大大",
0,
"",
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, TruncateWithEllipsis(s.str, s.limit))
}
}
2021-10-31 13:29:43 +02:00
func TestRenderDisplayStrings(t *testing.T) {
type scenario struct {
input [][]string
columnAlignments []Alignment
expectedOutput string
expectedColumnPositions []int
2021-10-31 13:29:43 +02:00
}
tests := []scenario{
{
input: [][]string{{""}, {""}},
columnAlignments: nil,
expectedOutput: "",
expectedColumnPositions: []int{0, 0},
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"a"}, {""}},
columnAlignments: nil,
expectedOutput: "a\n",
expectedColumnPositions: []int{0},
},
{
input: [][]string{{"a"}, {"b"}},
columnAlignments: nil,
expectedOutput: "a\nb",
expectedColumnPositions: []int{0},
},
{
input: [][]string{{"a", "b"}, {"c", "d"}},
columnAlignments: nil,
expectedOutput: "a b\nc d",
expectedColumnPositions: []int{0, 2},
},
{
input: [][]string{{"a", "", "c"}, {"d", "", "f"}},
columnAlignments: nil,
expectedOutput: "a c\nd f",
expectedColumnPositions: []int{0, 2, 2},
},
{
input: [][]string{{"a", "", "c", ""}, {"d", "", "f", ""}},
columnAlignments: nil,
expectedOutput: "a c\nd f",
expectedColumnPositions: []int{0, 2, 2},
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
columnAlignments: nil,
expectedOutput: "abc d\ne f",
expectedColumnPositions: []int{0, 4, 4},
},
{
input: [][]string{{"", "abc", "", "", "d", "e"}, {"", "f", "", "", "g", "h"}},
columnAlignments: nil,
expectedOutput: "abc d e\nf g h",
expectedColumnPositions: []int{0, 0, 4, 4, 4, 6},
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
columnAlignments: []Alignment{AlignLeft, AlignLeft}, // same as nil (default)
expectedOutput: "abc d\ne f",
expectedColumnPositions: []int{0, 4, 4},
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
columnAlignments: []Alignment{AlignRight, AlignLeft},
expectedOutput: "abc d\n e f",
expectedColumnPositions: []int{0, 4, 4},
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"a", "", "bcd", "efg", "h"}, {"i", "", "j", "k", "l"}},
columnAlignments: []Alignment{AlignLeft, AlignLeft, AlignRight, AlignLeft},
expectedOutput: "a bcd efg h\ni j k l",
expectedColumnPositions: []int{0, 2, 2, 6, 10},
},
2021-10-31 13:29:43 +02:00
{
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
columnAlignments: []Alignment{AlignRight}, // gracefully defaults unspecified columns to left-align
expectedOutput: "abc d\n e f",
expectedColumnPositions: []int{0, 4, 4},
2021-10-31 13:29:43 +02:00
},
}
for _, test := range tests {
output, columnPositions := RenderDisplayStrings(test.input, test.columnAlignments)
assert.EqualValues(t, test.expectedOutput, strings.Join(output, "\n"))
assert.EqualValues(t, test.expectedColumnPositions, columnPositions)
2021-10-31 13:29:43 +02:00
}
}