-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys_test.go
95 lines (82 loc) · 3.23 KB
/
keys_test.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package verdeter
import (
"testing"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
func TestKeyString(t *testing.T) {
var flagSet *pflag.FlagSet = new(pflag.FlagSet)
keyString(flagSet, "port", "p", "port description")
assert.True(t, flagSet.HasFlags())
assert.Equal(t, "port", flagSet.Lookup("port").Name)
assert.Equal(t, "p", flagSet.Lookup("port").Shorthand)
assert.Equal(t, "port description", flagSet.Lookup("port").Usage)
assert.Equal(t, "string", flagSet.Lookup("port").Value.Type())
}
func TestKeyUint(t *testing.T) {
var flagSet *pflag.FlagSet = new(pflag.FlagSet)
keyUint(flagSet, "port", "p", "port description")
assert.True(t, flagSet.HasFlags())
assert.Equal(t, "port", flagSet.Lookup("port").Name)
assert.Equal(t, "p", flagSet.Lookup("port").Shorthand)
assert.Equal(t, "uint", flagSet.Lookup("port").Value.Type())
}
func TestKeyInt(t *testing.T) {
var flagSet *pflag.FlagSet = new(pflag.FlagSet)
keyInt(flagSet, "port", "p", "port description")
assert.True(t, flagSet.HasFlags())
assert.Equal(t, "port", flagSet.Lookup("port").Name)
assert.Equal(t, "p", flagSet.Lookup("port").Shorthand)
assert.Equal(t, "int", flagSet.Lookup("port").Value.Type())
}
func TestKeyBool(t *testing.T) {
var flagSet *pflag.FlagSet = new(pflag.FlagSet)
keyBool(flagSet, "port", "p", "port description")
assert.True(t, flagSet.HasFlags())
assert.Equal(t, "port", flagSet.Lookup("port").Name)
assert.Equal(t, "p", flagSet.Lookup("port").Shorthand)
assert.Equal(t, "bool", flagSet.Lookup("port").Value.Type())
}
func TestKeyGlobalTrue(t *testing.T) {
// testing if the flag end up in the global flags
var cmd *cobra.Command = new(cobra.Command)
Key(cmd, "port", IsStr, "p", "port usage", true)
assert.True(t, cmd.PersistentFlags().HasFlags())
assert.False(t, cmd.Flags().HasFlags())
}
func TestKeyGlobalFalse(t *testing.T) {
// testing if the flag end in the local flags
var cmd *cobra.Command = new(cobra.Command)
Key(cmd, "port", IsStr, "p", "port usage", false)
assert.False(t, cmd.PersistentFlags().HasFlags())
assert.True(t, cmd.Flags().HasFlags())
}
func TestKey(t *testing.T) {
var cmd *cobra.Command = new(cobra.Command)
Key(cmd, "port", IsStr, "p", "port usage", true)
assert.Equal(t, "port", cmd.PersistentFlags().Lookup("port").Name)
assert.Equal(t, "p", cmd.PersistentFlags().Lookup("port").Shorthand)
assert.Equal(t, "port usage", cmd.PersistentFlags().Lookup("port").Usage)
}
func TestKeyIsStr(t *testing.T) {
var cmd *cobra.Command = new(cobra.Command)
Key(cmd, "port", IsStr, "p", "port usage", false)
println(cmd.PersistentFlags())
assert.Equal(t, "string", cmd.Flags().Lookup("port").Value.Type())
}
func TestKeyIsBool(t *testing.T) {
var cmd *cobra.Command = new(cobra.Command)
Key(cmd, "port", IsBool, "p", "port usage", false)
assert.Equal(t, "bool", cmd.Flags().Lookup("port").Value.Type())
}
func TestKeyIsInt(t *testing.T) {
var cmd *cobra.Command = new(cobra.Command)
Key(cmd, "port", IsInt, "p", "port usage", false)
assert.Equal(t, "int", cmd.Flags().Lookup("port").Value.Type())
}
func TestKeyIsUint(t *testing.T) {
var cmd *cobra.Command = new(cobra.Command)
Key(cmd, "port", IsUint, "p", "port usage", false)
assert.Equal(t, "uint", cmd.Flags().Lookup("port").Value.Type())
}