-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate_test.go
103 lines (89 loc) · 1.91 KB
/
generate_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package ifacecodegen
import (
"strings"
"testing"
)
func TestGenerate(t *testing.T) {
pkg := &Package{
Name: "foo",
Interfaces: []*Interface{
&Interface{
Name: "Service",
Methods: []*Method{
&Method{
Name: "Foo",
In: []*Parameter{
&Parameter{
Name: "param1",
Type: TypeBuiltin("string"),
},
},
Out: []*Parameter{
&Parameter{
Name: "result1",
Type: &TypeExported{
Package: "foo",
Type: TypeBuiltin("Bar"),
},
},
},
},
},
},
},
}
opts := GenerateOptions{
Package: pkg,
Imports: []*Import{
&Import{
Package: "github.com/bar/foo",
Path: "foo",
},
},
Meta: map[string]string{
"service": "account",
},
Template: strings.NewReader(`
type logger{{ .Name }} struct {
s {{ .Name }}
}
{{ $ifaceRef := . }}
{{ range .Methods }}
func (w *logger{{ $ifaceRef.Name }}) {{ .Name }}({{ input_parameters . }}) {{ $methodRef := . }}{{ output_parameters . }} {
defer func(begin time.Time) {
var err error {{ if ne (output_var_error .) "" -}}
= {{ output_var_error . }}
{{- end }}
log.Print("service", "{{ meta "service" }}", "took", time.Since(begin), "error", err)
}(time.Now())
{{ return . }} w.s.{{ .Name }}({{ input_calls . }})
}
{{ end }}
`),
}
content, err := Generate(opts)
if err != nil {
t.Fatalf("Error should be nil, actual %v", err)
}
expected := `// Code generated by ifacecodegen tool. DO NOT EDIT.
// Source:
package foo
import (
"log"
"time"
)
type loggerService struct {
s Service
}
func (w *loggerService) Foo(param1 string) (result1 Bar) {
defer func(begin time.Time) {
var err error
log.Print("service", "account", "took", time.Since(begin), "error", err)
}(time.Now())
return w.s.Foo(param1)
}
`
if actual := string(content); actual != expected {
t.Errorf("expected '%s', actual '%s'", expected, actual)
}
}