-
Notifications
You must be signed in to change notification settings - Fork 0
/
cases_test.go
284 lines (231 loc) · 5.47 KB
/
cases_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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package cases_test
import (
"fmt"
"regexp"
"strings"
"testing"
"github.com/rossmacarthur/cases"
"github.com/stretchr/testify/require"
)
type testCase struct {
in string
snakeCase string
camelCase string
}
var tests []testCase = []testCase{
{},
{in: "Test",
snakeCase: "test",
camelCase: "test",
},
{in: "test case",
snakeCase: "test_case",
camelCase: "testCase",
},
{in: " test case",
snakeCase: "test_case",
camelCase: "testCase",
},
{in: "test case ",
snakeCase: "test_case",
camelCase: "testCase",
},
{in: "Test Case",
snakeCase: "test_case",
camelCase: "testCase",
},
{in: " Test Case",
snakeCase: "test_case",
camelCase: "testCase",
},
{in: "camelCase",
snakeCase: "camel_case",
camelCase: "camelCase",
},
{in: "PascalCase",
snakeCase: "pascal_case",
camelCase: "pascalCase",
},
{in: "snake_case",
snakeCase: "snake_case",
camelCase: "snakeCase",
},
{in: "SCREAMING_SNAKE_CASE",
snakeCase: "screaming_snake_case",
camelCase: "screamingSnakeCase",
},
{in: "kebab-case",
snakeCase: "kebab_case",
camelCase: "kebabCase",
},
{in: "SCREAMING-KEBAB-CASE",
snakeCase: "screaming_kebab_case",
camelCase: "screamingKebabCase",
},
{in: "Title Case ",
snakeCase: "title_case",
camelCase: "titleCase",
},
{in: "Train-Case ",
snakeCase: "train_case",
camelCase: "trainCase",
},
{in: "This is a Test case.",
snakeCase: "this_is_a_test_case",
camelCase: "thisIsATestCase",
},
{in: "MixedUP CamelCase, with some Spaces",
snakeCase: "mixed_up_camel_case_with_some_spaces",
camelCase: "mixedUpCamelCaseWithSomeSpaces",
},
{in: "mixed_up_ snake_case with some _spaces",
snakeCase: "mixed_up_snake_case_with_some_spaces",
camelCase: "mixedUpSnakeCaseWithSomeSpaces",
},
{in: "this-contains_ ALLKinds OfWord_Boundaries",
snakeCase: "this_contains_all_kinds_of_word_boundaries",
camelCase: "thisContainsAllKindsOfWordBoundaries",
},
{in: "XΣXΣ baffle",
snakeCase: "xσxσ_baffle",
camelCase: "xσxσBaffle",
},
{in: "XMLHttpRequest",
snakeCase: "xml_http_request",
camelCase: "xmlHttpRequest",
},
{in: "FIELD_NAME11",
snakeCase: "field_name11",
camelCase: "fieldName11",
},
{in: "99BOTTLES",
snakeCase: "99bottles",
camelCase: "99bottles",
},
{in: "FieldNamE11",
snakeCase: "field_nam_e11",
camelCase: "fieldNamE11",
},
{in: "abc123def456",
snakeCase: "abc123def456",
camelCase: "abc123def456",
},
{in: "abc123DEF456",
snakeCase: "abc123_def456",
camelCase: "abc123Def456",
},
{in: "abc123Def456",
snakeCase: "abc123_def456",
camelCase: "abc123Def456",
},
{in: "abc123DEf456",
snakeCase: "abc123_d_ef456",
camelCase: "abc123DEf456",
},
{in: "ABC123def456",
snakeCase: "abc123def456",
camelCase: "abc123def456",
},
{in: "ABC123DEF456",
snakeCase: "abc123def456",
camelCase: "abc123def456",
},
{in: "ABC123Def456",
snakeCase: "abc123_def456",
camelCase: "abc123Def456",
},
{in: "ABC123DEf456",
snakeCase: "abc123d_ef456",
camelCase: "abc123dEf456",
},
{in: "ABC123dEEf456FOO",
snakeCase: "abc123d_e_ef456_foo",
camelCase: "abc123dEEf456Foo",
},
{in: "abcDEF",
snakeCase: "abc_def",
camelCase: "abcDef",
},
{in: "ABcDE",
snakeCase: "a_bc_de",
camelCase: "aBcDe",
},
}
func TestToCamel(t *testing.T) {
for _, tc := range tests {
t.Run(tc.in, func(t *testing.T) {
got := cases.ToCamel(tc.in)
require.Equal(t, tc.camelCase, got, fmt.Sprintf("'%s'", tc.in))
})
}
}
func TestToPascal(t *testing.T) {
result := cases.ToPascal("test case")
require.Equal(t, "TestCase", result)
}
func TestToSnake(t *testing.T) {
for _, tc := range tests {
t.Run(tc.in, func(t *testing.T) {
got := cases.ToSnake(tc.in)
require.Equal(t, tc.snakeCase, got, fmt.Sprintf("'%s'", tc.in))
})
}
}
func TestToScreamingSnake(t *testing.T) {
result := cases.ToScreamingSnake("test case")
require.Equal(t, "TEST_CASE", result)
}
func TestToKebab(t *testing.T) {
result := cases.ToKebab("test case")
require.Equal(t, "test-case", result)
}
func TestToScreamingKebab(t *testing.T) {
result := cases.ToScreamingKebab("test case")
require.Equal(t, "TEST-CASE", result)
}
func TestToTrain(t *testing.T) {
result := cases.ToTrain("test case")
require.Equal(t, "Test-Case", result)
}
func TestToLower(t *testing.T) {
result := cases.ToLower("Test-case")
require.Equal(t, "test case", result)
}
func TestToTitle(t *testing.T) {
result := cases.ToTitle("Test-case")
require.Equal(t, "Test Case", result)
}
func TestToUpper(t *testing.T) {
result := cases.ToUpper("Test-case")
require.Equal(t, "TEST CASE", result)
}
func BenchmarkToSnake(b *testing.B) {
s := strings.Repeat("ThisIsATestCase", 100)
require.True(b, cases.ToSnake(s) == regexToSnake(s))
b.Run("cases", func(b *testing.B) {
for i := 0; i < b.N; i++ {
cases.ToSnake(s)
}
})
b.Run("regex", func(b *testing.B) {
for i := 0; i < b.N; i++ {
regexToSnake(s)
}
})
}
// regexToSnake is a regex implementation to convert to snake case to compare
// the benchmark to.
//
// This function doesn't support as many word boundaries as cases.ToSnake but
// it is still much slower than the cases.ToSnake implementation.
//
// From https://stackoverflow.com/a/56616250/4591251
func regexToSnake(s string) string {
snake := matchFirstCap.ReplaceAllString(s, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
var (
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
)