How to use in method of main Package

Best Mock code snippet using main.in

imports_test.go

Source:imports_test.go Github

copy

Full Screen

1// Copyright 2013 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4package astutil5import (6 "bytes"7 "go/ast"8 "go/format"9 "go/parser"10 "go/token"11 "reflect"12 "strconv"13 "testing"14)15var fset = token.NewFileSet()16func parse(t *testing.T, name, in string) *ast.File {17 file, err := parser.ParseFile(fset, name, in, parser.ParseComments)18 if err != nil {19 t.Fatalf("%s parse: %v", name, err)20 }21 return file22}23func print(t *testing.T, name string, f *ast.File) string {24 var buf bytes.Buffer25 if err := format.Node(&buf, fset, f); err != nil {26 t.Fatalf("%s gofmt: %v", name, err)27 }28 return buf.String()29}30type test struct {31 name string32 renamedPkg string33 pkg string34 in string35 out string36 unchanged bool // Expect added/deleted return value to be false.37}38var addTests = []test{39 {40 name: "leave os alone",41 pkg: "os",42 in: `package main43import (44 "os"45)46`,47 out: `package main48import (49 "os"50)51`,52 unchanged: true,53 },54 {55 name: "import.1",56 pkg: "os",57 in: `package main58`,59 out: `package main60import "os"61`,62 },63 {64 name: "import.2",65 pkg: "os",66 in: `package main67// Comment68import "C"69`,70 out: `package main71// Comment72import "C"73import "os"74`,75 },76 {77 name: "import.3",78 pkg: "os",79 in: `package main80// Comment81import "C"82import (83 "io"84 "utf8"85)86`,87 out: `package main88// Comment89import "C"90import (91 "io"92 "os"93 "utf8"94)95`,96 },97 {98 name: "import.17",99 pkg: "x/y/z",100 in: `package main101// Comment102import "C"103import (104 "a"105 "b"106 "x/w"107 "d/f"108)109`,110 out: `package main111// Comment112import "C"113import (114 "a"115 "b"116 "x/w"117 "x/y/z"118 "d/f"119)120`,121 },122 {123 name: "issue #19190",124 pkg: "x.org/y/z",125 in: `package main126// Comment127import "C"128import (129 "bytes"130 "os"131 "d.com/f"132)133`,134 out: `package main135// Comment136import "C"137import (138 "bytes"139 "os"140 "d.com/f"141 "x.org/y/z"142)143`,144 },145 {146 name: "issue #19190 with existing grouped import packages",147 pkg: "x.org/y/z",148 in: `package main149// Comment150import "C"151import (152 "bytes"153 "os"154 "c.com/f"155 "d.com/f"156 "y.com/a"157 "y.com/b"158 "y.com/c"159)160`,161 out: `package main162// Comment163import "C"164import (165 "bytes"166 "os"167 "c.com/f"168 "d.com/f"169 "x.org/y/z"170 "y.com/a"171 "y.com/b"172 "y.com/c"173)174`,175 },176 {177 name: "issue #19190 - match score is still respected",178 pkg: "y.org/c",179 in: `package main180import (181 "x.org/a"182 "y.org/b"183)184`,185 out: `package main186import (187 "x.org/a"188 "y.org/b"189 "y.org/c"190)191`,192 },193 {194 name: "import into singular group",195 pkg: "bytes",196 in: `package main197import "os"198`,199 out: `package main200import (201 "bytes"202 "os"203)204`,205 },206 {207 name: "import into singular group with comment",208 pkg: "bytes",209 in: `package main210import /* why */ /* comment here? */ "os"211`,212 out: `package main213import /* why */ /* comment here? */ (214 "bytes"215 "os"216)217`,218 },219 {220 name: "import into group with leading comment",221 pkg: "strings",222 in: `package main223import (224 // comment before bytes225 "bytes"226 "os"227)228`,229 out: `package main230import (231 // comment before bytes232 "bytes"233 "os"234 "strings"235)236`,237 },238 {239 name: "",240 renamedPkg: "fmtpkg",241 pkg: "fmt",242 in: `package main243import "os"244`,245 out: `package main246import (247 fmtpkg "fmt"248 "os"249)250`,251 },252 {253 name: "struct comment",254 pkg: "time",255 in: `package main256// This is a comment before a struct.257type T struct {258 t time.Time259}260`,261 out: `package main262import "time"263// This is a comment before a struct.264type T struct {265 t time.Time266}267`,268 },269 {270 name: "issue 8729 import C",271 pkg: "time",272 in: `package main273import "C"274// comment275type T time.Time276`,277 out: `package main278import "C"279import "time"280// comment281type T time.Time282`,283 },284 {285 name: "issue 8729 empty import",286 pkg: "time",287 in: `package main288import ()289// comment290type T time.Time291`,292 out: `package main293import "time"294// comment295type T time.Time296`,297 },298 {299 name: "issue 8729 comment on package line",300 pkg: "time",301 in: `package main // comment302type T time.Time303`,304 out: `package main // comment305import "time"306type T time.Time307`,308 },309 {310 name: "issue 8729 comment after package",311 pkg: "time",312 in: `package main313// comment314type T time.Time315`,316 out: `package main317import "time"318// comment319type T time.Time320`,321 },322 {323 name: "issue 8729 comment before and on package line",324 pkg: "time",325 in: `// comment before326package main // comment on327type T time.Time328`,329 out: `// comment before330package main // comment on331import "time"332type T time.Time333`,334 },335 // Issue 9961: Match prefixes using path segments rather than bytes336 {337 name: "issue 9961",338 pkg: "regexp",339 in: `package main340import (341 "flag"342 "testing"343 "rsc.io/p"344)345`,346 out: `package main347import (348 "flag"349 "regexp"350 "testing"351 "rsc.io/p"352)353`,354 },355 // Issue 10337: Preserve comment position356 {357 name: "issue 10337",358 pkg: "fmt",359 in: `package main360import (361 "bytes" // a362 "log" // c363)364`,365 out: `package main366import (367 "bytes" // a368 "fmt"369 "log" // c370)371`,372 },373 {374 name: "issue 10337 new import at the start",375 pkg: "bytes",376 in: `package main377import (378 "fmt" // b379 "log" // c380)381`,382 out: `package main383import (384 "bytes"385 "fmt" // b386 "log" // c387)388`,389 },390 {391 name: "issue 10337 new import at the end",392 pkg: "log",393 in: `package main394import (395 "bytes" // a396 "fmt" // b397)398`,399 out: `package main400import (401 "bytes" // a402 "fmt" // b403 "log"404)405`,406 },407 // Issue 14075: Merge import declarations408 {409 name: "issue 14075",410 pkg: "bufio",411 in: `package main412import "bytes"413import "fmt"414`,415 out: `package main416import (417 "bufio"418 "bytes"419 "fmt"420)421`,422 },423 {424 name: "issue 14075 update position",425 pkg: "bufio",426 in: `package main427import "bytes"428import (429 "fmt"430)431`,432 out: `package main433import (434 "bufio"435 "bytes"436 "fmt"437)438`,439 },440 {441 name: `issue 14075 ignore import "C"`,442 pkg: "bufio",443 in: `package main444// Comment445import "C"446import "bytes"447import "fmt"448`,449 out: `package main450// Comment451import "C"452import (453 "bufio"454 "bytes"455 "fmt"456)457`,458 },459 {460 name: `issue 14075 ignore adjacent import "C"`,461 pkg: "bufio",462 in: `package main463// Comment464import "C"465import "fmt"466`,467 out: `package main468// Comment469import "C"470import (471 "bufio"472 "fmt"473)474`,475 },476 {477 name: `issue 14075 ignore adjacent import "C" (without factored import)`,478 pkg: "bufio",479 in: `package main480// Comment481import "C"482import "fmt"483`,484 out: `package main485// Comment486import "C"487import (488 "bufio"489 "fmt"490)491`,492 },493 {494 name: `issue 14075 ignore single import "C"`,495 pkg: "bufio",496 in: `package main497// Comment498import "C"499`,500 out: `package main501// Comment502import "C"503import "bufio"504`,505 },506 {507 name: `issue 17212 several single-import lines with shared prefix ending in a slash`,508 pkg: "net/http",509 in: `package main510import "bufio"511import "net/url"512`,513 out: `package main514import (515 "bufio"516 "net/http"517 "net/url"518)519`,520 },521 {522 name: `issue 17212 block imports lines with shared prefix ending in a slash`,523 pkg: "net/http",524 in: `package main525import (526 "bufio"527 "net/url"528)529`,530 out: `package main531import (532 "bufio"533 "net/http"534 "net/url"535)536`,537 },538 {539 name: `issue 17213 many single-import lines`,540 pkg: "fmt",541 in: `package main542import "bufio"543import "bytes"544import "errors"545`,546 out: `package main547import (548 "bufio"549 "bytes"550 "errors"551 "fmt"552)553`,554 },555 // Issue 28605: Add specified import, even if that import path is imported under another name556 {557 name: "issue 28605 add unnamed path",558 renamedPkg: "",559 pkg: "path",560 in: `package main561import (562 . "path"563 _ "path"564 pathpkg "path"565)566`,567 out: `package main568import (569 "path"570 . "path"571 _ "path"572 pathpkg "path"573)574`,575 },576 {577 name: "issue 28605 add pathpkg-renamed path",578 renamedPkg: "pathpkg",579 pkg: "path",580 in: `package main581import (582 "path"583 . "path"584 _ "path"585)586`,587 out: `package main588import (589 "path"590 . "path"591 _ "path"592 pathpkg "path"593)594`,595 },596 {597 name: "issue 28605 add blank identifier path",598 renamedPkg: "_",599 pkg: "path",600 in: `package main601import (602 "path"603 . "path"604 pathpkg "path"605)606`,607 out: `package main608import (609 "path"610 . "path"611 _ "path"612 pathpkg "path"613)614`,615 },616 {617 name: "issue 28605 add dot import path",618 renamedPkg: ".",619 pkg: "path",620 in: `package main621import (622 "path"623 _ "path"624 pathpkg "path"625)626`,627 out: `package main628import (629 "path"630 . "path"631 _ "path"632 pathpkg "path"633)634`,635 },636 {637 name: "duplicate import declarations, add existing one",638 renamedPkg: "f",639 pkg: "fmt",640 in: `package main641import "fmt"642import "fmt"643import f "fmt"644import f "fmt"645`,646 out: `package main647import "fmt"648import "fmt"649import f "fmt"650import f "fmt"651`,652 unchanged: true,653 },654}655func TestAddImport(t *testing.T) {656 for _, test := range addTests {657 file := parse(t, test.name, test.in)658 var before bytes.Buffer659 ast.Fprint(&before, fset, file, nil)660 added := AddNamedImport(fset, file, test.renamedPkg, test.pkg)661 if got := print(t, test.name, file); got != test.out {662 t.Errorf("first run: %s:\ngot: %s\nwant: %s", test.name, got, test.out)663 var after bytes.Buffer664 ast.Fprint(&after, fset, file, nil)665 t.Logf("AST before:\n%s\nAST after:\n%s\n", before.String(), after.String())666 }667 if got, want := added, !test.unchanged; got != want {668 t.Errorf("first run: %s: added = %v, want %v", test.name, got, want)669 }670 // AddNamedImport should be idempotent. Verify that by calling it again,671 // expecting no change to the AST, and the returned added value to always be false.672 added = AddNamedImport(fset, file, test.renamedPkg, test.pkg)673 if got := print(t, test.name, file); got != test.out {674 t.Errorf("second run: %s:\ngot: %s\nwant: %s", test.name, got, test.out)675 }676 if got, want := added, false; got != want {677 t.Errorf("second run: %s: added = %v, want %v", test.name, got, want)678 }679 }680}681func TestDoubleAddImport(t *testing.T) {682 file := parse(t, "doubleimport", "package main\n")683 AddImport(fset, file, "os")684 AddImport(fset, file, "bytes")685 want := `package main686import (687 "bytes"688 "os"689)690`691 if got := print(t, "doubleimport", file); got != want {692 t.Errorf("got: %s\nwant: %s", got, want)693 }694}695func TestDoubleAddNamedImport(t *testing.T) {696 file := parse(t, "doublenamedimport", "package main\n")697 AddNamedImport(fset, file, "o", "os")698 AddNamedImport(fset, file, "i", "io")699 want := `package main700import (701 i "io"702 o "os"703)704`705 if got := print(t, "doublenamedimport", file); got != want {706 t.Errorf("got: %s\nwant: %s", got, want)707 }708}709// Part of issue 8729.710func TestDoubleAddImportWithDeclComment(t *testing.T) {711 file := parse(t, "doubleimport", `package main712import (713)714// comment715type I int716`)717 // The AddImport order here matters.718 AddImport(fset, file, "golang.org/x/tools/go/ast/astutil")719 AddImport(fset, file, "os")720 want := `package main721import (722 "golang.org/x/tools/go/ast/astutil"723 "os"724)725// comment726type I int727`728 if got := print(t, "doubleimport_with_decl_comment", file); got != want {729 t.Errorf("got: %s\nwant: %s", got, want)730 }731}732var deleteTests = []test{733 {734 name: "import.4",735 pkg: "os",736 in: `package main737import (738 "os"739)740`,741 out: `package main742`,743 },744 {745 name: "import.5",746 pkg: "os",747 in: `package main748// Comment749import "C"750import "os"751`,752 out: `package main753// Comment754import "C"755`,756 },757 {758 name: "import.6",759 pkg: "os",760 in: `package main761// Comment762import "C"763import (764 "io"765 "os"766 "utf8"767)768`,769 out: `package main770// Comment771import "C"772import (773 "io"774 "utf8"775)776`,777 },778 {779 name: "import.7",780 pkg: "io",781 in: `package main782import (783 "io" // a784 "os" // b785 "utf8" // c786)787`,788 out: `package main789import (790 // a791 "os" // b792 "utf8" // c793)794`,795 },796 {797 name: "import.8",798 pkg: "os",799 in: `package main800import (801 "io" // a802 "os" // b803 "utf8" // c804)805`,806 out: `package main807import (808 "io" // a809 // b810 "utf8" // c811)812`,813 },814 {815 name: "import.9",816 pkg: "utf8",817 in: `package main818import (819 "io" // a820 "os" // b821 "utf8" // c822)823`,824 out: `package main825import (826 "io" // a827 "os" // b828 // c829)830`,831 },832 {833 name: "import.10",834 pkg: "io",835 in: `package main836import (837 "io"838 "os"839 "utf8"840)841`,842 out: `package main843import (844 "os"845 "utf8"846)847`,848 },849 {850 name: "import.11",851 pkg: "os",852 in: `package main853import (854 "io"855 "os"856 "utf8"857)858`,859 out: `package main860import (861 "io"862 "utf8"863)864`,865 },866 {867 name: "import.12",868 pkg: "utf8",869 in: `package main870import (871 "io"872 "os"873 "utf8"874)875`,876 out: `package main877import (878 "io"879 "os"880)881`,882 },883 {884 name: "handle.raw.quote.imports",885 pkg: "os",886 in: "package main\n\nimport `os`",887 out: `package main888`,889 },890 {891 name: "import.13",892 pkg: "io",893 in: `package main894import (895 "fmt"896 "io"897 "os"898 "utf8"899 "go/format"900)901`,902 out: `package main903import (904 "fmt"905 "os"906 "utf8"907 "go/format"908)909`,910 },911 {912 name: "import.14",913 pkg: "io",914 in: `package main915import (916 "fmt" // a917 "io" // b918 "os" // c919 "utf8" // d920 "go/format" // e921)922`,923 out: `package main924import (925 "fmt" // a926 // b927 "os" // c928 "utf8" // d929 "go/format" // e930)931`,932 },933 {934 name: "import.15",935 pkg: "double",936 in: `package main937import (938 "double"939 "double"940)941`,942 out: `package main943`,944 },945 {946 name: "import.16",947 pkg: "bubble",948 in: `package main949import (950 "toil"951 "bubble"952 "bubble"953 "trouble"954)955`,956 out: `package main957import (958 "toil"959 "trouble"960)961`,962 },963 {964 name: "import.17",965 pkg: "quad",966 in: `package main967import (968 "quad"969 "quad"970)971import (972 "quad"973 "quad"974)975`,976 out: `package main977`,978 },979 {980 name: "import.18",981 renamedPkg: "x",982 pkg: "fmt",983 in: `package main984import (985 "fmt"986 x "fmt"987)988`,989 out: `package main990import (991 "fmt"992)993`,994 },995 {996 name: "import.18",997 renamedPkg: "x",998 pkg: "fmt",999 in: `package main1000import x "fmt"1001import y "fmt"1002`,1003 out: `package main1004import y "fmt"1005`,1006 },1007 // Issue #15432, #180511008 {1009 name: "import.19",1010 pkg: "fmt",1011 in: `package main1012import (1013 "fmt"1014 // Some comment.1015 "io"1016)`,1017 out: `package main1018import (1019 // Some comment.1020 "io"1021)1022`,1023 },1024 {1025 name: "import.20",1026 pkg: "fmt",1027 in: `package main1028import (1029 "fmt"1030 // Some1031 // comment.1032 "io"1033)`,1034 out: `package main1035import (1036 // Some1037 // comment.1038 "io"1039)1040`,1041 },1042 {1043 name: "import.21",1044 pkg: "fmt",1045 in: `package main1046import (1047 "fmt"1048 /*1049 Some1050 comment.1051 */1052 "io"1053)`,1054 out: `package main1055import (1056 /*1057 Some1058 comment.1059 */1060 "io"1061)1062`,1063 },1064 {1065 name: "import.22",1066 pkg: "fmt",1067 in: `package main1068import (1069 /* Some */1070 // comment.1071 "io"1072 "fmt"1073)`,1074 out: `package main1075import (1076 /* Some */1077 // comment.1078 "io"1079)1080`,1081 },1082 {1083 name: "import.23",1084 pkg: "fmt",1085 in: `package main1086import (1087 // comment 11088 "fmt"1089 // comment 21090 "io"1091)`,1092 out: `package main1093import (1094 // comment 21095 "io"1096)1097`,1098 },1099 {1100 name: "import.24",1101 pkg: "fmt",1102 in: `package main1103import (1104 "fmt" // comment 11105 "io" // comment 21106)`,1107 out: `package main1108import (1109 "io" // comment 21110)1111`,1112 },1113 {1114 name: "import.25",1115 pkg: "fmt",1116 in: `package main1117import (1118 "fmt"1119 /* comment */ "io"1120)`,1121 out: `package main1122import (1123 /* comment */ "io"1124)1125`,1126 },1127 {1128 name: "import.26",1129 pkg: "fmt",1130 in: `package main1131import (1132 "fmt"1133 "io" /* comment */1134)`,1135 out: `package main1136import (1137 "io" /* comment */1138)1139`,1140 },1141 {1142 name: "import.27",1143 pkg: "fmt",1144 in: `package main1145import (1146 "fmt" /* comment */1147 "io"1148)`,1149 out: `package main1150import (1151 "io"1152)1153`,1154 },1155 {1156 name: "import.28",1157 pkg: "fmt",1158 in: `package main1159import (1160 /* comment */ "fmt"1161 "io"1162)`,1163 out: `package main1164import (1165 "io"1166)1167`,1168 },1169 {1170 name: "import.29",1171 pkg: "fmt",1172 in: `package main1173// comment 11174import (1175 "fmt"1176 "io" // comment 21177)`,1178 out: `package main1179// comment 11180import (1181 "io" // comment 21182)1183`,1184 },1185 {1186 name: "import.30",1187 pkg: "fmt",1188 in: `package main1189// comment 11190import (1191 "fmt" // comment 21192 "io"1193)`,1194 out: `package main1195// comment 11196import (1197 "io"1198)1199`,1200 },1201 {1202 name: "import.31",1203 pkg: "fmt",1204 in: `package main1205// comment 11206import (1207 "fmt"1208 /* comment 2 */ "io"1209)`,1210 out: `package main1211// comment 11212import (1213 /* comment 2 */ "io"1214)1215`,1216 },1217 {1218 name: "import.32",1219 pkg: "fmt",1220 renamedPkg: "f",1221 in: `package main1222// comment 11223import (1224 f "fmt"1225 /* comment 2 */ i "io"1226)`,1227 out: `package main1228// comment 11229import (1230 /* comment 2 */ i "io"1231)1232`,1233 },1234 {1235 name: "import.33",1236 pkg: "fmt",1237 renamedPkg: "f",1238 in: `package main1239// comment 11240import (1241 /* comment 2 */ f "fmt"1242 i "io"1243)`,1244 out: `package main1245// comment 11246import (1247 i "io"1248)1249`,1250 },1251 {1252 name: "import.34",1253 pkg: "fmt",1254 renamedPkg: "f",1255 in: `package main1256// comment 11257import (1258 f "fmt" /* comment 2 */1259 i "io"1260)`,1261 out: `package main1262// comment 11263import (1264 i "io"1265)1266`,1267 },1268 {1269 name: "import.35",1270 pkg: "fmt",1271 in: `package main1272// comment 11273import (1274 "fmt"1275 // comment 21276 "io"1277)`,1278 out: `package main1279// comment 11280import (1281 // comment 21282 "io"1283)1284`,1285 },1286 {1287 name: "import.36",1288 pkg: "fmt",1289 in: `package main1290/* comment 1 */1291import (1292 "fmt"1293 /* comment 2 */1294 "io"1295)`,1296 out: `package main1297/* comment 1 */1298import (1299 /* comment 2 */1300 "io"1301)1302`,1303 },1304 // Issue 20229: MergeLine panic on weird input1305 {1306 name: "import.37",1307 pkg: "io",1308 in: `package main1309import("_"1310"io")`,1311 out: `package main1312import (1313 "_"1314)1315`,1316 },1317 // Issue 28605: Delete specified import, even if that import path is imported under another name1318 {1319 name: "import.38",1320 renamedPkg: "",1321 pkg: "path",1322 in: `package main1323import (1324 "path"1325 . "path"1326 _ "path"1327 pathpkg "path"1328)1329`,1330 out: `package main1331import (1332 . "path"1333 _ "path"1334 pathpkg "path"1335)1336`,1337 },1338 {1339 name: "import.39",1340 renamedPkg: "pathpkg",1341 pkg: "path",1342 in: `package main1343import (1344 "path"1345 . "path"1346 _ "path"1347 pathpkg "path"1348)1349`,1350 out: `package main1351import (1352 "path"1353 . "path"1354 _ "path"1355)1356`,1357 },1358 {1359 name: "import.40",1360 renamedPkg: "_",1361 pkg: "path",1362 in: `package main1363import (1364 "path"1365 . "path"1366 _ "path"1367 pathpkg "path"1368)1369`,1370 out: `package main1371import (1372 "path"1373 . "path"1374 pathpkg "path"1375)1376`,1377 },1378 {1379 name: "import.41",1380 renamedPkg: ".",1381 pkg: "path",1382 in: `package main1383import (1384 "path"1385 . "path"1386 _ "path"1387 pathpkg "path"1388)1389`,1390 out: `package main1391import (1392 "path"1393 _ "path"1394 pathpkg "path"1395)1396`,1397 },1398 // Duplicate import declarations, all matching ones are deleted.1399 {1400 name: "import.42",1401 renamedPkg: "f",1402 pkg: "fmt",1403 in: `package main1404import "fmt"1405import "fmt"1406import f "fmt"1407import f "fmt"1408`,1409 out: `package main1410import "fmt"1411import "fmt"1412`,1413 },1414 {1415 name: "import.43",1416 renamedPkg: "x",1417 pkg: "fmt",1418 in: `package main1419import "fmt"1420import "fmt"1421import f "fmt"1422import f "fmt"1423`,1424 out: `package main1425import "fmt"1426import "fmt"1427import f "fmt"1428import f "fmt"1429`,1430 unchanged: true,1431 },1432}1433func TestDeleteImport(t *testing.T) {1434 for _, test := range deleteTests {1435 file := parse(t, test.name, test.in)1436 var before bytes.Buffer1437 ast.Fprint(&before, fset, file, nil)1438 deleted := DeleteNamedImport(fset, file, test.renamedPkg, test.pkg)1439 if got := print(t, test.name, file); got != test.out {1440 t.Errorf("first run: %s:\ngot: %s\nwant: %s", test.name, got, test.out)1441 var after bytes.Buffer1442 ast.Fprint(&after, fset, file, nil)1443 t.Logf("AST before:\n%s\nAST after:\n%s\n", before.String(), after.String())1444 }1445 if got, want := deleted, !test.unchanged; got != want {1446 t.Errorf("first run: %s: deleted = %v, want %v", test.name, got, want)1447 }1448 // DeleteNamedImport should be idempotent. Verify that by calling it again,1449 // expecting no change to the AST, and the returned deleted value to always be false.1450 deleted = DeleteNamedImport(fset, file, test.renamedPkg, test.pkg)1451 if got := print(t, test.name, file); got != test.out {1452 t.Errorf("second run: %s:\ngot: %s\nwant: %s", test.name, got, test.out)1453 }1454 if got, want := deleted, false; got != want {1455 t.Errorf("second run: %s: deleted = %v, want %v", test.name, got, want)1456 }1457 }1458}1459type rewriteTest struct {1460 name string1461 srcPkg string1462 dstPkg string1463 in string1464 out string1465}1466var rewriteTests = []rewriteTest{1467 {1468 name: "import.13",1469 srcPkg: "utf8",1470 dstPkg: "encoding/utf8",1471 in: `package main1472import (1473 "io"1474 "os"1475 "utf8" // thanks ken1476)1477`,1478 out: `package main1479import (1480 "encoding/utf8" // thanks ken1481 "io"1482 "os"1483)1484`,1485 },1486 {1487 name: "import.14",1488 srcPkg: "asn1",1489 dstPkg: "encoding/asn1",1490 in: `package main1491import (1492 "asn1"1493 "crypto"1494 "crypto/rsa"1495 _ "crypto/sha1"1496 "crypto/x509"1497 "crypto/x509/pkix"1498 "time"1499)1500var x = 11501`,1502 out: `package main1503import (1504 "crypto"1505 "crypto/rsa"1506 _ "crypto/sha1"1507 "crypto/x509"1508 "crypto/x509/pkix"1509 "encoding/asn1"1510 "time"1511)1512var x = 11513`,1514 },1515 {1516 name: "import.15",1517 srcPkg: "url",1518 dstPkg: "net/url",1519 in: `package main1520import (1521 "bufio"1522 "net"1523 "path"1524 "url"1525)1526var x = 1 // comment on x, not on url1527`,1528 out: `package main1529import (1530 "bufio"1531 "net"1532 "net/url"1533 "path"1534)1535var x = 1 // comment on x, not on url1536`,1537 },1538 {1539 name: "import.16",1540 srcPkg: "http",1541 dstPkg: "net/http",1542 in: `package main1543import (1544 "flag"1545 "http"1546 "log"1547 "text/template"1548)1549var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=181550`,1551 out: `package main1552import (1553 "flag"1554 "log"1555 "net/http"1556 "text/template"1557)1558var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=181559`,1560 },1561}1562func TestRewriteImport(t *testing.T) {1563 for _, test := range rewriteTests {1564 file := parse(t, test.name, test.in)1565 RewriteImport(fset, file, test.srcPkg, test.dstPkg)1566 if got := print(t, test.name, file); got != test.out {1567 t.Errorf("%s:\ngot: %s\nwant: %s", test.name, got, test.out)1568 }1569 }1570}1571var importsTests = []struct {1572 name string1573 in string1574 want [][]string1575}{1576 {1577 name: "no packages",1578 in: `package foo1579`,1580 want: nil,1581 },1582 {1583 name: "one group",1584 in: `package foo1585import (1586 "fmt"1587 "testing"1588)1589`,1590 want: [][]string{{"fmt", "testing"}},1591 },1592 {1593 name: "four groups",1594 in: `package foo1595import "C"1596import (1597 "fmt"1598 "testing"1599 "appengine"1600 "myproject/mylib1"1601 "myproject/mylib2"1602)1603`,1604 want: [][]string{1605 {"C"},1606 {"fmt", "testing"},1607 {"appengine"},1608 {"myproject/mylib1", "myproject/mylib2"},1609 },1610 },1611 {1612 name: "multiple factored groups",1613 in: `package foo1614import (1615 "fmt"1616 "testing"1617 "appengine"1618)1619import (1620 "reflect"1621 "bytes"1622)1623`,1624 want: [][]string{1625 {"fmt", "testing"},1626 {"appengine"},1627 {"reflect"},1628 {"bytes"},1629 },1630 },1631}1632func unquote(s string) string {1633 res, err := strconv.Unquote(s)1634 if err != nil {1635 return "could_not_unquote"1636 }1637 return res1638}1639func TestImports(t *testing.T) {1640 fset := token.NewFileSet()1641 for _, test := range importsTests {1642 f, err := parser.ParseFile(fset, "test.go", test.in, 0)1643 if err != nil {1644 t.Errorf("%s: %v", test.name, err)1645 continue1646 }1647 var got [][]string1648 for _, group := range Imports(fset, f) {1649 var b []string1650 for _, spec := range group {1651 b = append(b, unquote(spec.Path.Value))1652 }1653 got = append(got, b)1654 }1655 if !reflect.DeepEqual(got, test.want) {1656 t.Errorf("Imports(%s)=%v, want %v", test.name, got, test.want)1657 }1658 }1659}1660var usesImportTests = []struct {1661 name string1662 path string1663 in string1664 want bool1665}{1666 {1667 name: "no packages",1668 path: "io",1669 in: `package foo1670`,1671 want: false,1672 },1673 {1674 name: "import.1",1675 path: "io",1676 in: `package foo1677import "io"1678var _ io.Writer1679`,1680 want: true,1681 },1682 {1683 name: "import.2",1684 path: "io",1685 in: `package foo1686import "io"1687`,1688 want: false,1689 },1690 {1691 name: "import.3",1692 path: "io",1693 in: `package foo1694import "io"1695var io = 421696`,1697 want: false,1698 },1699 {1700 name: "import.4",1701 path: "io",1702 in: `package foo1703import i "io"1704var _ i.Writer1705`,1706 want: true,1707 },1708 {1709 name: "import.5",1710 path: "io",1711 in: `package foo1712import i "io"1713`,1714 want: false,1715 },1716 {1717 name: "import.6",1718 path: "io",1719 in: `package foo1720import i "io"1721var i = 421722var io = 421723`,1724 want: false,1725 },1726 {1727 name: "import.7",1728 path: "encoding/json",1729 in: `package foo1730import "encoding/json"1731var _ json.Encoder1732`,1733 want: true,1734 },1735 {1736 name: "import.8",1737 path: "encoding/json",1738 in: `package foo1739import "encoding/json"1740`,1741 want: false,1742 },1743 {1744 name: "import.9",1745 path: "encoding/json",1746 in: `package foo1747import "encoding/json"1748var json = 421749`,1750 want: false,1751 },1752 {1753 name: "import.10",1754 path: "encoding/json",1755 in: `package foo1756import j "encoding/json"1757var _ j.Encoder1758`,1759 want: true,1760 },1761 {1762 name: "import.11",1763 path: "encoding/json",1764 in: `package foo1765import j "encoding/json"1766`,1767 want: false,1768 },1769 {1770 name: "import.12",1771 path: "encoding/json",1772 in: `package foo1773import j "encoding/json"1774var j = 421775var json = 421776`,1777 want: false,1778 },1779 {1780 name: "import.13",1781 path: "io",1782 in: `package foo1783import _ "io"1784`,1785 want: true,1786 },1787 {1788 name: "import.14",1789 path: "io",1790 in: `package foo1791import . "io"1792`,1793 want: true,1794 },1795}1796func TestUsesImport(t *testing.T) {1797 fset := token.NewFileSet()1798 for _, test := range usesImportTests {1799 f, err := parser.ParseFile(fset, "test.go", test.in, 0)1800 if err != nil {1801 t.Errorf("%s: %v", test.name, err)1802 continue1803 }1804 got := UsesImport(f, test.path)1805 if got != test.want {1806 t.Errorf("UsesImport(%s)=%v, want %v", test.name, got, test.want)1807 }1808 }1809}...

Full Screen

Full Screen

import_test.go

Source:import_test.go Github

copy

Full Screen

1// Copyright 2011 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4package main5import "go/ast"6func init() {7 addTestCases(importTests, nil)8}9var importTests = []testCase{10 {11 Name: "import.0",12 Fn: addImportFn("os"),13 In: `package main14import (15 "os"16)17`,18 Out: `package main19import (20 "os"21)22`,23 },24 {25 Name: "import.1",26 Fn: addImportFn("os"),27 In: `package main28`,29 Out: `package main30import "os"31`,32 },33 {34 Name: "import.2",35 Fn: addImportFn("os"),36 In: `package main37// Comment38import "C"39`,40 Out: `package main41// Comment42import "C"43import "os"44`,45 },46 {47 Name: "import.3",48 Fn: addImportFn("os"),49 In: `package main50// Comment51import "C"52import (53 "io"54 "utf8"55)56`,57 Out: `package main58// Comment59import "C"60import (61 "io"62 "os"63 "utf8"64)65`,66 },67 {68 Name: "import.4",69 Fn: deleteImportFn("os"),70 In: `package main71import (72 "os"73)74`,75 Out: `package main76`,77 },78 {79 Name: "import.5",80 Fn: deleteImportFn("os"),81 In: `package main82// Comment83import "C"84import "os"85`,86 Out: `package main87// Comment88import "C"89`,90 },91 {92 Name: "import.6",93 Fn: deleteImportFn("os"),94 In: `package main95// Comment96import "C"97import (98 "io"99 "os"100 "utf8"101)102`,103 Out: `package main104// Comment105import "C"106import (107 "io"108 "utf8"109)110`,111 },112 {113 Name: "import.7",114 Fn: deleteImportFn("io"),115 In: `package main116import (117 "io" // a118 "os" // b119 "utf8" // c120)121`,122 Out: `package main123import (124 // a125 "os" // b126 "utf8" // c127)128`,129 },130 {131 Name: "import.8",132 Fn: deleteImportFn("os"),133 In: `package main134import (135 "io" // a136 "os" // b137 "utf8" // c138)139`,140 Out: `package main141import (142 "io" // a143 // b144 "utf8" // c145)146`,147 },148 {149 Name: "import.9",150 Fn: deleteImportFn("utf8"),151 In: `package main152import (153 "io" // a154 "os" // b155 "utf8" // c156)157`,158 Out: `package main159import (160 "io" // a161 "os" // b162 // c163)164`,165 },166 {167 Name: "import.10",168 Fn: deleteImportFn("io"),169 In: `package main170import (171 "io"172 "os"173 "utf8"174)175`,176 Out: `package main177import (178 "os"179 "utf8"180)181`,182 },183 {184 Name: "import.11",185 Fn: deleteImportFn("os"),186 In: `package main187import (188 "io"189 "os"190 "utf8"191)192`,193 Out: `package main194import (195 "io"196 "utf8"197)198`,199 },200 {201 Name: "import.12",202 Fn: deleteImportFn("utf8"),203 In: `package main204import (205 "io"206 "os"207 "utf8"208)209`,210 Out: `package main211import (212 "io"213 "os"214)215`,216 },217 {218 Name: "import.13",219 Fn: rewriteImportFn("utf8", "encoding/utf8"),220 In: `package main221import (222 "io"223 "os"224 "utf8" // thanks ken225)226`,227 Out: `package main228import (229 "encoding/utf8" // thanks ken230 "io"231 "os"232)233`,234 },235 {236 Name: "import.14",237 Fn: rewriteImportFn("asn1", "encoding/asn1"),238 In: `package main239import (240 "asn1"241 "crypto"242 "crypto/rsa"243 _ "crypto/sha1"244 "crypto/x509"245 "crypto/x509/pkix"246 "time"247)248var x = 1249`,250 Out: `package main251import (252 "crypto"253 "crypto/rsa"254 _ "crypto/sha1"255 "crypto/x509"256 "crypto/x509/pkix"257 "encoding/asn1"258 "time"259)260var x = 1261`,262 },263 {264 Name: "import.15",265 Fn: rewriteImportFn("url", "net/url"),266 In: `package main267import (268 "bufio"269 "net"270 "path"271 "url"272)273var x = 1 // comment on x, not on url274`,275 Out: `package main276import (277 "bufio"278 "net"279 "net/url"280 "path"281)282var x = 1 // comment on x, not on url283`,284 },285 {286 Name: "import.16",287 Fn: rewriteImportFn("http", "net/http", "template", "text/template"),288 In: `package main289import (290 "flag"291 "http"292 "log"293 "template"294)295var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18296`,297 Out: `package main298import (299 "flag"300 "log"301 "net/http"302 "text/template"303)304var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18305`,306 },307 {308 Name: "import.17",309 Fn: addImportFn("x/y/z", "x/a/c"),310 In: `package main311// Comment312import "C"313import (314 "a"315 "b"316 "x/w"317 "d/f"318)319`,320 Out: `package main321// Comment322import "C"323import (324 "a"325 "b"326 "x/a/c"327 "x/w"328 "x/y/z"329 "d/f"330)331`,332 },333 {334 Name: "import.18",335 Fn: addDelImportFn("e", "o"),336 In: `package main337import (338 "f"339 "o"340 "z"341)342`,343 Out: `package main344import (345 "e"346 "f"347 "z"348)349`,350 },351}352func addImportFn(path ...string) func(*ast.File) bool {353 return func(f *ast.File) bool {354 fixed := false355 for _, p := range path {356 if !imports(f, p) {357 addImport(f, p)358 fixed = true359 }360 }361 return fixed362 }363}364func deleteImportFn(path string) func(*ast.File) bool {365 return func(f *ast.File) bool {366 if imports(f, path) {367 deleteImport(f, path)368 return true369 }370 return false371 }372}373func addDelImportFn(p1 string, p2 string) func(*ast.File) bool {374 return func(f *ast.File) bool {375 fixed := false376 if !imports(f, p1) {377 addImport(f, p1)378 fixed = true379 }380 if imports(f, p2) {381 deleteImport(f, p2)382 fixed = true383 }384 return fixed385 }386}387func rewriteImportFn(oldnew ...string) func(*ast.File) bool {388 return func(f *ast.File) bool {389 fixed := false390 for i := 0; i < len(oldnew); i += 2 {391 if imports(f, oldnew[i]) {392 rewriteImport(f, oldnew[i], oldnew[i+1])393 fixed = true394 }395 }396 return fixed397 }398}...

Full Screen

Full Screen

in

Using AI Code Generation

copy

Full Screen

1func (s *Server) handleRequests() {2 http.HandleFunc("/", s.homePage)3 http.HandleFunc("/all", s.returnAllArticles)4 log.Fatal(http.ListenAndServe(":10000", nil))5}6func (s *Server) returnAllArticles(w http.ResponseWriter, r *http.Request) {7 fmt.Println("Endpoint Hit: returnAllArticles")8 json.NewEncoder(w).Encode(Articles)9}10func (s *Server) homePage(w http.ResponseWriter, r *http.Request) {11 fmt.Fprintf(w, "Homepage Endpoint Hit")12}13func handleRequests() {14 http.HandleFunc("/", homePage)15 http.HandleFunc("/all", returnAllArticles)16 log.Fatal(http.ListenAndServe(":10000", nil))17}18func returnAllArticles(w http.ResponseWriter, r *http.Request) {19 fmt.Println("Endpoint Hit: returnAllArticles")20 json.NewEncoder(w).Encode(Articles)21}22func homePage(w http.ResponseWriter, r *http.Request) {23 fmt.Fprintf(w, "Homepage Endpoint Hit")24}25func handleRequests() {26 http.HandleFunc("/", homePage)27 http.HandleFunc("/all", returnAllArticles)28 log.Fatal(http.ListenAndServe(":10000", nil))29}30func returnAllArticles(w http.ResponseWriter, r *http.Request) {31 fmt.Println("Endpoint Hit: returnAllArticles")32 json.NewEncoder(w).Encode(Articles)33}34func homePage(w http.ResponseWriter, r *http.Request) {35 fmt.Fprintf(w, "Homepage Endpoint Hit")36}37func handleRequests() {38 http.HandleFunc("/", homePage)39 http.HandleFunc("/all", returnAllArticles)40 log.Fatal(http.ListenAndServe(":10000", nil))41}42func returnAllArticles(w http.ResponseWriter, r *http.Request

Full Screen

Full Screen

in

Using AI Code Generation

copy

Full Screen

1 func main() {2 c = add(a, b)3 fmt.Println("Sum of a and b is: ", c)4 }5 func add(a int, b int) int {6 }7 func main() {8 c = add(a, b)9 fmt.Println("Sum of a and b is: ", c)10 }11 func add(a int, b int) int {12 }13 func main() {14 c = add(a, b)15 fmt.Println("Sum of a and b is: ", c)16 }17 func add(a int, b int) int {18 }19 func main() {20 c = add(a, b)21 fmt.Println("Sum of a and b is: ", c)22 }23 func add(a int, b int) int {24 }25 func main() {26 c = add(a, b)27 fmt.Println("Sum of a and b is: ", c)28 }29 func add(a int, b int) int {30 }31 func main() {

Full Screen

Full Screen

in

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f := func() {4 fmt.Println("this is an anonymous function")5 }6 foo(f)7}8func foo(f func()) {9 f()10}11import (12func main() {13 f := func() {14 fmt.Println("this is an anonymous function")15 }16 foo(f)17}18func foo(f func()) {19 f()20}21import (22func main() {23 f := func() {24 fmt.Println("this is an anonymous function")25 }26 foo(f)27}28func foo(f func()) {29 f()30}31import (32func main() {33 f := func() {34 fmt.Println("this is an anonymous function")35 }36 foo(f)37}38func foo(f func()) {39 f()40}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful