Skip to content

Commit

Permalink
Render Row Tests (charmbracelet#487)
Browse files Browse the repository at this point in the history
* test(table): add renderRow test

* refactor(table): row tests

---------

Co-authored-by: Bill Havanki <[email protected]>
  • Loading branch information
maaslalani and bhavanki committed Feb 28, 2024
1 parent 144d53d commit 81efbf7
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion table/table_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package table

import "testing"
import (
"testing"

"github.com/charmbracelet/lipgloss"
)

func TestFromValues(t *testing.T) {
input := "foo1,bar1\nfoo2,bar2\nfoo3,bar3"
Expand Down Expand Up @@ -52,3 +56,55 @@ func deepEqual(a, b []Row) bool {
}
return true
}

var (
cols = []Column{
{Title: "col1", Width: 10},
{Title: "col2", Width: 10},
{Title: "col3", Width: 10},
}
)

func TestRenderRow(t *testing.T) {
tests := []struct {
name string
table *Model
expected string
}{
{
name: "simple row",
table: &Model{
rows: []Row{{"Foooooo", "Baaaaar", "Baaaaaz"}},
cols: cols,
styles: Styles{Cell: lipgloss.NewStyle()},
},
expected: "Foooooo Baaaaar Baaaaaz ",
},
{
name: "simple row with truncations",
table: &Model{
rows: []Row{{"Foooooooooo", "Baaaaaaaaar", "Quuuuuuuuux"}},
cols: cols,
styles: Styles{Cell: lipgloss.NewStyle()},
},
expected: "Foooooooo…Baaaaaaaa…Quuuuuuuu…",
},
{
name: "simple row avoiding truncations",
table: &Model{
rows: []Row{{"Fooooooooo", "Baaaaaaaar", "Quuuuuuuux"}},
cols: cols,
styles: Styles{Cell: lipgloss.NewStyle()},
},
expected: "FoooooooooBaaaaaaaarQuuuuuuuux",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
row := tc.table.renderRow(0)
if row != tc.expected {
t.Fatalf("\n\nWant: \n%s\n\nGot: \n%s\n", tc.expected, row)
}
})
}
}

0 comments on commit 81efbf7

Please sign in to comment.