Skip to content

Commit

Permalink
Merge pull request #43 from bytedance/feat/unpatch_all
Browse files Browse the repository at this point in the history
feat: support UnpatchAll
  • Loading branch information
Sychorius authored Oct 19, 2023
2 parents 544448c + 2519fab commit 01cec8f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
30 changes: 30 additions & 0 deletions convey.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,33 @@ func removeFromGlobal(mocker mockerInstance) {
tool.DebugPrintf("%v removed\n", mocker.key())
delete(gMocker[len(gMocker)-1], mocker.key())
}

// Unpatch all mocks in current 'PatchConvey' context
//
// If the caller is out of 'PatchConvey', it will unpatch all mocks
//
// For example:
//
// Test1(t) {
// Mock(a).Build()
// Mock(b).Build()
//
// // a and b will be unpatched
// UnpatchAll()
// }
// })
//
// Test2(t) {
// Mock(a).Build()
// PatchConvey(t,func(){
// Mock(b).Build()
//
// // only b will be unpatched
// UnpatchAll()
// }
// })
func UnPatchAll() {
for _, mocker := range gMocker[len(gMocker)-1] {
mocker.unPatch()
}
}
43 changes: 43 additions & 0 deletions convey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,46 @@ func TestConvey(t *testing.T) {
})
})
}

func TestUnpatchAll_Convey(t *testing.T) {
fn1 := func() string {
return "fn1"
}
fn2 := func() string {
return "fn2"
}
fn3 := func() string {
return "fn3"
}

Mock(fn1).Return("mocked").Build()
if fn1() != "mocked" {
t.Error("mock fn1 failed")
}

PatchConvey("UnpatchAll_Convey", t, func() {
Mock(fn2).Return("mocked").Build()
Mock(fn3).Return("mocked").Build()
convey.So(fn1(), convey.ShouldEqual, "mocked")
convey.So(fn2(), convey.ShouldEqual, "mocked")
convey.So(fn3(), convey.ShouldEqual, "mocked")

UnPatchAll()

convey.So(fn1(), convey.ShouldEqual, "mocked")
convey.So(fn2(), convey.ShouldEqual, "fn2")
convey.So(fn3(), convey.ShouldEqual, "fn3")
})

r1, r2, r3 := fn1(), fn2(), fn3()
if r1 != "mocked" || r2 != "fn2" || r3 != "fn3" {
t.Error("mock failed", r1, r2, r3)
}

UnPatchAll()

r1, r2, r3 = fn1(), fn2(), fn3()
if r1 != "fn1" || r2 != "fn2" || r3 != "fn3" {
t.Error("mock failed", r1, r2, r3)
}
}

0 comments on commit 01cec8f

Please sign in to comment.