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)
| 130 | // analyzeCode takes the types scope and the docs and returns the import |
| 131 | // information and information about all the assertion functions. |
| 132 | func 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 |
| 176 | func parsePackageSource(pkg string) (*types.Scope, *doc.Package, error) { |
no test coverage detected