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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
// Copyright 2015 go-swagger maintainers
//
// 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.
package mangling
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
// parts used to build fixtures
textTitle = "Text"
blankText = " text"
dashText = "-text"
uscoreText = "_text"
sampleTitle = "Sample"
sampleString = "sample"
sampleBlank = "sample "
sampleDash = "sample-"
sampleUscore = "sample_"
)
func TestManglerToGoName(t *testing.T) {
m := NewNameMangler()
t.Run("with simple input", func(t *testing.T) {
samples := []translationSample{
// input, expected
{"@Type", "AtType"},
{"Sample@where", "SampleAtWhere"},
{"Id", "ID"},
{"SomethingTTLSeconds", "SomethingTTLSeconds"},
{"sample text", "SampleText"},
{"IPv6Address", "IPv6Address"}, // changed assertion: favor IPv6 over IPV6
{"IPv4Address", "IPv4Address"}, // changed assertion: favor IPv4 over IPV4
{"sample-text", "SampleText"},
{"sample_text", "SampleText"},
{"sampleText", "SampleText"},
{"sample 2 Text", "Sample2Text"},
{"findThingById", "FindThingByID"},
{"日本語sample 2 Text", "X日本語sample2Text"},
{"日本語findThingById", "X日本語findThingByID"},
{"findTHINGSbyID", "FindTHINGSbyID"},
{"x-isAnOptionalHeader0", "XIsAnOptionalHeader0"},
{"get$ref", "GetDollarRef"},
{"éget$ref", "ÉgetDollarRef"},
{"日get$ref", "X日getDollarRef"},
{"", ""},
{"?", ""},
{"!", "Bang"},
{"", ""},
{"Http Server", "HTTPServer"},
}
t.Run("ToGoName should convert names as expected", func(t *testing.T) {
for _, sample := range samples {
result := m.ToGoName(sample.str)
assert.Equal(t, sample.out, result,
"expected ToGoName(%q) == %q but got %q", sample.str, sample.out, result)
}
})
})
t.Run("with composed with initialism sample", func(t *testing.T) {
for _, k := range m.Initialisms() {
samples := []translationSample{
// input, expected
{sampleBlank + lower(k) + blankText, sampleTitle + k + textTitle},
{sampleDash + lower(k) + dashText, sampleTitle + k + textTitle},
{sampleUscore + lower(k) + uscoreText, sampleTitle + k + textTitle},
{sampleString + titleize(k) + textTitle, sampleTitle + k + textTitle},
{sampleBlank + lower(k), sampleTitle + k},
{sampleDash + lower(k), sampleTitle + k},
{sampleUscore + lower(k), sampleTitle + k},
{sampleString + titleize(k), sampleTitle + k},
{sampleBlank + titleize(k) + blankText, sampleTitle + k + textTitle},
{sampleDash + titleize(k) + dashText, sampleTitle + k + textTitle},
{sampleUscore + titleize(k) + uscoreText, sampleTitle + k + textTitle},
// leading initialism preserving case in initialism, e.g. Ipv4_Address -> IPv4Address and no IPV4Address
{titleize(k) + uscoreText, k + textTitle},
// leading initialism preserving case in initialism, e.g. ipv4_Address -> IPv4Address and no IPV4Address
{lower(k) + uscoreText, k + textTitle},
}
for _, sample := range samples {
result := m.ToGoName(sample.str)
assert.Equal(t, sample.out, result,
"with initialism %q, expected ToGoName(%q) == %q but got %q", k, sample.str, sample.out, result)
}
}
})
t.Run("with prefix rule", func(t *testing.T) {
samples := []translationSample{
{"123_a", "Nr123a"},
{"!123_a", "Bang123a"},
{"+123_a", "Plus123a"},
{"abc", "Abc"},
{"éabc", "Éabc"},
{":éabc", "Éabc"},
{"get$ref", "GetDollarRef"},
{"get!ref", "GetBangRef"},
{"get&ref", "GetAndRef"},
{"get|ref", "GetPipeRef"},
}
t.Run("with GoNamePrefixFunc", func(t *testing.T) {
m := NewNameMangler(
WithGoNamePrefixFunc(func(name string) string {
// this is the pascalize func from go-swagger codegen
arg := []rune(name)
if len(arg) == 0 || arg[0] > '9' {
return ""
}
if arg[0] == '+' {
return "Plus"
}
if arg[0] == '-' {
return "Minus"
}
return "Nr"
}),
)
for _, sample := range samples {
assert.Equal(t, sample.out, m.ToGoName(sample.str))
}
})
t.Run("with GoNamePrefixFuncPtr", func(t *testing.T) {
var fn PrefixFunc = func(name string) string {
arg := []rune(name)
if len(arg) == 0 || arg[0] > '9' {
return ""
}
if arg[0] == '+' {
return "Plus"
}
if arg[0] == '-' {
return "Minus"
}
return "Nr"
}
m := NewNameMangler(
WithGoNamePrefixFuncPtr(&fn),
)
for _, sample := range samples {
assert.Equal(t, sample.out, m.ToGoName(sample.str))
}
})
})
t.Run("with Unicode edge cases", func(t *testing.T) {
m := NewNameMangler()
samples := []translationSample{
{
// single letter rune
"ã",
`Ã`,
},
{
// single non letter rune (ascii)
"3",
`X3`,
},
{
// multi non letter rune (ascii)
"23",
`X23`,
},
{
// single non letter rune (devanagari digit)
"१",
`X१`,
},
{
// single letter, no uppercase rune (devanagari letter)
"आ",
`Xआ`,
},
// TODO: non unicode char
}
for _, sample := range samples {
assert.Equal(t, sample.out, m.ToGoName(sample.str))
}
})
t.Run("with replace table", func(t *testing.T) {
m := NewNameMangler(
WithReplaceFunc(func(r rune) (string, bool) {
switch r {
case '$':
return "Dollar ", true
case '€':
return "Euro ", true
default:
return "", false
}
}),
)
samples := []translationSample{
{"a$ b", "ADollarb"},
{"a€ b", "AEurob"},
}
for _, sample := range samples {
assert.Equal(t, sample.out, m.ToGoName(sample.str))
}
})
}
func TestManglerToFileName(t *testing.T) {
m := NewNameMangler(
WithAdditionalInitialisms("elb", "cap", "capwd", "wd"),
)
samples := []translationSample{
{"SampleText", "sample_text"},
{"FindThingByID", "find_thing_by_id"},
{"FindThingByIDs", "find_thing_by_ids"},
{"CAPWD.folwdBylc", "capwd_folwd_bylc"},
{"CAPWDfolwdBylc", "cap_w_dfolwd_bylc"},
{"CAP_WD_folwdBylc", "cap_wd_folwd_bylc"},
{"TypeOAI_alias", "type_oai_alias"},
{"Type_OAI_alias", "type_oai_alias"},
{"Type_OAIAlias", "type_oai_alias"},
{"ELB.HTTPLoadBalancer", "elb_http_load_balancer"},
{"elbHTTPLoadBalancer", "elb_http_load_balancer"},
{"ELBHTTPLoadBalancer", "elb_http_load_balancer"},
{"get$Ref", "get_dollar_ref"},
}
for _, k := range m.Initialisms() {
samples = append(samples,
translationSample{sampleTitle + k + textTitle, sampleUscore + lower(k) + uscoreText},
)
}
for _, sample := range samples {
result := m.ToFileName(sample.str)
assert.Equal(t, sample.out, m.ToFileName(sample.str),
"ToFileName(%q) == %q but got %q", sample.str, sample.out, result)
}
}
func TestManglerToCommandName(t *testing.T) {
m := NewNameMangler(
WithAdditionalInitialisms("elb", "cap", "capwd", "wd"),
)
samples := []translationSample{
{"SampleText", "sample-text"},
{"FindThingByID", "find-thing-by-id"},
{"elbHTTPLoadBalancer", "elb-http-load-balancer"},
{"get$ref", "get-dollar-ref"},
{"get!ref", "get-bang-ref"},
}
for _, k := range m.Initialisms() {
samples = append(samples,
translationSample{sampleTitle + k + textTitle, sampleDash + lower(k) + dashText},
)
}
for _, sample := range samples {
assert.Equal(t, sample.out, m.ToCommandName(sample.str))
}
}
func TestManglerToHumanName(t *testing.T) {
m := NewNameMangler(
WithAdditionalInitialisms("elb", "cap", "capwd", "wd"),
)
samples := []translationSample{
{"Id", "Id"},
{"IDs", "IDs"},
{"SampleText", "sample text"},
{"FindThingByID", "find thing by ID"},
{"elbHTTPLoadBalancer", "elb HTTP load balancer"},
}
for _, k := range m.Initialisms() {
samples = append(samples,
translationSample{sampleTitle + k + textTitle, sampleBlank + k + blankText},
)
}
for _, sample := range samples {
assert.Equal(t, sample.out, m.ToHumanNameLower(sample.str))
}
}
func TestManglerToJSONName(t *testing.T) {
m := NewNameMangler(
WithAdditionalInitialisms("elb", "cap", "capwd", "wd"),
)
samples := []translationSample{
{"SampleText", "sampleText"},
{"FindThingByID", "findThingById"},
{"elbHTTPLoadBalancer", "elbHttpLoadBalancer"},
{"get$ref", "getDollarRef"},
{"get!ref", "getBangRef"},
}
for _, k := range m.Initialisms() {
samples = append(samples,
translationSample{sampleTitle + k + textTitle, sampleString + titleize(k) + textTitle},
)
}
for _, sample := range samples {
assert.Equal(t, sample.out, m.ToJSONName(sample.str))
}
}
func TestManglerCamelize(t *testing.T) {
m := NewNameMangler(
WithAdditionalInitialisms("elb", "cap", "capwd", "wd"),
)
t.Run("with empty input", func(t *testing.T) {
assert.Empty(t, m.Camelize(""))
})
t.Run("with single byte", func(t *testing.T) {
assert.Equal(t, "A", m.Camelize("a"))
})
t.Run("with single multi-byte rune", func(t *testing.T) {
assert.Equal(t, "Ã", m.Camelize("ã"))
})
samples := []translationSample{
{"SampleText", "Sampletext"},
{"FindThingByID", "Findthingbyid"},
{"CAPWD.folwdBylc", "Capwd.folwdbylc"},
{"CAPWDfolwdBylc", "Capwdfolwdbylc"},
{"CAP_WD_folwdBylc", "Cap_wd_folwdbylc"},
{"TypeOAI_alias", "Typeoai_alias"},
{"Type_OAI_alias", "Type_oai_alias"},
{"Type_OAIAlias", "Type_oaialias"},
{"ELB.HTTPLoadBalancer", "Elb.httploadbalancer"},
{"elbHTTPLoadBalancer", "Elbhttploadbalancer"},
{"ELBHTTPLoadBalancer", "Elbhttploadbalancer"},
{"12ab", "12ab"},
{"get$Ref", "Get$ref"},
{"get!Ref", "Get!ref"},
}
for _, sample := range samples {
res := m.Camelize(sample.str)
assert.Equalf(t, sample.out, res, "expected Camelize(%q)=%q, got %q", sample.str, sample.out, res)
}
}
func TestManglerToHumanNameTitle(t *testing.T) {
m := NewNameMangler(
WithAdditionalInitialisms("elb", "cap", "capwd", "wd"),
)
samples := []translationSample{
{"SampleText", "Sample Text"},
{"FindThingByID", "Find Thing By ID"},
{"CAPWD.folwdBylc", "CAPWD Folwd Bylc"},
{"CAPWDfolwdBylc", "CAP W Dfolwd Bylc"},
{"CAP_WD_folwdBylc", "CAP WD Folwd Bylc"},
{"TypeOAI_alias", "Type OAI Alias"},
{"Type_OAI_alias", "Type OAI Alias"},
{"Type_OAIAlias", "Type OAI Alias"},
{"ELB.HTTPLoadBalancer", "ELB HTTP Load Balancer"},
{"elbHTTPLoadBalancer", "elb HTTP Load Balancer"},
{"ELBHTTPLoadBalancer", "ELB HTTP Load Balancer"},
{"get$ref", "Get Dollar Ref"},
{"get!ref", "Get Bang Ref"},
}
for _, sample := range samples {
res := m.ToHumanNameTitle(sample.str)
assert.Equalf(t, sample.out, res, "expected ToHumanNameTitle(%q)=%q, got %q", sample.str, sample.out, res)
}
}
func TestManglerToVarName(t *testing.T) {
m := NewNameMangler(
WithAdditionalInitialisms("elb", "cap", "capwd", "wd"),
)
samples := []translationSample{
{"SampleText", "sampleText"},
{"FindThingByID", "findThingByID"},
{"CAPWD.folwdBylc", "capwdFolwdBylc"},
{"CAPWDfolwdBylc", "capWDfolwdBylc"}, // first part not detected as initialism (contentious point)
{"CAP_WD_folwdBylc", "capWDFolwdBylc"}, // first part is initialism
{"TypeOAI_alias", "typeOAIAlias"},
{"Type_OAI_alias", "typeOAIAlias"},
{"Type_OAIAlias", "typeOAIAlias"},
{"ELB.HTTPLoadBalancer", "elbHTTPLoadBalancer"}, // first part is initialism
{"elbHTTPLoadBalancer", "elbHTTPLoadBalancer"},
{"ELBHTTPLoadBalancer", "elbHTTPLoadBalancer"},
{"Id", "id"},
{"HTTP", "http"}, // single initialism
{"A", "a"}, // single byte
{"a", "a"}, // single byte, unchanged
{"get$ref", "getDollarRef"},
{"get!ref", "getBangRef"},
{"日get$ref", "x日getDollarRef"}, // prefix rule (no uppercase letter - Japanese rune)
}
for _, sample := range samples {
res := m.ToVarName(sample.str)
assert.Equalf(t, sample.out, res, "expected ToVarName(%q)=%q, got %q", sample.str, sample.out, res)
}
t.Run("with Unicode edge cases", func(t *testing.T) {
m := NewNameMangler()
samples := []translationSample{
{
// single letter rune
`Ã`,
"ã",
},
{
// single non letter rune (ascii)
"3",
`x3`,
},
{
// multi non letter rune (ascii)
"23",
`x23`,
},
{
// single non letter rune (devanagari digit)
"१",
`x१`,
},
{
// single letter, no uppercase rune (devanagari letter)
"आ",
`xआ`,
},
// TODO: non unicode char
}
for _, sample := range samples {
assert.Equal(t, sample.out, m.ToVarName(sample.str))
}
})
}
func TestManglerInitialisms(t *testing.T) {
t.Run("with AddInitialisms", func(t *testing.T) {
m := NewNameMangler()
m.AddInitialisms("ELB", "OLTP")
assert.Equal(t, "ELBEndpoint", m.ToGoName("elb_endpoint"))
assert.Equal(t, "HTTPEndpoint", m.ToGoName("http_endpoint"))
assert.Equal(t, "OLTPEndpoint", m.ToGoName("oltp endpoint"))
})
t.Run("with Initialisms", func(t *testing.T) {
m := NewNameMangler(
WithInitialisms("ELB", "OLTP"),
)
assert.Equal(t, "ELBEndpoint", m.ToGoName("elb_endpoint"))
assert.Equal(t, "HttpEndpoint", m.ToGoName("http_endpoint")) // no recognized as initialisms (default override)
assert.Equal(t, "OLTPEndpoint", m.ToGoName("oltp endpoint"))
})
}
|