2024-10-29 07:22:04 +01:00
|
|
|
package command_test
|
2024-11-01 07:31:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2024-11-07 07:25:02 +01:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"go-mod.ewintr.nl/planner/plan/command"
|
2024-11-01 07:31:21 +01:00
|
|
|
)
|
|
|
|
|
2024-11-07 07:25:02 +01:00
|
|
|
func TestArgSet(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
as := command.ArgSet{
|
|
|
|
Main: "main",
|
|
|
|
Flags: map[string]string{
|
|
|
|
"name 1": "value 1",
|
|
|
|
"name 2": "value 2",
|
|
|
|
"name 3": "value 3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("hasflag", func(t *testing.T) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("flag", func(t *testing.T) {
|
|
|
|
t.Run("known", func(t *testing.T) {
|
|
|
|
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) {
|
|
|
|
exp := "new value"
|
|
|
|
as.SetFlag("new name", exp)
|
|
|
|
if act := as.Flag("new name"); exp != act {
|
|
|
|
t.Errorf("exp %v, got %v", exp, act)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-06 07:29:29 +01:00
|
|
|
func TestParseArgs(t *testing.T) {
|
2024-11-01 07:31:21 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
2024-11-07 07:25:02 +01:00
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expAS *command.ArgSet
|
|
|
|
expErr bool
|
2024-11-01 07:31:21 +01:00
|
|
|
}{
|
|
|
|
{
|
2024-11-06 07:29:29 +01:00
|
|
|
name: "empty",
|
2024-11-07 07:25:02 +01:00
|
|
|
expAS: &command.ArgSet{
|
|
|
|
Flags: map[string]string{},
|
|
|
|
},
|
2024-11-01 07:31:21 +01:00
|
|
|
},
|
2024-11-08 08:02:43 +01:00
|
|
|
{
|
|
|
|
name: "just main",
|
|
|
|
args: []string{"one", "two three", "four"},
|
|
|
|
expAS: &command.ArgSet{
|
|
|
|
Main: "one two three four",
|
|
|
|
Flags: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "with flags",
|
|
|
|
args: []string{"-flag1", "value1", "one", "two", "-flag2", "value2", "-flag3", "value3"},
|
|
|
|
expAS: &command.ArgSet{
|
|
|
|
Main: "one two",
|
|
|
|
Flags: map[string]string{
|
|
|
|
"flag1": "value1",
|
|
|
|
"flag2": "value2",
|
|
|
|
"flag3": "value3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "flag without value",
|
|
|
|
args: []string{"one", "two", "-flag1"},
|
|
|
|
expErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "split main",
|
|
|
|
args: []string{"one", "-flag1", "value1", "two"},
|
|
|
|
expErr: true,
|
|
|
|
},
|
2024-11-01 07:31:21 +01:00
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-11-07 07:25:02 +01:00
|
|
|
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)
|
|
|
|
}
|
2024-11-01 07:31:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|