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

     1  package mock
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"reflect"
     7  
     8  	"github.com/ymz-ncnk/mok"
     9  )
    10  
    11  func NewServerDelegate() ServerDelegate {
    12  	return ServerDelegate{
    13  		Mock: mok.New("ServerDelegate"),
    14  	}
    15  }
    16  
    17  type ServerDelegate struct {
    18  	*mok.Mock
    19  }
    20  
    21  func (m ServerDelegate) RegisterHandle(
    22  	fn func(ctx context.Context, conn net.Conn) (err error)) ServerDelegate {
    23  	m.Register("Handle", fn)
    24  	return m
    25  }
    26  
    27  func (m ServerDelegate) RegisterNHandle(n int,
    28  	fn func(ctx context.Context, conn net.Conn) (err error)) ServerDelegate {
    29  	m.RegisterN("Handle", n, fn)
    30  	return m
    31  }
    32  
    33  func (m ServerDelegate) Handle(ctx context.Context, conn net.Conn) (err error) {
    34  	var ctxVal reflect.Value
    35  	if ctx == nil {
    36  		ctxVal = reflect.Zero(reflect.TypeOf((*context.Context)(nil)).Elem())
    37  	} else {
    38  		ctxVal = reflect.ValueOf(ctx)
    39  	}
    40  	var connVal reflect.Value
    41  	if conn == nil {
    42  		connVal = reflect.Zero(reflect.TypeOf((*net.Conn)(nil)).Elem())
    43  	} else {
    44  		connVal = reflect.ValueOf(conn)
    45  	}
    46  	vals, err := m.Call("Handle", ctxVal, connVal)
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  	err, _ = vals[0].(error)
    51  	return
    52  }