github.com/tencent/goom@v1.0.1/nocgo/mocker_nocgo_test.go (about)

     1  package nocgo_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  	mocker "github.com/tencent/goom"
    10  )
    11  
    12  // foo foo 测试函数
    13  //
    14  //go:noinline
    15  func foo(i int) int {
    16  	// check 对 defer 的支持
    17  	defer func() { fmt.Printf("defer\n") }()
    18  	//cgoFuncAny()
    19  	return i * 1
    20  }
    21  
    22  // TestUnitBuilderNoCGOTestSuite 测试入口, 测试没有引入cgo的情况
    23  func TestUnitBuilderNoCGOTestSuite(t *testing.T) {
    24  	mocker.OpenDebug()
    25  	suite.Run(t, new(mockerNoCGOTestSuite))
    26  }
    27  
    28  type mockerNoCGOTestSuite struct {
    29  	suite.Suite
    30  	fakeErr error
    31  }
    32  
    33  func (s *mockerNoCGOTestSuite) SetupTest() {
    34  	s.fakeErr = errors.New("fake error")
    35  }
    36  
    37  // TestMultiReturn 测试调用原函数多返回
    38  func (s *mockerNoCGOTestSuite) TestMultiReturn() {
    39  	s.Run("success", func() {
    40  		mock := mocker.Create()
    41  		mock.Func(foo).When(1).Return(3).AndReturn(2)
    42  		s.Equal(3, foo(1), "foo mock check")
    43  		s.Equal(2, foo(1), "foo mock check")
    44  
    45  		mock.Reset()
    46  		s.Equal(1, foo(1), "foo mock reset check")
    47  	})
    48  }