continued refactoring, added more tests, better error handling

This commit is contained in:
2022-10-02 14:22:31 +02:00
parent 66c4b68036
commit e6723a6951
9 changed files with 244 additions and 41 deletions

View File

@@ -40,16 +40,18 @@ func contains(s []int, e int) bool {
return false
}
func PrepareColumns() {
func PrepareColumns() error {
if len(Columns) > 0 {
for _, use := range strings.Split(Columns, ",") {
usenum, err := strconv.Atoi(use)
if err != nil {
die(err)
msg := fmt.Sprintf("Could not parse columns list %s: %v", Columns, err)
return errors.New(msg)
}
UseColumns = append(UseColumns, usenum)
}
}
return nil
}
func PrepareModeFlags() error {

View File

@@ -19,20 +19,22 @@ package lib
import (
"fmt"
"reflect"
"testing"
)
func TestArrayContains(t *testing.T) {
func Testcontains(t *testing.T) {
var tests = []struct {
list []int
search int
want bool
}{
{[]int{1, 2, 3}, 2, true},
{[]int{2, 3, 4}, 5, false},
}
for _, tt := range tests {
testname := fmt.Sprintf("%d,%d,%t", tt.list, tt.search, tt.want)
testname := fmt.Sprintf("contains-%d,%d,%t", tt.list, tt.search, tt.want)
t.Run(testname, func(t *testing.T) {
answer := contains(tt.list, tt.search)
if answer != tt.want {
@@ -41,3 +43,31 @@ func TestArrayContains(t *testing.T) {
})
}
}
func TestPrepareColumns(t *testing.T) {
var tests = []struct {
input string
exp []int
wanterror bool // expect error
}{
{"1,2,3", []int{1, 2, 3}, false},
{"1,2,", []int{}, true},
}
for _, tt := range tests {
testname := fmt.Sprintf("PrepareColumns-%s-%t", tt.input, tt.wanterror)
t.Run(testname, func(t *testing.T) {
Columns = tt.input
err := PrepareColumns()
if err != nil {
if !tt.wanterror {
t.Errorf("got error: %v", err)
}
} else {
if !reflect.DeepEqual(UseColumns, tt.exp) {
t.Errorf("got: %v, expected: %v", UseColumns, tt.exp)
}
}
})
}
}

View File

@@ -19,34 +19,48 @@ package lib
import (
"errors"
"github.com/alecthomas/repr"
"io"
"os"
)
func ProcessFiles(args []string) error {
var pattern string
havefiles := false
fds, pattern, err := determineIO(args)
//prepareColumns()
if err != nil {
return err
}
for _, fd := range fds {
printData(parseFile(fd, pattern))
}
return nil
}
func determineIO(args []string) ([]io.Reader, string, error) {
var pattern string
var fds []io.Reader
var havefiles bool
if len(args) > 0 {
// threre were args left, take a look
if _, err := os.Stat(args[0]); err != nil {
// first one is not a file, consider it as regexp and
// shift arg list
pattern = args[0]
args = args[1:]
}
if len(args) > 0 {
// only files
for _, file := range args {
fd, err := os.OpenFile(file, os.O_RDONLY, 0755)
if err != nil {
die(err)
return nil, "", err
}
data := parseFile(fd, pattern)
if Debug {
repr.Print(data)
}
printData(data)
fds = append(fds, fd)
}
havefiles = true
}
@@ -55,15 +69,11 @@ func ProcessFiles(args []string) error {
if !havefiles {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
data := parseFile(os.Stdin, pattern)
if Debug {
repr.Print(data)
}
printData(data)
fds = append(fds, os.Stdin)
} else {
return errors.New("No file specified and nothing to read on stdin!")
return nil, "", errors.New("No file specified and nothing to read on stdin!")
}
}
return nil
return fds, pattern, nil
}

View File

@@ -20,6 +20,7 @@ package lib
import (
"bufio"
"fmt"
"github.com/alecthomas/repr"
"io"
"regexp"
"strings"
@@ -59,7 +60,7 @@ func parseFile(input io.Reader, pattern string) Tabdata {
scanner = bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
line := strings.TrimSpace(scanner.Text())
values := []string{}
patternR, err := regexp.Compile(pattern)
@@ -109,22 +110,18 @@ func parseFile(input io.Reader, pattern string) Tabdata {
// done
hadFirst = true
}
// if Debug {
// fmt.Println(data.headerIndices)
// }
} else {
// data processing
if len(pattern) > 0 {
//fmt.Println(patternR.MatchString(line))
if !patternR.MatchString(line) {
continue
}
}
idx := 0 // we cannot use the header index, because we could exclude columns
for _, index := range data.headerIndices {
value := ""
if index["end"] == 0 {
value = string(line[index["beg"]:])
} else {
@@ -159,5 +156,9 @@ func parseFile(input io.Reader, pattern string) Tabdata {
die(scanner.Err())
}
if Debug {
repr.Print(data)
}
return data
}

77
lib/parser_test.go Normal file
View File

@@ -0,0 +1,77 @@
/*
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 (
"reflect"
"strings"
"testing"
)
func TestParser(t *testing.T) {
data := Tabdata{
maxwidthHeader: 5,
maxwidthPerCol: []int{
5,
5,
8,
},
columns: 3,
headerIndices: []map[string]int{
map[string]int{
"beg": 0,
"end": 6,
},
map[string]int{
"end": 13,
"beg": 7,
},
map[string]int{
"beg": 14,
"end": 0,
},
},
headers: []string{
"ONE",
"TWO",
"THREE",
},
entries: [][]string{
[]string{
"asd",
"igig",
"cxxxncnc",
},
[]string{
"19191",
"EDD 1",
"X",
},
},
}
table := `ONE TWO THREE
asd igig cxxxncnc
19191 EDD 1 X`
readFd := strings.NewReader(table)
gotdata := parseFile(readFd, "")
if !reflect.DeepEqual(data, gotdata) {
t.Errorf("Parser returned invalid data\nExp: %+v\nGot: %+v\n", data, gotdata)
}
}

76
lib/printer_test.go Normal file
View File

@@ -0,0 +1,76 @@
/*
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 (
"os"
"strings"
"testing"
)
func TestPrinter(t *testing.T) {
table := `ONE TWO THREE
asd igig cxxxncnc
19191 EDD 1 X`
expects := map[string]string{
"ascii": `ONE(1) TWO(2) THREE(3)
asd igig cxxxncnc
19191 EDD 1 X`,
"orgtbl": `|--------+--------+----------|
| ONE(1) | TWO(2) | THREE(3) |
|--------+--------+----------|
| asd | igig | cxxxncnc |
| 19191 | EDD 1 | X |
|--------+--------+----------|`,
"markdown": `| ONE(1) | TWO(2) | THREE(3) |
|--------|--------|----------|
| asd | igig | cxxxncnc |
| 19191 | EDD 1 | X |`,
}
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
origStdout := os.Stdout
os.Stdout = w
for mode, expect := range expects {
OutputMode = mode
fd := strings.NewReader(table)
data := parseFile(fd, "")
printData(data)
buf := make([]byte, 1024)
n, err := r.Read(buf)
if err != nil {
t.Fatal(err)
}
buf = buf[:n]
output := strings.TrimSpace(string(buf))
if output != expect {
t.Errorf("output mode: %s, got:\n%s\nwant:\n%s\n (%d <=> %d)", mode, output, expect, len(output), len(expect))
}
}
// Restore
os.Stdout = origStdout
}