Skip to content

Commit

Permalink
test(table): ensure README example works
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Oct 10, 2023
1 parent 4476263 commit 42db873
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
5 changes: 3 additions & 2 deletions examples/table/languages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func main() {
{"Arabic", "Ψ£Ω‡Ω„ΩŠΩ†", "Ψ£Ω‡Ω„Ψ§"},
{"Russian", "ЗдравствуйтС", "ΠŸΡ€ΠΈΠ²Π΅Ρ‚"},
{"Spanish", "Hola", "ΒΏQuΓ© tal?"},
{"English", "You look absolutely fabulous.", "How's it going?"},
}

t := table.New().
Expand All @@ -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)
}

Expand All @@ -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)
}
48 changes: 48 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()).
Expand Down

0 comments on commit 42db873

Please sign in to comment.