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-05 14:31:01 +02:00
|
|
|
"fmt"
|
2022-10-10 20:14:51 +02:00
|
|
|
"github.com/gookit/color"
|
2022-10-02 14:22:31 +02:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-13 18:56:34 +02:00
|
|
|
func stdout2pipe(t *testing.T) (*os.File, *os.File) {
|
|
|
|
|
reader, writer, err := os.Pipe()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
origStdout := os.Stdout
|
|
|
|
|
os.Stdout = writer
|
|
|
|
|
|
|
|
|
|
// we need to tell the color mode the io.Writer, even if we don't usw colorization
|
|
|
|
|
color.SetOutput(writer)
|
|
|
|
|
|
|
|
|
|
return origStdout, reader
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-02 14:22:31 +02:00
|
|
|
func TestPrinter(t *testing.T) {
|
2022-10-05 14:31:01 +02:00
|
|
|
startdata := Tabdata{
|
|
|
|
|
maxwidthHeader: 5,
|
|
|
|
|
maxwidthPerCol: []int{
|
|
|
|
|
5,
|
|
|
|
|
5,
|
|
|
|
|
8,
|
|
|
|
|
},
|
|
|
|
|
columns: 3,
|
|
|
|
|
headers: []string{
|
|
|
|
|
"ONE", "TWO", "THREE",
|
|
|
|
|
},
|
|
|
|
|
entries: [][]string{
|
2022-10-14 19:51:19 +02:00
|
|
|
{
|
2022-10-05 14:31:01 +02:00
|
|
|
"asd", "igig", "cxxxncnc",
|
|
|
|
|
},
|
2022-10-14 19:51:19 +02:00
|
|
|
{
|
2022-10-05 14:31:01 +02:00
|
|
|
"19191", "EDD 1", "X",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2022-10-02 14:22:31 +02:00
|
|
|
|
|
|
|
|
expects := map[string]string{
|
|
|
|
|
"ascii": `ONE(1) TWO(2) THREE(3)
|
|
|
|
|
asd igig cxxxncnc
|
|
|
|
|
19191 EDD 1 X`,
|
2022-10-13 18:56:34 +02:00
|
|
|
|
2022-10-02 14:22:31 +02:00
|
|
|
"orgtbl": `|--------+--------+----------|
|
|
|
|
|
| ONE(1) | TWO(2) | THREE(3) |
|
|
|
|
|
|--------+--------+----------|
|
|
|
|
|
| asd | igig | cxxxncnc |
|
|
|
|
|
| 19191 | EDD 1 | X |
|
|
|
|
|
|--------+--------+----------|`,
|
2022-10-13 18:56:34 +02:00
|
|
|
|
2022-10-02 14:22:31 +02:00
|
|
|
"markdown": `| ONE(1) | TWO(2) | THREE(3) |
|
|
|
|
|
|--------|--------|----------|
|
|
|
|
|
| asd | igig | cxxxncnc |
|
|
|
|
|
| 19191 | EDD 1 | X |`,
|
2022-10-13 18:56:34 +02:00
|
|
|
|
2022-10-05 14:31:01 +02:00
|
|
|
"shell": `ONE="asd" TWO="igig" THREE="cxxxncnc"
|
|
|
|
|
ONE="19191" TWO="EDD 1" THREE="X"`,
|
2022-10-13 18:56:34 +02:00
|
|
|
|
2022-10-11 09:11:33 +02:00
|
|
|
"extended": `ONE(1): asd
|
|
|
|
|
TWO(2): igig
|
|
|
|
|
THREE(3): cxxxncnc
|
|
|
|
|
|
|
|
|
|
ONE(1): 19191
|
|
|
|
|
TWO(2): EDD 1
|
|
|
|
|
THREE(3): X`,
|
2022-10-02 14:22:31 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 20:14:51 +02:00
|
|
|
NoColor = true
|
2022-10-13 18:56:34 +02:00
|
|
|
SortByColumn = 0 // disable sorting
|
2022-10-10 20:14:51 +02:00
|
|
|
|
2022-10-13 18:56:34 +02:00
|
|
|
origStdout, reader := stdout2pipe(t)
|
2022-10-10 20:14:51 +02:00
|
|
|
|
2022-10-02 14:22:31 +02:00
|
|
|
for mode, expect := range expects {
|
2022-10-05 14:31:01 +02:00
|
|
|
testname := fmt.Sprintf("print-%s", mode)
|
|
|
|
|
t.Run(testname, func(t *testing.T) {
|
2022-10-03 12:52:26 +02:00
|
|
|
|
2022-10-05 14:31:01 +02:00
|
|
|
OutputMode = mode
|
2022-10-14 19:51:19 +02:00
|
|
|
// we need to reset our mock data, since it's being
|
|
|
|
|
// modified in printData()
|
|
|
|
|
data := startdata
|
2022-10-05 14:31:01 +02:00
|
|
|
printData(&data)
|
2022-10-02 14:22:31 +02:00
|
|
|
|
2022-10-05 14:31:01 +02:00
|
|
|
buf := make([]byte, 1024)
|
2022-10-13 18:56:34 +02:00
|
|
|
n, err := reader.Read(buf)
|
2022-10-05 14:31:01 +02:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
buf = buf[:n]
|
|
|
|
|
output := strings.TrimSpace(string(buf))
|
2022-10-02 14:22:31 +02:00
|
|
|
|
2022-10-05 14:31:01 +02:00
|
|
|
if output != expect {
|
2022-10-13 18:56:34 +02:00
|
|
|
t.Errorf("output mode: %s, got:\n%s\nwant:\n%s\n (%d <=> %d)",
|
|
|
|
|
mode, output, expect, len(output), len(expect))
|
2022-10-05 14:31:01 +02:00
|
|
|
}
|
|
|
|
|
})
|
2022-10-02 14:22:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restore
|
|
|
|
|
os.Stdout = origStdout
|
|
|
|
|
|
|
|
|
|
}
|
2022-10-13 18:56:34 +02:00
|
|
|
|
|
|
|
|
func TestSortPrinter(t *testing.T) {
|
|
|
|
|
startdata := Tabdata{
|
|
|
|
|
maxwidthHeader: 5,
|
|
|
|
|
maxwidthPerCol: []int{
|
|
|
|
|
3,
|
|
|
|
|
3,
|
|
|
|
|
2,
|
|
|
|
|
},
|
|
|
|
|
columns: 3,
|
|
|
|
|
headers: []string{
|
|
|
|
|
"ONE", "TWO", "THREE",
|
|
|
|
|
},
|
|
|
|
|
entries: [][]string{
|
2022-10-14 19:51:19 +02:00
|
|
|
{
|
2022-10-13 18:56:34 +02:00
|
|
|
"abc", "345", "b1",
|
|
|
|
|
},
|
2022-10-14 19:51:19 +02:00
|
|
|
{
|
2022-10-13 18:56:34 +02:00
|
|
|
"bcd", "234", "a2",
|
|
|
|
|
},
|
2022-10-14 19:51:19 +02:00
|
|
|
{
|
2022-10-13 18:56:34 +02:00
|
|
|
"cde", "123", "c3",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
|
data Tabdata
|
|
|
|
|
sortby int
|
2022-10-15 16:27:04 +02:00
|
|
|
desc bool
|
2022-10-13 18:56:34 +02:00
|
|
|
expect string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
data: startdata,
|
|
|
|
|
sortby: 1,
|
2022-10-15 16:27:04 +02:00
|
|
|
desc: false,
|
2022-10-13 18:56:34 +02:00
|
|
|
expect: `ONE(1) TWO(2) THREE(3)
|
|
|
|
|
abc 345 b1
|
|
|
|
|
bcd 234 a2
|
|
|
|
|
cde 123 c3`,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
data: startdata,
|
|
|
|
|
sortby: 2,
|
2022-10-15 16:27:04 +02:00
|
|
|
desc: false,
|
2022-10-13 18:56:34 +02:00
|
|
|
expect: `ONE(1) TWO(2) THREE(3)
|
|
|
|
|
cde 123 c3
|
|
|
|
|
bcd 234 a2
|
|
|
|
|
abc 345 b1`,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
data: startdata,
|
|
|
|
|
sortby: 3,
|
2022-10-15 16:27:04 +02:00
|
|
|
desc: false,
|
2022-10-13 18:56:34 +02:00
|
|
|
expect: `ONE(1) TWO(2) THREE(3)
|
|
|
|
|
bcd 234 a2
|
|
|
|
|
abc 345 b1
|
|
|
|
|
cde 123 c3`,
|
|
|
|
|
},
|
2022-10-15 16:27:04 +02:00
|
|
|
{
|
|
|
|
|
data: startdata,
|
|
|
|
|
sortby: 1,
|
|
|
|
|
desc: true,
|
|
|
|
|
expect: `ONE(1) TWO(2) THREE(3)
|
|
|
|
|
cde 123 c3
|
|
|
|
|
bcd 234 a2
|
|
|
|
|
abc 345 b1`,
|
|
|
|
|
},
|
2022-10-13 18:56:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NoColor = true
|
|
|
|
|
OutputMode = "ascii"
|
|
|
|
|
origStdout, reader := stdout2pipe(t)
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2022-10-15 16:27:04 +02:00
|
|
|
testname := fmt.Sprintf("print-sorted-table-by-%d-desc-%t", tt.sortby, tt.desc)
|
2022-10-13 18:56:34 +02:00
|
|
|
t.Run(testname, func(t *testing.T) {
|
|
|
|
|
SortByColumn = tt.sortby
|
2022-10-15 16:27:04 +02:00
|
|
|
SortDescending = tt.desc
|
2022-10-13 18:56:34 +02:00
|
|
|
|
|
|
|
|
printData(&tt.data)
|
|
|
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
n, err := reader.Read(buf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
buf = buf[:n]
|
|
|
|
|
output := strings.TrimSpace(string(buf))
|
|
|
|
|
|
|
|
|
|
if output != tt.expect {
|
|
|
|
|
t.Errorf("sort column: %d, got:\n%s\nwant:\n%s",
|
|
|
|
|
tt.sortby, output, tt.expect)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restore
|
|
|
|
|
os.Stdout = origStdout
|
|
|
|
|
}
|