diff --git a/README.md b/README.md index 08634d7..6fcffee 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ $ go get github.com/agiledragon/gomonkey ``` ## Using gomonkey -The following just make some tests as typical examples. +The following just make some tests as idioms. **Please refer to the test cases, very complete and detailed.** ### ApplyFunc @@ -481,4 +481,62 @@ func TestApplyFuncVarSeq(t *testing.T) { }) } -``` \ No newline at end of file +``` + +## NewPatches + +```go +mport ( + . "github.com/agiledragon/gomonkey" + . "github.com/smartystreets/goconvey/convey" + "testing" + "github.com/agiledragon/gomonkey/test/fake" + "encoding/json" +) + +func TestPatchPair(t *testing.T) { + + Convey("TestPatchPair", t, func() { + + Convey("TestPatchPair", func() { + patchPairs := [][2]interface{} { + { + fake.Exec, + func(_ string, _ ...string) (string, error) { + return outputExpect, nil + }, + }, + { + json.Unmarshal, + func(_ []byte, v interface{}) error { + p := v.(*map[int]int) + *p = make(map[int]int) + (*p)[1] = 2 + (*p)[2] = 4 + return nil + }, + }, + + } + patches := NewPatches() + defer patches.Reset() + for _, pair := range patchPairs { + patches.ApplyFunc(pair[0], pair[1]) + } + + output, err := fake.Exec("", "") + So(err, ShouldEqual, nil) + So(output, ShouldEqual, outputExpect) + + var m map[int]int + err = json.Unmarshal(nil, &m) + So(err, ShouldEqual, nil) + So(m[1], ShouldEqual, 2) + So(m[2], ShouldEqual, 4) + }) + }) +} + +``` + +