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-17 20:04:05 +02:00
|
|
|
//"github.com/alecthomas/repr"
|
2022-10-02 14:22:31 +02:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-17 20:04:05 +02:00
|
|
|
var mockdata = Tabdata{
|
|
|
|
|
maxwidthHeader: 8,
|
|
|
|
|
maxwidthPerCol: []int{
|
|
|
|
|
5,
|
|
|
|
|
9,
|
|
|
|
|
3,
|
|
|
|
|
26,
|
|
|
|
|
},
|
|
|
|
|
columns: 4,
|
|
|
|
|
headers: []string{
|
|
|
|
|
"NAME",
|
|
|
|
|
"DURATION",
|
|
|
|
|
"COUNT",
|
|
|
|
|
"WHEN",
|
|
|
|
|
},
|
|
|
|
|
entries: [][]string{
|
2022-10-13 18:56:34 +02:00
|
|
|
{
|
2022-10-17 20:04:05 +02:00
|
|
|
"beta",
|
|
|
|
|
"1d10h5m1s",
|
|
|
|
|
"33",
|
|
|
|
|
"3/1/2014",
|
2022-10-13 18:56:34 +02:00
|
|
|
},
|
|
|
|
|
{
|
2022-10-17 20:04:05 +02:00
|
|
|
"alpha",
|
|
|
|
|
"4h35m",
|
|
|
|
|
"170",
|
|
|
|
|
"2013-Feb-03",
|
2022-10-13 18:56:34 +02:00
|
|
|
},
|
2022-10-15 16:27:04 +02:00
|
|
|
{
|
2022-10-17 20:04:05 +02:00
|
|
|
"ceta",
|
|
|
|
|
"33d12h",
|
|
|
|
|
"9",
|
|
|
|
|
"06/Jan/2008 15:04:05 -0700",
|
2022-10-15 16:27:04 +02:00
|
|
|
},
|
2022-10-17 20:04:05 +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
|
|
|
|
|
column int // sort by this column, 0 == default first or NO Sort
|
|
|
|
|
desc bool // sort in descending order, default == ascending
|
|
|
|
|
nonum bool // hide numbering
|
|
|
|
|
mode string // shell, orgtbl, etc. empty == default: ascii
|
|
|
|
|
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 ``
|
|
|
|
|
{
|
|
|
|
|
mode: "ascii",
|
|
|
|
|
name: "default",
|
|
|
|
|
expect: `
|
|
|
|
|
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`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "default",
|
|
|
|
|
mode: "orgtbl",
|
|
|
|
|
expect: `
|
|
|
|
|
|---------+-------------+----------+----------------------------|
|
|
|
|
|
| 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 |
|
|
|
|
|
|---------+-------------+----------+----------------------------|`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "default",
|
|
|
|
|
mode: "markdown",
|
|
|
|
|
expect: `
|
|
|
|
|
| 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 |`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "default",
|
|
|
|
|
mode: "shell",
|
|
|
|
|
nonum: true,
|
|
|
|
|
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"`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "default",
|
|
|
|
|
mode: "yaml",
|
|
|
|
|
nonum: true,
|
|
|
|
|
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"`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "default",
|
|
|
|
|
mode: "extended",
|
|
|
|
|
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`,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// ----------------------- UseColumns Tests FIXME: when we put
|
|
|
|
|
// these tests AFTER the sort tests, then the order of rows
|
|
|
|
|
// follows the last sort call (-k 4 here), even if we set
|
|
|
|
|
// SortByColumn to 0, which is also the default if unset in the
|
|
|
|
|
// test struct. Somehow the SliceStable() seens to modify the data
|
|
|
|
|
// structure, which seems impossible, since it's copied during
|
|
|
|
|
// every test run. So, I really don't understand the
|
|
|
|
|
// issue. However, I'll keep this for the moment, because this
|
|
|
|
|
// only seems to be a unit test related issue. In real use, the
|
|
|
|
|
// program exits after each call anyway - and it works everytime.
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
name: "usecolumns",
|
|
|
|
|
usecol: []int{2, 4},
|
|
|
|
|
usecolstr: "2,4",
|
|
|
|
|
expect: `
|
|
|
|
|
DURATION(2) WHEN(4)
|
|
|
|
|
1d10h5m1s 3/1/2014
|
|
|
|
|
4h35m 2013-Feb-03
|
|
|
|
|
33d12h 06/Jan/2008 15:04:05 -0700`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "usecolumns",
|
|
|
|
|
usecol: []int{1, 4},
|
|
|
|
|
usecolstr: "1,4",
|
|
|
|
|
expect: `
|
|
|
|
|
NAME(1) WHEN(4)
|
|
|
|
|
beta 3/1/2014
|
|
|
|
|
alpha 2013-Feb-03
|
|
|
|
|
ceta 06/Jan/2008 15:04:05 -0700`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "usecolumns",
|
|
|
|
|
usecol: []int{2},
|
|
|
|
|
usecolstr: "2",
|
|
|
|
|
expect: `
|
|
|
|
|
DURATION(2)
|
|
|
|
|
1d10h5m1s
|
|
|
|
|
4h35m
|
|
|
|
|
33d12h`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "usecolumns",
|
|
|
|
|
usecol: []int{3},
|
|
|
|
|
usecolstr: "3",
|
|
|
|
|
expect: `
|
|
|
|
|
COUNT(3)
|
|
|
|
|
33
|
|
|
|
|
170
|
|
|
|
|
9`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "usecolumns",
|
|
|
|
|
column: 0,
|
|
|
|
|
usecol: []int{1, 3},
|
|
|
|
|
usecolstr: "1,3",
|
|
|
|
|
expect: `
|
|
|
|
|
NAME(1) COUNT(3)
|
|
|
|
|
beta 33
|
|
|
|
|
alpha 170
|
|
|
|
|
ceta 9`,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
//------------------------ SORT TESTS
|
|
|
|
|
{
|
|
|
|
|
name: "sortbycolumn",
|
|
|
|
|
column: 3,
|
|
|
|
|
sortby: "numeric",
|
|
|
|
|
desc: false,
|
|
|
|
|
expect: `
|
|
|
|
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
2022-10-15 19:31:42 +02:00
|
|
|
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
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "sortbycolumn",
|
|
|
|
|
column: 2,
|
|
|
|
|
sortby: "duration",
|
|
|
|
|
desc: false,
|
|
|
|
|
expect: `
|
|
|
|
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
2022-10-15 19:31:42 +02:00
|
|
|
alpha 4h35m 170 2013-Feb-03
|
|
|
|
|
beta 1d10h5m1s 33 3/1/2014
|
|
|
|
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
|
2022-10-17 20:04:05 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "sortbycolumn",
|
|
|
|
|
column: 4,
|
|
|
|
|
sortby: "time",
|
|
|
|
|
desc: false,
|
|
|
|
|
expect: `
|
|
|
|
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
2022-10-15 19:31:42 +02:00
|
|
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700
|
|
|
|
|
alpha 4h35m 170 2013-Feb-03
|
|
|
|
|
beta 1d10h5m1s 33 3/1/2014`,
|
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) {
|
2022-10-15 19:31:42 +02:00
|
|
|
NoColor = true
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2022-10-17 20:04:05 +02:00
|
|
|
testname := fmt.Sprintf("print-sortcol-%d-desc-%t-sortby-%s-mode-%s-usecolumns-%s",
|
|
|
|
|
tt.column, tt.desc, tt.sortby, tt.mode, tt.usecolstr)
|
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
|
|
|
|
|
var w bytes.Buffer
|
|
|
|
|
|
|
|
|
|
// cmd flags
|
2022-10-15 19:31:42 +02:00
|
|
|
SortByColumn = tt.column
|
|
|
|
|
SortDescending = tt.desc
|
|
|
|
|
SortMode = tt.sortby
|
2022-10-17 20:04:05 +02:00
|
|
|
OutputMode = tt.mode
|
|
|
|
|
NoNumbering = tt.nonum
|
|
|
|
|
UseColumns = tt.usecol
|
2022-10-15 19:31:42 +02:00
|
|
|
|
2022-10-17 20:04:05 +02:00
|
|
|
// the test checks the len!
|
|
|
|
|
if len(tt.usecol) > 0 {
|
|
|
|
|
Columns = "yes"
|
|
|
|
|
} else {
|
|
|
|
|
Columns = ""
|
2022-10-15 19:31:42 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-17 20:04:05 +02:00
|
|
|
testdata := mockdata
|
|
|
|
|
output := strings.TrimSpace(printData(&w, &testdata))
|
|
|
|
|
exp := strings.TrimSpace(tt.expect)
|
|
|
|
|
if output != exp {
|
|
|
|
|
t.Errorf("not rendered correctly:\n+++ got:\n%s\n+++ want:\n%s",
|
|
|
|
|
output, exp)
|
2022-10-15 19:31:42 +02:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|