forked from anchore/fangs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
var_test.go
59 lines (46 loc) · 1.28 KB
/
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
package fangs
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)
func Test_Ptr(t *testing.T) {
cmd := &cobra.Command{}
type typ struct {
BoolVal *bool
NotBoolVal *bool
IntVal *int
StringVal *string
FloatVal *float64
}
a := &typ{
NotBoolVal: p(true),
}
flags := cmd.Flags()
BoolPtrVarP(flags, &a.BoolVal, "bool-ptr", "", "bool ptr usage")
BoolPtrVarP(flags, &a.NotBoolVal, "not-bool-ptr", "", "not bool ptr usage")
IntPtrVarP(flags, &a.IntVal, "int-ptr", "", "int ptr usage")
StringPtrVarP(flags, &a.StringVal, "string-ptr", "", "string ptr usage")
Float64PtrVarP(flags, &a.FloatVal, "float-ptr", "", "float ptr usage")
require.Nil(t, a.BoolVal)
require.Nil(t, a.IntVal)
require.Nil(t, a.StringVal)
err := flags.Parse([]string{
"--bool-ptr",
"--not-bool-ptr",
"--int-ptr", "17",
"--string-ptr", "some-string",
"--float-ptr", "64.8",
})
require.NoError(t, err)
require.NotNil(t, a.BoolVal)
require.Equal(t, true, *a.BoolVal)
require.NotNil(t, a.NotBoolVal)
require.Equal(t, false, *a.NotBoolVal)
require.NotNil(t, a.IntVal)
require.Equal(t, 17, *a.IntVal)
require.NotNil(t, a.StringVal)
require.Equal(t, "some-string", *a.StringVal)
require.NotNil(t, a.FloatVal)
require.Equal(t, 64.8, *a.FloatVal)
}