-
Notifications
You must be signed in to change notification settings - Fork 11
/
helpers.go
86 lines (75 loc) · 1.96 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package gounit
import (
"bytes"
"go/ast"
"go/printer"
"go/token"
"strings"
"text/template"
)
type matchFunc func(*ast.FuncDecl) bool
//findMissingTests filters funcs slice and returns only those functions that don't have tests yet
func findMissingTests(file *ast.File, funcs []*Func) []*Func {
tests := findFunctions(file.Decls, func(fd *ast.FuncDecl) bool {
for _, sourceFunc := range funcs {
f := NewFunc(fd)
if f.ReceiverType() == nil && f.Name() == sourceFunc.TestName() {
return true
}
}
return false
})
dontHaveTests := []*Func{}
for _, f := range funcs {
testIsFound := false
for _, test := range tests {
if test.Name() == f.TestName() {
testIsFound = true
break
}
}
if !testIsFound {
dontHaveTests = append(dontHaveTests, f)
}
}
return dontHaveTests
}
//findFunctions finds all matching function declarations
func findFunctions(decls []ast.Decl, match matchFunc) []*Func {
var funcs []*Func
for _, decl := range decls {
if fd, ok := decl.(*ast.FuncDecl); ok && match(fd) {
funcs = append(funcs, NewFunc(fd))
}
}
return funcs
}
//nodeToString returns a string representation of an AST node
//as it has in the original source code
func nodeToString(fs *token.FileSet, n ast.Node) string {
b := bytes.NewBuffer([]byte{})
printer.Fprint(b, fs, n)
return b.String()
}
//templateHelpers return FuncMap of template helpers to use within a template
func templateHelpers(fs *token.FileSet) template.FuncMap {
return template.FuncMap{
"ast": func(n ast.Node) string {
return nodeToString(fs, n)
},
"join": strings.Join,
"params": func(f *Func) []string {
return f.Params(fs)
},
"results": func(f *Func) []string {
return f.Results(fs)
},
"receiver": func(f *Func) string {
if f.ReceiverType() == nil {
return ""
}
return strings.Replace(nodeToString(fs, f.ReceiverType()), "*", "", -1) + "."
},
"want": func(s string) string { return strings.Replace(s, "got", "want", 1) },
}
}