adapted version generation to cfg module, added and fixed unit tests

This commit is contained in:
2022-10-19 19:32:41 +02:00
parent 90872e0c60
commit 9dd2a49d9b
7 changed files with 135 additions and 21 deletions

View File

@@ -17,4 +17,82 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cfg
// FIXME: add tests
import (
"fmt"
// "reflect"
"testing"
)
func TestPrepareModeFlags(t *testing.T) {
var tests = []struct {
flag Modeflag
mode string // input, if any
expect string // output
want bool
}{
// short commandline flags like -M
{Modeflag{X: true}, "", "extended", false},
{Modeflag{S: true}, "", "shell", false},
{Modeflag{O: true}, "", "orgtbl", false},
{Modeflag{Y: true}, "", "yaml", false},
{Modeflag{M: true}, "", "markdown", false},
{Modeflag{}, "", "ascii", false},
// long flags like -o yaml
{Modeflag{}, "extended", "extended", false},
{Modeflag{}, "shell", "shell", false},
{Modeflag{}, "orgtbl", "orgtbl", false},
{Modeflag{}, "yaml", "yaml", false},
{Modeflag{}, "markdown", "markdown", false},
// failing
{Modeflag{}, "blah", "", true},
}
for _, tt := range tests {
testname := fmt.Sprintf("PrepareModeFlags-flags-mode-%s-expect-%s-want-%t",
tt.mode, tt.expect, tt.want)
t.Run(testname, func(t *testing.T) {
c := Config{OutputMode: tt.mode}
// check either flag or pre filled mode, whatever is defined in tt
err := c.PrepareModeFlags(tt.flag, tt.mode)
if err != nil {
if !tt.want {
// expect to fail
t.Fatalf("PrepareModeFlags returned unexpected error: %s", err)
}
} else {
if c.OutputMode != tt.expect {
t.Errorf("got: %s, expect: %s", c.OutputMode, tt.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"},
}
for _, tt := range tests {
testname := fmt.Sprintf("PrepareSortFlags-expect-%s", tt.expect)
t.Run(testname, func(t *testing.T) {
c := Config{}
c.PrepareSortFlags(tt.flag)
if c.SortMode != tt.expect {
t.Errorf("got: %s, expect: %s", c.SortMode, tt.expect)
}
})
}
}