From af7e9ed3e13ae2db2e7b849ee1a2a2756ac24025 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Wed, 28 Feb 2024 11:30:22 -0500 Subject: [PATCH] Render Row Tests (#487) * test(table): add renderRow test * refactor(table): row tests --------- Co-authored-by: Bill Havanki --- table/table_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/table/table_test.go b/table/table_test.go index c2929c6..d927be0 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -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" @@ -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) + } + }) + } +}