-
Notifications
You must be signed in to change notification settings - Fork 16
/
dynamic_var_test.go
107 lines (98 loc) · 2.35 KB
/
dynamic_var_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
96
97
98
99
100
101
102
103
104
105
106
107
package goflags
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDynamicVar(t *testing.T) {
t.Run("with bool as type", func(t *testing.T) {
var b bool
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVar(&b, "kev", false, "kev with or without value"),
)
os.Args = []string{
os.Args[0],
"-kev=false",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, false, b)
tearDown(t.Name())
})
t.Run("without value for int as type", func(t *testing.T) {
var i int
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&i, "concurrency", "c", 25, "concurrency for the process"),
)
os.Args = []string{
os.Args[0],
"-c",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, 25, i)
tearDown(t.Name())
})
t.Run("with value for int as type", func(t *testing.T) {
var i int
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&i, "concurrency", "c", 25, "concurrency for the process"),
)
os.Args = []string{
os.Args[0],
"-c=100",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, 100, i)
tearDown(t.Name())
})
t.Run("with value for float64 as type", func(t *testing.T) {
var f float64
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&f, "percentage", "p", 0.0, "percentage for the process"),
)
os.Args = []string{
os.Args[0],
"-p=100.0",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, 100.0, f)
tearDown(t.Name())
})
t.Run("with value for string as type", func(t *testing.T) {
var s string
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&s, "name", "n", "", "name of the user"),
)
os.Args = []string{
os.Args[0],
"-n=test",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, "test", s)
tearDown(t.Name())
})
t.Run("with value for string slice as type", func(t *testing.T) {
var s []string
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&s, "name", "n", []string{}, "name of the user"),
)
os.Args = []string{
os.Args[0],
"-n=test1,test2",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, []string{"test1", "test2"}, s)
tearDown(t.Name())
})
}