-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_test.go
More file actions
287 lines (275 loc) · 6.36 KB
/
convert_test.go
File metadata and controls
287 lines (275 loc) · 6.36 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
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
285
286
287
package fmt
import (
"testing"
)
func TestConversions(t *testing.T) {
tests := []struct {
name string
input string
want string
function func(*Conv) *Conv
}{
{
name: "Tilde does not remove Ñ/ñ",
input: "Ñandú ñandú",
want: "Ñandu ñandu",
function: (*Conv).Tilde,
},
{
name: "Remove tildes",
input: "áéíóúÁÉÍÓÚ",
want: "aeiouAEIOU",
function: (*Conv).Tilde,
},
{
name: "Remove tildes with mixed Conv",
input: "Hôlà Mündó",
want: "Hola Mundo",
function: (*Conv).Tilde,
},
{
name: "CamelLow",
input: "hello world example",
want: "helloWorldExample",
function: func(t *Conv) *Conv {
return t.CamelLow()
},
},
{
name: "Convert to lower with tildes",
input: "HÓLA MÚNDO",
want: "hola mundo",
function: func(t *Conv) *Conv {
return t.Tilde().ToLower()
},
},
{
name: "Convert to upper with tildes",
input: "hóla múndo",
want: "HOLA MUNDO",
function: func(t *Conv) *Conv {
return t.Tilde().ToUpper()
},
},
{
name: "Special characters",
input: "ñÑàèìòùÀÈÌÒÙ",
want: "ñÑaeiouAEIOU",
function: (*Conv).Tilde,
},
{
name: "Tilde does not remove Ñ/ñ",
input: "Ñandú ñandú",
want: "Ñandu ñandu",
function: (*Conv).Tilde,
},
{
name: "Complete transformation",
input: "Él Múrcielago Rápido",
want: "elMurcielagoRapido",
function: func(t *Conv) *Conv {
return t.Tilde().CamelLow()
},
},
{
name: "Empty string",
input: "",
want: "",
function: func(t *Conv) *Conv {
return t.Tilde().ToLower().ToUpper().CamelLow()
},
},
{
name: "Single character",
input: "A",
want: "a",
function: func(t *Conv) *Conv {
return t.ToLower()
},
},
{
name: "Multiple spaces in camelCase",
input: "hello world example",
want: "helloWorldExample",
function: func(t *Conv) *Conv {
return t.CamelLow()
},
},
{
name: "Non-mappable characters",
input: "Hello! @#$%^ World 123",
want: "hello!@#$%^World123",
function: func(t *Conv) *Conv {
return t.CamelLow()
},
},
{
name: "Mixed transformations",
input: "HÉLLÔ WórLD",
want: "HELLO WORLD",
function: func(t *Conv) *Conv {
return t.Tilde().ToUpper()
},
},
{
name: "CamelCase with accents",
input: "él múrcielago RÁPIDO vuela",
want: "elMurcielagoRapidoVuela",
function: func(t *Conv) *Conv {
return t.Tilde().CamelLow()
},
},
{
name: "CamelLow",
input: "hello world example",
want: "helloWorldExample",
function: func(t *Conv) *Conv {
return t.CamelLow()
},
},
{
name: "CamelUp",
input: "hello world example",
want: "HelloWorldExample",
function: func(t *Conv) *Conv {
return t.CamelUp()
},
},
{
name: "SnakeLow",
input: "hello world example",
want: "hello_world_example",
function: func(t *Conv) *Conv {
return t.SnakeLow()
},
},
{
name: "Mixed case with numbers to CamelLow",
input: "User123Name",
want: "user123name",
function: func(t *Conv) *Conv {
return t.CamelLow()
},
},
{
name: "Mixed case with numbers to CamelUp",
input: "User123Name",
want: "User123Name",
function: func(t *Conv) *Conv {
return t.CamelUp()
},
},
{
name: "Mixed case with numbers to SnakeLow",
input: "User123Name",
want: "user123_name",
function: func(t *Conv) *Conv {
return t.SnakeLow()
},
},
{
name: "Accented Conv to camelCase",
input: "Él Múrcielago Rápido",
want: "elMurcielagoRapido",
function: func(t *Conv) *Conv {
return t.Tilde().CamelLow()
},
},
{
name: "Accented Conv to PascalCase",
input: "Él Múrcielago Rápido",
want: "ElMurcielagoRapido",
function: func(t *Conv) *Conv {
return t.Tilde().CamelUp()
},
},
{
name: "Accented Conv to snake_case",
input: "Él Múrcielago Rápido",
want: "el_murcielago_rapido",
function: func(t *Conv) *Conv {
return t.Tilde().SnakeLow()
},
},
{
name: "Accented Conv to snake-case",
input: "Él Múrcielago Rápido",
want: "el-murcielago-rapido",
function: func(t *Conv) *Conv {
return t.Tilde().SnakeLow("-")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.function(Convert(tt.input)).String()
if got != tt.want {
t.Fatalf("\n🎯Test: %q\ninput: %q\n got: %q\n want: %q", tt.name, tt.input, got, tt.want)
}
})
}
}
func TestConvertToStringOptimized(t *testing.T) {
testCases := []struct {
name string
input any
expected string
}{
{"nil", nil, ""},
{"empty string", "", ""},
{"string", "hello", "hello"},
{"int zero", 0, "0"},
{"int one", 1, "1"},
{"int small", 42, "42"},
{"int negative", -10, "-10"},
{"int large", 12345678, "12345678"},
{"int8", int8(8), "8"},
{"int16", int16(16), "16"},
{"int32", int32(32), "32"},
{"int64", int64(64), "64"},
{"uint", uint(5), "5"},
{"uint8", uint8(8), "8"},
{"uint16", uint16(16), "16"},
{"uint32", uint32(32), "32"},
{"uint64", uint64(64), "64"},
{"bool true", true, "true"},
{"bool false", false, "false"},
{"float32 zero", float32(0), "0"},
{"float32 one", float32(1), "1"},
{"float32", float32(3.14), "3.14"},
{"float64 zero", 0.0, "0"},
{"float64 one", 1.0, "1"},
{"float64", 3.14159, "3.14159"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out := Convert(tc.input).String()
if out != tc.expected {
t.Errorf("Convert(%v).String() = %q, want %q", tc.input, out, tc.expected)
}
})
}
}
// Test to ensure that repeated conversions return the expected value
func TestConvertStringConsistency(t *testing.T) {
// For small integers
s1 := Convert(42).String()
s2 := Convert(42).String()
if s1 != "42" || s2 != "42" {
t.Errorf("Expected Convert(42).String() to return \"42\"")
}
// For boolean values
b1 := Convert(true).String()
b2 := Convert(true).String()
if b1 != "true" || b2 != "true" {
t.Errorf("Expected Convert(true).String() to return \"true\"")
}
// For zero value
z1 := Convert(0).String()
z2 := Convert(0).String()
if z1 != "0" || z2 != "0" {
t.Errorf("Expected Convert(0).String() to return \"0\"")
}
// Prueba adicional para verificar la consistencia
t.Log("Tests for Convert().String() consistency passed")
}