-
Notifications
You must be signed in to change notification settings - Fork 10
/
template.go
334 lines (285 loc) · 12.1 KB
/
template.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"bytes"
"fmt"
"text/template"
"golang.org/x/tools/imports"
)
const sourceTemplate = `// generated by "{{.CommandLine}}". DO NOT EDIT.
package {{.PackageName}}
{{if .NeedsReflect}}import "reflect"{{end}}
{{range .Imports}}import {{if .Alias}}{{.Alias}} {{end}}{{.Path}}
{{end}}
{{range $i := .Interfaces}}{{range .Methods}}
// {{.Interface}}{{.Name}}Invocation represents a single call of Fake{{.Interface}}.{{.Name}}
type {{.Interface}}{{.Name}}Invocation struct {
{{if .Parameters}} Parameters struct {
{{range .Parameters}} {{.FieldFormat}}
{{end}}
}{{end}}
{{if .Results}} Results struct {
{{range .Results}} {{.FieldFormat}}
{{end}}
}{{end}}
}
{{if and .Parameters .Results}}
// New{{.Interface}}{{.Name}}Invocation creates a new instance of {{.Interface}}{{.Name}}Invocation
func New{{.Interface}}{{.Name}}Invocation({{range .Parameters}}{{.Name}} {{.ValueType.FieldFormat}}, {{end}}{{.ResultsDeclaration}}) *{{.Interface}}{{.Name}}Invocation {
invocation := new({{.Interface}}{{.Name}}Invocation)
{{range .Parameters}} invocation.Parameters.{{.TitleCase}} = {{.Name}}
{{end}}
{{range .Results}}invocation.Results.{{.TitleCase}} = {{.Name}}
{{end}}
return invocation
}{{end}}
{{end}}{{/* end range .Methods */}}
// {{.Name}}TestingT represents the methods of "testing".T used by charlatan Fakes. It avoids importing the testing package.
type {{.Name}}TestingT interface {
Error(...interface{})
Errorf(string, ...interface{})
Fatal(...interface{})
Helper()
}
/*
Fake{{.Name}} is a mock implementation of {{.Name}} for testing.
{{if .Methods}}{{with $m := index .Methods 0}}Use it in your tests as in this example:
package example
func TestWith{{$m.Interface}}(t *testing.T) {
f := &{{$.PackageName}}.Fake{{$m.Interface}}{
{{$m.Name}}Hook: func({{$m.ParametersDeclaration}}) ({{$m.ResultsDeclaration}}) {
// ensure parameters meet expectations, signal errors using t, etc
return
},
}
// test code goes here ...
// assert state of Fake{{.Name}} ...
f.Assert{{$m.Name}}CalledOnce(t)
}
Create anonymous function implementations for only those interface methods that
should be called in the code under test. This will force a panic if any
unexpected calls are made to Fake{{.Name}}.
{{end}}{{end}}*/
type Fake{{.Name}} struct {
{{range .Methods}} {{.Name}}Hook func({{.ParametersSignature}}) ({{.ResultsSignature}})
{{end}}
{{range .Methods}} {{.Name}}Calls []*{{.Interface}}{{.Name}}Invocation
{{end}}}
// NewFake{{.Name}}DefaultPanic returns an instance of Fake{{.Name}} with all hooks configured to panic
func NewFake{{.Name}}DefaultPanic() *Fake{{.Name}} {
return &Fake{{.Name}}{
{{range .Methods}} {{.Name}}Hook: func({{.ParametersSignature}}) ({{.ResultsDeclaration}}) {
panic("Unexpected call to {{.Interface}}.{{.Name}}")
},
{{end}}
}
}
// NewFake{{$i.Name}}DefaultFatal returns an instance of Fake{{$i.Name}} with all hooks configured to call t.Fatal
{{with $sym := gensym}}func NewFake{{$i.Name}}DefaultFatal(t{{$sym}} {{$i.Name}}TestingT) *Fake{{$i.Name}} {
return &Fake{{$i.Name}}{
{{range $i.Methods}} {{.Name}}Hook: func({{.ParametersSignature}}) ({{.ResultsDeclaration}}) {
t{{$sym}}.Fatal("Unexpected call to {{.Interface}}.{{.Name}}")
return
},
{{end}}
}
}{{end}}
// NewFake{{$i.Name}}DefaultError returns an instance of Fake{{$i.Name}} with all hooks configured to call t.Error
{{with $sym := gensym}}func NewFake{{$i.Name}}DefaultError(t{{$sym}} {{$i.Name}}TestingT) *Fake{{$i.Name}} {
return &Fake{{$i.Name}}{
{{range $i.Methods}} {{.Name}}Hook: func({{.ParametersSignature}}) ({{.ResultsDeclaration}}) {
t{{$sym}}.Error("Unexpected call to {{.Interface}}.{{.Name}}")
return
},
{{end}}
}
}{{end}}
func (f *Fake{{.Name}}) Reset() {
{{range .Methods}} f.{{.Name}}Calls = []*{{.Interface}}{{.Name}}Invocation{}
{{end}}}
{{range $m := .Methods}}
{{with $sym := gensym}}func (f{{$sym}} *Fake{{$m.Interface}}) {{$m.Name}}({{$m.ParametersDeclaration}}) ({{$m.ResultsDeclaration}}) {
if f{{$sym}}.{{$m.Name}}Hook == nil {
panic("{{$m.Interface}}.{{$m.Name}}() called but Fake{{$m.Interface}}.{{$m.Name}}Hook is nil")
}
invocation{{$sym}} := new({{$m.Interface}}{{$m.Name}}Invocation)
f{{$sym}}.{{$m.Name}}Calls = append(f{{$sym}}.{{$m.Name}}Calls, invocation{{$sym}})
{{if $m.Parameters}}{{range $m.Parameters}} invocation{{$sym}}.Parameters.{{.TitleCase}} = {{.Name}}
{{end}}{{end}}
{{if $m.Results}} {{$m.ResultsReference}} = f{{$sym}}.{{$m.Name}}Hook({{$m.ParametersReference}})
{{else}} f{{$sym}}.{{$m.Name}}Hook({{$m.ParametersReference}})
{{end}}
{{if $m.Results}}{{range $m.Results}}invocation{{$sym}}.Results.{{.TitleCase}} = {{.Name}}
{{end}}{{end}}
return
}{{end}}
{{if .Results}}
// Set{{.Name}}Stub configures {{.Interface}}.{{.Name}} to always return the given values
{{with $sym := gensym}}func (f{{$sym}} *Fake{{$m.Interface}}) Set{{$m.Name}}Stub({{$m.ResultsDeclaration}}) {
f{{$sym}}.{{$m.Name}}Hook = func({{$m.ParametersSignature}}) ({{$m.ResultsSignature}}) {
return {{range $idx, $r := $m.Results}}{{if $idx}}, {{end}}{{$r.Name}}{{end}}
}
}{{end}}{{end}}{{/* end if .Results */}}
{{if and .Parameters .Results}}
// Set{{.Name}}Invocation configures {{.Interface}}.{{.Name}} to return the given results when called with the given parameters
// If no match is found for an invocation the result(s) of the fallback function are returned
{{with $sym := gensym}}func (f{{$sym}} *Fake{{$m.Interface}}) Set{{$m.Name}}Invocation(calls{{$sym}} []*{{$m.Interface}}{{$m.Name}}Invocation, fallback{{$sym}} func() ({{$m.ResultsSignature}})) {
f{{$sym}}.{{$m.Name}}Hook = func({{$m.ParametersDeclaration}}) ({{$m.ResultsDeclaration}}) {
for _, call{{$sym}} := range calls{{$sym}} {
if {{range $idx, $p := $m.Parameters}}{{if $idx}} && {{end}}reflect.DeepEqual(call{{$sym}}.Parameters.{{$p.TitleCase}}, {{$p.Name}}){{end}} {
{{range $m.Results}}{{.Name}} = call{{$sym}}.Results.{{.TitleCase}}
{{end}}
return
}
}
return fallback{{$sym}}()
}
}{{end}}{{end}}{{/* end if and .Parameters .Results */}}
// {{.Name}}Called returns true if Fake{{.Interface}}.{{.Name}} was called
func (f *Fake{{.Interface}}) {{.Name}}Called() bool {
return len(f.{{.Name}}Calls) != 0
}
// Assert{{.Name}}Called calls t.Error if Fake{{.Interface}}.{{.Name}} was not called
func (f *Fake{{.Interface}}) Assert{{.Name}}Called(t {{.Interface}}TestingT) {
t.Helper()
if len(f.{{.Name}}Calls) == 0 {
t.Error("Fake{{.Interface}}.{{.Name}} not called, expected at least one")
}
}
// {{.Name}}NotCalled returns true if Fake{{.Interface}}.{{.Name}} was not called
func (f *Fake{{.Interface}}) {{.Name}}NotCalled() bool {
return len(f.{{.Name}}Calls) == 0
}
// Assert{{.Name}}NotCalled calls t.Error if Fake{{.Interface}}.{{.Name}} was called
func (f *Fake{{.Interface}}) Assert{{.Name}}NotCalled(t {{.Interface}}TestingT) {
t.Helper()
if len(f.{{.Name}}Calls) != 0 {
t.Error("Fake{{.Interface}}.{{.Name}} called, expected none")
}
}
// {{.Name}}CalledOnce returns true if Fake{{.Interface}}.{{.Name}} was called exactly once
func (f *Fake{{.Interface}}) {{.Name}}CalledOnce() bool {
return len(f.{{.Name}}Calls) == 1
}
// Assert{{.Name}}CalledOnce calls t.Error if Fake{{.Interface}}.{{.Name}} was not called exactly once
func (f *Fake{{.Interface}}) Assert{{.Name}}CalledOnce(t {{.Interface}}TestingT) {
t.Helper()
if len(f.{{.Name}}Calls) != 1 {
t.Errorf("Fake{{.Interface}}.{{.Name}} called %d times, expected 1", len(f.{{.Name}}Calls))
}
}
// {{.Name}}CalledN returns true if Fake{{.Interface}}.{{.Name}} was called at least n times
func (f *Fake{{.Interface}}) {{.Name}}CalledN(n int) bool {
return len(f.{{.Name}}Calls) >= n
}
// Assert{{.Name}}CalledN calls t.Error if Fake{{.Interface}}.{{.Name}} was called less than n times
func (f *Fake{{.Interface}}) Assert{{.Name}}CalledN(t {{.Interface}}TestingT, n int) {
t.Helper()
if len(f.{{.Name}}Calls) < n {
t.Errorf("Fake{{.Interface}}.{{.Name}} called %d times, expected >= %d", len(f.{{.Name}}Calls), n)
}
}
{{if .Parameters}}// {{.Name}}CalledWith returns true if Fake{{.Interface}}.{{.Name}} was called with the given values
{{with $sym := gensym}}func (f{{$sym}} *Fake{{$m.Interface}}) {{$m.Name}}CalledWith({{$m.ParametersDeclaration}}) bool {
for _, call{{$sym}} := range f{{$sym}}.{{$m.Name}}Calls {
if {{range $idx, $p := $m.Parameters}}{{if $idx}} && {{end}}reflect.DeepEqual(call{{$sym}}.Parameters.{{$p.TitleCase}}, {{$p.Name}}){{end}} {
return true
}
}
return false
}{{end}}
// Assert{{.Name}}CalledWith calls t.Error if Fake{{.Interface}}.{{.Name}} was not called with the given values
{{with $sym := gensym}}func (f{{$sym}} *Fake{{$m.Interface}}) Assert{{$m.Name}}CalledWith(t {{$m.Interface}}TestingT, {{$m.ParametersDeclaration}}) {
t.Helper()
var found{{$sym}} bool
for _, call{{$sym}} := range f{{$sym}}.{{$m.Name}}Calls {
if {{range $idx, $p := $m.Parameters}}{{if $idx}} && {{end}}reflect.DeepEqual(call{{$sym}}.Parameters.{{$p.TitleCase}}, {{$p.Name}}){{end}} {
found{{$sym}} = true
break
}
}
if !found{{$sym}} {
t.Error("Fake{{$m.Interface}}.{{$m.Name}} not called with expected parameters")
}
}{{end}}
// {{.Name}}CalledOnceWith returns true if Fake{{.Interface}}.{{.Name}} was called exactly once with the given values
{{with $sym := gensym}}func (f{{$sym}} *Fake{{$m.Interface}}) {{$m.Name}}CalledOnceWith({{$m.ParametersDeclaration}}) bool {
var count{{$sym}} int
for _, call{{$sym}} := range f{{$sym}}.{{$m.Name}}Calls {
if {{range $idx, $p := $m.Parameters}}{{if $idx}} && {{end}}reflect.DeepEqual(call{{$sym}}.Parameters.{{$p.TitleCase}}, {{$p.Name}}){{end}} {
count{{$sym}}++
}
}
return count{{$sym}} == 1
}{{end}}
// Assert{{.Name}}CalledOnceWith calls t.Error if Fake{{.Interface}}.{{.Name}} was not called exactly once with the given values
{{with $sym := gensym}}func (f{{$sym}} *Fake{{$m.Interface}}) Assert{{$m.Name}}CalledOnceWith(t {{$m.Interface}}TestingT, {{$m.ParametersDeclaration}}) {
t.Helper()
var count{{$sym}} int
for _, call{{$sym}} := range f{{$sym}}.{{$m.Name}}Calls {
if {{range $idx, $p := $m.Parameters}}{{if $idx}} && {{end}}reflect.DeepEqual(call{{$sym}}.Parameters.{{$p.TitleCase}}, {{$p.Name}}){{end}} {
count{{$sym}}++
}
}
if count{{$sym}} != 1 {
t.Errorf("Fake{{$m.Interface}}.{{$m.Name}} called %d times with expected parameters, expected one", count{{$sym}})
}
}{{end}}
{{if len $m.Results }}
// {{.Name}}ResultsForCall returns the result values for the first call to Fake{{.Interface}}.{{.Name}} with the given values
{{with $sym := gensym}}func (f{{$sym}} *Fake{{$m.Interface}}) {{$m.Name}}ResultsForCall({{$m.ParametersDeclaration}}) ({{$m.ResultsDeclaration}}, found{{$sym}} bool) {
for _, call{{$sym}} := range f{{$sym}}.{{$m.Name}}Calls {
if {{range $idx, $p := $m.Parameters}}{{if $idx}} && {{end}}reflect.DeepEqual(call{{$sym}}.Parameters.{{$p.TitleCase}}, {{$p.Name}}){{end}} {
{{range $m.Results}}{{.Name}} = call{{$sym}}.Results.{{.TitleCase}}
{{end}}found{{$sym}} = true
break
}
}
return
}{{end}}{{end}}{{/* end if len $m.Results */}}
{{end}}{{/* end if .Parameters */}}
{{end}}{{/* end range $m := .Methods */}}
{{end}}{{/* end range .Interfaces */}}
`
var (
symGen = symbolGenerator{Prefix: "_sym"}
funky = template.FuncMap{"gensym": func() string { return symGen.next() }}
tmpl = template.Must(template.New("charlatan").Funcs(funky).Parse(sourceTemplate))
)
type charlatanTemplate struct {
CommandLine string
PackageName string
Imports []*Import
Interfaces []*Interface
}
func (t *charlatanTemplate) execute() ([]byte, error) {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, t); err != nil {
return nil, err
}
src, err := imports.Process("", buf.Bytes(), nil)
if err != nil {
// Should not happen except when developing this code.
// The user can compile the output to see the error.
return buf.Bytes(), fmt.Errorf("internal error: invalid code generated: %s", err)
}
return src, nil
}
func (t *charlatanTemplate) NeedsReflect() bool {
var needed bool
for _, intf := range t.Interfaces {
for _, mthd := range intf.Methods {
if len(mthd.Parameters) > 0 {
needed = true
}
}
}
if needed {
for _, imp := range t.Imports {
if "reflect" == imp.Name {
needed = false
break
}
}
}
return needed
}