-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
140 lines (132 loc) · 2.87 KB
/
utils_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package gowpcli
import (
"errors"
"net/http"
"reflect"
"sort"
"strings"
"testing"
"time"
)
type BasicHandler struct{}
func (b BasicHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Second * 5)
w.Write([]byte("1"))
w.WriteHeader(200)
}
func Test_formatParam(t *testing.T) {
type args struct {
k string
v string
}
tests := []struct {
name string
args args
want string
}{
{name: "1", args: args{
k: "[--post_types=<post-types>]",
v: "test",
}, want: "--post_types=test"},
{name: "2", args: args{
k: "[--comment_status=<comment_status>]",
v: "test1",
}, want: "--comment_status=test1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := formatNamed(tt.args.k, tt.args.v); got != tt.want {
t.Errorf("formatNamed() = %v, want %v, len %v:%v", got, tt.want, len(got), len(tt.want))
}
})
}
}
func TestRequiredOptional(t *testing.T) {
// check if we handle required / optional
for k, v := range TESTS {
if !opt(k) && !isRequired(k) {
t.Error(v)
}
}
for k, v := range TESTS {
if strings.HasPrefix(k, "--") && !isRequired(k) {
t.Error(v)
}
if strings.HasPrefix(k, "[") && !opt(k) {
t.Error(v)
}
if strings.HasPrefix(k, "--") && !isRequired(k) {
t.Error(v)
}
}
}
func TestMulti(t *testing.T) {
forkv(t, func(k, v string) error {
if strings.Index(k, "...") != -1 && !mult(k) {
return errors.New(k)
}
return nil
})
}
func TestMakeArg(t *testing.T) {
//LogLevel = LevelDebug
type test struct{
args map[string]interface{}
want []string
}
tests := []test{
{
args: map[string]interface{}{
"<cap>": "xa",
"[--network_id=<network-id>]": "haw",
"[--grant]": true,
},
want: []string{"xa", "--network_id=haw", "--grant"},
},
{
// wp core install
args: map[string]interface{}{
"--url=<url>": "http://localhost.com",
"--title=<site-title>": "testTitle",
"--admin_user=<username>": "admin",
"[--admin_password=<password>]": "passwd",
"--admin_email=<email>": "example@e.com",
"[--skip-email]": true,
},
want: []string{
"--url=http://localhost.com",
"--title=testTitle",
"--admin_user=admin",
"--admin_password=passwd",
"--admin_email=example@e.com",
"--skip-email",
},
},
{
// wp plugin install
args: map[string]interface{}{
"<plugin|zip|url>": []string{"1plugin1", "1plugin2"},
"[--version=<version>]": "",
"[--force]": false,
"[--activate]": true,
"[--activate-network]": false,
},
want: []string{
"1plugin1",
"1plugin2",
"--activate",
},
},
}
for _, test := range tests {
var args []string
for k, v := range test.args {
args = MakeArg(args, k, v)
}
sort.Strings(args)
sort.Strings(test.want)
if !reflect.DeepEqual(args, test.want) {
t.Errorf("%s !should be! %s, len: %v:%v", args, test.want, len(args), len(test.want))
}
}
}