1 Star 0 Fork 0

zhuchance / kubernetes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
test_finder.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
package convert
import (
"go/ast"
"regexp"
)
/*
* Given a root node, walks its top level statements and returns
* points to function nodes to rewrite as It statements.
* These functions, according to Go testing convention, must be named
* TestWithCamelCasedName and receive a single *testing.T argument.
*/
func findTestFuncs(rootNode *ast.File) (testsToRewrite []*ast.FuncDecl) {
testNameRegexp := regexp.MustCompile("^Test[0-9A-Z].+")
ast.Inspect(rootNode, func(node ast.Node) bool {
if node == nil {
return false
}
switch node := node.(type) {
case *ast.FuncDecl:
matches := testNameRegexp.MatchString(node.Name.Name)
if matches && receivesTestingT(node) {
testsToRewrite = append(testsToRewrite, node)
}
}
return true
})
return
}
/*
* convenience function that looks at args to a function and determines if its
* params include an argument of type *testing.T
*/
func receivesTestingT(node *ast.FuncDecl) bool {
if len(node.Type.Params.List) != 1 {
return false
}
base, ok := node.Type.Params.List[0].Type.(*ast.StarExpr)
if !ok {
return false
}
intermediate := base.X.(*ast.SelectorExpr)
isTestingPackage := intermediate.X.(*ast.Ident).Name == "testing"
isTestingT := intermediate.Sel.Name == "T"
return isTestingPackage && isTestingT
}
Go
1
https://gitee.com/meoom/kubernetes.git
git@gitee.com:meoom/kubernetes.git
meoom
kubernetes
kubernetes
v1.2.0-alpha.7

搜索帮助