github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/test/apply_method_func_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/agiledragon/gomonkey/v2"
     7  	"github.com/agiledragon/gomonkey/v2/test/fake"
     8  	. "github.com/smartystreets/goconvey/convey"
     9  )
    10  
    11  /*
    12  	compare with apply_method_test.go, no need pass receiver
    13  */
    14  
    15  func TestApplyMethodFunc(t *testing.T) {
    16  	slice := fake.NewSlice()
    17  	var s *fake.Slice
    18  	Convey("TestApplyMethodFunc", t, func() {
    19  		Convey("for succ", func() {
    20  			err := slice.Add(1)
    21  			So(err, ShouldEqual, nil)
    22  			patches := ApplyMethodFunc(s, "Add", func(_ int) error {
    23  				return nil
    24  			})
    25  			defer patches.Reset()
    26  			err = slice.Add(1)
    27  			So(err, ShouldEqual, nil)
    28  			err = slice.Remove(1)
    29  			So(err, ShouldEqual, nil)
    30  			So(len(slice), ShouldEqual, 0)
    31  		})
    32  
    33  		Convey("for already exist", func() {
    34  			err := slice.Add(2)
    35  			So(err, ShouldEqual, nil)
    36  			patches := ApplyMethodFunc(s, "Add", func(_ int) error {
    37  				return fake.ErrElemExsit
    38  			})
    39  			defer patches.Reset()
    40  			err = slice.Add(1)
    41  			So(err, ShouldEqual, fake.ErrElemExsit)
    42  			err = slice.Remove(2)
    43  			So(err, ShouldEqual, nil)
    44  			So(len(slice), ShouldEqual, 0)
    45  		})
    46  
    47  		Convey("two methods", func() {
    48  			err := slice.Add(3)
    49  			So(err, ShouldEqual, nil)
    50  			defer slice.Remove(3)
    51  			patches := ApplyMethodFunc(s, "Add", func(_ int) error {
    52  				return fake.ErrElemExsit
    53  			})
    54  			defer patches.Reset()
    55  			patches.ApplyMethodFunc(s, "Remove", func(_ int) error {
    56  				return fake.ErrElemNotExsit
    57  			})
    58  			err = slice.Add(2)
    59  			So(err, ShouldEqual, fake.ErrElemExsit)
    60  			err = slice.Remove(1)
    61  			So(err, ShouldEqual, fake.ErrElemNotExsit)
    62  			So(len(slice), ShouldEqual, 1)
    63  			So(slice[0], ShouldEqual, 3)
    64  		})
    65  
    66  		Convey("one func and one method", func() {
    67  			err := slice.Add(4)
    68  			So(err, ShouldEqual, nil)
    69  			defer slice.Remove(4)
    70  			patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
    71  				return outputExpect, nil
    72  			})
    73  			defer patches.Reset()
    74  			patches.ApplyMethodFunc(s, "Remove", func(_ int) error {
    75  				return fake.ErrElemNotExsit
    76  			})
    77  			output, err := fake.Exec("", "")
    78  			So(err, ShouldEqual, nil)
    79  			So(output, ShouldEqual, outputExpect)
    80  			err = slice.Remove(1)
    81  			So(err, ShouldEqual, fake.ErrElemNotExsit)
    82  			So(len(slice), ShouldEqual, 1)
    83  			So(slice[0], ShouldEqual, 4)
    84  		})
    85  
    86  		Convey("for variadic method", func() {
    87  			slice = fake.NewSlice()
    88  			count := slice.Append(1, 2, 3)
    89  			So(count, ShouldEqual, 3)
    90  			patches := ApplyMethodFunc(s, "Append", func(_ ...int) int {
    91  				return 0
    92  			})
    93  			defer patches.Reset()
    94  			count = slice.Append(4, 5, 6)
    95  			So(count, ShouldEqual, 0)
    96  			So(len(slice), ShouldEqual, 3)
    97  		})
    98  	})
    99  }