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

Add getters to table.Column #158

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 40 additions & 0 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,43 @@ func (c Column) WithFormatString(fmtString string) Column {
func (c *Column) isFlex() bool {
return c.flexFactor != 0
}

// Title returns the title of the column.
func (c Column) Title() string {
return c.title
}

// Key returns the key of the column.
func (c Column) Key() string {
return c.key
}

// Width returns the width of the column.
func (c Column) Width() int {
return c.width
}

// FlexFactor returns the flex factor of the column.
func (c Column) FlexFactor() int {
return c.flexFactor
}

// IsFlex returns whether the column is a flex column.
func (c Column) IsFlex() bool {
return c.isFlex()
}

// Filterable returns whether the column is filterable.
func (c Column) Filterable() bool {
return c.filterable
}

// Style returns the style of the column.
func (c Column) Style() lipgloss.Style {
return c.style
}

// FmtString returns the format string of the column.
func (c Column) FmtString() string {
return c.fmtString
}
222 changes: 222 additions & 0 deletions table/column_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package table

import (
"fmt"
"testing"

"github.com/charmbracelet/lipgloss"
"github.com/stretchr/testify/assert"
)

func TestColumnTitle(t *testing.T) {
tests := []struct {
title string
expected string
}{
{
title: "foo",
expected: "foo",
},
{
title: "bar",
expected: "bar",
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("title %s gives %s", test.title, test.expected), func(t *testing.T) {
col := NewColumn("key", test.title, 10)
assert.Equal(t, test.expected, col.Title())
})
}

Check failure on line 32 in table/column_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}

func TestColumnKey(t *testing.T) {
tests := []struct {
key string
expected string
}{
{
key: "foo",
expected: "foo",
},
{
key: "bar",
expected: "bar",
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("key %s gives %s", test.key, test.expected), func(t *testing.T) {
col := NewColumn(test.key, "title", 10)
assert.Equal(t, test.expected, col.Key())
})
}

Check failure on line 56 in table/column_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}

func TestColumnWidth(t *testing.T) {
tests := []struct {
width int
expected int
}{
{
width: 3,
expected: 3,
},
{
width: 4,
expected: 4,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("width %d gives %d", test.width, test.expected), func(t *testing.T) {
col := NewColumn("key", "title", test.width)
assert.Equal(t, test.expected, col.Width())
})
}

Check failure on line 80 in table/column_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}

func TestColumnFlexFactor(t *testing.T) {
tests := []struct {
flexFactor int
expected int
}{
{
flexFactor: 3,
expected: 3,
},
{
flexFactor: 4,
expected: 4,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("flexFactor %d gives %d", test.flexFactor, test.expected), func(t *testing.T) {
col := NewFlexColumn("key", "title", test.flexFactor)
assert.Equal(t, test.expected, col.FlexFactor())
})
}

}

func TestColumnIsFlex(t *testing.T) {
testsFlexColumn := []struct {
flexFactor int
expected bool
}{
{
flexFactor: 3,
expected: true,
},
{
flexFactor: 0,
expected: true,
},
}

for _, test := range testsFlexColumn {
t.Run(fmt.Sprintf("flexFactor %d gives %t", test.flexFactor, test.expected), func(t *testing.T) {
col := NewFlexColumn("key", "title", test.flexFactor)
assert.Equal(t, test.expected, col.IsFlex())
})
}

testsRegularColumn := []struct {
width int
expected bool
}{
{
width: 3,
expected: false,
},
{
width: 0,
expected: false,
},
}

for _, test := range testsRegularColumn {
t.Run(fmt.Sprintf("width %d gives %t", test.width, test.expected), func(t *testing.T) {
col := NewColumn("key", "title", 10)
Evertras marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, test.expected, col.IsFlex())
})
}

}

func TestColumnFilterable(t *testing.T) {
tests := []struct {
filterable bool
expected bool
}{
{
filterable: true,
expected: true,
},
{
filterable: false,
expected: false,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("filterable %t gives %t", test.filterable, test.expected), func(t *testing.T) {
col := NewColumn("key", "title", 10)
col = col.WithFiltered(test.filterable)
assert.Equal(t, test.expected, col.Filterable())
})
}
}

func TestColumnStyle(t *testing.T) {
width := 10
tests := []struct {
style lipgloss.Style
expected lipgloss.Style
}{
{
style: lipgloss.NewStyle(),
expected: lipgloss.NewStyle().Width(width),
},
{
style: lipgloss.NewStyle().Bold(true),
expected: lipgloss.NewStyle().Bold(true).Width(width),
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("style %v gives %v", test.style, test.expected), func(t *testing.T) {
col := NewColumn("key", "title", width).WithStyle(test.style)
assert.Equal(t, test.expected, col.Style())
})
}
}

func TestColumnFormatString(t *testing.T) {
tests := []struct {
fmtString string
expected string
}{
{
fmtString: "%v",
expected: "%v",
},
{
fmtString: "%.2f",
expected: "%.2f",
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("fmtString %s gives %s", test.fmtString, test.expected), func(t *testing.T) {
col := NewColumn("key", "title", 10)
col = col.WithFormatString(test.fmtString)
assert.Equal(t, test.expected, col.FmtString())
})
}
}
Loading