planner/plan/command/command_test.go

127 lines
2.5 KiB
Go
Raw Permalink Normal View History

2024-10-29 07:22:04 +01:00
package command_test
2024-12-19 12:06:03 +01:00
import (
"testing"
2024-10-29 07:22:04 +01:00
2024-12-19 12:06:03 +01:00
"github.com/google/go-cmp/cmp"
"go-mod.ewintr.nl/planner/plan/command"
)
2024-10-29 07:22:04 +01:00
2024-12-27 11:20:32 +01:00
func TestFindFields(t *testing.T) {
2024-12-19 12:06:03 +01:00
t.Parallel()
2024-10-29 07:22:04 +01:00
2024-12-19 12:06:03 +01:00
for _, tc := range []struct {
2024-12-27 11:20:32 +01:00
name string
args []string
expMain []string
expFields map[string]string
2024-12-19 12:06:03 +01:00
}{
{
2024-12-27 11:20:32 +01:00
name: "empty",
expMain: []string{},
expFields: map[string]string{},
2024-12-19 12:06:03 +01:00
},
{
2024-12-27 11:20:32 +01:00
name: "just main",
args: []string{"one", "two three", "four"},
expMain: []string{"one", "two three", "four"},
expFields: map[string]string{},
2024-12-19 12:06:03 +01:00
},
{
name: "with flags",
2024-12-27 11:20:32 +01:00
args: []string{"flag1:value1", "one", "two", "flag2:value2", "flag3:value3"},
2024-12-19 12:06:03 +01:00
expMain: []string{"one", "two"},
2024-12-27 11:20:32 +01:00
expFields: map[string]string{
2024-12-19 12:06:03 +01:00
"flag1": "value1",
"flag2": "value2",
"flag3": "value3",
},
},
{
2024-12-27 11:20:32 +01:00
name: "flag without value",
args: []string{"one", "two", "flag1:"},
expMain: []string{"one", "two"},
expFields: map[string]string{
"flag1": "",
},
},
{
name: "split main",
args: []string{"one", "flag1:value1", "two"},
expMain: []string{"one", "two"},
expFields: map[string]string{
"flag1": "value1",
},
},
} {
t.Run(tc.name, func(t *testing.T) {
actMain, actFields := command.FindFields(tc.args)
if diff := cmp.Diff(tc.expMain, actMain); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff)
}
if diff := cmp.Diff(tc.expFields, actFields); diff != "" {
t.Errorf("(exp +, got -)\n%s", diff)
}
})
}
}
func TestResolveFields(t *testing.T) {
t.Parallel()
tmpl := map[string][]string{
"one": []string{"a", "b"},
"two": []string{"c", "d"},
}
for _, tc := range []struct {
name string
fields map[string]string
expRes map[string]string
expErr bool
}{
{
name: "empty",
expRes: map[string]string{},
},
{
name: "unknown",
fields: map[string]string{
"unknown": "value",
},
2024-12-19 12:06:03 +01:00
expErr: true,
},
{
2024-12-27 11:20:32 +01:00
name: "duplicate",
fields: map[string]string{
"a": "val1",
"b": "val2",
},
2024-12-19 12:06:03 +01:00
expErr: true,
},
2024-12-27 11:20:32 +01:00
{
name: "valid",
fields: map[string]string{
"a": "val1",
"d": "val2",
},
expRes: map[string]string{
"one": "val1",
"two": "val2",
},
},
2024-12-19 12:06:03 +01:00
} {
t.Run(tc.name, func(t *testing.T) {
2024-12-27 11:20:32 +01:00
actRes, actErr := command.ResolveFields(tc.fields, tmpl)
2024-12-19 12:06:03 +01:00
if tc.expErr != (actErr != nil) {
2024-12-27 11:20:32 +01:00
t.Errorf("exp %v, got %v", tc.expErr, actErr != nil)
2024-12-19 12:06:03 +01:00
}
if tc.expErr {
return
}
2024-12-27 11:20:32 +01:00
if diff := cmp.Diff(tc.expRes, actRes); diff != "" {
t.Errorf("(+exp, -got)%s\n", diff)
2024-12-19 12:06:03 +01:00
}
})
}
}