github.com/searKing/golang/go@v1.2.117/strings/format_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package strings_test 6 7 import ( 8 "strings" 9 "testing" 10 "unicode" 11 12 strings_ "github.com/searKing/golang/go/strings" 13 unicode_ "github.com/searKing/golang/go/unicode" 14 ) 15 16 type TransformCaseTest struct { 17 input string 18 seps []rune 19 f func(r string) string 20 output string 21 } 22 23 var ( 24 transformCaseTests = []TransformCaseTest{ 25 { 26 "name____+++2", 27 []rune{'_', '+'}, 28 strings.ToUpper, 29 "NAME2", 30 }, 31 { 32 "_my__field__Name2y_2age.gender", 33 []rune{'_', '.'}, 34 strings.ToUpper, 35 "MYFIELDNAME2Y2AGEGENDER", 36 }, 37 { 38 "one__two_+_+three.four__", 39 []rune{'_', '.', '+'}, 40 strings.ToUpper, 41 "ONETWOTHREEFOUR", 42 }, 43 { 44 "ONE__two_+_+three.four__", 45 []rune{'_', '.', '+'}, 46 strings.ToLower, 47 "onetwothreefour", 48 }, 49 } 50 ) 51 52 func TestTransformCase(t *testing.T) { 53 for n, test := range transformCaseTests { 54 out := strings_.TransformCase(test.input, test.f, test.seps...) 55 if strings.Compare(out, test.output) != 0 { 56 t.Errorf("#%d: src %v; sep %s; got %v; expected %v", n, test.input, string(test.seps), out, test.output) 57 } 58 } 59 } 60 61 type CamelCaseTest struct { 62 input string 63 seps []rune 64 output string 65 } 66 67 var ( 68 upperCamelCaseTests = []CamelCaseTest{ 69 { 70 "name____+++2", 71 []rune{'_', '+'}, 72 "Name2", 73 }, 74 { 75 "_my__field__Name2y_2age.gender", 76 []rune{'_', '.'}, 77 "XMyFieldName2y2ageGender", 78 }, 79 { 80 "one__two_+_+three.four__", 81 []rune{'_', '.', '+'}, 82 "OneTwoThreeFour", 83 }, 84 } 85 ) 86 87 func TestUpperCamelCases(t *testing.T) { 88 for n, test := range upperCamelCaseTests { 89 out := strings_.UpperCamelCase(test.input, test.seps...) 90 if strings.Compare(out, test.output) != 0 { 91 t.Errorf("#%d: src %v; sep %s; got %v; expected %v", n, test.input, string(test.seps), out, test.output) 92 } 93 } 94 } 95 96 type CamelCaseSliceTest struct { 97 input []string 98 output string 99 } 100 101 var ( 102 upperCamelCaseSliceTests = []CamelCaseSliceTest{ 103 { 104 []string{"name", "2"}, 105 "Name2", 106 }, 107 { 108 []string{"", "my", "field", "name", "2"}, 109 "XMyFieldName2", 110 }, 111 } 112 ) 113 114 func TestUpperCamelCaseSlices(t *testing.T) { 115 for n, test := range upperCamelCaseSliceTests { 116 out := strings_.UpperCamelCaseSlice(test.input...) 117 if strings.Compare(out, test.output) != 0 { 118 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 119 } 120 } 121 } 122 123 var ( 124 lowerCamelCaseTests = []CamelCaseTest{ 125 { 126 "name_2", 127 []rune{'_'}, 128 "name2", 129 }, 130 { 131 "_my_field_name_2", 132 []rune{'_'}, 133 "xMyFieldName2", 134 }, 135 } 136 ) 137 138 func TestLowerCamelCases(t *testing.T) { 139 for n, test := range lowerCamelCaseTests { 140 out := strings_.LowerCamelCase(test.input, test.seps...) 141 if strings.Compare(out, test.output) != 0 { 142 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 143 } 144 } 145 } 146 147 var ( 148 snakeCamelCaseTests = []CamelCaseTest{ 149 { 150 "name_2", 151 nil, 152 "name_2", 153 }, 154 { 155 "_my_field_name_2", 156 []rune{'_'}, 157 "x_my_field_name_2", 158 }, 159 } 160 ) 161 162 func TestSnakeCamelCases(t *testing.T) { 163 for n, test := range snakeCamelCaseTests { 164 out := strings_.SnakeCase(test.input, test.seps...) 165 if strings.Compare(out, test.output) != 0 { 166 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 167 } 168 } 169 } 170 171 var ( 172 darwinCamelCaseTests = []CamelCaseTest{ 173 { 174 "name_2", 175 []rune{'_'}, 176 "Name_2", 177 }, 178 { 179 "_my_field_name_2", 180 []rune{'_'}, 181 "X_My_Field_Name_2", 182 }, 183 } 184 ) 185 186 func TestDarwinCamelCases(t *testing.T) { 187 for n, test := range darwinCamelCaseTests { 188 out := strings_.DarwinCase(test.input, test.seps...) 189 if strings.Compare(out, test.output) != 0 { 190 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 191 } 192 } 193 } 194 195 var ( 196 kebabCamelCaseTests = []CamelCaseTest{ 197 { 198 "name_2", 199 []rune{'_'}, 200 "name-2", 201 }, 202 { 203 "_my_field_name_2", 204 []rune{'_'}, 205 "x-my-field-name-2", 206 }, 207 } 208 ) 209 210 func TestKebabCamelCases(t *testing.T) { 211 for n, test := range kebabCamelCaseTests { 212 out := strings_.KebabCase(test.input, test.seps...) 213 if strings.Compare(out, test.output) != 0 { 214 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 215 } 216 } 217 } 218 219 var ( 220 dotCamelCaseTests = []CamelCaseTest{ 221 { 222 "name_2", 223 []rune{'_'}, 224 "name.2", 225 }, 226 { 227 "_my_field_name_2", 228 []rune{'_'}, 229 "x.my.field.name.2", 230 }, 231 } 232 ) 233 234 type studlyCapsCaseTest struct { 235 input string 236 upperCase unicode.SpecialCase 237 output string 238 } 239 240 var ( 241 studlyCapsCaseTests = []studlyCapsCaseTest{ 242 { 243 "abcdefghijklmnopqrstuvwxyz", 244 unicode_.VowelCase(nil, func(r rune) rune { 245 return unicode.ToUpper(r) 246 }, nil), 247 "AbcdEfghIjklmnOpqrstUvwxyz", 248 }, 249 { 250 "abcdefghijklmnopqrstuvwxyz", 251 unicode_.ConsonantCase(nil, func(r rune) rune { 252 return unicode.ToUpper(r) 253 }, nil), 254 "aBCDeFGHiJKLMNoPQRSTuVWXYZ", 255 }, 256 { 257 "the quick brown fox jumps over the lazy dog", 258 unicode_.VowelCase(nil, func(r rune) rune { 259 return unicode.ToUpper(r) 260 }, nil), 261 "thE qUIck brOwn fOx jUmps OvEr thE lAzy dOg", 262 }, 263 { 264 "the quick brown fox jumps over the lazy dog", 265 unicode_.ConsonantCase(nil, func(r rune) rune { 266 return unicode.ToUpper(r) 267 }, nil), 268 "THe QuiCK BRoWN FoX JuMPS oVeR THe LaZY DoG", 269 }, 270 { 271 "name_2", 272 unicode_.VowelCase(nil, func(r rune) rune { 273 return unicode.ToUpper(r) 274 }, nil), 275 "nAmE_2", 276 }, 277 { 278 "_i_love_you_2", 279 unicode_.VowelCase(nil, func(r rune) rune { 280 return unicode.ToUpper(r) 281 }, nil), 282 "_I_lOvE_yOU_2", 283 }, 284 } 285 ) 286 287 func TestStudlyCapsCases(t *testing.T) { 288 for n, test := range studlyCapsCaseTests { 289 out := strings_.StudlyCapsCase(test.upperCase, test.input) 290 if strings.Compare(out, test.output) != 0 { 291 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 292 } 293 } 294 } 295 296 type studlyCapsVowelUpperCaseTest struct { 297 input string 298 output string 299 } 300 301 var ( 302 studlyCapsVowelUpperCaseTests = []studlyCapsVowelUpperCaseTest{ 303 { 304 "abcdefghijklmnopqrstuvwxyz", 305 "AbcdEfghIjklmnOpqrstUvwxyz", 306 }, 307 { 308 "the quick brown fox jumps over the lazy dog", 309 "thE qUIck brOwn fOx jUmps OvEr thE lAzy dOg", 310 }, 311 { 312 "name_2", 313 "nAmE_2", 314 }, 315 { 316 "_i_love_you_2", 317 "_I_lOvE_yOU_2", 318 }, 319 } 320 ) 321 322 func TestStudlyCapsVowelUpperCase(t *testing.T) { 323 for n, test := range studlyCapsVowelUpperCaseTests { 324 out := strings_.StudlyCapsVowelUpperCase(test.input) 325 if strings.Compare(out, test.output) != 0 { 326 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 327 } 328 } 329 } 330 331 var ( 332 studlyCapsConsonantUpperCaseTests = []studlyCapsVowelUpperCaseTest{ 333 { 334 "abcdefghijklmnopqrstuvwxyz", 335 "aBCDeFGHiJKLMNoPQRSTuVWXYZ", 336 }, 337 { 338 "the quick brown fox jumps over the lazy dog", 339 "THe QuiCK BRoWN FoX JuMPS oVeR THe LaZY DoG", 340 }, 341 { 342 "name_2", 343 "NaMe_2", 344 }, 345 { 346 "_i_love_you_2", 347 "_i_LoVe_You_2", 348 }, 349 } 350 ) 351 352 func TestStudlyCapsConsonantUpperCase(t *testing.T) { 353 for n, test := range studlyCapsConsonantUpperCaseTests { 354 out := strings_.StudlyCapsConsonantUpperCase(test.input) 355 if strings.Compare(out, test.output) != 0 { 356 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 357 } 358 } 359 } 360 361 func TestDotCamelCases(t *testing.T) { 362 for n, test := range dotCamelCaseTests { 363 out := strings_.DotCase(test.input, test.seps...) 364 if strings.Compare(out, test.output) != 0 { 365 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 366 } 367 } 368 } 369 370 var ( 371 smallCamelCaseSliceTests = []CamelCaseSliceTest{ 372 { 373 []string{"name", "2"}, 374 "name2", 375 }, 376 { 377 []string{"", "my", "field", "name", "2"}, 378 "xMyFieldName2", 379 }, 380 } 381 ) 382 383 func TestSmallCamelCaseSlices(t *testing.T) { 384 for n, test := range smallCamelCaseSliceTests { 385 out := strings_.LowerCamelCaseSlice(test.input...) 386 if strings.Compare(out, test.output) != 0 { 387 t.Errorf("#%d: got %v; expected %v", n, out, test.output) 388 } 389 } 390 }