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

     1  package test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	. "github.com/agiledragon/gomonkey/v2"
     8  	"github.com/agiledragon/gomonkey/v2/test/fake"
     9  	. "github.com/smartystreets/goconvey/convey"
    10  )
    11  
    12  func TestPatchPair(t *testing.T) {
    13  
    14  	Convey("TestPatchPair", t, func() {
    15  
    16  		Convey("TestPatchPair", func() {
    17  			patchPairs := [][2]interface{}{
    18  				{
    19  					fake.Exec,
    20  					func(_ string, _ ...string) (string, error) {
    21  						return outputExpect, nil
    22  					},
    23  				},
    24  				{
    25  					json.Unmarshal,
    26  					func(_ []byte, v interface{}) error {
    27  						p := v.(*map[int]int)
    28  						*p = make(map[int]int)
    29  						(*p)[1] = 2
    30  						(*p)[2] = 4
    31  						return nil
    32  					},
    33  				},
    34  			}
    35  			patches := NewPatches()
    36  			defer patches.Reset()
    37  			for _, pair := range patchPairs {
    38  				patches.ApplyFunc(pair[0], pair[1])
    39  			}
    40  
    41  			output, err := fake.Exec("", "")
    42  			So(err, ShouldEqual, nil)
    43  			So(output, ShouldEqual, outputExpect)
    44  
    45  			var m map[int]int
    46  			err = json.Unmarshal(nil, &m)
    47  			So(err, ShouldEqual, nil)
    48  			So(m[1], ShouldEqual, 2)
    49  			So(m[2], ShouldEqual, 4)
    50  		})
    51  
    52  	})
    53  }