Skip to content

Instantly share code, notes, and snippets.

@k-nishijima
Created February 17, 2017 08:49
Show Gist options
  • Save k-nishijima/dc148acc22565f617582d20c0dc5ec88 to your computer and use it in GitHub Desktop.
Save k-nishijima/dc148acc22565f617582d20c0dc5ec88 to your computer and use it in GitHub Desktop.
ファイル名渡してテストメソッドをまとめるテストメソッドを出力する
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"strings"
)
func main() {
arg := os.Args[1]
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, arg, nil, parser.Mode(0))
fmt.Println(`func TestAllMethodInThisFile(t *testing.T) {`)
for _, decl := range f.Decls {
switch td := decl.(type) {
case *ast.FuncDecl:
if strings.HasPrefix(td.Name.String(), "Test") {
fmt.Printf(" %v(t)\n", td.Name)
}
}
fmt.Println()
}
fmt.Println(`}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment