From d0187024a6534cba151eba83bd6a1bf362f59066 Mon Sep 17 00:00:00 2001 From: Tom Lazar Date: Fri, 15 Apr 2022 18:49:43 -0500 Subject: [PATCH] testing: add tests for issue #8 --- go.mod | 1 + go.sum | 2 ++ table_test.go | 50 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 6732534..951f325 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,5 @@ require ( github.com/mattn/go-runewidth v0.0.13 github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d github.com/stretchr/testify v1.7.0 + golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect ) diff --git a/go.sum b/go.sum index 3e87fac..50012ec 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/table_test.go b/table_test.go index 9a49fd9..e8a9a21 100644 --- a/table_test.go +++ b/table_test.go @@ -8,13 +8,8 @@ import ( "github.com/stretchr/testify/require" ) -func TestTable_WriteTable(t *testing.T) { - r := require.New(t) - - var buf bytes.Buffer - - // write the table - tab := Table{ +func TestingTable() Table { + return Table{ Headers: []string{"something", "another"}, Rows: [][]string{ {"1", "2"}, @@ -23,6 +18,15 @@ func TestTable_WriteTable(t *testing.T) { {"but this one is longer", "shorter now"}, }, } +} + +func TestTable_WriteTable(t *testing.T) { + r := require.New(t) + + var buf bytes.Buffer + + // write the table + tab := TestingTable() err := tab.WriteTable(&buf, nil) r.NoError(err) @@ -47,11 +51,7 @@ func TestTable_WriteLargeTable(t *testing.T) { var buf bytes.Buffer // write the table - tab := Table{ - Headers: []string{"something"}, - Rows: [][]string{}, - } - + tab := TestingTable() for i := 0; i < 200; i++ { tab.Rows = append(tab.Rows, []string{"x"}) } @@ -67,11 +67,27 @@ func TestTable_WriteEmptyTable(t *testing.T) { var buf bytes.Buffer // write the table - tab := Table{ - Headers: []string{"something"}, - Rows: [][]string{}, - } + tab := TestingTable() err := tab.WriteTable(&buf, nil) r.NoError(err) - r.Equal(2, len(strings.Split(buf.String(), "\n"))) + r.Equal(6, len(strings.Split(buf.String(), "\n"))) +} + +func TestTable_WriteColorNoAlts(t *testing.T) { + r := require.New(t) + + var buf bytes.Buffer + + // write the table + tab := TestingTable() + c := Config{ + ShowIndex: true, + Color: true, + AlternateColors: true, + AltColorCodes: nil, + TitleColorCode: "", + } + err := tab.WriteTable(&buf, &c) + r.NoError(err) + r.Equal(6, len(strings.Split(buf.String(), "\n"))) }