This commit is contained in:
Erik Winter 2024-11-15 07:25:23 +01:00
parent e50e08eefd
commit aeda0158dc
3 changed files with 250 additions and 261 deletions

View File

@ -43,7 +43,9 @@ func (add *AddCmd) Parse(main []string, flags map[string]string) error {
return ErrWrongCommand return ErrWrongCommand
} }
as := add.argSet as := add.argSet
if len(main) > 1 {
as.Main = strings.Join(main[1:], " ") as.Main = strings.Join(main[1:], " ")
}
for k := range as.Flags { for k := range as.Flags {
if err := as.Set(k, flags[k]); err != nil { if err := as.Set(k, flags[k]); err != nil {
return fmt.Errorf("could not set %s: %v", k, err) return fmt.Errorf("could not set %s: %v", k, err)

View File

@ -2,10 +2,7 @@ package command_test
import ( import (
"testing" "testing"
"time"
"github.com/google/go-cmp/cmp"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/command" "go-mod.ewintr.nl/planner/plan/command"
"go-mod.ewintr.nl/planner/plan/storage/memory" "go-mod.ewintr.nl/planner/plan/storage/memory"
) )
@ -14,30 +11,33 @@ func TestAddParse(t *testing.T) {
t.Parallel() t.Parallel()
aDateStr := "2024-11-02" aDateStr := "2024-11-02"
aDate := time.Date(2024, 11, 9, 0, 0, 0, 0, time.UTC) // aDate := time.Date(2024, 11, 9, 0, 0, 0, 0, time.UTC)
aTimeStr := "12:00" aTimeStr := "12:00"
aTime := time.Date(0, 0, 0, 12, 0, 0, 0, time.UTC) // aTime := time.Date(0, 0, 0, 12, 0, 0, 0, time.UTC)
aDayStr := "24h" // aDayStr := "24h"
aDay := time.Duration(24) * time.Hour // aDay := time.Duration(24) * time.Hour
flagOn := &command.FlagDate{ // flagOn := &command.FlagDate{
Name: command.FlagOn, // Name: command.FlagOn,
Value: aDate, // Value: aDate,
} // }
flagAt := &command.FlagTime{ // flagAt := &command.FlagTime{
Name: command.FlagAt, // Name: command.FlagAt,
Value: aTime, // Value: aTime,
} // }
flagFor := &command.FlagDuration{ // flagFor := &command.FlagDuration{
Name: command.FlagFor, // Name: command.FlagFor,
Value: aDay, // Value: aDay,
} // }
cmd := command.AddCmd{} eventRepo := memory.NewEvent()
localRepo := memory.NewLocalID()
syncRepo := memory.NewSync()
cmd := command.NewAddCmd(localRepo, eventRepo, syncRepo)
for _, tc := range []struct { for _, tc := range []struct {
name string name string
main []string main []string
flags map[string]command.Flag flags map[string]string
expErr bool expErr bool
}{ }{
{ {
@ -47,8 +47,8 @@ func TestAddParse(t *testing.T) {
{ {
name: "title missing", name: "title missing",
main: []string{"add"}, main: []string{"add"},
flags: map[string]command.Flag{ flags: map[string]string{
command.FlagOn: flagOn, command.FlagOn: aDateStr,
}, },
expErr: true, expErr: true,
}, },
@ -60,16 +60,16 @@ func TestAddParse(t *testing.T) {
{ {
name: "minimal", name: "minimal",
main: []string{"add", "title"}, main: []string{"add", "title"},
flags: map[string]command.Flag{ flags: map[string]string{
command.FlagOn: flagOn, command.FlagOn: aDateStr,
}, },
}, },
{ {
name: "start", name: "start",
main: []string{"add", "title"}, main: []string{"add", "title"},
flags: map[string]command.Flag{ flags: map[string]string{
command.FlagOn: flagOn, command.FlagOn: aDateStr,
command.FlagAt: flagAt, command.FlagAt: aTimeStr,
}, },
}, },
// { // {
@ -95,135 +95,129 @@ func TestAddParse(t *testing.T) {
if tc.expErr != (actErr != nil) { if tc.expErr != (actErr != nil) {
t.Errorf("exp nil, got %v", actErr) 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()
oneHour, err := time.ParseDuration("1h") // oneHour, err := time.ParseDuration("1h")
if err != nil { // if err != nil {
t.Errorf("exp nil, got %v", err) // t.Errorf("exp nil, got %v", err)
} // }
oneDay, err := time.ParseDuration("24h") // oneDay, err := time.ParseDuration("24h")
if err != nil { // if err != nil {
t.Errorf("exp nil, got %v", err) // t.Errorf("exp nil, got %v", err)
} // }
for _, tc := range []struct { // for _, tc := range []struct {
name string // name string
args *command.ArgSet // args *command.ArgSet
expEvent item.Event // expEvent item.Event
expErr bool // expErr bool
}{ // }{
{ // {
name: "time, but no duration", // name: "time, but no duration",
args: &command.ArgSet{ // args: &command.ArgSet{
Main: "event", // Main: "event",
Flags: map[string]string{ // Flags: map[string]string{
command.FlagOn: "2024-10-01", // command.FlagOn: "2024-10-01",
command.FlagAt: "9:00", // command.FlagAt: "9:00",
}, // },
}, // },
expEvent: item.Event{ // expEvent: item.Event{
ID: "a", // ID: "a",
EventBody: item.EventBody{ // EventBody: item.EventBody{
Title: "event", // Title: "event",
Start: time.Date(2024, 10, 1, 9, 0, 0, 0, time.UTC), // Start: time.Date(2024, 10, 1, 9, 0, 0, 0, time.UTC),
}, // },
}, // },
}, // },
{ // {
name: "no time, no duration", // name: "no time, no duration",
args: &command.ArgSet{ // args: &command.ArgSet{
Main: "event", // Main: "event",
Flags: map[string]string{ // Flags: map[string]string{
command.FlagOn: "2024-10-01", // command.FlagOn: "2024-10-01",
command.FlagFor: "24h", // command.FlagFor: "24h",
}, // },
}, // },
expEvent: item.Event{ // expEvent: item.Event{
ID: "a", // ID: "a",
EventBody: item.EventBody{ // EventBody: item.EventBody{
Title: "event", // Title: "event",
Start: time.Date(2024, 10, 1, 0, 0, 0, 0, time.UTC), // Start: time.Date(2024, 10, 1, 0, 0, 0, 0, time.UTC),
Duration: oneDay, // Duration: oneDay,
}, // },
}, // },
}, // },
{ // {
name: "full", // name: "full",
args: &command.ArgSet{ // args: &command.ArgSet{
Main: "event", // Main: "event",
Flags: map[string]string{ // Flags: map[string]string{
command.FlagOn: "2024-10-01", // command.FlagOn: "2024-10-01",
command.FlagAt: "9:00", // command.FlagAt: "9:00",
command.FlagFor: "1h", // command.FlagFor: "1h",
}, // },
}, // },
expEvent: item.Event{ // expEvent: item.Event{
ID: "a", // ID: "a",
EventBody: item.EventBody{ // EventBody: item.EventBody{
Title: "event", // Title: "event",
Start: time.Date(2024, 10, 1, 9, 0, 0, 0, time.UTC), // Start: time.Date(2024, 10, 1, 9, 0, 0, 0, time.UTC),
Duration: oneHour, // Duration: oneHour,
}, // },
}, // },
}, // },
} { // } {
t.Run(tc.name, func(t *testing.T) { // t.Run(tc.name, func(t *testing.T) {
eventRepo := memory.NewEvent() // eventRepo := memory.NewEvent()
localRepo := memory.NewLocalID() // localRepo := memory.NewLocalID()
syncRepo := memory.NewSync() // syncRepo := memory.NewSync()
cmd := command.NewAddCmd(localRepo, eventRepo, syncRepo) // cmd := command.NewAddCmd(localRepo, eventRepo, syncRepo)
actErr := cmd.Do(tc.args) != nil // actErr := cmd.Do(tc.args) != nil
if tc.expErr != actErr { // if tc.expErr != actErr {
t.Errorf("exp %v, got %v", tc.expErr, actErr) // t.Errorf("exp %v, got %v", tc.expErr, actErr)
} // }
if tc.expErr { // if tc.expErr {
return // return
} // }
actEvents, err := eventRepo.FindAll() // actEvents, err := eventRepo.FindAll()
if err != nil { // if err != nil {
t.Errorf("exp nil, got %v", err) // t.Errorf("exp nil, got %v", err)
} // }
if len(actEvents) != 1 { // if len(actEvents) != 1 {
t.Errorf("exp 1, got %d", len(actEvents)) // t.Errorf("exp 1, got %d", len(actEvents))
} // }
actLocalIDs, err := localRepo.FindAll() // actLocalIDs, err := localRepo.FindAll()
if err != nil { // if err != nil {
t.Errorf("exp nil, got %v", err) // t.Errorf("exp nil, got %v", err)
} // }
if len(actLocalIDs) != 1 { // if len(actLocalIDs) != 1 {
t.Errorf("exp 1, got %v", len(actLocalIDs)) // t.Errorf("exp 1, got %v", len(actLocalIDs))
} // }
if _, ok := actLocalIDs[actEvents[0].ID]; !ok { // if _, ok := actLocalIDs[actEvents[0].ID]; !ok {
t.Errorf("exp true, got %v", ok) // t.Errorf("exp true, got %v", ok)
} // }
if actEvents[0].ID == "" { // if actEvents[0].ID == "" {
t.Errorf("exp string not te be empty") // t.Errorf("exp string not te be empty")
} // }
tc.expEvent.ID = actEvents[0].ID // tc.expEvent.ID = actEvents[0].ID
if diff := cmp.Diff(tc.expEvent, actEvents[0]); diff != "" { // if diff := cmp.Diff(tc.expEvent, actEvents[0]); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff) // t.Errorf("(exp +, got -)\n%s", diff)
} // }
updated, err := syncRepo.FindAll() // updated, err := syncRepo.FindAll()
if err != nil { // if err != nil {
t.Errorf("exp nil, got %v", err) // t.Errorf("exp nil, got %v", err)
} // }
if len(updated) != 1 { // if len(updated) != 1 {
t.Errorf("exp 1, got %v", len(updated)) // t.Errorf("exp 1, got %v", len(updated))
} // }
}) // })
} // }
} // }

View File

@ -1,116 +1,109 @@
package command_test package command_test
import ( // func TestArgSet(t *testing.T) {
"testing" // t.Parallel()
"github.com/google/go-cmp/cmp" // as := command.ArgSet{
"go-mod.ewintr.nl/planner/plan/command" // Main: "main",
) // Flags: map[string]string{
// "name 1": "value 1",
// "name 2": "value 2",
// "name 3": "value 3",
// },
// }
func TestArgSet(t *testing.T) { // t.Run("hasflag", func(t *testing.T) {
t.Parallel() // 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)
// }
// })
// })
as := command.ArgSet{ // t.Run("flag", func(t *testing.T) {
Main: "main", // t.Run("known", func(t *testing.T) {
Flags: map[string]string{ // if val := as.Flag("name 1"); val != "value 1" {
"name 1": "value 1", // t.Errorf("exp value 1, got %v", val)
"name 2": "value 2", // }
"name 3": "value 3", // })
}, // t.Run("unknown", func(t *testing.T) {
} // if val := as.Flag("unknown"); val != "" {
// t.Errorf(`exp "", got %v`, val)
// }
// })
// })
t.Run("hasflag", func(t *testing.T) { // t.Run("setflag", func(t *testing.T) {
t.Run("true", func(t *testing.T) { // exp := "new value"
if has := as.HasFlag("name 1"); !has { // as.SetFlag("new name", exp)
t.Errorf("exp true, got %v", has) // if act := as.Flag("new name"); exp != act {
} // t.Errorf("exp %v, got %v", exp, act)
}) // }
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) { // func TestParseArgs(t *testing.T) {
t.Run("known", func(t *testing.T) { // t.Parallel()
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) { // for _, tc := range []struct {
exp := "new value" // name string
as.SetFlag("new name", exp) // args []string
if act := as.Flag("new name"); exp != act { // expAS *command.ArgSet
t.Errorf("exp %v, got %v", exp, act) // expErr bool
} // }{
}) // {
} // name: "empty",
// expAS: &command.ArgSet{
func TestParseArgs(t *testing.T) { // Flags: map[string]string{},
t.Parallel() // },
// },
for _, tc := range []struct { // {
name string // name: "just main",
args []string // args: []string{"one", "two three", "four"},
expAS *command.ArgSet // expAS: &command.ArgSet{
expErr bool // Main: "one two three four",
}{ // Flags: map[string]string{},
{ // },
name: "empty", // },
expAS: &command.ArgSet{ // {
Flags: map[string]string{}, // name: "with flags",
}, // args: []string{"-flag1", "value1", "one", "two", "-flag2", "value2", "-flag3", "value3"},
}, // expAS: &command.ArgSet{
{ // Main: "one two",
name: "just main", // Flags: map[string]string{
args: []string{"one", "two three", "four"}, // "flag1": "value1",
expAS: &command.ArgSet{ // "flag2": "value2",
Main: "one two three four", // "flag3": "value3",
Flags: map[string]string{}, // },
}, // },
}, // },
{ // {
name: "with flags", // name: "flag without value",
args: []string{"-flag1", "value1", "one", "two", "-flag2", "value2", "-flag3", "value3"}, // args: []string{"one", "two", "-flag1"},
expAS: &command.ArgSet{ // expErr: true,
Main: "one two", // },
Flags: map[string]string{ // {
"flag1": "value1", // name: "split main",
"flag2": "value2", // args: []string{"one", "-flag1", "value1", "two"},
"flag3": "value3", // expErr: true,
}, // },
}, // } {
}, // t.Run(tc.name, func(t *testing.T) {
{ // actAS, actErr := command.ParseArgs(tc.args)
name: "flag without value", // if tc.expErr != (actErr != nil) {
args: []string{"one", "two", "-flag1"}, // t.Errorf("exp %v, got %v", tc.expErr, actErr)
expErr: true, // }
}, // if tc.expErr {
{ // return
name: "split main", // }
args: []string{"one", "-flag1", "value1", "two"}, // if diff := cmp.Diff(tc.expAS, actAS); diff != "" {
expErr: true, // t.Errorf("(exp +, got -)\n%s", diff)
}, // }
} { // })
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)
}
})
}
}