How to use parse method of main Package

Best Rod code snippet using main.parse

analyze_test.go

Source:analyze_test.go Github

copy

Full Screen

1package analysis2import (3 "fmt"4 "go/ast"5 "go/parser"6 "go/token"7 "log"8 "testing"9 "gitlab.com/verygoodsoftwarenotvirus/blanket/lib/util"10 "github.com/fatih/set"11 "github.com/stretchr/testify/assert"12)13////////////////////////////////////////////////////////14// //15// Test Helper Functions //16// //17////////////////////////////////////////////////////////18func parseChunkOfCode(t *testing.T, chunkOfCode string) *ast.File {19 p, err := parser.ParseFile(token.NewFileSet(), "example.go", chunkOfCode, parser.AllErrors)20 if err != nil {21 log.Println(err)22 t.FailNow()23 }24 return p25}26////////////////////////////////////////////////////////27// //28// Actual Tests //29// //30////////////////////////////////////////////////////////31func TestParseExpr(t *testing.T) {32 t.Run("ident", func(_t *testing.T) {33 analyzer := NewAnalyzer()34 codeSample := `35 package main36 func main() {37 functionCall()38 }39 `40 p := parseChunkOfCode(t, codeSample)41 input := p.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.ExprStmt).X.(*ast.CallExpr).Fun42 expected := set.New("init", "functionCall")43 analyzer.parseExpr(input)44 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")45 })46 t.Run("selector", func(_t *testing.T) {47 analyzer := NewAnalyzer()48 analyzer.nameToTypeMap["class"] = "Example"49 codeSample := `50 package main51 func main() {52 class.methodCall()53 }54 `55 p := parseChunkOfCode(t, codeSample)56 input := p.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.ExprStmt).X.(*ast.CallExpr).Fun57 expected := set.New("init", "Example.methodCall")58 analyzer.parseExpr(input)59 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")60 })61 t.Run("function literal", func(_t *testing.T) {62 analyzer := NewAnalyzer()63 codeSample := `64 package main65 func main() {66 func() {67 functionCall()68 }()69 }70 `71 p := parseChunkOfCode(t, codeSample)72 input := p.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.ExprStmt).X.(*ast.CallExpr).Fun73 expected := set.New("init", "functionCall")74 analyzer.parseExpr(input)75 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")76 })77}78func TestParseCallExpr(t *testing.T) {79 t.Run("with ast.Ident", func(_t *testing.T) {80 analyzer := NewAnalyzer()81 codeSample := `82 package main83 var function func()84 func main(){85 fart := function()86 }87 `88 p := parseChunkOfCode(t, codeSample)89 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt).Rhs[0].(*ast.CallExpr)90 expected := set.New("init", "function")91 analyzer.parseCallExpr(input)92 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")93 })94 t.Run("with ast.SelectorExpr", func(_t *testing.T) {95 analyzer := NewAnalyzer()96 analyzer.nameToTypeMap["s"] = "Struct"97 codeSample := `98 package main99 type Struct struct{}100 func (s Struct) method(){}101 func main(){102 s := Struct{}103 s.method()104 }105 `106 p := parseChunkOfCode(t, codeSample)107 input := p.Decls[2].(*ast.FuncDecl).Body.List[1].(*ast.ExprStmt).X.(*ast.CallExpr)108 expected := set.New("init", "Struct.method")109 analyzer.parseCallExpr(input)110 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")111 })112 t.Run("with ast.SelectorExpr, but no matching entity", func(_t *testing.T) {113 analyzer := NewAnalyzer()114 codeSample := `115 package main116 type Struct struct{}117 func (s Struct) method(){}118 func main(){119 s := Struct{}120 s.method()121 }122 `123 p := parseChunkOfCode(t, codeSample)124 input := p.Decls[2].(*ast.FuncDecl).Body.List[1].(*ast.ExprStmt).X.(*ast.CallExpr)125 expected := set.New("init")126 analyzer.parseCallExpr(input)127 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")128 })129 t.Run("with funcLit in argument list", func(_t *testing.T) {130 analyzer := NewAnalyzer()131 _t.Parallel()132 codeSample := `133 package main134 import "log"135 func main(){136 arbitraryCallExpression(func(i int) {137 log.Println(i)138 })139 }140 `141 p := parseChunkOfCode(t, codeSample)142 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.ExprStmt).X.(*ast.CallExpr)143 expected := set.New("init", "arbitraryCallExpression")144 analyzer.parseCallExpr(input)145 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")146 })147}148func TestParseUnaryExpr(t *testing.T) {149 analyzer := NewAnalyzer()150 codeSample := `151 package main152 type Struct struct{}153 func main(){154 s := &Struct{}155 }156 `157 p := parseChunkOfCode(t, codeSample)158 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt).Rhs[0].(*ast.UnaryExpr)159 expected := map[string]string{"s": "Struct"}160 analyzer.parseUnaryExpr(input, "s")161 assert.Equal(t, expected, analyzer.nameToTypeMap, "actual output does not match expected output")162}163func TestParseDeclStmt(t *testing.T) {164 analyzer := NewAnalyzer()165 codeSample := `166 package main167 func main(){168 var test bool169 }170 `171 p := parseChunkOfCode(t, codeSample)172 input := p.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.DeclStmt)173 expected := map[string]string{"test": "bool"}174 analyzer.parseDeclStmt(input)175 assert.Equal(t, expected, analyzer.nameToTypeMap, "actual output does not match expected output")176}177func TestParseExprStmt(t *testing.T) {178 t.Run("CallExpr.Fun.(*ast.Ident)", func(_t *testing.T) {179 analyzer := NewAnalyzer()180 codeSample := `181 package main182 var example func()183 func main(){184 example()185 }186 `187 p := parseChunkOfCode(t, codeSample)188 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.ExprStmt)189 expected := set.New("init", "example")190 analyzer.parseExprStmt(input)191 assert.Equal(t, expected, analyzer.calledFuncs, "actual output does not match expected output")192 })193 t.Run("CallExpr.Fun.(*ast.Selector)", func(_t *testing.T) {194 analyzer := NewAnalyzer()195 analyzer.nameToTypeMap["e"] = "Example"196 codeSample := `197 package main198 type Example struct{}199 func (e Example) method() {}200 func main() {201 var e Example202 e.method()203 }204 `205 p := parseChunkOfCode(t, codeSample)206 input := p.Decls[2].(*ast.FuncDecl).Body.List[1].(*ast.ExprStmt)207 expected := set.New("init", "Example.method")208 analyzer.parseExprStmt(input)209 assert.Equal(t, expected, analyzer.calledFuncs, "actual output does not match expected output")210 })211}212func TestParseCompositeLit(t *testing.T) {213 t.Run("ident", func(_t *testing.T) {214 analyzer := NewAnalyzer()215 codeSample := `216 package main217 func main() {218 x := &Example{219 methodCallAsArg(),220 }221 }222 `223 p := parseChunkOfCode(t, codeSample)224 input := p.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt).Rhs[0].(*ast.UnaryExpr).X.(*ast.CompositeLit)225 expected := set.New("init", "methodCallAsArg")226 analyzer.parseCompositeLit(input, "e")227 assert.Equal(t, expected, analyzer.calledFuncs, "actual output does not match expected output")228 })229 t.Run("selector", func(_t *testing.T) {230 analyzer := NewAnalyzer()231 analyzer.nameToTypeMap["e"] = "Example"232 codeSample := `233 package main234 func main() {235 x := &pkg.Example{236 e.methodCallAsArg(),237 }238 }239 `240 p := parseChunkOfCode(t, codeSample)241 input := p.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt).Rhs[0].(*ast.UnaryExpr).X.(*ast.CompositeLit)242 expected := set.New("init", "Example.methodCallAsArg")243 analyzer.parseCompositeLit(input, "e")244 assert.Equal(t, expected, analyzer.calledFuncs, "actual output does not match expected output")245 })246}247func TestParseGenDecl(t *testing.T) {248 analyzer := NewAnalyzer()249 codeSample := `250 package main251 var thing string252 func main(){}253 `254 p := parseChunkOfCode(t, codeSample)255 input := p.Decls[0].(*ast.GenDecl)256 expected := map[string]string{"thing": "string"}257 analyzer.parseGenDecl(input)258 assert.Equal(t, expected, analyzer.nameToTypeMap, "expected function name to be added to output")259}260func TestParseFuncDecl(t *testing.T) {261 t.Run("simple", func(_t *testing.T) {262 analyzer := NewAnalyzer()263 codeSample := `264 package test265 func example(){}266 `267 p := parseChunkOfCode(t, codeSample)268 input := p.Decls[0].(*ast.FuncDecl)269 expected := "example"270 actual := analyzer.parseFuncDecl(input)271 assert.Equal(t, expected, actual, "actual output does not match expected output")272 })273 t.Run("with receiver", func(_t *testing.T) {274 analyzer := NewAnalyzer()275 codeSample := `276 package test277 type Example struct{}278 func (e Example) method(){}279 `280 p := parseChunkOfCode(t, codeSample)281 input := p.Decls[1].(*ast.FuncDecl)282 expected := "Example.method"283 actual := analyzer.parseFuncDecl(input)284 assert.Equal(t, expected, actual, "actual output does not match expected output")285 })286 t.Run("with ptr receiver", func(_t *testing.T) {287 analyzer := NewAnalyzer()288 codeSample := `289 package test290 type Example struct{}291 func (e *Example) method(){}292 `293 p := parseChunkOfCode(t, codeSample)294 input := p.Decls[1].(*ast.FuncDecl)295 expected := "Example.method"296 actual := analyzer.parseFuncDecl(input)297 assert.Equal(t, expected, actual, "actual output does not match expected output")298 })299}300func TestParseAssignStmt(t *testing.T) {301 t.Run("CallExpr", func(_t *testing.T) {302 analyzer := NewAnalyzer()303 codeSample := `304 package main305 import "testing"306 func example() error {307 return nil308 }309 func test() {310 e := example()311 }312 `313 p := parseChunkOfCode(t, codeSample)314 input := p.Decls[2].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)315 expected := set.New("init", "example")316 analyzer.parseAssignStmt(input)317 assert.Equal(t, expected, analyzer.calledFuncs, "actual output does not match expected output")318 })319 t.Run("CallExpr with multiple returns and ast.Ident Fun value", func(_t *testing.T) {320 analyzer := NewAnalyzer()321 analyzer.helperFunctionReturnMap["example"] = []string{"X", "Y"}322 // this case handles when a helper function is declared in another file.323 codeSample := `324 package main325 import "testing"326 func TestX() {327 x, y := example()328 }329 `330 p := parseChunkOfCode(t, codeSample)331 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)332 expected := map[string]string{333 "x": "X",334 "y": "Y",335 }336 analyzer.parseAssignStmt(input)337 assert.Equal(t, expected, analyzer.nameToTypeMap, "actual output does not match expected output")338 })339 t.Run("Assign statement with multiple returns from some external function", func(_t *testing.T) {340 analyzer := NewAnalyzer()341 codeSample := `342 package main343 import "testing"344 func TestX() {345 req, err := http.NewRequest(http.MethodGet, "http://example.com", nil)346 }347 `348 p := parseChunkOfCode(t, codeSample)349 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)350 expected := map[string]string{}351 analyzer.parseAssignStmt(input)352 assert.Equal(t, expected, analyzer.nameToTypeMap, "actual output does not match expected output")353 })354 t.Run("Assign statement with multiple returns from some internal function", func(_t *testing.T) {355 analyzer := NewAnalyzer()356 analyzer.helperFunctionReturnMap["someHelperFunctionForTestsOnly"] = []string{"http.Request", "error"}357 codeSample := `358 package main359 import "testing"360 func TestX() {361 req, err := someHelperFunctionForTestsOnly()362 }363 `364 p := parseChunkOfCode(t, codeSample)365 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)366 expected := map[string]string{367 "req": "http.Request",368 "err": "error",369 }370 analyzer.parseAssignStmt(input)371 assert.Equal(t, expected, analyzer.nameToTypeMap, "actual output does not match expected output")372 })373 t.Run("UnaryExpr", func(_t *testing.T) {374 analyzer := NewAnalyzer()375 codeSample := `376 package main377 import "testing"378 func TestX(t *testing.T) {379 test := &SomeStruct{}380 }381 `382 p := parseChunkOfCode(t, codeSample)383 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)384 expected := map[string]string{385 "test": "SomeStruct",386 }387 analyzer.parseAssignStmt(input)388 assert.Equal(t, expected, analyzer.nameToTypeMap, "actual output does not match expected output")389 })390 t.Run("multiple unary expressions", func(_t *testing.T) {391 analyzer := NewAnalyzer()392 codeSample := `393 package main394 import "testing"395 func TestX(t *testing.T) {396 one, other := &SomeStruct{}, &SomeOtherStruct{}397 }398 `399 p := parseChunkOfCode(t, codeSample)400 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)401 expected := map[string]string{402 "one": "SomeStruct",403 "other": "SomeOtherStruct",404 }405 analyzer.parseAssignStmt(input)406 assert.Equal(t, expected, analyzer.nameToTypeMap, "actual output does not match expected output")407 })408 t.Run("FuncLit", func(_t *testing.T) {409 analyzer := NewAnalyzer()410 codeSample := `411 package main412 import "testing"413 func TestX(t *testing. T) {414 subtest := func(_t *testing.T) {415 analyzer := NewAnalyzer()416}417 t.Run("subtest", subtest)418 }419 `420 p := parseChunkOfCode(t, codeSample)421 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)422 expected := map[string]string{}423 analyzer.parseAssignStmt(input)424 assert.Equal(t, expected, analyzer.nameToTypeMap, "actual output does not match expected output")425 })426 t.Run("composite literal", func(_t *testing.T) {427 analyzer := NewAnalyzer()428 codeSample := `429 package main430 import "testing"431 func TestX(t *testing. T) {432 os.Args = []string{433 "fart",434 }435 }436 `437 p := parseChunkOfCode(t, codeSample)438 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)439 expected := map[string]string{}440 analyzer.parseAssignStmt(input)441 assert.Equal(t, expected, analyzer.nameToTypeMap, "actual output does not match expected output")442 })443}444func TestParseHelperSelectorExpr(t *testing.T) {445 analyzer := NewAnalyzer()446 codeSample := `447 package main448 import "testing"449 func helperGenerator(t *testing.T) (ast.SelectorExpr, error) {450 return ast.SelectorExpr{}, nil451 }452 `453 p := parseChunkOfCode(t, codeSample)454 input := p.Decls[1].(*ast.FuncDecl).Type.Results.List[0].Type.(*ast.SelectorExpr)455 name := "arbitraryFunctionName"456 expected := map[string][]string{457 name: {"ast.SelectorExpr"},458 }459 analyzer.parseHelperSelectorExpr(input, name)460 assert.Equal(t, expected, analyzer.helperFunctionReturnMap, "expected output did not match actual output")461}462func TestParseHelperFunction(t *testing.T) {463 t.Run("ident", func(_t *testing.T) {464 analyzer := NewAnalyzer()465 codeSample := `466 package main467 import "testing"468 func helperGenerator(t *testing.T) (*Example, error) {469 return &Example{}, nil470 }471 `472 p := parseChunkOfCode(t, codeSample)473 input := p.Decls[1].(*ast.FuncDecl)474 expected := map[string][]string{475 "helperGenerator": {476 "Example",477 "error",478 },479 }480 analyzer.parseHelperFunction(input)481 assert.Equal(t, expected, analyzer.helperFunctionReturnMap, "expected output did not match actual output")482 })483 t.Run("selector", func(_t *testing.T) {484 analyzer := NewAnalyzer()485 codeSample := `486 package main487 import "testing"488 func helperGenerator(t *testing.T) (ast.SelectorExpr, error) {489 return ast.SelectorExpr{}, nil490 }491 `492 p := parseChunkOfCode(t, codeSample)493 input := p.Decls[1].(*ast.FuncDecl)494 expected := map[string][]string{495 "helperGenerator": {496 "ast.SelectorExpr",497 "error",498 },499 }500 analyzer.parseHelperFunction(input)501 assert.Equal(t, expected, analyzer.helperFunctionReturnMap, "expected output did not match actual output")502 })503 t.Run("star selector", func(_t *testing.T) {504 analyzer := NewAnalyzer()505 codeSample := `506 package main507 import "testing"508 func helperGenerator(t *testing.T) (*ast.SelectorExpr, error) {509 return &ast.SelectorExpr{}, nil510 }511 `512 p := parseChunkOfCode(t, codeSample)513 input := p.Decls[1].(*ast.FuncDecl)514 expected := map[string][]string{515 "helperGenerator": {516 "ast.SelectorExpr",517 "error",518 },519 }520 analyzer.parseHelperFunction(input)521 assert.Equal(t, expected, analyzer.helperFunctionReturnMap, "expected output did not match actual output")522 })523}524func TestParseFuncLit(t *testing.T) {525 analyzer := NewAnalyzer()526 codeSample := `527 package main528 import "testing"529 func TestX(t *testing. T) {530 subtest := func(_t *testing.T) {531 var err error532 doSomeThings()533 err = doSomeOtherThings()534 }535 t.Run("subtest", subtest)536 }537`538 p := parseChunkOfCode(t, codeSample)539 input := p.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt).Rhs[0].(*ast.FuncLit)540 expected := set.New("init", "doSomeThings", "doSomeOtherThings")541 analyzer.parseFuncLit(input)542 assert.Equal(t, expected, analyzer.calledFuncs, "actual output does not match expected output")543}544func TestParseReturnStmt(t *testing.T) {545 analyzer := NewAnalyzer()546 codeSample := `547 package main548 func main(){549 return functionCall()550 }551 `552 p := parseChunkOfCode(t, codeSample)553 input := p.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.ReturnStmt)554 expected := set.New("init", "functionCall")555 analyzer.parseReturnStmt(input)556 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")557}558func TestParseSelectStmt(t *testing.T) {559 analyzer := NewAnalyzer()560 codeSample := `561 package main562 func main(){563 temp := make(chan int)564 go func() {565 temp <- 0566 }()567 for {568 select {569 case <-temp:570 functionCall()571 return572 }573 }574 }575 `576 p := parseChunkOfCode(t, codeSample)577 input := p.Decls[0].(*ast.FuncDecl).Body.List[2].(*ast.ForStmt).Body.List[0].(*ast.SelectStmt)578 expected := set.New("init", "functionCall")579 analyzer.parseSelectStmt(input)580 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")581}582func TestParseSendStmt(t *testing.T) {583 analyzer := NewAnalyzer()584 analyzer.nameToTypeMap["x"] = "Example"585 codeSample := `586 package main587 func main(){588 thing <- First()589 thing <- func(){590 Second()591 }()592 thing <- x.Third()593 }594 `595 p := parseChunkOfCode(t, codeSample)596 input := p.Decls[0].(*ast.FuncDecl).Body.List597 expected := set.New(598 "init",599 "First",600 "Second",601 "Example.Third",602 )603 for _, x := range input {604 in := x.(*ast.SendStmt)605 analyzer.parseSendStmt(in)606 }607 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")608}609func TestParseSwitchStmt(t *testing.T) {610 analyzer := NewAnalyzer()611 codeSample := `612 package main613 func main(){614 switch tmp {615 case tmp:616 functionCall()617 }618 }619`620 p := parseChunkOfCode(t, codeSample)621 input := p.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.SwitchStmt)622 expected := set.New("init", "functionCall")623 analyzer.parseSwitchStmt(input)624 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")625}626func TestParseTypeSwitchStmt(t *testing.T) {627 analyzer := NewAnalyzer()628 codeSample := `629 package main630 func main(){631 func(i interface{}) {632 switch i.(type) {633 case string:634 functionCall()635 }636 }(tmp)637 }638 `639 p := parseChunkOfCode(t, codeSample)640 input := p.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.ExprStmt).X.(*ast.CallExpr).Fun.(*ast.FuncLit).Body.List[0].(*ast.TypeSwitchStmt)641 expected := set.New("init", "functionCall")642 analyzer.parseTypeSwitchStmt(input)643 assert.Equal(t, expected, analyzer.calledFuncs, "expected function name to be added to output")644}645func TestParseStmt(t *testing.T) {646 analyzer := NewAnalyzer()647 codeSample := `648 package main649 import "testing"650 func TestX(t *testing.T) string {651 // AssignStmt:652 tmp := "AssignStmt"653 foo := Foo{}654 bar := bar.Bar{}655 baz := &baz.Baz{}656 var x Example657 // RangeStmt658 for range [1]struct{}{} {659 A()660 }661 // IfStmt662 if true {663 B()664 }665 // DeclStmt666 var declStmt ast.DeclStmt667 // ExprStmt668 C()669 // DeferStmt670 defer func() {671 D()672 }()673 defer E()674 defer x.MethodOne()675 // ForStmt676 for i := 0; i < 1; i++ {677 F()678 }679 // GoStmt680 go G()681 go func() {682 H()683 }()684 go x.MethodTwo()685 // SelectStmt686 temp := make(chan int)687 go func() {688 temp <- 0689 }()690 for {691 select {692 case <-temp:693 I()694 return695 }696 }697 // SendStmt698 thing <- J()699 thing <- func(){700 K()701 }()702 thing <- x.MethodThree()703 // Args704 assert.True(t, x.MethodFour())705 assert.True(t, x.MethodFive())706 // SwitchStmt707 switch tmp {708 case tmp:709 L()710 }711 // TypeSwitchStmt712 func(i interface{}) {713 switch i.(type) {714 case string:715 M()716 }717 }(tmp)718 // miscellany719 n := []string{720 N(),721 }722 o := &Example{723 o: O(),724 }725 p := &Example{726 P(),727 }728 // ReturnStmt729 return Q()730 }731 `732 //analyzer.calledFuncs.Add("make")733 expected := set.New(734 "A",735 "B",736 "C",737 "D",738 "E",739 "F",740 "G",741 "H",742 "I",743 "J",744 "K",745 "L",746 "M",747 "N",748 "O",749 "P",750 "Q",751 "init",752 "make",753 "Example.MethodOne",754 "Example.MethodTwo",755 "Example.MethodThree",756 "Example.MethodFour",757 "Example.MethodFive",758 )759 p := parseChunkOfCode(t, codeSample)760 for _, input := range p.Decls[1].(*ast.FuncDecl).Body.List {761 analyzer.parseStmt(input)762 }763 diff := set.StringSlice(set.Difference(expected, analyzer.calledFuncs))764 assert.Empty(t, diff, "diff should be empty")765 assert.Equal(t, expected, analyzer.calledFuncs, "actual output does not match expected output")766}767func TestGetDeclaredNames(t *testing.T) {768 t.Run("simple", func(_t *testing.T) {769 analyzer := NewAnalyzer()770 in, err := parser.ParseFile(token.NewFileSet(), "../example_packages/simple/main.go", nil, parser.AllErrors)771 if err != nil {772 t.Logf("failing because ParseFile returned error: %v", err)773 t.FailNow()774 }775 expected := map[string]BlanketFunc{776 "a": {Name: "a"},777 "b": {Name: "b"},778 "c": {Name: "c"},779 "wrapper": {Name: "wrapper"},780 }781 analyzer.getDeclaredNames(in)782 assert.Equal(t, expected, analyzer.declaredFuncInfo, "expected output did not match actual output")783 })784 t.Run("methods", func(_t *testing.T) {785 analyzer := NewAnalyzer()786 in, err := parser.ParseFile(token.NewFileSet(), "../example_packages/methods/main.go", nil, parser.AllErrors)787 if err != nil {788 t.Logf("failing because ParseFile returned error: %v", err)789 t.FailNow()790 }791 expected := map[string]BlanketFunc{792 "example.A": {Name: "example.A"},793 "example.B": {Name: "example.B"},794 "example.C": {Name: "example.C"},795 "example.D": {Name: "example.D"},796 "example.E": {Name: "example.E"},797 "example.F": {Name: "example.F"},798 "wrapper": {Name: "wrapper"},799 }800 analyzer.getDeclaredNames(in)801 assert.Equal(t, expected, analyzer.declaredFuncInfo, "expected output did not match actual output")802 })803}804func TestGetCalledNames(t *testing.T) {805 t.Run("simple", func(_t *testing.T) {806 analyzer := NewAnalyzer()807 in, err := parser.ParseFile(token.NewFileSet(), "../example_packages/simple/main_test.go", nil, parser.AllErrors)808 if err != nil {809 t.Logf("failing because ParseFile returned error: %v", err)810 t.FailNow()811 }812 expectedDeclarations := []string{"a", "c", "wrapper", "init"}813 expected := set.New()814 for _, x := range expectedDeclarations {815 expected.Add(x)816 }817 analyzer.getCalledNames(in)818 assert.Equal(t, expected, analyzer.calledFuncs, "expected output did not match actual output")819 })820 t.Run("methods", func(_t *testing.T) {821 analyzer := NewAnalyzer()822 analyzer.helperFunctionReturnMap["helperGenerator"] = []string{"example", "error"}823 in, err := parser.ParseFile(token.NewFileSet(), "../example_packages/methods/main_test.go", nil, parser.AllErrors)824 if err != nil {825 t.Logf("failing because ParseFile returned error: %v", err)826 t.FailNow()827 }828 expected := set.New(829 "example.A",830 "example.B",831 "example.C",832 "helperGenerator",833 "example.D",834 "example.E",835 "wrapper",836 "init",837 )838 analyzer.getCalledNames(in)839 use := func(...interface{}) {}840 x := analyzer.calledFuncs.List()841 use(x)842 assert.Equal(t, expected, analyzer.calledFuncs, "expected output did not match actual output")843 })844}845func TestFindHelperFuncs(t *testing.T) {846 analyzer := NewAnalyzer()847 in, err := parser.ParseFile(token.NewFileSet(), "../example_packages/methods/main_test.go", nil, parser.AllErrors)848 if err != nil {849 t.Logf("failing because ParseFile returned error: %v", err)850 t.FailNow()851 }852 expected := map[string][]string{853 "helperGenerator": {854 "example",855 "error",856 },857 }858 analyzer.findHelperFuncs(in)859 assert.Equal(t, expected, analyzer.helperFunctionReturnMap, "expected output did not match actual output")860}861func TestAnalyze(t *testing.T) {...

Full Screen

Full Screen

parser_test.go

Source:parser_test.go Github

copy

Full Screen

1// Copyright 2009 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 parser5import (6 "go/token"7 "os"8 "testing"9)10var fset = token.NewFileSet()11var illegalInputs = []interface{}{12 nil,13 3.14,14 []byte(nil),15 "foo!",16}17func TestParseIllegalInputs(t *testing.T) {18 for _, src := range illegalInputs {19 _, err := ParseFile(fset, "", src, 0)20 if err == nil {21 t.Errorf("ParseFile(%v) should have failed", src)22 }23 }24}25var validPrograms = []interface{}{26 "package main\n",27 `package main;`,28 `package main; import "fmt"; func main() { fmt.Println("Hello, World!") };`,29 `package main; func main() { if f(T{}) {} };`,30 `package main; func main() { _ = (<-chan int)(x) };`,31 `package main; func main() { _ = (<-chan <-chan int)(x) };`,32 `package main; func f(func() func() func());`,33 `package main; func f(...T);`,34 `package main; func f(float, ...int);`,35 `package main; func f(x int, a ...int) { f(0, a...); f(1, a...,) };`,36 `package main; type T []int; var a []bool; func f() { if a[T{42}[0]] {} };`,37 `package main; type T []int; func g(int) bool { return true }; func f() { if g(T{42}[0]) {} };`,38 `package main; type T []int; func f() { for _ = range []int{T{42}[0]} {} };`,39 `package main; var a = T{{1, 2}, {3, 4}}`,40}41func TestParseValidPrograms(t *testing.T) {42 for _, src := range validPrograms {43 _, err := ParseFile(fset, "", src, 0)44 if err != nil {45 t.Errorf("ParseFile(%q): %v", src, err)46 }47 }48}49var validFiles = []string{50 "parser.go",51 "parser_test.go",52}53func TestParse3(t *testing.T) {54 for _, filename := range validFiles {55 _, err := ParseFile(fset, filename, nil, 0)56 if err != nil {57 t.Errorf("ParseFile(%s): %v", filename, err)58 }59 }60}61func nameFilter(filename string) bool {62 switch filename {63 case "parser.go":64 case "interface.go":65 case "parser_test.go":66 default:67 return false68 }69 return true70}71func dirFilter(f *os.FileInfo) bool { return nameFilter(f.Name) }72func TestParse4(t *testing.T) {73 path := "."74 pkgs, err := ParseDir(fset, path, dirFilter, 0)75 if err != nil {76 t.Fatalf("ParseDir(%s): %v", path, err)77 }78 if len(pkgs) != 1 {79 t.Errorf("incorrect number of packages: %d", len(pkgs))80 }81 pkg := pkgs["parser"]82 if pkg == nil {83 t.Errorf(`package "parser" not found`)84 return85 }86 for filename := range pkg.Files {87 if !nameFilter(filename) {88 t.Errorf("unexpected package file: %s", filename)89 }90 }91}...

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World")4 parse.Parse()5}6import (7func Parse() {8 fmt.Println("Parse")9}

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 mylib.Parse()4}5import (6func main() {7 mylib.Parse()8}9import (10func main() {11 mylib.Parse()12}13import (14func main() {15 mylib.Parse()16}17import (18func main() {19 mylib.Parse()20}21import (22func main() {23 mylib.Parse()24}25import (26func main() {27 mylib.Parse()28}29import (30func main() {31 mylib.Parse()32}33import (34func main() {35 mylib.Parse()36}37import (38func main() {39 mylib.Parse()40}41import (42func main() {43 mylib.Parse()44}45import (

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, playground")4 a := new(A)5 a.parse()6}7import "fmt"8type A struct {9}10func (a *A) parse() {11 fmt.Println("Hello, playground")12}

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("main method")4 x := parse.Parse()5 fmt.Println(x)6}7import (8func main() {9 fmt.Println("main method")10 x := parse.Parse()11 fmt.Println(x)12}13import "fmt"14func Parse() string {15 fmt.Println("parse method")16}17I am getting this error because I am not using the init() method in any of the files. But I want to run the program without using the init() method. Can anyone help me with this?

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(parse.Parse())4}5parse.Parse()6import (7func main() {8 fmt.Println(parse.Parse())9}10parse.Parse()11import (12func main() {13 fmt.Println(parse.Parse())14}15parse.Parse()16import (

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