-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate_test.go
85 lines (78 loc) · 2.04 KB
/
template_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
package mokku
import (
"testing"
)
func TestTemplate(t *testing.T) {
const templateStr = `
type {{.TypeName}}Mock struct { {{ range .Methods }}
{{.Name}}Func func{{.Signature}}{{ end }}
}
{{if .Methods }}{{$typeName := .TypeName}}
{{range $val := .Methods}}func (m *{{$typeName}}Mock) {{$val.Name}}{{$val.Signature}} {
if m.{{$val.Name}}Func == nil {
panic("unexpected call to {{$val.Name}}")
}
{{if $val.HasReturn}}return {{ end }}m.{{$val.Name}}Func{{$val.OrderedParams}}
}
{{ end }}{{ end }}`
for _, tc := range []struct {
name string
in *targetInterface
exp string
}{
{
name: "basic case",
exp: `
type Mock struct {
}
`,
in: &targetInterface{},
},
{
name: "advanced case",
exp: `
type FooBarMock struct {
ActFunc func( ) error
DoStuffFunc func( a , b string, other ... interface{} ) ( int , error )
NoReturnParamFunc func( a string )
}
func (m *FooBarMock) Act( ) error {
if m.ActFunc == nil {
panic("unexpected call to Act")
}
return m.ActFunc( )
}
func (m *FooBarMock) DoStuff( a , b string, other ... interface{} ) ( int , error ) {
if m.DoStuffFunc == nil {
panic("unexpected call to DoStuff")
}
return m.DoStuffFunc( a , b , other ... )
}
func (m *FooBarMock) NoReturnParam( a string ) {
if m.NoReturnParamFunc == nil {
panic("unexpected call to NoReturnParam")
}
m.NoReturnParamFunc( a )
}
`,
in: &targetInterface{
TypeName: "FooBar",
Methods: []*method{
{Name: "Act", Signature: "( ) error", OrderedParams: "( )", HasReturn: true},
{Name: "DoStuff", Signature: "( a , b string, other ... interface{} ) ( int , error )", OrderedParams: "( a , b , other ... )", HasReturn: true},
{Name: "NoReturnParam", Signature: "( a string )", OrderedParams: "( a )", HasReturn: false},
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
b, err := mockFromTemplate(tc.in, templateStr)
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
if got := string(b); got != tc.exp {
t.Errorf("got different output than what was expected:\n%s", got)
}
})
}
}