Skip to content

Commit f21dfa7

Browse files
authored
Merge pull request #19 from vchepeli/t_clientBuild
Add unit test for clientBuild.go
2 parents 43e4489 + 7008cf8 commit f21dfa7

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

pkg/cmd/clientBuild_test.go

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package cmd
2+
3+
import (
4+
"reflect"
5+
"runtime"
6+
"strings"
7+
"testing"
8+
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func funcName(v reflect.Value, stripFm bool) (pkg, name string) {
13+
var pkgRes, nameRes string
14+
if v.Kind() == reflect.Func { // check if function is passed
15+
name = runtime.FuncForPC(v.Pointer()).Name()
16+
if i := strings.LastIndex(name, "."); i != -1 {
17+
pkgRes, nameRes = name[:i], name[i+1:]
18+
}
19+
}
20+
21+
if stripFm { // remove -fm suffix if true
22+
fmPos := strings.LastIndex(nameRes, "-fm")
23+
nameRes = nameRes[:fmPos]
24+
}
25+
26+
return pkgRes, nameRes
27+
}
28+
29+
func TestNewClientBuildsCmd(t *testing.T) {
30+
tests := []struct {
31+
name string
32+
want *ClientBuildsCmd
33+
}{
34+
{
35+
name: "test new client builds command",
36+
want: &ClientBuildsCmd{},
37+
},
38+
}
39+
for _, tc := range tests {
40+
t.Run(tc.name, func(t *testing.T) {
41+
if got := NewClientBuildsCmd(); !reflect.DeepEqual(got, tc.want) {
42+
t.Errorf("NewClientBuildsCmd() = %v, want %v", got, tc.want)
43+
}
44+
})
45+
}
46+
}
47+
48+
func TestClientBuildsCmd_GetClientBuildsCmd(t *testing.T) {
49+
tests := []struct {
50+
name string
51+
cbcClosureFunc func(cbc *ClientBuildsCmd) func() *cobra.Command
52+
want *cobra.Command
53+
}{
54+
{
55+
name: "test get client builds command",
56+
cbcClosureFunc: func(cbc *ClientBuildsCmd) func() *cobra.Command {
57+
return cbc.GetClientBuildsCmd
58+
},
59+
want: &cobra.Command{
60+
Use: "clientbuilds",
61+
Short: "get clientbuilds for a mobile client",
62+
RunE: func(cmd *cobra.Command, args []string) error {
63+
return nil
64+
},
65+
},
66+
},
67+
{
68+
name: "test list client builds command",
69+
cbcClosureFunc: func(cbc *ClientBuildsCmd) func() *cobra.Command {
70+
return cbc.ListClientBuildsCmd
71+
},
72+
want: &cobra.Command{
73+
Use: "clientbuild",
74+
Short: "get a specific clientbuild for a mobile client",
75+
RunE: func(cmd *cobra.Command, args []string) error {
76+
return nil
77+
},
78+
},
79+
},
80+
{
81+
name: "test create client builds command",
82+
cbcClosureFunc: func(cbc *ClientBuildsCmd) func() *cobra.Command {
83+
return cbc.CreateClientBuildsCmd
84+
},
85+
want: &cobra.Command{
86+
Use: "clientbuild",
87+
Short: "create a build for a mobile client",
88+
RunE: func(cmd *cobra.Command, args []string) error {
89+
return nil
90+
},
91+
},
92+
},
93+
{
94+
name: "test delete client builds command",
95+
cbcClosureFunc: func(cbc *ClientBuildsCmd) func() *cobra.Command {
96+
return cbc.DeleteClientBuildsCmd
97+
},
98+
want: &cobra.Command{
99+
Use: "clientbuild",
100+
Short: "delete a build for a mobile client",
101+
RunE: func(cmd *cobra.Command, args []string) error {
102+
return nil
103+
},
104+
},
105+
},
106+
{
107+
name: "test stop client builds command",
108+
cbcClosureFunc: func(cbc *ClientBuildsCmd) func() *cobra.Command {
109+
return cbc.StopClientBuildsCmd
110+
},
111+
want: &cobra.Command{
112+
Use: "clientbuild",
113+
Short: "stop a build for a mobile client",
114+
RunE: func(cmd *cobra.Command, args []string) error {
115+
return nil
116+
},
117+
},
118+
},
119+
{
120+
name: "test start client builds command",
121+
cbcClosureFunc: func(cbc *ClientBuildsCmd) func() *cobra.Command {
122+
return cbc.StartClientBuildsCmd
123+
},
124+
want: &cobra.Command{
125+
Use: "clientbuild",
126+
Short: "start a build for a mobile client",
127+
RunE: func(cmd *cobra.Command, args []string) error {
128+
return nil
129+
},
130+
},
131+
},
132+
}
133+
for _, tc := range tests {
134+
t.Run(tc.name, func(t *testing.T) {
135+
cbc := &ClientBuildsCmd{}
136+
gotFunc := tc.cbcClosureFunc(cbc)
137+
_, gotFuncName := funcName(reflect.ValueOf(gotFunc), true)
138+
139+
got := gotFunc()
140+
if use := got.Use; !reflect.DeepEqual(use, tc.want.Use) {
141+
t.Errorf("ClientBuildsCmd.%v().Use = %v, want %v", gotFuncName, use, tc.want.Use)
142+
}
143+
if short := got.Short; !reflect.DeepEqual(short, tc.want.Short) {
144+
t.Errorf("ClientBuildsCmd.%v().Short = %v, want %v", gotFuncName, short, tc.want.Short)
145+
}
146+
if runE := got.RunE; !reflect.DeepEqual(reflect.TypeOf(runE), reflect.TypeOf(tc.want.RunE)) {
147+
t.Errorf("ClientBuildsCmd.%v().RunE = %v, want %v", gotFuncName, reflect.TypeOf(runE), reflect.TypeOf(tc.want.RunE))
148+
}
149+
if runE := got.RunE; !reflect.DeepEqual(runE(nil, nil), tc.want.RunE(nil, nil)) {
150+
t.Errorf("ClientBuildsCmd.%v().RunE = %v, want %v", gotFuncName, runE(nil, nil), tc.want.RunE(nil, nil))
151+
}
152+
})
153+
}
154+
}

0 commit comments

Comments
 (0)