MCPcopy
hub / github.com/uber-go/nilaway / TestClosureCollection

Function TestClosureCollection

assertion/anonymousfunc/analyzer_test.go:45–131  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

43}
44
45func 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.

Callers

nothing calls this directly

Calls 10

FindExpectedValuesFunction · 0.92
ExportedMethod · 0.80
RunMethod · 0.65
PositionMethod · 0.65
EqualMethod · 0.45
NotNilMethod · 0.45
TrueMethod · 0.45
PosMethod · 0.45
FalseMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected