MCPcopy Create free account
hub / github.com/stretchr/testify / analyzeCode

Function analyzeCode

_codegen/main.go:132–173  ·  view source on GitHub ↗

analyzeCode takes the types scope and the docs and returns the import information and information about all the assertion functions.

(scope *types.Scope, docs *doc.Package)

Source from the content-addressed store, hash-verified

130// analyzeCode takes the types scope and the docs and returns the import
131// information and information about all the assertion functions.
132func analyzeCode(scope *types.Scope, docs *doc.Package) (imports.Importer, []testFunc, error) {
133 testingT := scope.Lookup("TestingT").Type().Underlying().(*types.Interface)
134
135 importer := imports.New(*outputPkg)
136 var funcs []testFunc
137 // Go through all the top level functions
138 for _, fdocs := range docs.Funcs {
139 // Find the function
140 obj := scope.Lookup(fdocs.Name)
141
142 fn, ok := obj.(*types.Func)
143 if !ok {
144 continue
145 }
146 // Check function signature has at least two arguments
147 sig := fn.Type().(*types.Signature)
148 if sig.Params().Len() < 2 {
149 continue
150 }
151 // Check first argument is of type testingT
152 first, ok := sig.Params().At(0).Type().(*types.Named)
153 if !ok {
154 continue
155 }
156 firstType, ok := first.Underlying().(*types.Interface)
157 if !ok {
158 continue
159 }
160 if !types.Implements(firstType, testingT) {
161 continue
162 }
163
164 // Skip functions ending with f
165 if strings.HasSuffix(fdocs.Name, "f") && !*includeF {
166 continue
167 }
168
169 funcs = append(funcs, testFunc{*outputPkg, fdocs, fn})
170 importer.AddImportsFrom(sig.Params())
171 }
172 return importer, funcs, nil
173}
174
175// parsePackageSource returns the types scope and the package documentation from the package
176func parsePackageSource(pkg string) (*types.Scope, *doc.Package, error) {

Callers 1

mainFunction · 0.85

Calls 3

ParamsMethod · 0.80
LenMethod · 0.45
ImplementsMethod · 0.45

Tested by

no test coverage detected