From b7da3fba1e3679c087116be77261b3ac7c1720fc Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Sat, 9 Nov 2024 17:22:05 +0100 Subject: [PATCH] wip --- plan/command/add.go | 5 +-- plan/command/add_test.go | 72 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/plan/command/add.go b/plan/command/add.go index d2ff534..716d59e 100644 --- a/plan/command/add.go +++ b/plan/command/add.go @@ -44,15 +44,12 @@ func (add *AddCmd) Parse(args []string) (*ArgSet, error) { if !as.HasFlag(FlagOn) { 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) } if !as.HasFlag(FlagAt) && !as.HasFlag(FlagFor) { as.SetFlag(FlagFor, "24h") } - if err := add.Do(as); err != nil { - return nil, err - } return as, nil } diff --git a/plan/command/add_test.go b/plan/command/add_test.go index 886713d..bda5d5c 100644 --- a/plan/command/add_test.go +++ b/plan/command/add_test.go @@ -1,5 +1,77 @@ 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) { // t.Parallel()