| 43 | } |
| 44 | |
| 45 | func TestClosureCollection(t *testing.T) { |
| 46 | t.Parallel() |
| 47 | |
| 48 | testdata := analysistest.TestData() |
| 49 | |
| 50 | r := analysistest.Run(t, testdata, Analyzer, "go.uber.org/anonymousfunc") |
| 51 | require.Equal(t, 1, len(r)) |
| 52 | require.NotNil(t, r[0]) |
| 53 | |
| 54 | pass, result := r[0].Pass, r[0].Result |
| 55 | require.IsType(t, &analysishelper.Result[map[*ast.FuncLit]*FuncLitInfo]{}, result) |
| 56 | |
| 57 | // Iterate over the result of the anonymous function analyzer to see if there |
| 58 | // is a missmatch between the expected result written in the test file with the |
| 59 | // collected result from the analyzer. |
| 60 | funcLitMap := result.(*analysishelper.Result[map[*ast.FuncLit]*FuncLitInfo]).Res |
| 61 | require.NotZero(t, len(funcLitMap)) |
| 62 | |
| 63 | // Get the expected closure vars from comments written for each function literal in the test file. |
| 64 | // FindExpectedValues inspects test files and gathers comment strings at the same line of the |
| 65 | // *ast.FuncLit nodes, so that we know which *ast.FuncLit node corresponds to which anonymous |
| 66 | // function comment in the source. |
| 67 | expectedValues := nilawaytest.FindExpectedValues(pass, _wantClosurePrefix) |
| 68 | require.Equal(t, len(expectedValues), len(funcLitMap)) |
| 69 | |
| 70 | funcLitExpectedClosure := make(map[*ast.FuncLit][]string) |
| 71 | for node, closureVars := range expectedValues { |
| 72 | if funcLit, ok := node.(*ast.FuncLit); ok { |
| 73 | funcLitExpectedClosure[funcLit] = closureVars |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | for funcLit, expectedVars := range funcLitExpectedClosure { |
| 78 | info, ok := funcLitMap[funcLit] |
| 79 | require.True(t, ok) |
| 80 | |
| 81 | // Check if the expected closure vars are collected. We intentionally omit the default |
| 82 | // length for `resultVars` to avoid comparing nil slices against empty slices. |
| 83 | var resultVars []string |
| 84 | for _, closureVar := range info.ClosureVars { |
| 85 | resultVars = append(resultVars, closureVar.Ident.Name) |
| 86 | require.NotNil(t, closureVar.Obj) |
| 87 | } |
| 88 | pos := pass.Fset.Position(funcLit.Pos()) |
| 89 | require.Equal(t, expectedVars, resultVars, |
| 90 | "closure mismatch at %s:%d:%d", pos.Filename, pos.Line, pos.Column, |
| 91 | ) |
| 92 | |
| 93 | // Check if the fake func decl nodes are properly created. |
| 94 | require.NotNil(t, info.FakeFuncDecl) |
| 95 | // The fake func name should be an illegal identifier to avoid collisions, it should |
| 96 | // also be unexported just to avoid accidental uses of such variables in the rest of |
| 97 | // the system. |
| 98 | require.True(t, strings.HasPrefix(info.FakeFuncDecl.Name.Name, _fakeFuncDeclPrefix)) |
| 99 | require.False(t, token.IsIdentifier(info.FakeFuncDecl.Name.Name)) |
| 100 | require.False(t, token.IsExported(info.FakeFuncDecl.Name.Name)) |
| 101 | |
| 102 | // The func lit body and the fake func decl body should be shared. |