-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathparamconv_test.go
More file actions
247 lines (240 loc) · 5.67 KB
/
paramconv_test.go
File metadata and controls
247 lines (240 loc) · 5.67 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2020 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Based on https://github.com/iancoleman/strcase
package paramconv
import (
"reflect"
"testing"
)
func TestMapToCamel(t *testing.T) {
type args struct {
in map[string]interface{}
}
tests := []struct {
name string
args args
want map[string]interface{}
}{
{
name: "should convert the Map to Camel",
args: args{map[string]interface{}{
"var": "value",
"appService": "value",
"app_8sk_": "value",
"_app_8sk_test": "value",
}},
want: map[string]interface{}{
"var": "value",
"appService": "value",
"app8Sk": "value",
"App8SkTest": "value",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MapToCamel(tt.args.in); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapToCamel() = %v, want %v", got, tt.want)
}
})
}
}
func TestMapToSnake(t *testing.T) {
type args struct {
in map[string]interface{}
}
tests := []struct {
name string
args args
want map[string]interface{}
}{
{
name: "should convert the Map to Snake",
args: args{map[string]interface{}{
"var": "value",
"var_var": "value",
"size_k8s_test": "value",
"888": "value",
}},
want: map[string]interface{}{
"var": "value",
"var_var": "value",
"size_k8s_test": "value",
"888": "value",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MapToSnake(tt.args.in); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapToSnake() = %v, want %v", got, tt.want)
}
})
}
}
func TestToCamel(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "should convert to Camel",
args: args{"app_test"},
want: "appTest",
},
{
name: "should convert to Camel when start with _",
args: args{"_app_test"},
want: "AppTest",
},
{
name: "should convert to Camel when has numbers",
args: args{"_app_test_k8s"},
want: "AppTestK8s",
}, {
name: "should convert to Camel when has numbers and _",
args: args{"var_k8s"},
want: "varK8s",
},
{
name: "should handle special words",
args: args{"egressIPs"},
want: "egressIPs",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToCamel(tt.args.s); got != tt.want {
t.Errorf("ToCamel() = %v, want %v", got, tt.want)
}
})
}
}
func TestToSnake(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "should keep the same",
args: args{"var"},
want: "var",
},
{
name: "should convert to Snake when is only numbers",
args: args{"888"},
want: "888",
},
{
name: "should convert to Snake when has numbers and _",
args: args{"k8s_var"},
want: "k8s_var",
},
{
name: "should convert to Snake when start with _",
args: args{"_k8s_var"},
want: "_k8s_var",
},
{
name: "should convert to Snake and replace the space for _",
args: args{"k8s var"},
want: "k8s_var",
},
{
name: "should handle Camel and add _ prefix when starts with",
args: args{"ThisShouldHaveUnderscores"},
want: "_this_should_have_underscores",
},
{
name: "should convert to snake when has Camel and numbers",
args: args{"sizeK8sBuckets"},
want: "size_k8s_buckets",
},
{
name: "should be able to handle mixed vars",
args: args{"_CanYou_Handle_mixedVars"},
want: "_can_you_handle_mixed_vars",
},
{
name: "should be a noop",
args: args{"this_should_be_a_noop"},
want: "this_should_be_a_noop",
},
{
name: "should handle special plural word at end",
args: args{"egressIPs"},
want: "egress_ips",
},
{
name: "should handle special plural word in middle",
args: args{"egressIPsEgress"},
want: "egress_ips_egress",
},
{
name: "should handle special plural word in middle followed by lowercase letter",
args: args{"egressIPsegress"},
want: "egress_ips_egress",
},
{
name: "should handle special plural word at the start",
args: args{"IPsegress"},
want: "_ips_egress",
},
{
name: "should handle special word at the end",
args: args{"egressIP"},
want: "egress_ip",
},
{
name: "should handle special word in the middle",
args: args{"egressIPEgress"},
want: "egress_ip_egress",
},
{
name: "should handle special word in the middle followed by lowercase",
args: args{"egressIPegress"},
want: "egress_ip_egress",
},
{
name: "should handle multiple special words",
args: args{"URLegressIPEgressHTTP"},
want: "url_egress_ip_egress_http",
},
{
name: "should handle multiple plural special words",
args: args{"URLsegressIPsEgressHTTPs"},
want: "_urls_egress_ips_egress_https",
},
{
name: "should not interpret special word when entire word is uppercase",
args: args{"THIS_IS_SKIP_NOT_SK_IP"},
want: "_this_is_skip_not_sk_ip",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToSnake(tt.args.s); got != tt.want {
t.Errorf("ToSnake() = %v, want %v", got, tt.want)
}
})
}
}