forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
54 lines (48 loc) · 1.27 KB
/
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
package generate
import (
"fmt"
"strconv"
"strings"
"github.com/JasonTangJun/gen/internalopen/parser"
)
//GetTestParamInTmpl return param list
func (m *InterfaceMethod) GetTestParamInTmpl() string {
return testParamToString(m.Params)
}
// GetTestResultParamInTmpl return result list
func (m *InterfaceMethod) GetTestResultParamInTmpl() string {
var res []string
for i := range m.Result {
tmplString := fmt.Sprintf("res%d", i+1)
res = append(res, tmplString)
}
return strings.Join(res, ",")
}
// testParamToString param list to string used in tmpl
func testParamToString(params []parser.Param) string {
var res []string
for i, param := range params {
// TODO manage array and pointer
typ := param.Type
if param.Package != "" {
typ = param.Package + "." + typ
}
if param.IsArray {
typ = "[]" + typ
}
if param.IsPointer {
typ = "*" + typ
}
res = append(res, fmt.Sprintf("tt.Input.Args[%d].(%s)", i, typ))
}
return strings.Join(res, ",")
}
// GetAssertInTmpl assert in diy test
func (m *InterfaceMethod) GetAssertInTmpl() string {
var res []string
for i := range m.Result {
tmplString := fmt.Sprintf("assert(t, %v, res%d, tt.Expectation.Ret[%d])", strconv.Quote(m.MethodName), i+1, i)
res = append(res, tmplString)
}
return strings.Join(res, "\n")
}