github.com/bytedance/mockey@v1.2.10/convey_test.go (about) 1 /* 2 * Copyright 2022 ByteDance Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package mockey 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/smartystreets/goconvey/convey" 24 ) 25 26 //go:noinline 27 func Fun0() string { 28 // TODO: deal with branch optimization 29 ok1 := Fun1() 30 fmt.Printf("Fun0: call Fun1, %v\n", ok1) 31 if !ok1 { 32 return "exit" 33 } 34 ok2 := Fun2() 35 fmt.Printf("Fun0: call Fun2, %v\n", ok2) 36 if ok2 { 37 return "fun2" 38 } 39 ok3 := Fun3() 40 fmt.Printf("Fun0: call Fun3, %v\n", ok3) 41 if ok3 { 42 return "fun3" 43 } 44 return "xxx" 45 } 46 47 //go:noinline 48 func Fun1() bool { 49 fmt.Println("Fun1") 50 return false 51 } 52 53 //go:noinline 54 func Fun2() bool { 55 fmt.Println("Fun2") 56 return false 57 } 58 59 //go:noinline 60 func Fun3() bool { 61 fmt.Println("Fun3") 62 return false 63 } 64 65 func TestConvey(t *testing.T) { 66 PatchConvey("test", t, func() { 67 Mock(Fun1).Return(true).Build() 68 69 PatchConvey("test case 2", func() { 70 m2 := Mock(Fun2).Return(true).Build() 71 m3 := Mock(Fun3).Return(true).Build() 72 73 r := Fun0() 74 convey.So(r, convey.ShouldEqual, "fun2") 75 convey.So(m2.Times(), convey.ShouldEqual, 1) 76 convey.So(m3.Times(), convey.ShouldEqual, 0) 77 }) 78 79 PatchConvey("test case 3", func() { 80 m2 := Mock(Fun2).Build() 81 m3 := Mock(Fun3).Return(true).Build() 82 83 r := Fun0() 84 convey.So(r, convey.ShouldEqual, "fun3") 85 convey.So(m2.Times(), convey.ShouldEqual, 1) 86 convey.So(m3.Times(), convey.ShouldEqual, 1) 87 }) 88 89 PatchConvey("test case mock times", func() { 90 m2 := Mock(Fun2).When(func() bool { return false }).Build() 91 m3 := Mock(Fun3).Return(true).Build() 92 93 r := Fun0() 94 convey.So(r, convey.ShouldEqual, "fun3") 95 convey.So(m2.Times(), convey.ShouldEqual, 1) 96 convey.So(m2.MockTimes(), convey.ShouldEqual, 0) 97 convey.So(m3.Times(), convey.ShouldEqual, 1) 98 }) 99 }) 100 } 101 102 func TestUnpatchAll_Convey(t *testing.T) { 103 fn1 := func() string { 104 return "fn1" 105 } 106 fn2 := func() string { 107 return "fn2" 108 } 109 fn3 := func() string { 110 return "fn3" 111 } 112 113 Mock(fn1).Return("mocked").Build() 114 if fn1() != "mocked" { 115 t.Error("mock fn1 failed") 116 } 117 118 PatchConvey("UnpatchAll_Convey", t, func() { 119 Mock(fn2).Return("mocked").Build() 120 Mock(fn3).Return("mocked").Build() 121 convey.So(fn1(), convey.ShouldEqual, "mocked") 122 convey.So(fn2(), convey.ShouldEqual, "mocked") 123 convey.So(fn3(), convey.ShouldEqual, "mocked") 124 125 UnPatchAll() 126 127 convey.So(fn1(), convey.ShouldEqual, "mocked") 128 convey.So(fn2(), convey.ShouldEqual, "fn2") 129 convey.So(fn3(), convey.ShouldEqual, "fn3") 130 }) 131 132 r1, r2, r3 := fn1(), fn2(), fn3() 133 if r1 != "mocked" || r2 != "fn2" || r3 != "fn3" { 134 t.Error("mock failed", r1, r2, r3) 135 } 136 137 UnPatchAll() 138 139 r1, r2, r3 = fn1(), fn2(), fn3() 140 if r1 != "fn1" || r2 != "fn2" || r3 != "fn3" { 141 t.Error("mock failed", r1, r2, r3) 142 } 143 }