From 544bf61fea6064463a58f583d083ed7deb495c5e Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Mon, 26 Aug 2024 12:10:27 +1000 Subject: [PATCH 01/12] test: add table padding unit tests --- table/table_test.go | 57 +++++++++++++++++++ .../TestCellPadding/With_padding.golden | 4 ++ .../Without_padding;_exact_width.golden | 4 ++ .../Without_padding;_too_narrow.golden | 4 ++ 4 files changed, 69 insertions(+) create mode 100644 table/testdata/TestCellPadding/With_padding.golden create mode 100644 table/testdata/TestCellPadding/Without_padding;_exact_width.golden create mode 100644 table/testdata/TestCellPadding/Without_padding;_too_narrow.golden diff --git a/table/table_test.go b/table/table_test.go index cc49f0d3..2c185449 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -157,3 +157,60 @@ func TestTableAlignment(t *testing.T) { golden.RequireEqual(t, []byte(got)) }) } + +func TestCellPadding(t *testing.T) { + tt := map[string]struct { + tableWidth int + styles Styles + }{ + "With padding": { + tableWidth: 21, + styles: Styles{ + Selected: lipgloss.NewStyle(), + Header: lipgloss.NewStyle().Padding(0, 1), + Cell: lipgloss.NewStyle().Padding(0, 1), + }, + }, + "Without padding; exact width": { + tableWidth: 15, + styles: Styles{ + Selected: lipgloss.NewStyle(), + Header: lipgloss.NewStyle(), + Cell: lipgloss.NewStyle(), + }, + }, + "Without padding; too narrow": { + tableWidth: 10, + styles: Styles{ + Selected: lipgloss.NewStyle(), + Header: lipgloss.NewStyle(), + Cell: lipgloss.NewStyle(), + }, + }, + } + + for name, tc := range tt { + t.Run(name, func(t *testing.T) { + table := New( + WithHeight(4), + WithWidth(tc.tableWidth), + WithColumns([]Column{ + {Title: "One", Width: 5}, + {Title: "Two", Width: 5}, + {Title: "Three", Width: 5}, + }), + WithRows([]Row{ + {"r1c1-", "r1c2-", "r1c3-"}, + {"r2c1-", "r2c2-", "r2c3-"}, + {"r3c1-", "r3c2-", "r3c3-"}, + {"r4c1-", "r4c2-", "r4c3-"}, + }), + WithStyles(tc.styles), + ) + + got := ansi.Strip(table.View()) + + golden.RequireEqual(t, []byte(got)) + }) + } +} diff --git a/table/testdata/TestCellPadding/With_padding.golden b/table/testdata/TestCellPadding/With_padding.golden new file mode 100644 index 00000000..50266968 --- /dev/null +++ b/table/testdata/TestCellPadding/With_padding.golden @@ -0,0 +1,4 @@ + One Two Three + r1c1- r1c2- r1c3- + r2c1- r2c2- r2c3- + r3c1- r3c2- r3c3- \ No newline at end of file diff --git a/table/testdata/TestCellPadding/Without_padding;_exact_width.golden b/table/testdata/TestCellPadding/Without_padding;_exact_width.golden new file mode 100644 index 00000000..2d097867 --- /dev/null +++ b/table/testdata/TestCellPadding/Without_padding;_exact_width.golden @@ -0,0 +1,4 @@ +One Two Three +r1c1-r1c2-r1c3- +r2c1-r2c2-r2c3- +r3c1-r3c2-r3c3- \ No newline at end of file diff --git a/table/testdata/TestCellPadding/Without_padding;_too_narrow.golden b/table/testdata/TestCellPadding/Without_padding;_too_narrow.golden new file mode 100644 index 00000000..02c62132 --- /dev/null +++ b/table/testdata/TestCellPadding/Without_padding;_too_narrow.golden @@ -0,0 +1,4 @@ +One Two Three +r1c1-r1c2- +r1c3- +r2c1-r2c2- \ No newline at end of file From 62e39b2c0a574b58e9734bafe8274afcebc186e3 Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Mon, 26 Aug 2024 12:33:46 +1000 Subject: [PATCH 02/12] test: add table centering unit test --- table/table_test.go | 27 +++++++++++++++++++ .../Centered_in_a_box.golden | 5 ++++ 2 files changed, 32 insertions(+) create mode 100644 table/testdata/TestTableCentering/Centered_in_a_box.golden diff --git a/table/table_test.go b/table/table_test.go index 2c185449..76e50633 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -214,3 +214,30 @@ func TestCellPadding(t *testing.T) { }) } } + +func TestTableCentering(t *testing.T) { + t.Run("Centered in a box", func(t *testing.T) { + table := New( + WithHeight(5), + WithWidth(40), + WithColumns([]Column{ + {Title: "One", Width: 5}, + {Title: "Two", Width: 5}, + {Title: "Three", Width: 5}, + }), + WithRows([]Row{ + {"r1c1-", "r1c2-", "r1c3-"}, + {"r2c1-", "r2c2-", "r2c3-"}, + {"r3c1-", "r3c2-", "r3c3-"}, + {"r4c1-", "r4c2-", "r4c3-"}, + }), + ) + + tableView := ansi.Strip(table.View()) + got := lipgloss.NewStyle().Align(lipgloss.Center).Render( + tableView, + ) + + golden.RequireEqual(t, []byte(got)) + }) +} diff --git a/table/testdata/TestTableCentering/Centered_in_a_box.golden b/table/testdata/TestTableCentering/Centered_in_a_box.golden new file mode 100644 index 00000000..36dfec40 --- /dev/null +++ b/table/testdata/TestTableCentering/Centered_in_a_box.golden @@ -0,0 +1,5 @@ + One Two Three + r1c1- r1c2- r1c3- + r2c1- r2c2- r2c3- + r3c1- r3c2- r3c3- + r4c1- r4c2- r4c3- \ No newline at end of file From 14aeeeca7fb91c84da190e4b602e09aa950bb7b2 Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Mon, 26 Aug 2024 12:41:10 +1000 Subject: [PATCH 03/12] chroe: add todo comment to table padding test --- table/table_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/table/table_test.go b/table/table_test.go index 76e50633..e2f2a9db 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -179,6 +179,8 @@ func TestCellPadding(t *testing.T) { Cell: lipgloss.NewStyle(), }, }, + // TODO: Adjust the golden file once a desired output has been decided + // https://github.com/charmbracelet/bubbles/issues/472 "Without padding; too narrow": { tableWidth: 10, styles: Styles{ From efa59d48f28f3dadb23b88514c4ea34f03efa86e Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Mon, 26 Aug 2024 12:43:42 +1000 Subject: [PATCH 04/12] test: fix table centering unit test --- table/table_test.go | 8 +++++--- .../TestTableCentering/Centered_in_a_box.golden | 12 +++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/table/table_test.go b/table/table_test.go index e2f2a9db..c2d06014 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -219,6 +219,10 @@ func TestCellPadding(t *testing.T) { func TestTableCentering(t *testing.T) { t.Run("Centered in a box", func(t *testing.T) { + boxStyle := lipgloss.NewStyle(). + BorderStyle(lipgloss.NormalBorder()). + Align(lipgloss.Center) + table := New( WithHeight(5), WithWidth(40), @@ -236,9 +240,7 @@ func TestTableCentering(t *testing.T) { ) tableView := ansi.Strip(table.View()) - got := lipgloss.NewStyle().Align(lipgloss.Center).Render( - tableView, - ) + got := boxStyle.Render(tableView) golden.RequireEqual(t, []byte(got)) }) diff --git a/table/testdata/TestTableCentering/Centered_in_a_box.golden b/table/testdata/TestTableCentering/Centered_in_a_box.golden index 36dfec40..c8998994 100644 --- a/table/testdata/TestTableCentering/Centered_in_a_box.golden +++ b/table/testdata/TestTableCentering/Centered_in_a_box.golden @@ -1,5 +1,7 @@ - One Two Three - r1c1- r1c2- r1c3- - r2c1- r2c2- r2c3- - r3c1- r3c2- r3c3- - r4c1- r4c2- r4c3- \ No newline at end of file +┌────────────────────────────────────────┐ +│ One Two Three │ +│ r1c1- r1c2- r1c3- │ +│ r2c1- r2c2- r2c3- │ +│ r3c1- r3c2- r3c3- │ +│ r4c1- r4c2- r4c3- │ +└────────────────────────────────────────┘ \ No newline at end of file From 0503cd345fa839e195d0a194975fb83e7d72254b Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Tue, 27 Aug 2024 15:09:06 +1000 Subject: [PATCH 05/12] test: add table cursor nav unit tests --- table/table_test.go | 129 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/table/table_test.go b/table/table_test.go index c2d06014..d0eab0be 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -245,3 +245,132 @@ func TestTableCentering(t *testing.T) { golden.RequireEqual(t, []byte(got)) }) } + +func TestCursorNavigation(t *testing.T) { + tests := map[string]struct { + rows []Row + action func(*Model) + want int + }{ + "New": { + rows: []Row{ + {"r1"}, + {"r2"}, + {"r3"}, + }, + action: func(_ *Model) {}, + want: 0, + }, + "MoveDown": { + rows: []Row{ + {"r1"}, + {"r2"}, + {"r3"}, + {"r4"}, + }, + action: func(t *Model) { + t.MoveDown(2) + }, + want: 2, + }, + "MoveUp": { + rows: []Row{ + {"r1"}, + {"r2"}, + {"r3"}, + {"r4"}, + }, + action: func(t *Model) { + t.cursor = 3 + t.MoveUp(2) + }, + want: 1, + }, + "GotoBottom": { + rows: []Row{ + {"r1"}, + {"r2"}, + {"r3"}, + {"r4"}, + }, + action: func(t *Model) { + t.GotoBottom() + }, + want: 3, + }, + "GotoTop": { + rows: []Row{ + {"r1"}, + {"r2"}, + {"r3"}, + {"r4"}, + }, + action: func(t *Model) { + t.cursor = 3 + t.GotoTop() + }, + want: 0, + }, + "SetCursor": { + rows: []Row{ + {"r1"}, + {"r2"}, + {"r3"}, + {"r4"}, + }, + action: func(t *Model) { + t.SetCursor(2) + }, + want: 2, + }, + "MoveDown with overflow": { + rows: []Row{ + {"r1"}, + {"r2"}, + {"r3"}, + {"r4"}, + }, + action: func(t *Model) { + t.MoveDown(5) + }, + want: 3, + }, + "MoveUp with overflow": { + rows: []Row{ + {"r1"}, + {"r2"}, + {"r3"}, + {"r4"}, + }, + action: func(t *Model) { + t.cursor = 3 + t.MoveUp(5) + }, + want: 0, + }, + "Blur does not stop movement": { + rows: []Row{ + {"r1"}, + {"r2"}, + {"r3"}, + {"r4"}, + }, + action: func(t *Model) { + t.Blur() + t.MoveDown(2) + }, + want: 2, + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + table := New(WithColumns(cols), WithRows(tc.rows)) + tc.action(&table) + + if table.Cursor() != tc.want { + t.Errorf("want %d, got %d", tc.want, table.Cursor()) + } + }) + } +} From c54e735333455403304ba6fd552adb22792b013f Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Tue, 27 Aug 2024 15:45:47 +1000 Subject: [PATCH 06/12] test: add table new func unit tests --- table/table_test.go | 163 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) diff --git a/table/table_test.go b/table/table_test.go index d0eab0be..6280cf1e 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -1,8 +1,11 @@ package table import ( + "reflect" "testing" + "github.com/charmbracelet/bubbles/help" + "github.com/charmbracelet/bubbles/viewport" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "github.com/charmbracelet/x/exp/golden" @@ -374,3 +377,163 @@ func TestCursorNavigation(t *testing.T) { }) } } + +func TestNew(t *testing.T) { + tests := map[string]struct { + opts []Option + want Model + }{ + "Default": { + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + }, + }, + "WithColumns": { + opts: []Option{ + WithColumns([]Column{ + {Title: "Foo", Width: 1}, + {Title: "Bar", Width: 2}, + }), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + cols: []Column{ + {Title: "Foo", Width: 1}, + {Title: "Bar", Width: 2}, + }, + }, + }, + "WithCols; WithRows": { + opts: []Option{ + WithColumns([]Column{ + {Title: "Foo", Width: 1}, + {Title: "Bar", Width: 2}, + }), + WithRows([]Row{ + {"1", "Foo"}, + {"2", "Bar"}, + }), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + cols: []Column{ + {Title: "Foo", Width: 1}, + {Title: "Bar", Width: 2}, + }, + rows: []Row{ + {"1", "Foo"}, + {"2", "Bar"}, + }, + }, + }, + "WithHeight": { + opts: []Option{ + WithHeight(10), + }, + want: Model{ + // Default fields + cursor: 0, + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + // Viewport height is 1 less than the provided height when no header is present since lipgloss.Height adds 1 + viewport: viewport.New(0, 9), + }, + }, + "WithWidth": { + opts: []Option{ + WithWidth(10), + }, + want: Model{ + // Default fields + cursor: 0, + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + // Viewport height is 1 less than the provided height when no header is present since lipgloss.Height adds 1 + viewport: viewport.New(10, 20), + }, + }, + "WithFocused": { + opts: []Option{ + WithFocused(true), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + focus: true, + }, + }, + "WithStyles": { + opts: []Option{ + WithStyles(Styles{}), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + + // Modified fields + styles: Styles{}, + }, + }, + "WithKeyMap": { + opts: []Option{ + WithKeyMap(KeyMap{}), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + KeyMap: KeyMap{}, + }, + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + tc.want.UpdateViewport() + + got := New(tc.opts...) + + if !reflect.DeepEqual(tc.want, got) { + t.Errorf("\n\nwant %v\n\ngot %v", tc.want, got) + } + }) + } +} From 03f2f6b9b095e9d70eeb72a281f5ae74a1c89ac8 Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Tue, 27 Aug 2024 16:06:29 +1000 Subject: [PATCH 07/12] test: refactor table unit tests --- table/table_test.go | 354 ++++++++++++++++++++++---------------------- 1 file changed, 177 insertions(+), 177 deletions(-) diff --git a/table/table_test.go b/table/table_test.go index 6280cf1e..d0d74dc6 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -11,6 +11,172 @@ import ( "github.com/charmbracelet/x/exp/golden" ) +var cols = []Column{ + {Title: "col1", Width: 10}, + {Title: "col2", Width: 10}, + {Title: "col3", Width: 10}, +} + +func TestNew(t *testing.T) { + tests := map[string]struct { + opts []Option + want Model + }{ + "Default": { + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + }, + }, + "WithColumns": { + opts: []Option{ + WithColumns([]Column{ + {Title: "Foo", Width: 1}, + {Title: "Bar", Width: 2}, + }), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + cols: []Column{ + {Title: "Foo", Width: 1}, + {Title: "Bar", Width: 2}, + }, + }, + }, + "WithCols; WithRows": { + opts: []Option{ + WithColumns([]Column{ + {Title: "Foo", Width: 1}, + {Title: "Bar", Width: 2}, + }), + WithRows([]Row{ + {"1", "Foo"}, + {"2", "Bar"}, + }), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + cols: []Column{ + {Title: "Foo", Width: 1}, + {Title: "Bar", Width: 2}, + }, + rows: []Row{ + {"1", "Foo"}, + {"2", "Bar"}, + }, + }, + }, + "WithHeight": { + opts: []Option{ + WithHeight(10), + }, + want: Model{ + // Default fields + cursor: 0, + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + // Viewport height is 1 less than the provided height when no header is present since lipgloss.Height adds 1 + viewport: viewport.New(0, 9), + }, + }, + "WithWidth": { + opts: []Option{ + WithWidth(10), + }, + want: Model{ + // Default fields + cursor: 0, + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + // Viewport height is 1 less than the provided height when no header is present since lipgloss.Height adds 1 + viewport: viewport.New(10, 20), + }, + }, + "WithFocused": { + opts: []Option{ + WithFocused(true), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + focus: true, + }, + }, + "WithStyles": { + opts: []Option{ + WithStyles(Styles{}), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + KeyMap: DefaultKeyMap(), + Help: help.New(), + + // Modified fields + styles: Styles{}, + }, + }, + "WithKeyMap": { + opts: []Option{ + WithKeyMap(KeyMap{}), + }, + want: Model{ + // Default fields + cursor: 0, + viewport: viewport.New(0, 20), + Help: help.New(), + styles: DefaultStyles(), + + // Modified fields + KeyMap: KeyMap{}, + }, + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + tc.want.UpdateViewport() + + got := New(tc.opts...) + + if !reflect.DeepEqual(tc.want, got) { + t.Errorf("\n\nwant %v\n\ngot %v", tc.want, got) + } + }) + } +} + func TestFromValues(t *testing.T) { input := "foo1,bar1\nfoo2,bar2\nfoo3,bar3" table := New(WithColumns([]Column{{Title: "Foo"}, {Title: "Bar"}})) @@ -25,7 +191,7 @@ func TestFromValues(t *testing.T) { {"foo2", "bar2"}, {"foo3", "bar3"}, } - if !deepEqual(table.rows, expect) { + if !deepEqualRows(table.rows, expect) { t.Fatal("table rows is not equals to the input") } } @@ -43,31 +209,11 @@ func TestFromValuesWithTabSeparator(t *testing.T) { {"foo1.", "bar1"}, {"foo,bar,baz", "bar,2"}, } - if !deepEqual(table.rows, expect) { + if !deepEqualRows(table.rows, expect) { t.Fatal("table rows is not equals to the input") } } -func deepEqual(a, b []Row) bool { - if len(a) != len(b) { - return false - } - for i, r := range a { - for j, f := range r { - if f != b[i][j] { - return false - } - } - } - 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 @@ -378,162 +524,16 @@ func TestCursorNavigation(t *testing.T) { } } -func TestNew(t *testing.T) { - tests := map[string]struct { - opts []Option - want Model - }{ - "Default": { - want: Model{ - // Default fields - cursor: 0, - viewport: viewport.New(0, 20), - KeyMap: DefaultKeyMap(), - Help: help.New(), - styles: DefaultStyles(), - }, - }, - "WithColumns": { - opts: []Option{ - WithColumns([]Column{ - {Title: "Foo", Width: 1}, - {Title: "Bar", Width: 2}, - }), - }, - want: Model{ - // Default fields - cursor: 0, - viewport: viewport.New(0, 20), - KeyMap: DefaultKeyMap(), - Help: help.New(), - styles: DefaultStyles(), - - // Modified fields - cols: []Column{ - {Title: "Foo", Width: 1}, - {Title: "Bar", Width: 2}, - }, - }, - }, - "WithCols; WithRows": { - opts: []Option{ - WithColumns([]Column{ - {Title: "Foo", Width: 1}, - {Title: "Bar", Width: 2}, - }), - WithRows([]Row{ - {"1", "Foo"}, - {"2", "Bar"}, - }), - }, - want: Model{ - // Default fields - cursor: 0, - viewport: viewport.New(0, 20), - KeyMap: DefaultKeyMap(), - Help: help.New(), - styles: DefaultStyles(), - - // Modified fields - cols: []Column{ - {Title: "Foo", Width: 1}, - {Title: "Bar", Width: 2}, - }, - rows: []Row{ - {"1", "Foo"}, - {"2", "Bar"}, - }, - }, - }, - "WithHeight": { - opts: []Option{ - WithHeight(10), - }, - want: Model{ - // Default fields - cursor: 0, - KeyMap: DefaultKeyMap(), - Help: help.New(), - styles: DefaultStyles(), - - // Modified fields - // Viewport height is 1 less than the provided height when no header is present since lipgloss.Height adds 1 - viewport: viewport.New(0, 9), - }, - }, - "WithWidth": { - opts: []Option{ - WithWidth(10), - }, - want: Model{ - // Default fields - cursor: 0, - KeyMap: DefaultKeyMap(), - Help: help.New(), - styles: DefaultStyles(), - - // Modified fields - // Viewport height is 1 less than the provided height when no header is present since lipgloss.Height adds 1 - viewport: viewport.New(10, 20), - }, - }, - "WithFocused": { - opts: []Option{ - WithFocused(true), - }, - want: Model{ - // Default fields - cursor: 0, - viewport: viewport.New(0, 20), - KeyMap: DefaultKeyMap(), - Help: help.New(), - styles: DefaultStyles(), - - // Modified fields - focus: true, - }, - }, - "WithStyles": { - opts: []Option{ - WithStyles(Styles{}), - }, - want: Model{ - // Default fields - cursor: 0, - viewport: viewport.New(0, 20), - KeyMap: DefaultKeyMap(), - Help: help.New(), - - // Modified fields - styles: Styles{}, - }, - }, - "WithKeyMap": { - opts: []Option{ - WithKeyMap(KeyMap{}), - }, - want: Model{ - // Default fields - cursor: 0, - viewport: viewport.New(0, 20), - Help: help.New(), - styles: DefaultStyles(), - - // Modified fields - KeyMap: KeyMap{}, - }, - }, +func deepEqualRows(a, b []Row) bool { + if len(a) != len(b) { + return false } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - tc.want.UpdateViewport() - - got := New(tc.opts...) - - if !reflect.DeepEqual(tc.want, got) { - t.Errorf("\n\nwant %v\n\ngot %v", tc.want, got) + for i, r := range a { + for j, f := range r { + if f != b[i][j] { + return false } - }) + } } + return true } From a521a426f72d68dba48ce6e018ef8d9873a4c690 Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Tue, 27 Aug 2024 20:00:37 +1000 Subject: [PATCH 08/12] test: cleanup table unit tests --- table/table_test.go | 70 +++++++++++++------ .../Centered_in_a_box.golden | 8 +-- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/table/table_test.go b/table/table_test.go index d0d74dc6..a290ed41 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -11,7 +11,7 @@ import ( "github.com/charmbracelet/x/exp/golden" ) -var cols = []Column{ +var testCols = []Column{ {Title: "col1", Width: 10}, {Title: "col2", Width: 10}, {Title: "col3", Width: 10}, @@ -177,7 +177,7 @@ func TestNew(t *testing.T) { } } -func TestFromValues(t *testing.T) { +func TestModel_FromValues(t *testing.T) { input := "foo1,bar1\nfoo2,bar2\nfoo3,bar3" table := New(WithColumns([]Column{{Title: "Foo"}, {Title: "Bar"}})) table.FromValues(input, ",") @@ -191,12 +191,12 @@ func TestFromValues(t *testing.T) { {"foo2", "bar2"}, {"foo3", "bar3"}, } - if !deepEqualRows(table.rows, expect) { - t.Fatal("table rows is not equals to the input") + if !reflect.DeepEqual(table.rows, expect) { + t.Fatalf("\n\nwant %v\n\ngot %v", expect, table.rows) } } -func TestFromValuesWithTabSeparator(t *testing.T) { +func TestModel_FromValues_WithTabSeparator(t *testing.T) { input := "foo1.\tbar1\nfoo,bar,baz\tbar,2" table := New(WithColumns([]Column{{Title: "Foo"}, {Title: "Bar"}})) table.FromValues(input, "\t") @@ -209,12 +209,12 @@ func TestFromValuesWithTabSeparator(t *testing.T) { {"foo1.", "bar1"}, {"foo,bar,baz", "bar,2"}, } - if !deepEqualRows(table.rows, expect) { - t.Fatal("table rows is not equals to the input") + if !reflect.DeepEqual(table.rows, expect) { + t.Fatalf("\n\nwant %v\n\ngot %v", expect, table.rows) } } -func TestRenderRow(t *testing.T) { +func TestModel_RenderRow(t *testing.T) { tests := []struct { name string table *Model @@ -224,7 +224,7 @@ func TestRenderRow(t *testing.T) { name: "simple row", table: &Model{ rows: []Row{{"Foooooo", "Baaaaar", "Baaaaaz"}}, - cols: cols, + cols: testCols, styles: Styles{Cell: lipgloss.NewStyle()}, }, expected: "Foooooo Baaaaar Baaaaaz ", @@ -233,7 +233,7 @@ func TestRenderRow(t *testing.T) { name: "simple row with truncations", table: &Model{ rows: []Row{{"Foooooooooo", "Baaaaaaaaar", "Quuuuuuuuux"}}, - cols: cols, + cols: testCols, styles: Styles{Cell: lipgloss.NewStyle()}, }, expected: "Foooooooo…Baaaaaaaa…Quuuuuuuu…", @@ -242,7 +242,7 @@ func TestRenderRow(t *testing.T) { name: "simple row avoiding truncations", table: &Model{ rows: []Row{{"Fooooooooo", "Baaaaaaaar", "Quuuuuuuux"}}, - cols: cols, + cols: testCols, styles: Styles{Cell: lipgloss.NewStyle()}, }, expected: "FoooooooooBaaaaaaaarQuuuuuuuux", @@ -361,6 +361,8 @@ func TestCellPadding(t *testing.T) { got := ansi.Strip(table.View()) + // TODO: Adjust the golden file once this bug has been resolved + // https://github.com/charmbracelet/bubbles/issues/576 golden.RequireEqual(t, []byte(got)) }) } @@ -514,7 +516,7 @@ func TestCursorNavigation(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { - table := New(WithColumns(cols), WithRows(tc.rows)) + table := New(WithColumns(testCols), WithRows(tc.rows)) tc.action(&table) if table.Cursor() != tc.want { @@ -524,16 +526,40 @@ func TestCursorNavigation(t *testing.T) { } } -func deepEqualRows(a, b []Row) bool { - if len(a) != len(b) { - return false +func TestModel_SetRows(t *testing.T) { + table := New(WithColumns(testCols)) + + if len(table.rows) != 0 { + t.Fatalf("want 0, got %d", len(table.rows)) } - for i, r := range a { - for j, f := range r { - if f != b[i][j] { - return false - } - } + + table.SetRows([]Row{{"r1"}, {"r2"}}) + + if len(table.rows) != 2 { + t.Fatalf("want 2, got %d", len(table.rows)) + } + + want := []Row{{"r1"}, {"r2"}} + if !reflect.DeepEqual(table.rows, want) { + t.Fatalf("\n\nwant %v\n\ngot %v", want, table.rows) + } +} + +func TestModel_SetColumns(t *testing.T) { + table := New() + + if len(table.cols) != 0 { + t.Fatalf("want 0, got %d", len(table.cols)) + } + + table.SetColumns([]Column{{Title: "Foo"}, {Title: "Bar"}}) + + if len(table.cols) != 2 { + t.Fatalf("want 2, got %d", len(table.cols)) + } + + want := []Column{{Title: "Foo"}, {Title: "Bar"}} + if !reflect.DeepEqual(table.cols, want) { + t.Fatalf("\n\nwant %v\n\ngot %v", want, table.cols) } - return true } diff --git a/table/testdata/TestTableCentering/Centered_in_a_box.golden b/table/testdata/TestTableCentering/Centered_in_a_box.golden index c8998994..e3ffe5c5 100644 --- a/table/testdata/TestTableCentering/Centered_in_a_box.golden +++ b/table/testdata/TestTableCentering/Centered_in_a_box.golden @@ -1,7 +1,7 @@ ┌────────────────────────────────────────┐ │ One Two Three │ -│ r1c1- r1c2- r1c3- │ -│ r2c1- r2c2- r2c3- │ -│ r3c1- r3c2- r3c3- │ -│ r4c1- r4c2- r4c3- │ +│ r1c1- r1c2- r1c3- │ +│ r2c1- r2c2- r2c3- │ +│ r3c1- r3c2- r3c3- │ +│ r4c1- r4c2- r4c3- │ └────────────────────────────────────────┘ \ No newline at end of file From e7b3c2361a503c6068ce8ae9f152776af61ee9da Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Wed, 28 Aug 2024 11:47:44 +1000 Subject: [PATCH 09/12] test: add table view func unit tests --- table/table_test.go | 343 +++++++++++++----- .../TestCellPadding/With_padding.golden | 4 - .../Without_padding;_exact_width.golden | 4 - .../Without_padding;_too_narrow.golden | 4 - .../TestModel_View/Bordered_cells.golden | 21 ++ .../TestModel_View/Bordered_headers.golden | 23 ++ table/testdata/TestModel_View/Empty.golden | 20 + .../TestModel_View/Extra_padding.golden | 14 + .../Manual_height_greater_than_rows.golden | 6 + .../Manual_height_less_than_rows.golden | 2 + .../Manual_width_greater_than_columns.golden | 21 ++ .../Manual_width_less_than_columns.golden | 21 ++ .../Modified_viewport_height.golden | 3 + .../Multiple_rows_and_columns.golden | 21 ++ .../testdata/TestModel_View/No_padding.golden | 10 + .../Single_row_and_column.golden | 21 ++ .../TestModel_View_CenteredInABox.golden | 8 + .../Centered_in_a_box.golden | 7 - 18 files changed, 444 insertions(+), 109 deletions(-) delete mode 100644 table/testdata/TestCellPadding/With_padding.golden delete mode 100644 table/testdata/TestCellPadding/Without_padding;_exact_width.golden delete mode 100644 table/testdata/TestCellPadding/Without_padding;_too_narrow.golden create mode 100644 table/testdata/TestModel_View/Bordered_cells.golden create mode 100644 table/testdata/TestModel_View/Bordered_headers.golden create mode 100644 table/testdata/TestModel_View/Empty.golden create mode 100644 table/testdata/TestModel_View/Extra_padding.golden create mode 100644 table/testdata/TestModel_View/Manual_height_greater_than_rows.golden create mode 100644 table/testdata/TestModel_View/Manual_height_less_than_rows.golden create mode 100644 table/testdata/TestModel_View/Manual_width_greater_than_columns.golden create mode 100644 table/testdata/TestModel_View/Manual_width_less_than_columns.golden create mode 100644 table/testdata/TestModel_View/Modified_viewport_height.golden create mode 100644 table/testdata/TestModel_View/Multiple_rows_and_columns.golden create mode 100644 table/testdata/TestModel_View/No_padding.golden create mode 100644 table/testdata/TestModel_View/Single_row_and_column.golden create mode 100644 table/testdata/TestModel_View_CenteredInABox.golden delete mode 100644 table/testdata/TestTableCentering/Centered_in_a_box.golden diff --git a/table/table_test.go b/table/table_test.go index a290ed41..c5ab6e48 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -307,96 +307,6 @@ func TestTableAlignment(t *testing.T) { }) } -func TestCellPadding(t *testing.T) { - tt := map[string]struct { - tableWidth int - styles Styles - }{ - "With padding": { - tableWidth: 21, - styles: Styles{ - Selected: lipgloss.NewStyle(), - Header: lipgloss.NewStyle().Padding(0, 1), - Cell: lipgloss.NewStyle().Padding(0, 1), - }, - }, - "Without padding; exact width": { - tableWidth: 15, - styles: Styles{ - Selected: lipgloss.NewStyle(), - Header: lipgloss.NewStyle(), - Cell: lipgloss.NewStyle(), - }, - }, - // TODO: Adjust the golden file once a desired output has been decided - // https://github.com/charmbracelet/bubbles/issues/472 - "Without padding; too narrow": { - tableWidth: 10, - styles: Styles{ - Selected: lipgloss.NewStyle(), - Header: lipgloss.NewStyle(), - Cell: lipgloss.NewStyle(), - }, - }, - } - - for name, tc := range tt { - t.Run(name, func(t *testing.T) { - table := New( - WithHeight(4), - WithWidth(tc.tableWidth), - WithColumns([]Column{ - {Title: "One", Width: 5}, - {Title: "Two", Width: 5}, - {Title: "Three", Width: 5}, - }), - WithRows([]Row{ - {"r1c1-", "r1c2-", "r1c3-"}, - {"r2c1-", "r2c2-", "r2c3-"}, - {"r3c1-", "r3c2-", "r3c3-"}, - {"r4c1-", "r4c2-", "r4c3-"}, - }), - WithStyles(tc.styles), - ) - - got := ansi.Strip(table.View()) - - // TODO: Adjust the golden file once this bug has been resolved - // https://github.com/charmbracelet/bubbles/issues/576 - golden.RequireEqual(t, []byte(got)) - }) - } -} - -func TestTableCentering(t *testing.T) { - t.Run("Centered in a box", func(t *testing.T) { - boxStyle := lipgloss.NewStyle(). - BorderStyle(lipgloss.NormalBorder()). - Align(lipgloss.Center) - - table := New( - WithHeight(5), - WithWidth(40), - WithColumns([]Column{ - {Title: "One", Width: 5}, - {Title: "Two", Width: 5}, - {Title: "Three", Width: 5}, - }), - WithRows([]Row{ - {"r1c1-", "r1c2-", "r1c3-"}, - {"r2c1-", "r2c2-", "r2c3-"}, - {"r3c1-", "r3c2-", "r3c3-"}, - {"r4c1-", "r4c2-", "r4c3-"}, - }), - ) - - tableView := ansi.Strip(table.View()) - got := boxStyle.Render(tableView) - - golden.RequireEqual(t, []byte(got)) - }) -} - func TestCursorNavigation(t *testing.T) { tests := map[string]struct { rows []Row @@ -563,3 +473,256 @@ func TestModel_SetColumns(t *testing.T) { t.Fatalf("\n\nwant %v\n\ngot %v", want, table.cols) } } + +func TestModel_View(t *testing.T) { + tests := map[string]struct { + modelFunc func() Model + }{ + // TODO(?): should the view/output of empty tables use the same default height? (this has height 21) + "Empty": { + modelFunc: func() Model { + return New() + }, + }, + "Single row and column": { + modelFunc: func() Model { + return New( + WithColumns([]Column{ + {Title: "Name", Width: 25}, + }), + WithRows([]Row{ + {"Chocolate Digestives"}, + }), + ) + }, + }, + "Multiple rows and columns": { + modelFunc: func() Model { + return New( + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + ) + }, + }, + // TODO(fix): since the table height is tied to the viewport height, adding vertical padding to the headers' height directly increases the table height. + "Extra padding": { + modelFunc: func() Model { + s := DefaultStyles() + s.Header = lipgloss.NewStyle().Padding(2, 2) + s.Cell = lipgloss.NewStyle().Padding(2, 2) + + return New( + WithHeight(10), + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + WithStyles(s), + ) + }, + }, + "No padding": { + modelFunc: func() Model { + s := DefaultStyles() + s.Header = lipgloss.NewStyle() + s.Cell = lipgloss.NewStyle() + + return New( + WithHeight(10), + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + WithStyles(s), + ) + }, + }, + // TODO(?): the total height is modified with borderd headers, however not with bordered cells. Is this expected/desired? + "Bordered headers": { + modelFunc: func() Model { + return New( + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + WithStyles(Styles{ + Header: lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()), + }), + ) + }, + }, + // TODO(fix): Headers are not horizontally aligned with cells due to the border adding width to the cells. + "Bordered cells": { + modelFunc: func() Model { + return New( + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + WithStyles(Styles{ + Cell: lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()), + }), + ) + }, + }, + "Manual height greater than rows": { + modelFunc: func() Model { + return New( + WithHeight(6), + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + ) + }, + }, + "Manual height less than rows": { + modelFunc: func() Model { + return New( + WithHeight(2), + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + ) + }, + }, + // TODO(fix): spaces are added to the right of the viewport to fill the width, but the headers end as though they are not aware of the width. + "Manual width greater than columns": { + modelFunc: func() Model { + return New( + WithWidth(80), + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + ) + }, + }, + // TODO(fix): Setting the table width does not affect the total headers' width. Cells are wrapped. Headers are not affected. A desired outcome needs to be determined. + "Manual width less than columns": { + modelFunc: func() Model { + return New( + WithWidth(30), + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + ) + }, + }, + "Modified viewport height": { + modelFunc: func() Model { + m := New( + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + ) + + m.viewport.Height = 2 + + return m + }, + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + table := tc.modelFunc() + + got := ansi.Strip(table.View()) + + golden.RequireEqual(t, []byte(got)) + }) + } +} + +// TODO: Fix table such that this test will fail because the cells are properly centered. +func TestModel_View_CenteredInABox(t *testing.T) { + boxStyle := lipgloss.NewStyle(). + BorderStyle(lipgloss.NormalBorder()). + Align(lipgloss.Center) + + table := New( + WithHeight(6), + WithWidth(80), + WithColumns([]Column{ + {Title: "Name", Width: 25}, + {Title: "Country of Origin", Width: 16}, + {Title: "Dunk-able", Width: 12}, + }), + WithRows([]Row{ + {"Chocolate Digestives", "UK", "Yes"}, + {"Tim Tams", "Australia", "No"}, + {"Hobnobs", "UK", "Yes"}, + }), + ) + + tableView := ansi.Strip(table.View()) + got := boxStyle.Render(tableView) + + golden.RequireEqual(t, []byte(got)) +} diff --git a/table/testdata/TestCellPadding/With_padding.golden b/table/testdata/TestCellPadding/With_padding.golden deleted file mode 100644 index 50266968..00000000 --- a/table/testdata/TestCellPadding/With_padding.golden +++ /dev/null @@ -1,4 +0,0 @@ - One Two Three - r1c1- r1c2- r1c3- - r2c1- r2c2- r2c3- - r3c1- r3c2- r3c3- \ No newline at end of file diff --git a/table/testdata/TestCellPadding/Without_padding;_exact_width.golden b/table/testdata/TestCellPadding/Without_padding;_exact_width.golden deleted file mode 100644 index 2d097867..00000000 --- a/table/testdata/TestCellPadding/Without_padding;_exact_width.golden +++ /dev/null @@ -1,4 +0,0 @@ -One Two Three -r1c1-r1c2-r1c3- -r2c1-r2c2-r2c3- -r3c1-r3c2-r3c3- \ No newline at end of file diff --git a/table/testdata/TestCellPadding/Without_padding;_too_narrow.golden b/table/testdata/TestCellPadding/Without_padding;_too_narrow.golden deleted file mode 100644 index 02c62132..00000000 --- a/table/testdata/TestCellPadding/Without_padding;_too_narrow.golden +++ /dev/null @@ -1,4 +0,0 @@ -One Two Three -r1c1-r1c2- -r1c3- -r2c1-r2c2- \ No newline at end of file diff --git a/table/testdata/TestModel_View/Bordered_cells.golden b/table/testdata/TestModel_View/Bordered_cells.golden new file mode 100644 index 00000000..71e95988 --- /dev/null +++ b/table/testdata/TestModel_View/Bordered_cells.golden @@ -0,0 +1,21 @@ +Name Country of Orig…Dunk-able +┌─────────────────────────┐┌────────────────┐┌────────────┐ +│Chocolate Digestives ││UK ││Yes │ +└─────────────────────────┘└────────────────┘└────────────┘ +┌─────────────────────────┐┌────────────────┐┌────────────┐ +│Tim Tams ││Australia ││No │ +└─────────────────────────┘└────────────────┘└────────────┘ +┌─────────────────────────┐┌────────────────┐┌────────────┐ +│Hobnobs ││UK ││Yes │ +└─────────────────────────┘└────────────────┘└────────────┘ + + + + + + + + + + + \ No newline at end of file diff --git a/table/testdata/TestModel_View/Bordered_headers.golden b/table/testdata/TestModel_View/Bordered_headers.golden new file mode 100644 index 00000000..0e260bae --- /dev/null +++ b/table/testdata/TestModel_View/Bordered_headers.golden @@ -0,0 +1,23 @@ +┌─────────────────────────┐┌────────────────┐┌────────────┐ +│Name ││Country of Orig…││Dunk-able │ +└─────────────────────────┘└────────────────┘└────────────┘ +Chocolate Digestives UK Yes +Tim Tams Australia No +Hobnobs UK Yes + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/table/testdata/TestModel_View/Empty.golden b/table/testdata/TestModel_View/Empty.golden new file mode 100644 index 00000000..7b050800 --- /dev/null +++ b/table/testdata/TestModel_View/Empty.golden @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/table/testdata/TestModel_View/Extra_padding.golden b/table/testdata/TestModel_View/Extra_padding.golden new file mode 100644 index 00000000..3028ba66 --- /dev/null +++ b/table/testdata/TestModel_View/Extra_padding.golden @@ -0,0 +1,14 @@ + + + Name Country of Orig… Dunk-able + + + + + Chocolate Digestives UK Yes + + + + + Tim Tams Australia No + \ No newline at end of file diff --git a/table/testdata/TestModel_View/Manual_height_greater_than_rows.golden b/table/testdata/TestModel_View/Manual_height_greater_than_rows.golden new file mode 100644 index 00000000..0888bdeb --- /dev/null +++ b/table/testdata/TestModel_View/Manual_height_greater_than_rows.golden @@ -0,0 +1,6 @@ + Name Country of Orig… Dunk-able + Chocolate Digestives UK Yes + Tim Tams Australia No + Hobnobs UK Yes + + \ No newline at end of file diff --git a/table/testdata/TestModel_View/Manual_height_less_than_rows.golden b/table/testdata/TestModel_View/Manual_height_less_than_rows.golden new file mode 100644 index 00000000..9c331b39 --- /dev/null +++ b/table/testdata/TestModel_View/Manual_height_less_than_rows.golden @@ -0,0 +1,2 @@ + Name Country of Orig… Dunk-able + Chocolate Digestives UK Yes \ No newline at end of file diff --git a/table/testdata/TestModel_View/Manual_width_greater_than_columns.golden b/table/testdata/TestModel_View/Manual_width_greater_than_columns.golden new file mode 100644 index 00000000..68445049 --- /dev/null +++ b/table/testdata/TestModel_View/Manual_width_greater_than_columns.golden @@ -0,0 +1,21 @@ + Name Country of Orig… Dunk-able + Chocolate Digestives UK Yes + Tim Tams Australia No + Hobnobs UK Yes + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/table/testdata/TestModel_View/Manual_width_less_than_columns.golden b/table/testdata/TestModel_View/Manual_width_less_than_columns.golden new file mode 100644 index 00000000..5459eae6 --- /dev/null +++ b/table/testdata/TestModel_View/Manual_width_less_than_columns.golden @@ -0,0 +1,21 @@ + Name Country of Orig… Dunk-able + Chocolate Digestives UK +Yes + Tim Tams +Australia No + Hobnobs UK +Yes + + + + + + + + + + + + + + \ No newline at end of file diff --git a/table/testdata/TestModel_View/Modified_viewport_height.golden b/table/testdata/TestModel_View/Modified_viewport_height.golden new file mode 100644 index 00000000..91445d49 --- /dev/null +++ b/table/testdata/TestModel_View/Modified_viewport_height.golden @@ -0,0 +1,3 @@ + Name Country of Orig… Dunk-able + Chocolate Digestives UK Yes + Tim Tams Australia No \ No newline at end of file diff --git a/table/testdata/TestModel_View/Multiple_rows_and_columns.golden b/table/testdata/TestModel_View/Multiple_rows_and_columns.golden new file mode 100644 index 00000000..6ba436ce --- /dev/null +++ b/table/testdata/TestModel_View/Multiple_rows_and_columns.golden @@ -0,0 +1,21 @@ + Name Country of Orig… Dunk-able + Chocolate Digestives UK Yes + Tim Tams Australia No + Hobnobs UK Yes + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/table/testdata/TestModel_View/No_padding.golden b/table/testdata/TestModel_View/No_padding.golden new file mode 100644 index 00000000..f7487466 --- /dev/null +++ b/table/testdata/TestModel_View/No_padding.golden @@ -0,0 +1,10 @@ +Name Country of Orig…Dunk-able +Chocolate Digestives UK Yes +Tim Tams Australia No +Hobnobs UK Yes + + + + + + \ No newline at end of file diff --git a/table/testdata/TestModel_View/Single_row_and_column.golden b/table/testdata/TestModel_View/Single_row_and_column.golden new file mode 100644 index 00000000..84950d57 --- /dev/null +++ b/table/testdata/TestModel_View/Single_row_and_column.golden @@ -0,0 +1,21 @@ + Name + Chocolate Digestives + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/table/testdata/TestModel_View_CenteredInABox.golden b/table/testdata/TestModel_View_CenteredInABox.golden new file mode 100644 index 00000000..9a18aacd --- /dev/null +++ b/table/testdata/TestModel_View_CenteredInABox.golden @@ -0,0 +1,8 @@ +┌────────────────────────────────────────────────────────────────────────────────┐ +│ Name Country of Orig… Dunk-able │ +│ Chocolate Digestives UK Yes │ +│ Tim Tams Australia No │ +│ Hobnobs UK Yes │ +│ │ +│ │ +└────────────────────────────────────────────────────────────────────────────────┘ \ No newline at end of file diff --git a/table/testdata/TestTableCentering/Centered_in_a_box.golden b/table/testdata/TestTableCentering/Centered_in_a_box.golden deleted file mode 100644 index e3ffe5c5..00000000 --- a/table/testdata/TestTableCentering/Centered_in_a_box.golden +++ /dev/null @@ -1,7 +0,0 @@ -┌────────────────────────────────────────┐ -│ One Two Three │ -│ r1c1- r1c2- r1c3- │ -│ r2c1- r2c2- r2c3- │ -│ r3c1- r3c2- r3c3- │ -│ r4c1- r4c2- r4c3- │ -└────────────────────────────────────────┘ \ No newline at end of file From 2389d3844be742a9839c4af1f710864cb819b357 Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Fri, 30 Aug 2024 13:15:44 +1000 Subject: [PATCH 10/12] test: fix golden files --- .../Manual_width_less_than_columns.golden | 14 +++++++------- .../testdata/TestModel_View_CenteredInABox.golden | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/table/testdata/TestModel_View/Manual_width_less_than_columns.golden b/table/testdata/TestModel_View/Manual_width_less_than_columns.golden index 5459eae6..d2bc25b9 100644 --- a/table/testdata/TestModel_View/Manual_width_less_than_columns.golden +++ b/table/testdata/TestModel_View/Manual_width_less_than_columns.golden @@ -1,10 +1,7 @@ - Name Country of Orig… Dunk-able - Chocolate Digestives UK -Yes - Tim Tams -Australia No - Hobnobs UK -Yes + Name Country of Origin Dunk-able + Chocolate Digestives UK Yes + Tim Tams Australia No + Hobnobs UK Yes @@ -15,6 +12,9 @@ Yes + + + diff --git a/table/testdata/TestModel_View_CenteredInABox.golden b/table/testdata/TestModel_View_CenteredInABox.golden index 9a18aacd..59d9c703 100644 --- a/table/testdata/TestModel_View_CenteredInABox.golden +++ b/table/testdata/TestModel_View_CenteredInABox.golden @@ -1,8 +1,8 @@ ┌────────────────────────────────────────────────────────────────────────────────┐ │ Name Country of Orig… Dunk-able │ -│ Chocolate Digestives UK Yes │ -│ Tim Tams Australia No │ -│ Hobnobs UK Yes │ +│ Chocolate Digestives UK Yes │ +│ Tim Tams Australia No │ +│ Hobnobs UK Yes │ │ │ │ │ └────────────────────────────────────────────────────────────────────────────────┘ \ No newline at end of file From 96797b33542b2937ab62d3ef7d0acf52cff8e804 Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Fri, 30 Aug 2024 13:16:16 +1000 Subject: [PATCH 11/12] chore: update todo comments --- table/table_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/table/table_test.go b/table/table_test.go index c5ab6e48..7bd01a23 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -649,7 +649,8 @@ func TestModel_View(t *testing.T) { ) }, }, - // TODO(fix): Setting the table width does not affect the total headers' width. Cells are wrapped. Headers are not affected. A desired outcome needs to be determined. + // TODO(fix): Setting the table width does not affect the total headers' width. Cells are wrapped. + // Headers are not affected. Truncation/resizing should match lipgloss.table functionality. "Manual width less than columns": { modelFunc: func() Model { return New( @@ -700,7 +701,7 @@ func TestModel_View(t *testing.T) { } } -// TODO: Fix table such that this test will fail because the cells are properly centered. +// TODO: Fix table to make this test will pass. func TestModel_View_CenteredInABox(t *testing.T) { boxStyle := lipgloss.NewStyle(). BorderStyle(lipgloss.NormalBorder()). From e03da192806a20f5e876d33c14028c0bccf197d3 Mon Sep 17 00:00:00 2001 From: Broderick Westrope Date: Fri, 30 Aug 2024 13:20:35 +1000 Subject: [PATCH 12/12] test: fix test name --- table/table_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/table/table_test.go b/table/table_test.go index 7bd01a23..102c236c 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -54,7 +54,7 @@ func TestNew(t *testing.T) { }, }, }, - "WithCols; WithRows": { + "WithColumns; WithRows": { opts: []Option{ WithColumns([]Column{ {Title: "Foo", Width: 1},