planner/plan/command/command_test.go

87 lines
1.6 KiB
Go

package command_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"go-mod.ewintr.nl/planner/plan/command"
)
func TestArgSet(t *testing.T) {
t.Parallel()
as := command.ArgSet{
Main: "main",
Flags: map[string]string{
"name 1": "value 1",
"name 2": "value 2",
"name 3": "value 3",
},
}
t.Run("hasflag", func(t *testing.T) {
t.Run("true", func(t *testing.T) {
if has := as.HasFlag("name 1"); !has {
t.Errorf("exp true, got %v", has)
}
})
t.Run("false", func(t *testing.T) {
if has := as.HasFlag("unknown"); has {
t.Errorf("exp false, got %v", has)
}
})
})
t.Run("flag", func(t *testing.T) {
t.Run("known", func(t *testing.T) {
if val := as.Flag("name 1"); val != "value 1" {
t.Errorf("exp value 1, got %v", val)
}
})
t.Run("unknown", func(t *testing.T) {
if val := as.Flag("unknown"); val != "" {
t.Errorf(`exp "", got %v`, val)
}
})
})
t.Run("setflag", func(t *testing.T) {
exp := "new value"
as.SetFlag("new name", exp)
if act := as.Flag("new name"); exp != act {
t.Errorf("exp %v, got %v", exp, act)
}
})
}
func TestParseArgs(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
args []string
expAS *command.ArgSet
expErr bool
}{
{
name: "empty",
expAS: &command.ArgSet{
Flags: map[string]string{},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
actAS, actErr := command.ParseArgs(tc.args)
if tc.expErr != (actErr != nil) {
t.Errorf("exp %v, got %v", tc.expErr, actErr)
}
if tc.expErr {
return
}
if diff := cmp.Diff(tc.expAS, actAS); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff)
}
})
}
}