sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/plugins/golint/suggestion/golint_suggestion_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package suggestion 18 19 import ( 20 "go/token" 21 "testing" 22 23 "golang.org/x/lint" 24 ) 25 26 func TestLintNamesUnderscore(t *testing.T) { 27 var testcases = []struct { 28 problem lint.Problem 29 expectedSuggestion string 30 }{ 31 { 32 problem: lint.Problem{ 33 Position: token.Position{ 34 Filename: "qux.go", 35 }, 36 Text: "don't use underscores in Go names; func Qux_1 should be Qux1", 37 Link: "http://golang.org/doc/effective_go.html#mixed-caps", 38 Category: "naming", 39 LineText: "func Qux_1() error {", 40 Confidence: 100.00, 41 }, 42 expectedSuggestion: "```suggestion\nfunc Qux1() error {```\n", 43 }, 44 { 45 problem: lint.Problem{ 46 Position: token.Position{ 47 Filename: "qux.go", 48 }, 49 Text: "don't use underscores in Go names; func Qux_Foo_Func should be QuxFooFunc", 50 Link: "http://golang.org/doc/effective_go.html#mixed-caps", 51 Category: "naming", 52 LineText: "func Qux_Foo_Func() error {", 53 Confidence: 100.00, 54 }, 55 expectedSuggestion: "```suggestion\nfunc QuxFooFunc() error {```\n", 56 }, 57 { 58 problem: lint.Problem{ 59 Position: token.Position{ 60 Filename: "qux.go", 61 }, 62 Text: "don't use underscores in Go names; func Qux_Foo_Func should be QuxFooFunc", 63 Link: "http://golang.org/doc/effective_go.html#mixed-caps", 64 Category: "naming", 65 LineText: "func QuxCorrectFunc() error {", 66 Confidence: 100.00, 67 }, 68 expectedSuggestion: "", 69 }, 70 } 71 for _, test := range testcases { 72 suggestion := SuggestCodeChange(test.problem) 73 if suggestion != test.expectedSuggestion { 74 t.Errorf("Excepted code suggestion %s but got %s for LineText %s", test.expectedSuggestion, suggestion, test.problem.LineText) 75 } 76 } 77 } 78 79 func TestLintNamesAllCaps(t *testing.T) { 80 var testcases = []struct { 81 problem lint.Problem 82 expectedSuggestion string 83 }{ 84 { 85 problem: lint.Problem{ 86 Position: token.Position{ 87 Filename: "qux.go", 88 }, 89 Text: "don't use ALL_CAPS in Go names; use CamelCase", 90 Link: "", 91 Category: "naming", 92 LineText: "func QUX_FUNC() error {", 93 Confidence: 100.00, 94 }, 95 expectedSuggestion: "```suggestion\nfunc QuxFunc() error {```\n", 96 }, 97 { 98 problem: lint.Problem{ 99 Position: token.Position{ 100 Filename: "qux.go", 101 }, 102 Text: "don't use ALL_CAPS in Go names; use CamelCase", 103 Link: "http://golang.org/doc/effective_go.html#mixed-caps", 104 Category: "naming", 105 LineText: "func QUX() error {", 106 Confidence: 100.00, 107 }, 108 expectedSuggestion: "```suggestion\nfunc Qux() error {```\n", 109 }, 110 { 111 problem: lint.Problem{ 112 Position: token.Position{ 113 Filename: "qux.go", 114 }, 115 Text: "don't use ALL_CAPS in Go names; use CamelCase", 116 Link: "http://golang.org/doc/effective_go.html#mixed-caps", 117 Category: "naming", 118 LineText: "func QUX_FOO_FUNC() error {", 119 Confidence: 100.00, 120 }, 121 expectedSuggestion: "```suggestion\nfunc QuxFooFunc() error {```\n", 122 }, 123 { 124 problem: lint.Problem{ 125 Position: token.Position{ 126 Filename: "qux.go", 127 }, 128 Text: "don't use ALL_CAPS in Go names; use CamelCase", 129 Link: "http://golang.org/doc/effective_go.html#mixed-caps", 130 Category: "naming", 131 LineText: "func QUX_FOO_FUNC_1() error {", 132 Confidence: 100.00, 133 }, 134 expectedSuggestion: "```suggestion\nfunc QuxFooFunc1() error {```\n", 135 }, 136 { 137 problem: lint.Problem{ 138 Position: token.Position{ 139 Filename: "qux.go", 140 }, 141 Text: "don't use ALL_CAPS in Go names; use CamelCase", 142 Link: "http://golang.org/doc/effective_go.html#mixed-caps", 143 Category: "naming", 144 LineText: "func QuxCorrectFunc() error {", 145 Confidence: 100.00, 146 }, 147 expectedSuggestion: "", 148 }, 149 { 150 problem: lint.Problem{ 151 Position: token.Position{ 152 Filename: "qux.go", 153 }, 154 Text: "don't use ALL_CAPS in Go names; use CamelCase", 155 Link: "http://golang.org/doc/effective_go.html#mixed-caps", 156 Category: "naming", 157 LineText: "func Qux() error {", 158 Confidence: 100.00, 159 }, 160 expectedSuggestion: "", 161 }, 162 } 163 for _, test := range testcases { 164 suggestion := SuggestCodeChange(test.problem) 165 if suggestion != test.expectedSuggestion { 166 t.Errorf("Excepted code suggestion %s but got %s for LineText %s", test.expectedSuggestion, suggestion, test.problem.LineText) 167 } 168 } 169 } 170 171 func TestLintStutter(t *testing.T) { 172 var testcases = []struct { 173 problem lint.Problem 174 expectedSuggestion string 175 }{ 176 { 177 problem: lint.Problem{ 178 Position: token.Position{ 179 Filename: "qux.go", 180 }, 181 Text: "func name will be used as bar.BarFunc by other packages, and that stutters; consider calling this Func", 182 Link: "https://golang.org/wiki/CodeReviewComments#package-names", 183 Category: "naming", 184 LineText: "func BarFunc() error {", 185 Confidence: 100.00, 186 }, 187 expectedSuggestion: "```suggestion\nfunc Func() error {```\n", 188 }, 189 { 190 problem: lint.Problem{ 191 Position: token.Position{ 192 Filename: "qux.go", 193 }, 194 Text: "type name will be used as bar.BarMaker by other packages, and that stutters; consider calling this Maker", 195 Link: "https://golang.org/wiki/CodeReviewComments#package-names", 196 Category: "naming", 197 LineText: "type BarMaker struct{}", 198 Confidence: 100.00, 199 }, 200 expectedSuggestion: "```suggestion\ntype Maker struct{}```\n", 201 }, 202 { 203 problem: lint.Problem{ 204 Position: token.Position{ 205 Filename: "qux.go", 206 }, 207 Text: "type name will be used as bar.Bar by other packages, and that stutters; consider calling this Bar", 208 Link: "https://golang.org/wiki/CodeReviewComments#package-names", 209 Category: "naming", 210 LineText: "type Bar struct{}", 211 Confidence: 100.00, 212 }, 213 expectedSuggestion: "", 214 }, 215 } 216 for _, test := range testcases { 217 suggestion := SuggestCodeChange(test.problem) 218 if suggestion != test.expectedSuggestion { 219 t.Errorf("Excepted code suggestion %s but got %s for LineText %s", test.expectedSuggestion, suggestion, test.problem.LineText) 220 } 221 } 222 } 223 224 func TestLintErrorf(t *testing.T) { 225 var testcases = []struct { 226 problem lint.Problem 227 expectedSuggestion string 228 }{ 229 { 230 problem: lint.Problem{ 231 Position: token.Position{ 232 Filename: "qux.go", 233 }, 234 Text: "should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)", 235 Link: "", 236 Category: "error", 237 LineText: " return errors.New(fmt.Sprintf(\"something %d\", x))", 238 Confidence: 100.00, 239 }, 240 expectedSuggestion: "```suggestion\n return fmt.Errorf(\"something %d\", x)```\n", 241 }, 242 { 243 problem: lint.Problem{ 244 Position: token.Position{ 245 Filename: "qux.go", 246 }, 247 Text: "should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)", 248 Link: "", 249 Category: "error", 250 LineText: " return errors.New(fmt.Sprintf(\"something %s %d\", fooFunc(foo), bar))", 251 Confidence: 100.00, 252 }, 253 expectedSuggestion: "```suggestion\n return fmt.Errorf(\"something %s %d\", fooFunc(foo), bar)```\n", 254 }, 255 { 256 problem: lint.Problem{ 257 Position: token.Position{ 258 Filename: "qux.go", 259 }, 260 Text: "should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)", 261 Link: "", 262 Category: "error", 263 LineText: " return errors.New(fmt.Sprintf(\"something %s %d\", fooFunc(barFunc(foo), string(x)), bar))", 264 Confidence: 100.00, 265 }, 266 expectedSuggestion: "```suggestion\n return fmt.Errorf(\"something %s %d\", fooFunc(barFunc(foo), string(x)), bar)```\n", 267 }, 268 { 269 problem: lint.Problem{ 270 Position: token.Position{ 271 Filename: "qux.go", 272 }, 273 Text: "should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)", 274 Link: "", 275 Category: "error", 276 LineText: " return fmt.Errorf(\"something %d\", x)", 277 Confidence: 100.00, 278 }, 279 expectedSuggestion: "", 280 }, 281 } 282 for _, test := range testcases { 283 suggestion := SuggestCodeChange(test.problem) 284 if suggestion != test.expectedSuggestion { 285 t.Errorf("Excepted code suggestion %s but got %s for LineText %s", test.expectedSuggestion, suggestion, test.problem.LineText) 286 } 287 } 288 } 289 290 func TestLintLoopRanges(t *testing.T) { 291 var testcases = []struct { 292 problem lint.Problem 293 expectedSuggestion string 294 }{ 295 { 296 problem: lint.Problem{ 297 Position: token.Position{ 298 Filename: "qux.go", 299 }, 300 Text: "should omit values from range; this loop is equivalent to `for range ...`", 301 Link: "", 302 Category: "range-loop", 303 LineText: "for _ = range m {", 304 Confidence: 100.00, 305 }, 306 expectedSuggestion: "```suggestion\nfor range m {```\n", 307 }, 308 { 309 problem: lint.Problem{ 310 Position: token.Position{ 311 Filename: "qux.go", 312 }, 313 Text: "should omit values from range; this loop is equivalent to `for range ...`", 314 Link: "", 315 Category: "range-loop", 316 LineText: "for _, _ = range m {", 317 Confidence: 100.00, 318 }, 319 expectedSuggestion: "```suggestion\nfor range m {```\n", 320 }, 321 { 322 problem: lint.Problem{ 323 Position: token.Position{ 324 Filename: "qux.go", 325 }, 326 Text: "should omit 2nd value from range; this loop is equivalent to `for y = range ...`", 327 Link: "", 328 Category: "range-loop", 329 LineText: "for y, _ = range m {", 330 Confidence: 100.00, 331 }, 332 expectedSuggestion: "```suggestion\nfor y = range m {```\n", 333 }, 334 { 335 problem: lint.Problem{ 336 Position: token.Position{ 337 Filename: "qux.go", 338 }, 339 Text: "should omit 2nd value from range; this loop is equivalent to `for yVar1 = range ...`", 340 Link: "", 341 Category: "range-loop", 342 LineText: "for yVar1, _ = range m {", 343 Confidence: 100.00, 344 }, 345 expectedSuggestion: "```suggestion\nfor yVar1 = range m {```\n", 346 }, 347 { 348 problem: lint.Problem{ 349 Position: token.Position{ 350 Filename: "qux.go", 351 }, 352 Text: "should omit 2nd value from range; this loop is equivalent to `for y := range ...`", 353 Link: "", 354 Category: "range-loop", 355 LineText: "for y, _ := range m {", 356 Confidence: 100.00, 357 }, 358 expectedSuggestion: "```suggestion\nfor y := range m {```\n", 359 }, 360 { 361 problem: lint.Problem{ 362 Position: token.Position{ 363 Filename: "qux.go", 364 }, 365 Text: "should omit values from range; this loop is equivalent to `for range ...`", 366 Link: "", 367 Category: "range-loop", 368 LineText: "for range m {", 369 Confidence: 100.00, 370 }, 371 expectedSuggestion: "", 372 }, 373 { 374 problem: lint.Problem{ 375 Position: token.Position{ 376 Filename: "qux.go", 377 }, 378 Text: "should omit 2nd value from range; this loop is equivalent to `for y = range ...`", 379 Link: "", 380 Category: "range-loop", 381 LineText: "for y = range m {", 382 Confidence: 100.00, 383 }, 384 expectedSuggestion: "", 385 }, 386 } 387 for _, test := range testcases { 388 suggestion := SuggestCodeChange(test.problem) 389 if suggestion != test.expectedSuggestion { 390 t.Errorf("Excepted code suggestion %s but got %s for LineText %s", test.expectedSuggestion, suggestion, test.problem.LineText) 391 } 392 } 393 } 394 395 func TestLintVarDecl(t *testing.T) { 396 var testcases = []struct { 397 problem lint.Problem 398 expectedSuggestion string 399 }{ 400 { 401 problem: lint.Problem{ 402 Position: token.Position{ 403 Filename: "qux.go", 404 }, 405 Text: "should omit type int from declaration of var myInt; it will be inferred from the right-hand side", 406 Link: "", 407 Category: "type-inference", 408 LineText: "var myInt int = 7", 409 Confidence: 100.00, 410 }, 411 expectedSuggestion: "```suggestion\nvar myInt = 7```\n", 412 }, 413 { 414 problem: lint.Problem{ 415 Position: token.Position{ 416 Filename: "qux.go", 417 }, 418 Text: "should drop = 0 from declaration of var myZeroInt; it is the zero value", 419 Link: "", 420 Category: "zero-value", 421 LineText: "var myZeroInt int = 0", 422 Confidence: 100.00, 423 }, 424 expectedSuggestion: "```suggestion\nvar myZeroInt int```\n", 425 }, 426 { 427 problem: lint.Problem{ 428 Position: token.Position{ 429 Filename: "qux.go", 430 }, 431 Text: "should drop = 0. from declaration of var myZeroFlt; it is the zero value", 432 Link: "", 433 Category: "zero-value", 434 LineText: "var myZeroFlt float32 = 0.", 435 Confidence: 100.00, 436 }, 437 expectedSuggestion: "```suggestion\nvar myZeroFlt float32```\n", 438 }, 439 { 440 problem: lint.Problem{ 441 Position: token.Position{ 442 Filename: "qux.go", 443 }, 444 Text: "should drop = 0.0 from declaration of var myZeroF64; it is the zero value", 445 Link: "", 446 Category: "zero-value", 447 LineText: "var myZeroF64 float64 = 0.0", 448 Confidence: 100.00, 449 }, 450 expectedSuggestion: "```suggestion\nvar myZeroF64 float64```\n", 451 }, 452 { 453 problem: lint.Problem{ 454 Position: token.Position{ 455 Filename: "qux.go", 456 }, 457 Text: "should drop = 0i from declaration of var myZeroImg; it is the zero value", 458 Link: "", 459 Category: "zero-value", 460 LineText: "var myZeroImg complex64 = 0i", 461 Confidence: 100.00, 462 }, 463 expectedSuggestion: "```suggestion\nvar myZeroImg complex64```\n", 464 }, 465 { 466 problem: lint.Problem{ 467 Position: token.Position{ 468 Filename: "qux.go", 469 }, 470 Text: "should drop = \"\" from declaration of var myZeroStr; it is the zero value", 471 Link: "", 472 Category: "zero-value", 473 LineText: "var myZeroStr string = \"\"", 474 Confidence: 100.00, 475 }, 476 expectedSuggestion: "```suggestion\nvar myZeroStr string```\n", 477 }, 478 { 479 problem: lint.Problem{ 480 Position: token.Position{ 481 Filename: "qux.go", 482 }, 483 Text: "should drop = `` from declaration of var myZeroStr; it is the zero value", 484 Link: "", 485 Category: "zero-value", 486 LineText: "var myZeroStr string = ``", 487 Confidence: 100.00, 488 }, 489 expectedSuggestion: "```suggestion\nvar myZeroStr string```\n", 490 }, 491 { 492 problem: lint.Problem{ 493 Position: token.Position{ 494 Filename: "qux.go", 495 }, 496 Text: "should drop = nil from declaration of var myZeroPtr; it is the zero value", 497 Link: "", 498 Category: "zero-value", 499 LineText: "var myZeroPtr *Q = nil", 500 Confidence: 100.00, 501 }, 502 expectedSuggestion: "```suggestion\nvar myZeroPtr *Q```\n", 503 }, 504 { 505 problem: lint.Problem{ 506 Position: token.Position{ 507 Filename: "qux.go", 508 }, 509 Text: "should drop = '\\x00' from declaration of var myZeroRune; it is the zero value", 510 Link: "", 511 Category: "zero-value", 512 LineText: "var myZeroRune rune = '\\x00'", 513 Confidence: 100.00, 514 }, 515 expectedSuggestion: "```suggestion\nvar myZeroRune rune```\n", 516 }, 517 { 518 problem: lint.Problem{ 519 Position: token.Position{ 520 Filename: "qux.go", 521 }, 522 Text: "should drop = '\\000' from declaration of var myZeroRune2; it is the zero value", 523 Link: "", 524 Category: "zero-value", 525 LineText: "var myZeroRune2 rune = '\\000'", 526 Confidence: 100.00, 527 }, 528 expectedSuggestion: "```suggestion\nvar myZeroRune2 rune```\n", 529 }, 530 { 531 problem: lint.Problem{ 532 Position: token.Position{ 533 Filename: "qux.go", 534 }, 535 Text: "should drop = `` from declaration of var myZeroStr; it is the zero value", 536 Link: "", 537 Category: "zero-value", 538 LineText: "var myZeroStr string", 539 Confidence: 100.00, 540 }, 541 expectedSuggestion: "", 542 }, 543 { 544 problem: lint.Problem{ 545 Position: token.Position{ 546 Filename: "qux.go", 547 }, 548 Text: "should omit type int from declaration of var myInt; it will be inferred from the right-hand side", 549 Link: "", 550 Category: "type-inference", 551 LineText: "var myInt = 7", 552 Confidence: 100.00, 553 }, 554 expectedSuggestion: "", 555 }, 556 } 557 for _, test := range testcases { 558 suggestion := SuggestCodeChange(test.problem) 559 if suggestion != test.expectedSuggestion { 560 t.Errorf("Excepted code suggestion %s but got %s for LineText %s", test.expectedSuggestion, suggestion, test.problem.LineText) 561 } 562 } 563 }