github.com/bytedance/mockey@v1.2.10/tests/remock_test.go (about)

     1  // Copyright 2023 2022 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package tests
    15  
    16  import (
    17  	"fmt"
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/bytedance/mockey"
    22  	"github.com/bytedance/mockey/internal/tool"
    23  	"github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  func callerFunc() {
    27  	panic("CallerFunc")
    28  }
    29  
    30  type callerStruct struct {
    31  	_ int
    32  }
    33  
    34  func (c *callerStruct) Foo() {
    35  	panic("CallerStruct")
    36  }
    37  
    38  var callerValue string
    39  
    40  func TestReMockPanic(t *testing.T) {
    41  	mockey.PatchConvey("TestReMockPanic", t, func() {
    42  		mockey.PatchConvey("callerFunc", func() {
    43  			mocker := mockey.Mock(callerFunc).To(func() { fmt.Println("should not panic") }).Build()
    44  			mocker.To(func() { fmt.Println("should also not panic") })
    45  			lastCaller := tool.Caller()
    46  			lastCaller.Line -= 1
    47  			var err interface{}
    48  			func() {
    49  				defer func() { err = recover() }()
    50  				mockey.Mock(callerFunc).To(func() { fmt.Println("should panic, but recovered") }).Build()
    51  			}()
    52  			errString, ok := err.(string)
    53  			convey.So(ok, convey.ShouldBeTrue)
    54  			convey.So(strings.Contains(errString, lastCaller.String()), convey.ShouldBeTrue)
    55  		})
    56  		mockey.PatchConvey("callerStruct", func() {
    57  			mocker := mockey.Mock((*callerStruct).Foo).To(func() { fmt.Println("should not panic") }).Build()
    58  			mocker.To(func() { fmt.Println("should also not panic") })
    59  			lastCaller := tool.Caller()
    60  			lastCaller.Line -= 1
    61  			var err interface{}
    62  			func() {
    63  				defer func() { err = recover() }()
    64  				mockey.Mock((*callerStruct).Foo).To(func() { fmt.Println("should panic, but recovered") }).Build()
    65  			}()
    66  			errString, ok := err.(string)
    67  			convey.So(ok, convey.ShouldBeTrue)
    68  			convey.So(strings.Contains(errString, lastCaller.String()), convey.ShouldBeTrue)
    69  		})
    70  		mockey.PatchConvey("callerValue", func() {
    71  			mockey.MockValue(&callerValue).To("should not panic")
    72  			lastCaller := tool.Caller()
    73  			lastCaller.Line -= 1
    74  			var err interface{}
    75  			func() {
    76  				defer func() { err = recover() }()
    77  				mockey.MockValue(&callerValue).To("should panic, but recovered")
    78  			}()
    79  			errString, ok := err.(string)
    80  			convey.So(ok, convey.ShouldBeTrue)
    81  			convey.So(strings.Contains(errString, lastCaller.String()), convey.ShouldBeTrue)
    82  		})
    83  	})
    84  }