github.com/daixiang0/gci@v0.13.0/pkg/gci/testdata.go (about) 1 package gci 2 3 type Cases struct { 4 name, config, in, out string 5 } 6 7 var commonConfig = `sections: 8 - Standard 9 - Default 10 - Prefix(github.com/daixiang0) 11 ` 12 13 var testCases = []Cases{ 14 { 15 "already-good", 16 17 commonConfig, 18 19 `package main 20 21 import ( 22 "fmt" 23 24 g "github.com/golang" 25 26 "github.com/daixiang0/gci" 27 ) 28 `, 29 `package main 30 31 import ( 32 "fmt" 33 34 g "github.com/golang" 35 36 "github.com/daixiang0/gci" 37 ) 38 `, 39 }, 40 { 41 "blank-format", 42 43 commonConfig, 44 45 `package main 46 import ( 47 "fmt" 48 49 // comment 50 g "github.com/golang" // comment 51 52 "github.com/daixiang0/gci" 53 ) 54 `, 55 `package main 56 57 import ( 58 "fmt" 59 60 // comment 61 g "github.com/golang" // comment 62 63 "github.com/daixiang0/gci" 64 ) 65 `, 66 }, 67 { 68 "cgo-block", 69 70 commonConfig, 71 72 `package main 73 74 import ( 75 /* 76 #include "types.h" 77 */ 78 "C" 79 ) 80 `, 81 `package main 82 83 import ( 84 /* 85 #include "types.h" 86 */ 87 "C" 88 ) 89 `, 90 }, 91 { 92 "cgo-block-after-import", 93 94 commonConfig, 95 96 `package main 97 98 import ( 99 "fmt" 100 101 "github.com/daixiang0/gci" 102 g "github.com/golang" 103 ) 104 105 // #cgo CFLAGS: -DPNG_DEBUG=1 106 // #cgo amd64 386 CFLAGS: -DX86=1 107 // #cgo LDFLAGS: -lpng 108 // #include <png.h> 109 import "C" 110 `, 111 `package main 112 113 // #cgo CFLAGS: -DPNG_DEBUG=1 114 // #cgo amd64 386 CFLAGS: -DX86=1 115 // #cgo LDFLAGS: -lpng 116 // #include <png.h> 117 import "C" 118 119 import ( 120 "fmt" 121 122 g "github.com/golang" 123 124 "github.com/daixiang0/gci" 125 ) 126 `, 127 }, 128 { 129 "cgo-block-before-import", 130 131 commonConfig, 132 133 `package main 134 135 // #cgo CFLAGS: -DPNG_DEBUG=1 136 // #cgo amd64 386 CFLAGS: -DX86=1 137 // #cgo LDFLAGS: -lpng 138 // #include <png.h> 139 import "C" 140 141 import ( 142 "fmt" 143 144 "github.com/daixiang0/gci" 145 146 g "github.com/golang" 147 ) 148 `, 149 `package main 150 151 // #cgo CFLAGS: -DPNG_DEBUG=1 152 // #cgo amd64 386 CFLAGS: -DX86=1 153 // #cgo LDFLAGS: -lpng 154 // #include <png.h> 155 import "C" 156 157 import ( 158 "fmt" 159 160 g "github.com/golang" 161 162 "github.com/daixiang0/gci" 163 ) 164 `, 165 }, 166 { 167 "cgo-block-mixed", 168 169 commonConfig, 170 171 `package main 172 173 import ( 174 /* #include "types.h" 175 */"C" 176 ) 177 `, 178 `package main 179 180 import ( 181 /* #include "types.h" 182 */"C" 183 ) 184 `, 185 }, 186 { 187 "cgo-block-mixed-with-content", 188 189 commonConfig, 190 191 `package main 192 193 import ( 194 /* #include "types.h" 195 #include "other.h" */"C" 196 ) 197 `, 198 `package main 199 200 import ( 201 /* #include "types.h" 202 #include "other.h" */"C" 203 ) 204 `, 205 }, 206 { 207 "cgo-block-prefix", 208 209 commonConfig, 210 211 `package main 212 213 import ( 214 /* #include "types.h" */ "C" 215 ) 216 `, 217 `package main 218 219 import ( 220 /* #include "types.h" */ "C" 221 ) 222 `, 223 }, 224 { 225 "cgo-block-single-line", 226 227 commonConfig, 228 229 `package main 230 231 import ( 232 /* #include "types.h" */ 233 "C" 234 ) 235 `, 236 `package main 237 238 import ( 239 /* #include "types.h" */ 240 "C" 241 ) 242 `, 243 }, 244 { 245 "cgo-line", 246 247 commonConfig, 248 249 `package main 250 251 import ( 252 // #include "types.h" 253 "C" 254 ) 255 `, 256 `package main 257 258 import ( 259 // #include "types.h" 260 "C" 261 ) 262 `, 263 }, 264 { 265 "cgo-multiline", 266 267 commonConfig, 268 269 `package main 270 271 import ( 272 // #include "types.h" 273 // #include "other.h" 274 "C" 275 ) 276 `, 277 `package main 278 279 import ( 280 // #include "types.h" 281 // #include "other.h" 282 "C" 283 ) 284 `, 285 }, 286 { 287 "cgo-single", 288 289 commonConfig, 290 291 `package main 292 293 import ( 294 "fmt" 295 296 "github.com/daixiang0/gci" 297 ) 298 299 import "C" 300 301 import "github.com/golang" 302 303 import ( 304 "github.com/daixiang0/gci" 305 ) 306 `, 307 `package main 308 309 import "C" 310 311 import ( 312 "fmt" 313 314 "github.com/golang" 315 316 "github.com/daixiang0/gci" 317 ) 318 `, 319 }, 320 { 321 "comment", 322 323 commonConfig, 324 325 `package main 326 import ( 327 //Do not forget to run Gci 328 "fmt" 329 ) 330 `, 331 `package main 332 import ( 333 //Do not forget to run Gci 334 "fmt" 335 ) 336 `, 337 }, 338 { 339 "comment-before-import", 340 341 commonConfig, 342 343 `package main 344 345 // comment 346 import ( 347 "fmt" 348 "os" 349 350 "github.com/daixiang0/gci" 351 ) 352 `, 353 `package main 354 355 // comment 356 import ( 357 "fmt" 358 "os" 359 360 "github.com/daixiang0/gci" 361 ) 362 `, 363 }, 364 { 365 "comment-in-the-tail", 366 367 `sections: 368 - Standard 369 - Default 370 - Prefix(github.com/daixiang0) 371 `, 372 `package main 373 374 import ( 375 "fmt" 376 377 g "github.com/golang" 378 379 "github.com/daixiang0/gci" 380 ) 381 382 type test int 383 384 // test 385 `, 386 `package main 387 388 import ( 389 "fmt" 390 391 g "github.com/golang" 392 393 "github.com/daixiang0/gci" 394 ) 395 396 type test int 397 398 // test 399 `, 400 }, 401 { 402 "comment-top", 403 404 commonConfig, 405 406 `package main 407 408 import ( 409 "os" // https://pkg.go.dev/os 410 // https://pkg.go.dev/fmt 411 "fmt" 412 ) 413 `, 414 `package main 415 416 import ( 417 // https://pkg.go.dev/fmt 418 "fmt" 419 "os" // https://pkg.go.dev/os 420 ) 421 `, 422 }, 423 { 424 "comment-whithout-whitespace", 425 426 commonConfig, 427 428 `package proc 429 430 import ( 431 "context"// no separating whitespace here //nolint:confusion 432 ) 433 `, 434 `package proc 435 436 import ( 437 "context"// no separating whitespace here //nolint:confusion 438 ) 439 `, 440 }, 441 { 442 "comment-with-slashslash", 443 444 commonConfig, 445 446 `package main 447 448 import ( 449 "fmt" // https://pkg.go.dev/fmt 450 ) 451 `, 452 `package main 453 454 import ( 455 "fmt" // https://pkg.go.dev/fmt 456 ) 457 `, 458 }, 459 { 460 "custom-order", 461 462 `customOrder: true 463 sections: 464 - Prefix(github.com/daixiang0) 465 - Default 466 - Standard 467 `, 468 `package main 469 470 import ( 471 "fmt" 472 473 g "github.com/golang" 474 475 "github.com/daixiang0/a" 476 ) 477 `, 478 `package main 479 480 import ( 481 "github.com/daixiang0/a" 482 483 g "github.com/golang" 484 485 "fmt" 486 ) 487 `, 488 }, 489 { 490 "default-order", 491 492 `sections: 493 - Standard 494 - Prefix(github.com/daixiang0) 495 - Default 496 `, 497 `package main 498 499 import ( 500 "fmt" 501 502 g "github.com/golang" 503 504 "github.com/daixiang0/a" 505 ) 506 `, 507 `package main 508 509 import ( 510 "fmt" 511 512 g "github.com/golang" 513 514 "github.com/daixiang0/a" 515 ) 516 `, 517 }, 518 { 519 "dot-and-blank", 520 521 `sections: 522 - Standard 523 - Default 524 - Prefix(github.com/daixiang0) 525 - Blank 526 - Dot 527 `, 528 `package main 529 530 import ( 531 "fmt" 532 533 g "github.com/golang" 534 . "github.com/golang/dot" 535 _ "github.com/golang/blank" 536 537 "github.com/daixiang0/a" 538 "github.com/daixiang0/gci" 539 "github.com/daixiang0/gci/subtest" 540 . "github.com/daixiang0/gci/dot" 541 _ "github.com/daixiang0/gci/blank" 542 ) 543 `, 544 `package main 545 546 import ( 547 "fmt" 548 549 g "github.com/golang" 550 551 "github.com/daixiang0/a" 552 "github.com/daixiang0/gci" 553 "github.com/daixiang0/gci/subtest" 554 555 _ "github.com/daixiang0/gci/blank" 556 _ "github.com/golang/blank" 557 558 . "github.com/daixiang0/gci/dot" 559 . "github.com/golang/dot" 560 ) 561 `, 562 }, 563 { 564 "duplicate-imports", 565 566 `sections: 567 - Standard 568 - Default 569 - Prefix(github.com/daixiang0) 570 `, 571 `package main 572 573 import ( 574 "fmt" 575 576 g "github.com/golang" 577 578 a "github.com/daixiang0/gci" 579 "github.com/daixiang0/gci" 580 ) 581 `, 582 `package main 583 584 import ( 585 "fmt" 586 587 g "github.com/golang" 588 589 "github.com/daixiang0/gci" 590 a "github.com/daixiang0/gci" 591 ) 592 `, 593 }, 594 { 595 "grouped-multiple-custom", 596 597 `sections: 598 - Standard 599 - Default 600 - Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0) 601 `, 602 `package main 603 604 import ( 605 "daixiang0/lib1" 606 "fmt" 607 "github.com/daixiang0/gci" 608 "gitlab.com/daixiang0/gci" 609 g "github.com/golang" 610 "github.com/daixiang0/gci/subtest" 611 ) 612 `, 613 `package main 614 615 import ( 616 "fmt" 617 618 g "github.com/golang" 619 620 "daixiang0/lib1" 621 "github.com/daixiang0/gci" 622 "github.com/daixiang0/gci/subtest" 623 "gitlab.com/daixiang0/gci" 624 ) 625 `, 626 }, 627 { 628 "leading-comment", 629 630 commonConfig, 631 632 `package main 633 634 import ( 635 // foo 636 "fmt" 637 ) 638 `, 639 `package main 640 641 import ( 642 // foo 643 "fmt" 644 ) 645 `, 646 }, 647 { 648 "linebreak", 649 650 `sections: 651 - Standard 652 - Default 653 - Prefix(github.com/daixiang0) 654 `, 655 `package main 656 657 import ( 658 g "github.com/golang" 659 660 "fmt" 661 662 "github.com/daixiang0/gci" 663 664 ) 665 `, 666 `package main 667 668 import ( 669 "fmt" 670 671 g "github.com/golang" 672 673 "github.com/daixiang0/gci" 674 ) 675 `, 676 }, 677 { 678 "linebreak-no-custom", 679 680 `sections: 681 - Standard 682 - Default 683 - Prefix(github.com/daixiang0) 684 `, 685 `package main 686 687 import ( 688 g "github.com/golang" 689 690 "fmt" 691 692 ) 693 `, 694 `package main 695 696 import ( 697 "fmt" 698 699 g "github.com/golang" 700 ) 701 `, 702 }, 703 { 704 "mismatch-section", 705 706 `sections: 707 - Standard 708 - Default 709 - Prefix(github.com/daixiang0) 710 - Prefix(github.com/daixiang0/gci) 711 `, 712 `package main 713 714 import ( 715 "fmt" 716 717 g "github.com/golang" 718 719 "github.com/daixiang0/gci" 720 ) 721 `, 722 `package main 723 724 import ( 725 "fmt" 726 727 g "github.com/golang" 728 729 "github.com/daixiang0/gci" 730 ) 731 `, 732 }, 733 { 734 "multiple-custom", 735 736 `sections: 737 - Standard 738 - Default 739 - Prefix(github.com/daixiang0) 740 - Prefix(github.com/daixiang0/gci) 741 - Prefix(github.com/daixiang0/gci/subtest) 742 `, 743 `package main 744 745 import ( 746 "fmt" 747 748 g "github.com/golang" 749 750 "github.com/daixiang0/a" 751 "github.com/daixiang0/gci" 752 "github.com/daixiang0/gci/subtest" 753 ) 754 `, 755 `package main 756 757 import ( 758 "fmt" 759 760 g "github.com/golang" 761 762 "github.com/daixiang0/a" 763 764 "github.com/daixiang0/gci" 765 766 "github.com/daixiang0/gci/subtest" 767 ) 768 `, 769 }, 770 { 771 "multiple-imports", 772 773 commonConfig, 774 775 `package main 776 777 import "fmt" 778 779 import "context" 780 781 import ( 782 "os" 783 784 "github.com/daixiang0/test" 785 ) 786 787 import "math" 788 789 790 // main 791 func main() { 792 } 793 `, 794 `package main 795 796 import ( 797 "context" 798 "fmt" 799 "math" 800 "os" 801 802 "github.com/daixiang0/test" 803 ) 804 805 // main 806 func main() { 807 } 808 `, 809 }, 810 { 811 "multiple-line-comment", 812 813 commonConfig, 814 815 `package proc 816 817 import ( 818 "context" // in-line comment 819 "fmt" 820 "os" 821 822 //nolint:depguard // A multi-line comment explaining why in 823 // this one case it's OK to use os/exec even though depguard 824 // is configured to force us to use dlib/exec instead. 825 "os/exec" 826 827 "golang.org/x/sys/unix" 828 "github.com/local/dlib/dexec" 829 ) 830 `, 831 `package proc 832 833 import ( 834 "context" // in-line comment 835 "fmt" 836 "os" 837 //nolint:depguard // A multi-line comment explaining why in 838 // this one case it's OK to use os/exec even though depguard 839 // is configured to force us to use dlib/exec instead. 840 "os/exec" 841 842 "github.com/local/dlib/dexec" 843 "golang.org/x/sys/unix" 844 ) 845 `, 846 }, 847 { 848 "nochar-after-import", 849 850 commonConfig, 851 852 `package main 853 854 import ( 855 "fmt" 856 ) 857 `, 858 `package main 859 860 import ( 861 "fmt" 862 ) 863 `, 864 }, 865 { 866 "no-format", 867 868 commonConfig, 869 870 `package main 871 872 import( 873 "fmt" 874 875 g "github.com/golang" 876 877 "github.com/daixiang0/gci" 878 ) 879 `, 880 `package main 881 882 import ( 883 "fmt" 884 885 g "github.com/golang" 886 887 "github.com/daixiang0/gci" 888 ) 889 `, 890 }, 891 { 892 "nolint", 893 894 commonConfig, 895 896 `package main 897 898 import ( 899 "fmt" 900 901 "github.com/forbidden/pkg" //nolint:depguard 902 903 _ "github.com/daixiang0/gci" //nolint:depguard 904 ) 905 `, 906 `package main 907 908 import ( 909 "fmt" 910 911 "github.com/forbidden/pkg" //nolint:depguard 912 913 _ "github.com/daixiang0/gci" //nolint:depguard 914 ) 915 `, 916 }, 917 { 918 "number-in-alias", 919 920 commonConfig, 921 922 `package main 923 924 import ( 925 "fmt" 926 927 go_V1 "github.com/golang" 928 929 "github.com/daixiang0/gci" 930 ) 931 `, 932 `package main 933 934 import ( 935 "fmt" 936 937 go_V1 "github.com/golang" 938 939 "github.com/daixiang0/gci" 940 ) 941 `, 942 }, 943 { 944 "one-import", 945 946 commonConfig, 947 948 `package main 949 import ( 950 "fmt" 951 ) 952 953 func main() { 954 } 955 `, 956 `package main 957 import ( 958 "fmt" 959 ) 960 961 func main() { 962 } 963 `, 964 }, 965 { 966 "one-import-one-line", 967 968 commonConfig, 969 970 `package main 971 972 import "fmt" 973 974 func main() { 975 } 976 `, 977 `package main 978 979 import "fmt" 980 981 func main() { 982 } 983 `, 984 }, 985 { 986 "one-line-import-after-import", 987 988 `sections: 989 - Standard 990 - Default 991 - Prefix(github.com/daixiang0) 992 `, 993 `package main 994 995 import ( 996 "fmt" 997 "os" 998 999 "github.com/daixiang0/test" 1000 ) 1001 1002 import "context" 1003 `, 1004 `package main 1005 1006 import ( 1007 "context" 1008 "fmt" 1009 "os" 1010 1011 "github.com/daixiang0/test" 1012 ) 1013 `, 1014 }, 1015 { 1016 "same-prefix-custom", 1017 1018 `sections: 1019 - Standard 1020 - Default 1021 - Prefix(github.com/daixiang0/gci) 1022 - Prefix(github.com/daixiang0/gci/subtest) 1023 `, 1024 `package main 1025 1026 import ( 1027 "fmt" 1028 1029 g "github.com/golang" 1030 1031 "github.com/daixiang0/gci" 1032 "github.com/daixiang0/gci/subtest" 1033 ) 1034 `, 1035 `package main 1036 1037 import ( 1038 "fmt" 1039 1040 g "github.com/golang" 1041 1042 "github.com/daixiang0/gci" 1043 1044 "github.com/daixiang0/gci/subtest" 1045 ) 1046 `, 1047 }, 1048 { 1049 "simple-case", 1050 1051 commonConfig, 1052 1053 `package main 1054 1055 import ( 1056 "golang.org/x/tools" 1057 1058 "fmt" 1059 1060 "github.com/daixiang0/gci" 1061 ) 1062 `, 1063 `package main 1064 1065 import ( 1066 "fmt" 1067 1068 "golang.org/x/tools" 1069 1070 "github.com/daixiang0/gci" 1071 ) 1072 `, 1073 }, 1074 { 1075 "whitespace-test", 1076 1077 commonConfig, 1078 1079 `package main 1080 1081 import ( 1082 "fmt" 1083 "github.com/golang" // golang 1084 alias "github.com/daixiang0/gci" 1085 ) 1086 `, 1087 `package main 1088 1089 import ( 1090 "fmt" 1091 1092 "github.com/golang" // golang 1093 1094 alias "github.com/daixiang0/gci" 1095 ) 1096 `, 1097 }, 1098 { 1099 "with-above-comment-and-alias", 1100 1101 commonConfig, 1102 1103 `package main 1104 1105 import ( 1106 "fmt" 1107 // golang 1108 _ "github.com/golang" 1109 "github.com/daixiang0/gci" 1110 ) 1111 `, 1112 `package main 1113 1114 import ( 1115 "fmt" 1116 1117 // golang 1118 _ "github.com/golang" 1119 1120 "github.com/daixiang0/gci" 1121 ) 1122 `, 1123 }, 1124 { 1125 "with-comment-and-alias", 1126 1127 commonConfig, 1128 1129 `package main 1130 1131 import ( 1132 "fmt" 1133 _ "github.com/golang" // golang 1134 "github.com/daixiang0/gci" 1135 ) 1136 `, 1137 `package main 1138 1139 import ( 1140 "fmt" 1141 1142 _ "github.com/golang" // golang 1143 1144 "github.com/daixiang0/gci" 1145 ) 1146 `, 1147 }, 1148 { 1149 "same-prefix-custom", 1150 1151 `sections: 1152 - Standard 1153 - Default 1154 - Prefix(github.com/daixiang0/gci) 1155 - Prefix(github.com/daixiang0/gci/subtest) 1156 `, 1157 `package main 1158 1159 import ( 1160 "fmt" 1161 1162 g "github.com/golang" 1163 1164 "github.com/daixiang0/gci" 1165 "github.com/daixiang0/gci/subtest" 1166 ) 1167 `, 1168 `package main 1169 1170 import ( 1171 "fmt" 1172 1173 g "github.com/golang" 1174 1175 "github.com/daixiang0/gci" 1176 1177 "github.com/daixiang0/gci/subtest" 1178 ) 1179 `, 1180 }, 1181 { 1182 "same-prefix-custom", 1183 1184 `sections: 1185 - Standard 1186 - Default 1187 - Prefix(github.com/daixiang0/gci) 1188 - Prefix(github.com/daixiang0/gci/subtest) 1189 `, 1190 `package main 1191 1192 import ( 1193 "fmt" 1194 1195 g "github.com/golang" 1196 1197 "github.com/daixiang0/gci" 1198 "github.com/daixiang0/gci/subtest" 1199 ) 1200 `, 1201 `package main 1202 1203 import ( 1204 "fmt" 1205 1206 g "github.com/golang" 1207 1208 "github.com/daixiang0/gci" 1209 1210 "github.com/daixiang0/gci/subtest" 1211 ) 1212 `, 1213 }, 1214 { 1215 "blank-in-config", 1216 1217 `sections: 1218 - Standard 1219 - Default 1220 - Prefix( github.com/daixiang0/gci, github.com/daixiang0/gci/subtest ) 1221 `, 1222 `package main 1223 1224 import ( 1225 "fmt" 1226 1227 g "github.com/golang" 1228 1229 "github.com/daixiang0/gci" 1230 "github.com/daixiang0/gci/subtest" 1231 ) 1232 `, 1233 `package main 1234 1235 import ( 1236 "fmt" 1237 1238 g "github.com/golang" 1239 1240 "github.com/daixiang0/gci" 1241 "github.com/daixiang0/gci/subtest" 1242 ) 1243 `, 1244 }, 1245 { 1246 "alias", 1247 1248 `sections: 1249 - Standard 1250 - Default 1251 - Alias 1252 `, 1253 `package main 1254 1255 import ( 1256 testing "github.com/daixiang0/test" 1257 "fmt" 1258 1259 g "github.com/golang" 1260 1261 "github.com/daixiang0/gci" 1262 "github.com/daixiang0/gci/subtest" 1263 ) 1264 `, 1265 `package main 1266 1267 import ( 1268 "fmt" 1269 1270 "github.com/daixiang0/gci" 1271 "github.com/daixiang0/gci/subtest" 1272 1273 testing "github.com/daixiang0/test" 1274 g "github.com/golang" 1275 ) 1276 `, 1277 }, 1278 { 1279 "no-trailing-newline", 1280 1281 `sections: 1282 - Standard 1283 `, 1284 `package main 1285 1286 import ( 1287 "net" 1288 "fmt" 1289 )`, 1290 `package main 1291 1292 import ( 1293 "fmt" 1294 "net" 1295 ) 1296 `, 1297 }, 1298 { 1299 "basic module", 1300 // implicitly relies on the local module name being: github.com/daixiang0/gci 1301 `sections: 1302 - Standard 1303 - LocalModule 1304 `, 1305 `package main 1306 1307 import ( 1308 "os" 1309 "github.com/daixiang0/gci/cmd/gci" 1310 ) 1311 `, 1312 `package main 1313 1314 import ( 1315 "os" 1316 1317 "github.com/daixiang0/gci/cmd/gci" 1318 ) 1319 `, 1320 }, 1321 }