-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_tb_test.go
88 lines (74 loc) · 1.92 KB
/
fake_tb_test.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
87
88
package faket
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"reflect"
"testing"
"github.com/prashantv/faket/internal/want"
)
// TestTBCoverage ensures faket implements all methods of testing.TB.
// Since fakeTB embeds testing.TB so it'll always implement the testing.TB
// interface, but unimplemented methods will panic if called.
// This test ensures each method is explicitly implemented.
func TestTBCoverage(t *testing.T) {
// We can't use reflection as fakeTB embeds testing.TB which causes reflect
// to include testing.TB methods under fakeTB.
ftMethods, err := findTypeMethods(".", reflect.TypeOf(fakeTB{}).Name())
want.NoErr(t, err)
ftSet := make(map[string]struct{})
for _, m := range ftMethods {
ftSet[m] = struct{}{}
}
tb := reflect.TypeOf((*testing.TB)(nil)).Elem()
for i := 0; i < tb.NumMethod(); i++ {
m := tb.Method(i)
if m.PkgPath != "" {
// unexported, ignore
continue
}
mn := m.Name
if _, ok := ftSet[mn]; !ok {
t.Errorf("faket missing testing.TB.%s", mn)
}
}
}
func findTypeMethods(path, typeName string) ([]string, error) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, path, nil, parser.SkipObjectResolution)
if err != nil {
return nil, err
}
var methods []string
for _, pkg := range pkgs {
for _, file := range pkg.Files {
for _, decl := range file.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok || funcDecl.Recv == nil {
continue
}
recv := funcDecl.Recv.List
if len(recv) == 0 {
continue
}
if receiverName(recv[0]) != typeName {
continue
}
methods = append(methods, funcDecl.Name.Name)
}
}
}
return methods, nil
}
func receiverName(f *ast.Field) string {
switch recv := f.Type.(type) {
case *ast.Ident:
return recv.Name
case *ast.StarExpr:
if ident, ok := recv.X.(*ast.Ident); ok {
return ident.Name
}
}
panic(fmt.Errorf("cannot parse type name from receiver: %#v", f.Type))
}