-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_test.go
69 lines (66 loc) · 1.6 KB
/
function_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
package enki
import (
"go/format"
"testing"
"github.com/stretchr/testify/require"
)
func TestFunction(t *testing.T) {
type args struct {
name string
receiver string
params []string
returns []string
body []Statement
}
tests := []struct {
name string
def args
want string
}{
{
name: "method",
def: args{
name: "SomeFunc",
receiver: "s SomeStruct",
params: []string{"a int", "b string"},
returns: []string{"res interface{}", "err error"},
body: []Statement{
Stmt().Line("return fmt.Sprint(a) + b, nil"),
},
},
want: "func (s SomeStruct) SomeFunc(a int, b string) (res interface{}, err error) {\nreturn fmt.Sprint(a) + b, nil\n}\n",
},
{
name: "function",
def: args{
name: "SomeFunc",
params: []string{"a int", "b string"},
returns: []string{"error"},
body: []Statement{
Stmt().Line("fmt.Print(b)"),
Stmt().Line("return nil"),
},
},
want: "func SomeFunc(a int, b string) error {\nfmt.Print(b)\nreturn nil\n}\n",
},
{
name: "function def",
def: args{
name: "SomeFunc",
params: []string{"a int", "b string"},
returns: []string{"error"},
},
want: "func SomeFunc(a int, b string) error\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
builder := M(tt.def.name).Receiver(tt.def.receiver).Params(tt.def.params...).Body(tt.def.body...).Returns(tt.def.returns...)
got := builder.materialize()
require.Equal(t, tt.want, got)
source, err := format.Source([]byte(got))
require.NoError(t, err)
t.Log("\n" + string(source))
})
}
}