This commit is contained in:
Erik Winter 2024-11-09 17:22:05 +01:00
parent aeb8e3d741
commit b7da3fba1e
2 changed files with 73 additions and 4 deletions

View File

@ -44,15 +44,12 @@ func (add *AddCmd) Parse(args []string) (*ArgSet, error) {
if !as.HasFlag(FlagOn) { if !as.HasFlag(FlagOn) {
return nil, fmt.Errorf("%w: date is required", ErrInvalidArg) return nil, fmt.Errorf("%w: date is required", ErrInvalidArg)
} }
if !as.HasFlag(FlagAt) && !as.HasFlag(FlagFor) { if !as.HasFlag(FlagAt) && as.HasFlag(FlagFor) {
return nil, fmt.Errorf("%w: can not have duration without start time", ErrInvalidArg) return nil, fmt.Errorf("%w: can not have duration without start time", ErrInvalidArg)
} }
if !as.HasFlag(FlagAt) && !as.HasFlag(FlagFor) { if !as.HasFlag(FlagAt) && !as.HasFlag(FlagFor) {
as.SetFlag(FlagFor, "24h") as.SetFlag(FlagFor, "24h")
} }
if err := add.Do(as); err != nil {
return nil, err
}
return as, nil return as, nil
} }

View File

@ -1,5 +1,77 @@
package command_test package command_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"go-mod.ewintr.nl/planner/plan/command"
)
func TestAddParse(t *testing.T) {
t.Parallel()
cmd := command.AddCmd{}
for _, tc := range []struct {
name string
args []string
expAS *command.ArgSet
expErr bool
}{
{
name: "empty",
expErr: true,
},
{
name: "title missing",
args: []string{"add", "-on", "2024-11-09"},
expErr: true,
},
{
name: "date missing",
args: []string{"add", "some", "title"},
expErr: true,
},
{
name: "minimal",
args: []string{"add", "title", "-on", "2024-11-09"},
expAS: &command.ArgSet{
Main: "title",
Flags: map[string]string{
command.FlagOn: "2024-11-09",
command.FlagFor: "24h",
},
},
},
{
name: "start",
args: []string{"add", "title", "-on", "2024-11-09", "-at", "12:00"},
expAS: &command.ArgSet{
Main: "title",
Flags: map[string]string{
command.FlagOn: "2024-11-09",
command.FlagAt: "12:00",
},
},
},
{
// name: "start and duration"
},
} {
t.Run(tc.name, func(t *testing.T) {
actAS, actErr := cmd.Parse(tc.args)
if tc.expErr != (actErr != nil) {
t.Errorf("exp nil, got %v", actErr)
}
if tc.expErr {
return
}
if diff := cmp.Diff(tc.expAS, actAS); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff)
}
})
}
}
// func TestAdd(t *testing.T) { // func TestAdd(t *testing.T) {
// t.Parallel() // t.Parallel()