Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(table): add renderRow test #385

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 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,75 @@ func deepEqual(a, b []Row) bool {
}
return true
}

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

func TestRenderRow(t *testing.T) {
type testcase struct {
m *Model
rowID int
expectedRow string
name string
}
testcases := []testcase{
{
m: &Model{
rows: []Row{
[]string{"valuea1", "valuea2", "valuea3"},
},
cols: modelCols,
cursor: -1,
styles: Styles{
Cell: lipgloss.NewStyle(),
},
},
rowID: 0,
expectedRow: "valuea1 valuea2 valuea3 ",
name: "simple row",
},
{
m: &Model{
rows: []Row{
[]string{"valuea11111", "valuea22222", "valuea33333"},
},
cols: modelCols,
cursor: -1,
styles: Styles{
Cell: lipgloss.NewStyle(),
},
},
rowID: 0,
expectedRow: "valuea111…valuea222…valuea333…",
name: "simple row with truncations",
},
{
m: &Model{
rows: []Row{
[]string{"valuea1111", "valuea2222", "valuea3333"},
},
cols: modelCols,
cursor: -1,
styles: Styles{
Cell: lipgloss.NewStyle(),
},
},
rowID: 0,
expectedRow: "valuea1111valuea2222valuea3333",
name: "simple row avoiding truncations",
},
}
for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
row := testcase.m.renderRow(testcase.rowID)
if row != testcase.expectedRow {
t.Fatalf("expected row contents |%s|, got |%s|", testcase.expectedRow, row)
}
})
}
}