2022-10-02 14:22:31 +02:00
|
|
|
/*
|
|
|
|
|
Copyright © 2022 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package lib
|
|
|
|
|
|
|
|
|
|
import (
|
2022-10-17 20:04:05 +02:00
|
|
|
"bytes"
|
2022-10-05 14:31:01 +02:00
|
|
|
"fmt"
|
2022-10-02 14:22:31 +02:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
2024-05-07 15:19:54 +02:00
|
|
|
|
2025-10-01 20:48:49 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-05-07 15:19:54 +02:00
|
|
|
"github.com/tlinden/tablizer/cfg"
|
2022-10-02 14:22:31 +02:00
|
|
|
)
|
|
|
|
|
|
2022-10-18 19:45:09 +02:00
|
|
|
func newData() Tabdata {
|
|
|
|
|
return Tabdata{
|
|
|
|
|
maxwidthHeader: 8,
|
2022-10-23 16:57:30 +02:00
|
|
|
columns: 4,
|
2022-10-18 19:45:09 +02:00
|
|
|
headers: []string{
|
|
|
|
|
"NAME",
|
|
|
|
|
"DURATION",
|
|
|
|
|
"COUNT",
|
|
|
|
|
"WHEN",
|
2022-10-13 18:56:34 +02:00
|
|
|
},
|
2022-10-18 19:45:09 +02:00
|
|
|
entries: [][]string{
|
|
|
|
|
{
|
|
|
|
|
"beta",
|
|
|
|
|
"1d10h5m1s",
|
|
|
|
|
"33",
|
|
|
|
|
"3/1/2014",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"alpha",
|
|
|
|
|
"4h35m",
|
|
|
|
|
"170",
|
|
|
|
|
"2013-Feb-03",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"ceta",
|
|
|
|
|
"33d12h",
|
|
|
|
|
"9",
|
|
|
|
|
"06/Jan/2008 15:04:05 -0700",
|
|
|
|
|
},
|
2022-10-15 16:27:04 +02:00
|
|
|
},
|
2022-10-18 19:45:09 +02:00
|
|
|
}
|
2022-10-13 18:56:34 +02:00
|
|
|
}
|
2022-10-15 19:31:42 +02:00
|
|
|
|
2022-10-17 20:04:05 +02:00
|
|
|
var tests = []struct {
|
|
|
|
|
name string // so we can identify which one fails, can be the same
|
|
|
|
|
// for multiple tests, because flags will be appended to the name
|
|
|
|
|
sortby string // empty == default
|
2025-01-15 18:51:15 +01:00
|
|
|
column int // sort by this column (numbers start by 1)
|
2022-10-17 20:04:05 +02:00
|
|
|
desc bool // sort in descending order, default == ascending
|
2025-03-06 17:02:34 +01:00
|
|
|
numberize bool // add header numbering
|
2022-10-21 10:21:07 +02:00
|
|
|
mode int // shell, orgtbl, etc. empty == default: ascii
|
2022-10-17 20:04:05 +02:00
|
|
|
usecol []int // columns to display, empty == display all
|
|
|
|
|
usecolstr string // for testname, must match usecol
|
|
|
|
|
expect string // rendered output we expect
|
|
|
|
|
}{
|
|
|
|
|
// --------------------- Default settings mode tests ``
|
|
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
mode: cfg.ASCII,
|
|
|
|
|
numberize: true,
|
|
|
|
|
name: "default",
|
2022-10-17 20:04:05 +02:00
|
|
|
expect: `
|
2025-09-29 20:46:33 +02:00
|
|
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
|
|
|
|
beta 1d10h5m1s 33 3/1/2014
|
|
|
|
|
alpha 4h35m 170 2013-Feb-03
|
|
|
|
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
|
2022-10-23 16:57:30 +02:00
|
|
|
},
|
|
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
mode: cfg.CSV,
|
|
|
|
|
numberize: false,
|
|
|
|
|
name: "csv",
|
2022-10-23 16:57:30 +02:00
|
|
|
expect: `
|
|
|
|
|
NAME,DURATION,COUNT,WHEN
|
|
|
|
|
beta,1d10h5m1s,33,3/1/2014
|
|
|
|
|
alpha,4h35m,170,2013-Feb-03
|
|
|
|
|
ceta,33d12h,9,06/Jan/2008 15:04:05 -0700`,
|
2022-10-17 20:04:05 +02:00
|
|
|
},
|
|
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
name: "orgtbl",
|
|
|
|
|
numberize: true,
|
|
|
|
|
mode: cfg.Orgtbl,
|
2022-10-17 20:04:05 +02:00
|
|
|
expect: `
|
2022-10-19 19:32:41 +02:00
|
|
|
+---------+-------------+----------+----------------------------+
|
2025-05-27 13:09:18 +02:00
|
|
|
| NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) |
|
2022-10-19 19:32:41 +02:00
|
|
|
+---------+-------------+----------+----------------------------+
|
2025-05-27 13:09:18 +02:00
|
|
|
| beta | 1d10h5m1s | 33 | 3/1/2014 |
|
|
|
|
|
| alpha | 4h35m | 170 | 2013-Feb-03 |
|
|
|
|
|
| ceta | 33d12h | 9 | 06/Jan/2008 15:04:05 -0700 |
|
2022-10-19 19:32:41 +02:00
|
|
|
+---------+-------------+----------+----------------------------+`,
|
2022-10-17 20:04:05 +02:00
|
|
|
},
|
|
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
name: "markdown",
|
|
|
|
|
mode: cfg.Markdown,
|
|
|
|
|
numberize: true,
|
2022-10-17 20:04:05 +02:00
|
|
|
expect: `
|
2025-05-27 13:09:18 +02:00
|
|
|
| NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) |
|
2022-10-17 20:04:05 +02:00
|
|
|
|---------|-------------|----------|----------------------------|
|
2025-05-27 13:09:18 +02:00
|
|
|
| beta | 1d10h5m1s | 33 | 3/1/2014 |
|
|
|
|
|
| alpha | 4h35m | 170 | 2013-Feb-03 |
|
|
|
|
|
| ceta | 33d12h | 9 | 06/Jan/2008 15:04:05 -0700 |`,
|
2022-10-17 20:04:05 +02:00
|
|
|
},
|
|
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
name: "shell",
|
|
|
|
|
mode: cfg.Shell,
|
|
|
|
|
numberize: false,
|
2022-10-17 20:04:05 +02:00
|
|
|
expect: `
|
|
|
|
|
NAME="beta" DURATION="1d10h5m1s" COUNT="33" WHEN="3/1/2014"
|
|
|
|
|
NAME="alpha" DURATION="4h35m" COUNT="170" WHEN="2013-Feb-03"
|
|
|
|
|
NAME="ceta" DURATION="33d12h" COUNT="9" WHEN="06/Jan/2008 15:04:05 -0700"`,
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
name: "yaml",
|
|
|
|
|
mode: cfg.Yaml,
|
|
|
|
|
numberize: false,
|
2022-10-17 20:04:05 +02:00
|
|
|
expect: `
|
|
|
|
|
entries:
|
|
|
|
|
- count: 33
|
|
|
|
|
duration: "1d10h5m1s"
|
|
|
|
|
name: "beta"
|
|
|
|
|
when: "3/1/2014"
|
|
|
|
|
- count: 170
|
|
|
|
|
duration: "4h35m"
|
|
|
|
|
name: "alpha"
|
|
|
|
|
when: "2013-Feb-03"
|
|
|
|
|
- count: 9
|
|
|
|
|
duration: "33d12h"
|
|
|
|
|
name: "ceta"
|
|
|
|
|
when: "06/Jan/2008 15:04:05 -0700"`,
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
name: "extended",
|
|
|
|
|
mode: cfg.Extended,
|
|
|
|
|
numberize: true,
|
2022-10-17 20:04:05 +02:00
|
|
|
expect: `
|
|
|
|
|
NAME(1): beta
|
|
|
|
|
DURATION(2): 1d10h5m1s
|
|
|
|
|
COUNT(3): 33
|
|
|
|
|
WHEN(4): 3/1/2014
|
|
|
|
|
|
|
|
|
|
NAME(1): alpha
|
|
|
|
|
DURATION(2): 4h35m
|
|
|
|
|
COUNT(3): 170
|
|
|
|
|
WHEN(4): 2013-Feb-03
|
|
|
|
|
|
|
|
|
|
NAME(1): ceta
|
|
|
|
|
DURATION(2): 33d12h
|
|
|
|
|
COUNT(3): 9
|
|
|
|
|
WHEN(4): 06/Jan/2008 15:04:05 -0700`,
|
|
|
|
|
},
|
|
|
|
|
|
2022-10-18 19:45:09 +02:00
|
|
|
//------------------------ SORT TESTS
|
2022-10-17 20:04:05 +02:00
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
name: "sortbycolumn3",
|
|
|
|
|
column: 3,
|
|
|
|
|
sortby: "numeric",
|
|
|
|
|
numberize: true,
|
|
|
|
|
desc: false,
|
2022-10-17 20:04:05 +02:00
|
|
|
expect: `
|
2025-09-29 20:46:33 +02:00
|
|
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
|
|
|
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700
|
|
|
|
|
beta 1d10h5m1s 33 3/1/2014
|
|
|
|
|
alpha 4h35m 170 2013-Feb-03`,
|
2022-10-17 20:04:05 +02:00
|
|
|
},
|
2022-10-18 19:45:09 +02:00
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
name: "sortbycolumn4",
|
|
|
|
|
column: 4,
|
|
|
|
|
sortby: "time",
|
|
|
|
|
desc: false,
|
|
|
|
|
numberize: true,
|
2022-10-18 19:45:09 +02:00
|
|
|
expect: `
|
2025-09-29 20:46:33 +02:00
|
|
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
|
|
|
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700
|
|
|
|
|
alpha 4h35m 170 2013-Feb-03
|
|
|
|
|
beta 1d10h5m1s 33 3/1/2014`,
|
2022-10-18 19:45:09 +02:00
|
|
|
},
|
|
|
|
|
{
|
2025-03-06 17:02:34 +01:00
|
|
|
name: "sortbycolumn2",
|
|
|
|
|
column: 2,
|
|
|
|
|
sortby: "duration",
|
|
|
|
|
numberize: true,
|
|
|
|
|
desc: false,
|
2022-10-18 19:45:09 +02:00
|
|
|
expect: `
|
2025-09-29 20:46:33 +02:00
|
|
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
|
|
|
|
alpha 4h35m 170 2013-Feb-03
|
|
|
|
|
beta 1d10h5m1s 33 3/1/2014
|
|
|
|
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
|
2022-10-18 19:45:09 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// ----------------------- UseColumns Tests
|
2022-10-17 20:04:05 +02:00
|
|
|
{
|
|
|
|
|
name: "usecolumns",
|
|
|
|
|
usecol: []int{1, 4},
|
2025-03-06 17:02:34 +01:00
|
|
|
numberize: true,
|
2022-10-17 20:04:05 +02:00
|
|
|
usecolstr: "1,4",
|
|
|
|
|
expect: `
|
2025-09-29 20:46:33 +02:00
|
|
|
NAME(1) WHEN(4)
|
|
|
|
|
beta 3/1/2014
|
|
|
|
|
alpha 2013-Feb-03
|
|
|
|
|
ceta 06/Jan/2008 15:04:05 -0700`,
|
2022-10-17 20:04:05 +02:00
|
|
|
},
|
|
|
|
|
{
|
2025-09-29 20:46:33 +02:00
|
|
|
name: "usecolumns2",
|
2022-10-17 20:04:05 +02:00
|
|
|
usecol: []int{2},
|
2025-03-06 17:02:34 +01:00
|
|
|
numberize: true,
|
2022-10-17 20:04:05 +02:00
|
|
|
usecolstr: "2",
|
|
|
|
|
expect: `
|
2025-09-29 20:46:33 +02:00
|
|
|
DURATION(2)
|
|
|
|
|
1d10h5m1s
|
|
|
|
|
4h35m
|
2022-10-17 20:04:05 +02:00
|
|
|
33d12h`,
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-09-29 20:46:33 +02:00
|
|
|
name: "usecolumns3",
|
2022-10-17 20:04:05 +02:00
|
|
|
usecol: []int{3},
|
2025-03-06 17:02:34 +01:00
|
|
|
numberize: true,
|
2022-10-17 20:04:05 +02:00
|
|
|
usecolstr: "3",
|
|
|
|
|
expect: `
|
2025-09-29 20:46:33 +02:00
|
|
|
COUNT(3)
|
|
|
|
|
33
|
|
|
|
|
170
|
2022-10-17 20:04:05 +02:00
|
|
|
9`,
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-09-29 20:46:33 +02:00
|
|
|
name: "usecolumns4",
|
2022-10-17 20:04:05 +02:00
|
|
|
column: 0,
|
|
|
|
|
usecol: []int{1, 3},
|
2025-03-06 17:02:34 +01:00
|
|
|
numberize: true,
|
2022-10-17 20:04:05 +02:00
|
|
|
usecolstr: "1,3",
|
|
|
|
|
expect: `
|
2025-09-29 20:46:33 +02:00
|
|
|
NAME(1) COUNT(3)
|
|
|
|
|
beta 33
|
|
|
|
|
alpha 170
|
|
|
|
|
ceta 9`,
|
2022-10-17 20:04:05 +02:00
|
|
|
},
|
|
|
|
|
{
|
2022-10-18 19:45:09 +02:00
|
|
|
name: "usecolumns",
|
|
|
|
|
usecol: []int{2, 4},
|
2025-03-06 17:02:34 +01:00
|
|
|
numberize: true,
|
2022-10-18 19:45:09 +02:00
|
|
|
usecolstr: "2,4",
|
2022-10-17 20:04:05 +02:00
|
|
|
expect: `
|
2025-09-29 20:46:33 +02:00
|
|
|
DURATION(2) WHEN(4)
|
|
|
|
|
1d10h5m1s 3/1/2014
|
|
|
|
|
4h35m 2013-Feb-03
|
|
|
|
|
33d12h 06/Jan/2008 15:04:05 -0700`,
|
2022-10-17 20:04:05 +02:00
|
|
|
},
|
|
|
|
|
}
|
2022-10-15 19:31:42 +02:00
|
|
|
|
2022-10-17 20:04:05 +02:00
|
|
|
func TestPrinter(t *testing.T) {
|
2024-05-07 18:01:12 +02:00
|
|
|
for _, testdata := range tests {
|
2025-03-06 17:02:34 +01:00
|
|
|
testname := fmt.Sprintf("print-%s-%d-desc-%t-sortby-%s-mode-%d-usecolumns-%s-numberize-%t",
|
|
|
|
|
testdata.name, testdata.column, testdata.desc, testdata.sortby,
|
|
|
|
|
testdata.mode, testdata.usecolstr, testdata.numberize)
|
|
|
|
|
|
2022-10-15 19:31:42 +02:00
|
|
|
t.Run(testname, func(t *testing.T) {
|
2022-10-17 20:04:05 +02:00
|
|
|
// replaces os.Stdout, but we ignore it
|
2024-05-07 18:01:12 +02:00
|
|
|
var writer bytes.Buffer
|
2022-10-17 20:04:05 +02:00
|
|
|
|
|
|
|
|
// cmd flags
|
2024-05-07 18:01:12 +02:00
|
|
|
conf := cfg.Config{
|
|
|
|
|
SortDescending: testdata.desc,
|
|
|
|
|
SortMode: testdata.sortby,
|
|
|
|
|
OutputMode: testdata.mode,
|
2025-03-06 17:02:34 +01:00
|
|
|
Numbering: testdata.numberize,
|
2024-05-07 18:01:12 +02:00
|
|
|
UseColumns: testdata.usecol,
|
2022-10-19 12:44:19 +02:00
|
|
|
NoColor: true,
|
2025-09-30 11:21:49 +02:00
|
|
|
OFS: " ",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if conf.OutputMode == cfg.CSV {
|
|
|
|
|
conf.OFS = ","
|
2022-10-19 12:44:19 +02:00
|
|
|
}
|
2022-10-15 19:31:42 +02:00
|
|
|
|
2025-01-15 18:51:15 +01:00
|
|
|
if testdata.column > 0 {
|
|
|
|
|
conf.UseSortByColumn = []int{testdata.column}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 18:01:12 +02:00
|
|
|
conf.ApplyDefaults()
|
2022-10-23 16:57:30 +02:00
|
|
|
|
2022-10-17 20:04:05 +02:00
|
|
|
// the test checks the len!
|
2024-05-07 18:01:12 +02:00
|
|
|
if len(testdata.usecol) > 0 {
|
|
|
|
|
conf.Columns = "yes"
|
2022-10-17 20:04:05 +02:00
|
|
|
} else {
|
2024-05-07 18:01:12 +02:00
|
|
|
conf.Columns = ""
|
2022-10-15 19:31:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-07 18:01:12 +02:00
|
|
|
data := newData()
|
|
|
|
|
exp := strings.TrimSpace(testdata.expect)
|
2022-10-17 23:40:53 +02:00
|
|
|
|
2024-05-07 18:01:12 +02:00
|
|
|
printData(&writer, conf, &data)
|
2022-10-17 23:40:53 +02:00
|
|
|
|
2024-05-07 18:01:12 +02:00
|
|
|
got := strings.TrimSpace(writer.String())
|
2022-10-17 23:40:53 +02:00
|
|
|
|
2025-10-01 20:48:49 +02:00
|
|
|
assert.EqualValues(t, exp, got)
|
2022-10-15 19:31:42 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|