test(table): ensure README example works

This commit is contained in:
Maas Lalani 2023-10-10 11:32:38 -04:00
parent 4476263d05
commit 42db873617
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
2 changed files with 51 additions and 2 deletions

View File

@ -36,7 +36,6 @@ func main() {
{"Arabic", "أهلين", "أهلا"},
{"Russian", "Здравствуйте", "Привет"},
{"Spanish", "Hola", "¿Qué tal?"},
{"English", "You look absolutely fabulous.", "How's it going?"},
}
t := table.New().
@ -60,7 +59,7 @@ func main() {
}
// Arabic is a right-to-left language, so right align the text.
if rows[row-1][0] == "Arabic" && col != 0 {
if row < len(rows) && rows[row-1][0] == "Arabic" && col != 0 {
style = style.Copy().Align(lipgloss.Right)
}
@ -69,5 +68,7 @@ func main() {
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
t.Row("English", "You look absolutely fabulous.", "How's it going?")
fmt.Println(t)
}

View File

@ -46,6 +46,54 @@ func TestTable(t *testing.T) {
}
}
func TestTableExample(t *testing.T) {
HeaderStyle := lipgloss.NewStyle().Padding(0, 1).Align(lipgloss.Center)
EvenRowStyle := lipgloss.NewStyle().Padding(0, 1)
OddRowStyle := lipgloss.NewStyle().Padding(0, 1)
rows := [][]string{
{"Chinese", "您好", "你好"},
{"Japanese", "こんにちは", "やあ"},
{"Russian", "Здравствуйте", "Привет"},
{"Spanish", "Hola", "¿Qué tal?"},
}
table := New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == 0:
return HeaderStyle
case row%2 == 0:
return EvenRowStyle
default:
return OddRowStyle
}
}).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)
// You can also add tables row-by-row
table.Row("English", "You look absolutely fabulous.", "How's it going?")
expected := strings.TrimSpace(`
LANGUAGE FORMAL INFORMAL
Chinese 您好 你好
Japanese こんにちは やあ
Russian Здравствуйте Привет
Spanish Hola ¿Qué tal?
English You look absolutely fabulous. How's it going?
`)
if table.String() != expected {
t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, table.String())
}
}
func TestTableEmpty(t *testing.T) {
table := New().
Border(lipgloss.NormalBorder()).