mirror of
https://github.com/charmbracelet/lipgloss.git
synced 2024-10-26 22:57:49 +03:00
test(table): ensure README example works
This commit is contained in:
parent
4476263d05
commit
42db873617
@ -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)
|
||||
}
|
||||
|
@ -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()).
|
||||
|
Loading…
Reference in New Issue
Block a user