github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/gormgen/internal/generate/test.go (about)

     1  package generate
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/unionj-cloud/go-doudou/v2/toolkit/gormgen/internal/parser"
     9  )
    10  
    11  //GetTestParamInTmpl return param list
    12  func (m *InterfaceMethod) GetTestParamInTmpl() string {
    13  	return testParamToString(m.Params)
    14  }
    15  
    16  // GetTestResultParamInTmpl return result list
    17  func (m *InterfaceMethod) GetTestResultParamInTmpl() string {
    18  	var res []string
    19  	for i := range m.Result {
    20  		tmplString := fmt.Sprintf("res%d", i+1)
    21  		res = append(res, tmplString)
    22  	}
    23  	return strings.Join(res, ",")
    24  }
    25  
    26  // testParamToString param list to string used in tmpl
    27  func testParamToString(params []parser.Param) string {
    28  	var res []string
    29  	for i, param := range params {
    30  		// TODO manage array and pointer
    31  		typ := param.Type
    32  		if param.Package != "" {
    33  			typ = param.Package + "." + typ
    34  		}
    35  		if param.IsArray {
    36  			typ = "[]" + typ
    37  		}
    38  		if param.IsPointer {
    39  			typ = "*" + typ
    40  		}
    41  		res = append(res, fmt.Sprintf("tt.Input.Args[%d].(%s)", i, typ))
    42  	}
    43  	return strings.Join(res, ",")
    44  }
    45  
    46  // GetAssertInTmpl assert in diy test
    47  func (m *InterfaceMethod) GetAssertInTmpl() string {
    48  	var res []string
    49  	for i := range m.Result {
    50  		tmplString := fmt.Sprintf("assert(t, %v, res%d, tt.Expectation.Ret[%d])", strconv.Quote(m.MethodName), i+1, i)
    51  		res = append(res, tmplString)
    52  	}
    53  	return strings.Join(res, "\n")
    54  }