mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
added -v flag, replace 'help' subcommand wich -m, more tests
This commit is contained in:
@@ -31,12 +31,13 @@ var (
|
||||
OutflagOrgtable bool
|
||||
OutflagShell bool
|
||||
OutputMode string
|
||||
InvertMatch bool
|
||||
|
||||
// used for validation
|
||||
validOutputmodes = "(orgtbl|markdown|extended|ascii)"
|
||||
|
||||
// main program version
|
||||
Version = "v1.0.4"
|
||||
Version = "v1.0.5"
|
||||
|
||||
// generated version string, used by -v contains lib.Version on
|
||||
// main branch, and lib.Version-$branch-$lastcommit-$date on
|
||||
|
||||
@@ -114,7 +114,11 @@ func parseFile(input io.Reader, pattern string) (Tabdata, error) {
|
||||
} else {
|
||||
// data processing
|
||||
if len(pattern) > 0 {
|
||||
if !patternR.MatchString(line) {
|
||||
if patternR.MatchString(line) == InvertMatch {
|
||||
// by default -v is false, so if a line does NOT
|
||||
// match the pattern, we will ignore it. However,
|
||||
// if the user specified -v, the matching is inverted,
|
||||
// so we ignore all lines, which DO match.
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -151,7 +155,7 @@ func parseFile(input io.Reader, pattern string) (Tabdata, error) {
|
||||
}
|
||||
|
||||
if scanner.Err() != nil {
|
||||
return data, errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", pattern, scanner.Err()))
|
||||
return data, errors.Unwrap(fmt.Errorf("Failed to read from io.Reader: %w", scanner.Err()))
|
||||
}
|
||||
|
||||
if Debug {
|
||||
|
||||
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -80,3 +81,57 @@ asd igig cxxxncnc
|
||||
t.Errorf("Parser returned invalid data\nExp: %+v\nGot: %+v\n", data, gotdata)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParserPatternmatching(t *testing.T) {
|
||||
var tests = []struct {
|
||||
entries [][]string
|
||||
pattern string
|
||||
invert bool
|
||||
}{
|
||||
{
|
||||
entries: [][]string{
|
||||
[]string{
|
||||
"asd",
|
||||
"igig",
|
||||
"cxxxncnc",
|
||||
},
|
||||
},
|
||||
pattern: "ig",
|
||||
invert: false,
|
||||
},
|
||||
{
|
||||
entries: [][]string{
|
||||
[]string{
|
||||
"19191",
|
||||
"EDD 1",
|
||||
"X",
|
||||
},
|
||||
},
|
||||
pattern: "ig",
|
||||
invert: true,
|
||||
},
|
||||
}
|
||||
|
||||
table := `ONE TWO THREE
|
||||
asd igig cxxxncnc
|
||||
19191 EDD 1 X`
|
||||
|
||||
for _, tt := range tests {
|
||||
testname := fmt.Sprintf("parse-with-inverted-pattern-%t", tt.invert)
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
InvertMatch = tt.invert
|
||||
|
||||
readFd := strings.NewReader(table)
|
||||
gotdata, err := parseFile(readFd, tt.pattern)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(tt.entries, gotdata.entries) {
|
||||
t.Errorf("Parser returned invalid data (pattern: %s, invert: %t)\nExp: %+v\nGot: %+v\n",
|
||||
tt.pattern, tt.invert, tt.entries, gotdata.entries)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user