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

223 lines
4.0 KiB
Go
Raw Normal View History

2021-05-30 07:22:04 +02:00
package utils
import (
"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
expected string
2021-10-31 13:29:43 +02:00
}
tests := []scenario{
{
input: [][]string{{""}, {""}},
columnAlignments: nil,
expected: "",
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"a"}, {""}},
columnAlignments: nil,
expected: "a\n",
},
{
input: [][]string{{"a"}, {"b"}},
columnAlignments: nil,
expected: "a\nb",
},
{
input: [][]string{{"a", "b"}, {"c", "d"}},
columnAlignments: nil,
expected: "a b\nc d",
},
{
input: [][]string{{"a", "", "c"}, {"d", "", "f"}},
columnAlignments: nil,
expected: "a c\nd f",
},
{
input: [][]string{{"a", "", "c", ""}, {"d", "", "f", ""}},
columnAlignments: nil,
expected: "a c\nd f",
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
columnAlignments: nil,
expected: "abc d\ne f",
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
columnAlignments: []Alignment{AlignLeft, AlignLeft}, // same as nil (default)
expected: "abc d\ne f",
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
columnAlignments: []Alignment{AlignRight, AlignLeft},
expected: "abc d\n e f",
2021-10-31 13:29:43 +02:00
},
{
input: [][]string{{"abc", "", "d", ""}, {"e", "", "f", ""}},
columnAlignments: []Alignment{AlignRight}, // gracefully defaults unspecified columns to left-align
expected: "abc d\n e f",
2021-10-31 13:29:43 +02:00
},
}
for _, test := range tests {
output := RenderDisplayStrings(test.input, test.columnAlignments)
2023-08-19 18:10:25 +02:00
assert.EqualValues(t, test.expected, output)
2021-10-31 13:29:43 +02:00
}
}