github.com/cmd-stream/base-go@v0.0.0-20230813145615-dd6ac24c16f5/testdata/mock/Cmd.go (about)

     1  package mock
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"time"
     7  
     8  	"github.com/cmd-stream/base-go"
     9  	"github.com/ymz-ncnk/mok"
    10  )
    11  
    12  func NewCmd() Cmd {
    13  	return Cmd{
    14  		Mock: mok.New("Cmd"),
    15  	}
    16  }
    17  
    18  type Cmd struct {
    19  	*mok.Mock
    20  }
    21  
    22  func (c Cmd) RegisterExec(
    23  	fn func(ctx context.Context, at time.Time, seq base.Seq, receiver any,
    24  		proxy base.Proxy) (err error)) Cmd {
    25  	c.Register("Exec", fn)
    26  	return c
    27  }
    28  
    29  func (c Cmd) RegisterTimeout(
    30  	fn func() (timeout time.Duration)) Cmd {
    31  	c.Register("Timeout", fn)
    32  	return c
    33  }
    34  
    35  func (c Cmd) Exec(ctx context.Context, at time.Time, seq base.Seq, receiver any,
    36  	proxy base.Proxy) (err error) {
    37  	var ctxVal reflect.Value
    38  	if ctx == nil {
    39  		ctxVal = reflect.Zero(reflect.TypeOf((*context.Context)(nil)).Elem())
    40  	} else {
    41  		ctxVal = reflect.ValueOf(ctx)
    42  	}
    43  	var receiverVal any
    44  	if receiver == nil {
    45  		receiverVal = reflect.Zero(reflect.TypeOf((*any)(nil)).Elem())
    46  	} else {
    47  		receiverVal = reflect.ValueOf(receiver)
    48  	}
    49  	var proxyVal any
    50  	if proxy == nil {
    51  		proxyVal = reflect.Zero(reflect.TypeOf((*base.Proxy)(nil)).Elem())
    52  	} else {
    53  		proxyVal = reflect.ValueOf(proxy)
    54  	}
    55  	vals, err := c.Call("Exec", ctxVal, at, seq, receiverVal, proxyVal)
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  	err, _ = vals[0].(error)
    60  	return
    61  }
    62  
    63  func (c Cmd) Timeout() (timeout time.Duration) {
    64  	vals, err := c.Call("Timeout")
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  	timeout = vals[0].(time.Duration)
    69  	return
    70  }