github.com/pubgo/xprocess@v0.1.11/xprocess_test/test.go (about)

     1  package xprocess_test
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/pubgo/xerror"
     8  	"github.com/pubgo/xprocess/xutil"
     9  )
    10  
    11  type Test = test
    12  
    13  type test struct {
    14  	fn     interface{}
    15  	params [][]interface{}
    16  }
    17  
    18  func (t *test) In(args ...interface{}) {
    19  	var params [][]interface{}
    20  	if len(t.params) == 0 {
    21  		for _, arg := range args {
    22  			params = append(params, []interface{}{arg})
    23  		}
    24  	} else {
    25  		for _, p := range t.params {
    26  			for _, arg := range args {
    27  				params = append(params, append(p, arg))
    28  			}
    29  		}
    30  	}
    31  	t.params = params
    32  }
    33  
    34  func (t *test) Do() {
    35  	vfn := xutil.Func(t.fn)
    36  	for i := range t.params {
    37  		vfn(t.params[i]...)
    38  	}
    39  	return
    40  }
    41  
    42  func TestFuncWith(fn interface{}) *test {
    43  	xerror.Assert(fn == nil, "[fn] should not be nil")
    44  	xerror.AssertFn(reflect.TypeOf(fn).Kind() != reflect.Func, func() string {
    45  		return fmt.Sprintf("kind: %s, name: %s", reflect.TypeOf(fn).Kind(), reflect.TypeOf(fn))
    46  	})
    47  	return &test{fn: fn}
    48  }