github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/test/apply_func_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  var (
    13  	outputExpect = "xxx-vethName100-yyy"
    14  )
    15  
    16  func TestApplyFunc(t *testing.T) {
    17  	Convey("TestApplyFunc", t, func() {
    18  
    19  		Convey("one func for succ", func() {
    20  			patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
    21  				return outputExpect, nil
    22  			})
    23  			defer patches.Reset()
    24  			output, err := fake.Exec("", "")
    25  			So(err, ShouldEqual, nil)
    26  			So(output, ShouldEqual, outputExpect)
    27  		})
    28  
    29  		Convey("one func for fail", func() {
    30  			patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
    31  				return "", fake.ErrActual
    32  			})
    33  			defer patches.Reset()
    34  			output, err := fake.Exec("", "")
    35  			So(err, ShouldEqual, fake.ErrActual)
    36  			So(output, ShouldEqual, "")
    37  		})
    38  
    39  		Convey("two funcs", func() {
    40  			patches := ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
    41  				return outputExpect, nil
    42  			})
    43  			defer patches.Reset()
    44  			patches.ApplyFunc(fake.Belong, func(_ string, _ []string) bool {
    45  				return true
    46  			})
    47  			output, err := fake.Exec("", "")
    48  			So(err, ShouldEqual, nil)
    49  			So(output, ShouldEqual, outputExpect)
    50  			flag := fake.Belong("", nil)
    51  			So(flag, ShouldBeTrue)
    52  		})
    53  
    54  		Convey("input and output param", func() {
    55  			patches := ApplyFunc(json.Unmarshal, func(data []byte, v interface{}) error {
    56  				if data == nil {
    57  					panic("input param is nil!")
    58  				}
    59  				p := v.(*map[int]int)
    60  				*p = make(map[int]int)
    61  				(*p)[1] = 2
    62  				(*p)[2] = 4
    63  				return nil
    64  			})
    65  			defer patches.Reset()
    66  			var m map[int]int
    67  			err := json.Unmarshal([]byte("123"), &m)
    68  			So(err, ShouldEqual, nil)
    69  			So(m[1], ShouldEqual, 2)
    70  			So(m[2], ShouldEqual, 4)
    71  		})
    72  
    73  		Convey("repeat patch same func", func() {
    74  			patches := ApplyFunc(fake.ReadLeaf, func(_ string) (string, error) {
    75  				return "patch1", nil
    76  			})
    77  			output, err := fake.ReadLeaf("")
    78  			So(err, ShouldEqual, nil)
    79  			So(output, ShouldEqual, "patch1")
    80  
    81  			patches.ApplyFunc(fake.ReadLeaf, func(_ string) (string, error) {
    82  				return "patch2", nil
    83  			})
    84  			output, err = fake.ReadLeaf("")
    85  			So(err, ShouldEqual, nil)
    86  			So(output, ShouldEqual, "patch2")
    87  
    88  			patches.Reset()
    89  			output, err = fake.ReadLeaf("")
    90  			So(err, ShouldEqual, nil)
    91  			So(output, ShouldEqual, "Hello, World!")
    92  		})
    93  
    94  		Convey("declare partial args", func() {
    95  			patches := ApplyFunc(fake.Exec, func() (string, error) {
    96  				return outputExpect, nil
    97  			})
    98  			defer patches.Reset()
    99  			output, err := fake.Exec("", "")
   100  			So(err, ShouldEqual, nil)
   101  			So(output, ShouldEqual, outputExpect)
   102  
   103  			patches.ApplyFunc(fake.Exec, func(_ string) (string, error) {
   104  				return outputExpect, nil
   105  			})
   106  			output, err = fake.Exec("", "")
   107  			So(err, ShouldEqual, nil)
   108  			So(output, ShouldEqual, outputExpect)
   109  
   110  			So(func() {
   111  				patches.ApplyFunc(fake.Exec, func(_ string, _ []string) (string, error) {
   112  					return outputExpect, nil
   113  				})
   114  			}, ShouldPanic)
   115  
   116  			patches.ApplyFunc(fake.Exec, func(_ string, _ ...string) (string, error) {
   117  				return outputExpect, nil
   118  			})
   119  			output, err = fake.Exec("", "")
   120  			So(err, ShouldEqual, nil)
   121  			So(output, ShouldEqual, outputExpect)
   122  		})
   123  	})
   124  }