Files
tablizer/cfg/config_test.go

140 lines
3.0 KiB
Go
Raw Normal View History

/*
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 cfg
import (
"fmt"
// "reflect"
"testing"
)
func TestPrepareModeFlags(t *testing.T) {
var tests = []struct {
flag Modeflag
expect int // output (constant enum)
}{
// short commandline flags like -M
{Modeflag{X: true}, Extended},
{Modeflag{S: true}, Shell},
{Modeflag{O: true}, Orgtbl},
{Modeflag{Y: true}, Yaml},
{Modeflag{M: true}, Markdown},
2024-05-07 15:19:54 +02:00
{Modeflag{}, ASCII},
}
// FIXME: use a map for easier printing
2024-05-07 18:01:12 +02:00
for _, testdata := range tests {
testname := fmt.Sprintf("PrepareModeFlags-expect-%d", testdata.expect)
t.Run(testname, func(t *testing.T) {
2024-05-07 18:01:12 +02:00
conf := Config{}
2024-05-07 18:01:12 +02:00
conf.PrepareModeFlags(testdata.flag)
if conf.OutputMode != testdata.expect {
t.Errorf("got: %d, expect: %d", conf.OutputMode, testdata.expect)
}
})
}
}
func TestPrepareSortFlags(t *testing.T) {
var tests = []struct {
flag Sortmode
expect string // output
}{
// short commandline flags like -M
{Sortmode{Numeric: true}, "numeric"},
{Sortmode{Age: true}, "duration"},
{Sortmode{Time: true}, "time"},
{Sortmode{}, "string"},
}
2024-05-07 15:19:54 +02:00
for _, testdata := range tests {
testname := fmt.Sprintf("PrepareSortFlags-expect-%s", testdata.expect)
t.Run(testname, func(t *testing.T) {
2024-05-07 15:19:54 +02:00
conf := Config{}
2024-05-07 15:19:54 +02:00
conf.PrepareSortFlags(testdata.flag)
2024-05-07 15:19:54 +02:00
if conf.SortMode != testdata.expect {
t.Errorf("got: %s, expect: %s", conf.SortMode, testdata.expect)
}
})
}
}
2022-10-25 18:34:28 +02:00
func TestPreparePattern(t *testing.T) {
var tests = []struct {
patterns []*Pattern
name string
wanterr bool
wanticase bool
wantneg bool
2022-10-25 18:34:28 +02:00
}{
{
[]*Pattern{{Pattern: "[A-Z]+"}},
"simple",
false,
false,
false,
},
{
[]*Pattern{{Pattern: "[a-z"}},
"regfail",
true,
false,
false,
},
{
[]*Pattern{{Pattern: "/[A-Z]+/i"}},
"icase",
false,
true,
false,
},
{
[]*Pattern{{Pattern: "/[A-Z]+/!"}},
"negate",
false,
false,
true,
},
{
[]*Pattern{{Pattern: "/[A-Z]+/!i"}},
"negicase",
false,
true,
true,
},
2022-10-25 18:34:28 +02:00
}
2024-05-07 15:19:54 +02:00
for _, testdata := range tests {
testname := fmt.Sprintf("PreparePattern-pattern-%s-wanterr-%t", testdata.name, testdata.wanterr)
2022-10-25 18:34:28 +02:00
t.Run(testname, func(t *testing.T) {
2024-05-07 15:19:54 +02:00
conf := Config{}
2022-10-25 18:34:28 +02:00
err := conf.PreparePattern(testdata.patterns)
2022-10-25 18:34:28 +02:00
if err != nil {
2024-05-07 15:19:54 +02:00
if !testdata.wanterr {
2022-10-25 18:34:28 +02:00
t.Errorf("PreparePattern returned error: %s", err)
}
}
})
}
}