planner/plan/command/argset_test.go

104 lines
2.2 KiB
Go
Raw Normal View History

2024-10-29 07:22:04 +01:00
package command_test
import (
"testing"
"time"
2024-12-01 10:22:47 +01:00
"go-mod.ewintr.nl/planner/item"
2024-10-29 07:22:04 +01:00
"go-mod.ewintr.nl/planner/plan/command"
)
func TestArgSet(t *testing.T) {
for _, tt := range []struct {
name string
flags map[string]command.Flag
flagName string
setValue string
exp interface{}
expErr bool
}{
{
name: "string flag success",
flags: map[string]command.Flag{
"title": &command.FlagString{Name: "title"},
},
flagName: "title",
setValue: "test title",
exp: "test title",
},
{
name: "date flag success",
flags: map[string]command.Flag{
"date": &command.FlagDate{Name: "date"},
},
flagName: "date",
setValue: "2024-01-02",
exp: time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC),
},
{
name: "time flag success",
flags: map[string]command.Flag{
"time": &command.FlagTime{Name: "time"},
},
flagName: "time",
setValue: "15:04",
exp: time.Date(0, 1, 1, 15, 4, 0, 0, time.UTC),
},
{
name: "duration flag success",
flags: map[string]command.Flag{
"duration": &command.FlagDuration{Name: "duration"},
},
flagName: "duration",
setValue: "2h30m",
exp: 2*time.Hour + 30*time.Minute,
},
2024-12-01 10:22:47 +01:00
{
name: "recur period flag success",
flags: map[string]command.Flag{
2024-12-19 12:06:03 +01:00
"recur": &command.FlagRecurrer{Name: "recur"},
2024-12-01 10:22:47 +01:00
},
2024-12-19 12:06:03 +01:00
flagName: "recur",
setValue: "2024-12-23, daily",
exp: item.NewRecurrer("2024-12-23, daily"),
2024-12-01 10:22:47 +01:00
},
2024-10-29 07:22:04 +01:00
{
name: "unknown flag error",
flags: map[string]command.Flag{},
flagName: "unknown",
setValue: "value",
expErr: true,
},
{
name: "invalid date format error",
flags: map[string]command.Flag{
"date": &command.FlagDate{Name: "date"},
},
flagName: "date",
setValue: "invalid",
expErr: true,
},
} {
t.Run(tt.name, func(t *testing.T) {
as := &command.ArgSet{
Main: "test",
Flags: tt.flags,
}
err := as.Set(tt.flagName, tt.setValue)
if (err != nil) != tt.expErr {
t.Errorf("ArgSet.Set() error = %v, expErr %v", err, tt.expErr)
return
}
if tt.expErr {
return
}
if !as.IsSet(tt.flagName) {
t.Errorf("ArgSet.IsSet() = false, want true for flag %s", tt.flagName)
}
})
}
}