-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathflag_to_value_test.go
More file actions
73 lines (67 loc) · 1.7 KB
/
flag_to_value_test.go
File metadata and controls
73 lines (67 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package flags
import (
"fmt"
"reflect"
"testing"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)
func TestFlagToStringToStringPointer(t *testing.T) {
const flagName = "labels"
tests := []struct {
name string
flagValue *string
want *map[string]string
}{
{
name: "flag unset",
flagValue: nil,
want: nil,
},
{
name: "flag set with single value",
flagValue: utils.Ptr("foo=bar"),
want: &map[string]string{
"foo": "bar",
},
},
{
name: "flag set with multiple values",
flagValue: utils.Ptr("foo=bar,label1=value1,label2=value2"),
want: &map[string]string{
"foo": "bar",
"label1": "value1",
"label2": "value2",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := print.NewPrinter()
// create a new, simple test command with a string-to-string flag
cmd := func() *cobra.Command {
cmd := &cobra.Command{
Use: "greet",
Short: "A simple greeting command",
Long: "A simple greeting command",
Run: func(_ *cobra.Command, _ []string) {
fmt.Println("Hello world")
},
}
cmd.Flags().StringToString(flagName, nil, "Labels are key-value string pairs.")
return cmd
}()
// set the flag value if a value use given, else consider the flag unset
if tt.flagValue != nil {
err := cmd.Flags().Set(flagName, *tt.flagValue)
if err != nil {
t.Error(err)
}
}
if got := FlagToStringToStringPointer(p, cmd, flagName); !reflect.DeepEqual(got, tt.want) {
t.Errorf("FlagToStringToStringPointer() = %v, want %v", got, tt.want)
}
})
}
}