github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/test/apply_func_var_seq_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  func TestApplyFuncVarSeq(t *testing.T) {
    12  	Convey("TestApplyFuncVarSeq", t, func() {
    13  
    14  		Convey("default times is 1", func() {
    15  			info1 := "hello cpp"
    16  			info2 := "hello golang"
    17  			info3 := "hello gomonkey"
    18  			outputs := []OutputCell{
    19  				{Values: Params{[]byte(info1), nil}},
    20  				{Values: Params{[]byte(info2), nil}},
    21  				{Values: Params{[]byte(info3), nil}},
    22  			}
    23  			patches := ApplyFuncVarSeq(&fake.Marshal, outputs)
    24  			defer patches.Reset()
    25  			bytes, err := fake.Marshal("")
    26  			So(err, ShouldEqual, nil)
    27  			So(string(bytes), ShouldEqual, info1)
    28  			bytes, err = fake.Marshal("")
    29  			So(err, ShouldEqual, nil)
    30  			So(string(bytes), ShouldEqual, info2)
    31  			bytes, err = fake.Marshal("")
    32  			So(err, ShouldEqual, nil)
    33  			So(string(bytes), ShouldEqual, info3)
    34  		})
    35  
    36  		Convey("retry succ util the third times", func() {
    37  			info1 := "hello cpp"
    38  			outputs := []OutputCell{
    39  				{Values: Params{[]byte(""), fake.ErrActual}, Times: 2},
    40  				{Values: Params{[]byte(info1), nil}},
    41  			}
    42  			patches := ApplyFuncVarSeq(&fake.Marshal, outputs)
    43  			defer patches.Reset()
    44  			bytes, err := fake.Marshal("")
    45  			So(err, ShouldEqual, fake.ErrActual)
    46  			bytes, err = fake.Marshal("")
    47  			So(err, ShouldEqual, fake.ErrActual)
    48  			bytes, err = fake.Marshal("")
    49  			So(err, ShouldEqual, nil)
    50  			So(string(bytes), ShouldEqual, info1)
    51  		})
    52  
    53  		Convey("batch operations failed on the third time", func() {
    54  			info1 := "hello gomonkey"
    55  			outputs := []OutputCell{
    56  				{Values: Params{[]byte(info1), nil}, Times: 2},
    57  				{Values: Params{[]byte(""), fake.ErrActual}},
    58  			}
    59  			patches := ApplyFuncVarSeq(&fake.Marshal, outputs)
    60  			defer patches.Reset()
    61  			bytes, err := fake.Marshal("")
    62  			So(err, ShouldEqual, nil)
    63  			So(string(bytes), ShouldEqual, info1)
    64  			bytes, err = fake.Marshal("")
    65  			So(err, ShouldEqual, nil)
    66  			So(string(bytes), ShouldEqual, info1)
    67  			bytes, err = fake.Marshal("")
    68  			So(err, ShouldEqual, fake.ErrActual)
    69  		})
    70  
    71  	})
    72  }