-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmocker_test.go
336 lines (285 loc) · 8.67 KB
/
mocker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Package mocker_test 对 mocker 包的测试
// 当前文件实现了对 mocker.go 的单测
package mocker_test
import (
"errors"
"math/rand"
"os"
"testing"
"time"
"github.com/stretchr/testify/suite"
mocker "github.com/tencent/goom"
"github.com/tencent/goom/test"
)
// TestUnitBuilderTestSuite 测试入口
func TestUnitBuilderTestSuite(t *testing.T) {
// 开启 debug
// 1.可以查看 apply 和 reset 的状态日志
// 2.查看 mock 调用日志
mocker.OpenDebug()
suite.Run(t, new(mockerTestSuite))
}
type mockerTestSuite struct {
suite.Suite
fakeErr error
}
func (s *mockerTestSuite) SetupTest() {
s.fakeErr = errors.New("fake error")
}
// TestUnitFuncApply 测试函数 mock apply
func (s *mockerTestSuite) TestUnitFuncApply() {
s.T().Log("args: ")
for i := range os.Args {
s.T().Log(os.Args[i], " ")
}
s.Run("success", func() {
mock := mocker.Create()
mock.Func(test.Foo).Apply(func(int) int {
return 3
})
s.Equal(3, test.Foo(1), "test.Foo mock check")
mock.Reset()
s.Equal(1, test.Foo(1), "test.Foo mock reset check")
})
}
// TestClosureFuncApply 测试闭包函数 mock apply
func (s *mockerTestSuite) TestClosureFuncApply() {
s.Run("success", func() {
mock := mocker.New()
var r = 1
mock.Func(test.Foo).Apply(func(int) int {
return r
})
r = 3
s.Equal(3, test.Foo(1), "test.Foo mock check")
mock.Reset()
s.Equal(1, test.Foo(1), "test.Foo mock reset check")
})
}
// TestUnitFuncReturn 测试函数 mock return
func (s *mockerTestSuite) TestUnitFuncReturn() {
s.Run("success", func() {
mock := mocker.Create()
mock.Func(test.Foo).When(1).Return(3)
s.Equal(3, test.Foo(1), "test.Foo mock check")
mock.Reset()
s.Equal(1, test.Foo(1), "test.Foo mock reset check")
})
}
// TestUnitUnexportedFuncApply 测试未导出函数 mock apply
func (s *mockerTestSuite) TestUnitUnexportedFuncApply() {
s.Run("success", func() {
mock := mocker.New()
mock.Pkg("github.com/tencent/goom/test").ExportFunc("foo").Apply(func(i int) int {
return i * 3
})
s.Equal(3, test.Invokefoo(1), "foo mock check")
mock.Reset()
s.Equal(1, test.Invokefoo(1), "foo mock reset check")
})
}
// TestUnitUnexportedFuncReturn 测试未导出函数 mock return
func (s *mockerTestSuite) TestUnitUnexportedFuncReturn() {
s.Run("success", func() {
mock := mocker.Create()
mock.Pkg("github.com/tencent/goom/test").ExportFunc("foo").As(func(i int) int {
return i * 1
}).Return(3)
s.Equal(3, test.Invokefoo(1), "foo mock check")
mock.Reset()
s.Equal(1, test.Invokefoo(1), "foo mock reset check")
})
}
// TestUnitMethodApply 测试结构体的方法 mock apply
func (s *mockerTestSuite) TestUnitMethodApply() {
s.Run("success", func() {
mock := mocker.Create()
mock.Struct(&test.Fake{}).Method("Call").Apply(func(*test.Fake, int) int {
return 5
})
f := &test.Fake{}
s.Equal(5, f.Call(1), "call mock check")
mock.Reset()
s.Equal(1, f.Call(1), "call mock reset check")
})
}
// TestUnitMethodReturn 测试结构体的方法 mock return
func (s *mockerTestSuite) TestUnitMethodReturn() {
s.Run("success", func() {
mock := mocker.Create()
mock.Struct(&test.Fake{}).Method("Call").Return(5).AndReturn(6)
mock.Struct(&test.Fake{}).Method("Call2").Return(7).AndReturn(8)
f := &test.Fake{}
s.Equal(5, f.Call(1), "call mock check")
s.Equal(6, f.Call(1), "call mock check")
s.Equal(7, f.Call2(1), "call mock check")
s.Equal(8, f.Call2(1), "call mock check")
mock.Reset()
s.Equal(1, f.Call(1), "call mock reset check")
})
}
// TestUnitUnExportedMethodApply 测试结构体的未导出方法 mock apply
func (s *mockerTestSuite) TestUnitUnExportedMethodApply() {
s.Run("success", func() {
mock := mocker.Create()
mock.Struct(&test.Fake{}).ExportMethod("call").Apply(func(_ *test.Fake, i int) int {
return i * 2
})
f := &test.Fake{}
s.Equal(2, f.Invokecall(1), "call mock check")
mock.Reset()
s.Equal(1, f.Invokecall(1), "call mock reset check")
})
}
// TestUnitUnexportedMethodReturn 测试结构体的未导出方法 mock return
func (s *mockerTestSuite) TestUnitUnexportedMethodReturn() {
s.Run("success", func() {
mock := mocker.Create()
mock.Struct(&test.Fake{}).ExportMethod("call").As(func(_ *test.Fake, i int) int {
return i * 2
}).Return(6)
f := &test.Fake{}
s.Equal(6, f.Invokecall(1), "call mock check")
mock.Reset()
s.Equal(1, f.Invokecall(1), "call mock reset check")
})
}
// TestUnitUnExportStruct 测试未导出结构体的方法 mock apply
func (s *mockerTestSuite) TestUnitUnExportStruct() {
s.Run("success", func() {
// _fake 从 test.fake 中拷贝过来
type _fake struct {
_ string // field1
_ int // field2
}
mock := mocker.Create()
// 指定包名
s.Equal("github.com/tencent/goom_test", mock.PkgName())
mock.Pkg("github.com/tencent/goom/test").ExportStruct("*fake").
Method("call").Apply(func(_ *_fake, i int) int {
return i * 2
})
s.Equal("github.com/tencent/goom_test", mock.PkgName())
f := test.NewUnexportedFake()
s.Equal(2, f.Invokecall(1), "call mock check")
mock.Reset()
s.Equal(1, f.Invokecall(1), "call mock reset check")
})
}
// TestMultiReturn 测试调用原函数多返回
func (s *mockerTestSuite) TestMultiReturn() {
s.Run("success", func() {
mock := mocker.Create()
mock.Func(test.Foo).When(1).Return(3).AndReturn(2)
s.Equal(3, test.Foo(1), "foo mock check")
s.Equal(2, test.Foo(1), "foo mock check")
mock.Reset()
s.Equal(1, test.Foo(1), "foo mock reset check")
})
}
// TestMultiReturns 测试调用原函数多返回
func (s *mockerTestSuite) TestMultiReturns() {
s.Run("success", func() {
mock := mocker.Create()
mock.Func(test.Foo).Returns(2, 3)
s.Equal(2, test.Foo(1), "foo mock check")
s.Equal(3, test.Foo(1), "foo mock check")
mock.Func(test.Foo).Returns(4, 5)
s.Equal(4, test.Foo(1), "foo mock check")
s.Equal(5, test.Foo(1), "foo mock check")
mock.Func(test.Foo).When(-1).Returns(6, 7)
s.Equal(6, test.Foo(-1), "foo mock check")
s.Equal(7, test.Foo(-1), "foo mock check")
mock.Func(test.Foo).When(-2).Returns(8, 9)
s.Equal(8, test.Foo(-2), "foo mock check")
s.Equal(9, test.Foo(-2), "foo mock check")
mock.Reset()
s.Equal(1, test.Foo(1), "foo mock reset check")
})
}
// TestUnitFuncTwiceApply 测试函数 mock apply 多次
func (s *mockerTestSuite) TestUnitFuncTwiceApply() {
s.Run("success", func() {
mock := mocker.Create()
mock.Func(test.Foo).When(1).Return(3)
mock.Func(test.Foo).When(2).Return(6)
s.Equal(3, test.Foo(1), "foo mock check")
s.Equal(6, test.Foo(2), "foo mock check")
mock.Reset()
mock.Func(test.Foo).When(1).Return(2)
s.Equal(2, test.Foo(1), "foo mock reset check")
mock.Reset()
})
}
// TestUnitDefaultReturn 测试函数 mock 返回默认值
func (s *mockerTestSuite) TestUnitDefaultReturn() {
s.Run("success", func() {
mock := mocker.Create()
mock.Func(test.Foo).Return(3).AndReturn(4)
mock.Func(test.Foo).Return(5).AndReturn(6)
s.Equal(3, test.Foo(1), "foo return check")
s.Equal(4, test.Foo(2), "foo return check")
s.Equal(5, test.Foo(1), "foo return check")
s.Equal(6, test.Foo(2), "foo return check")
mock.Reset()
})
}
// TestFakeReturn 测试返回 fake 值
func (s *mockerTestSuite) TestFakeReturn() {
s.Run("success", func() {
mock := mocker.Create()
defer mock.Reset()
mock.Func(test.Foo1).Return(&test.S1{
Field1: "ok",
Field2: 2,
})
s.Equal(&test.S{
Field1: "ok",
Field2: 2,
}, test.Foo1(), "foo mock check")
})
}
func (s *mockerTestSuite) TestUnitNilReturn() {
s.Run("nil return", func() {
mocker.Create().Func(test.GetS).Return(nil, s.fakeErr)
res, err := test.GetS()
s.Equal([]byte(nil), res)
s.Equal(s.fakeErr, err)
})
}
// TestVarMock 测试简单变量 mock
func (s *mockerTestSuite) TestVarMock() {
s.Run("simple var mock", func() {
mock := mocker.Create()
mock.Var(&test.GlobalVar).Set(2)
s.Equal(2, test.GlobalVar)
mock.Reset()
s.Equal(1, test.GlobalVar)
})
}
// TestVarApply 测试变量应用 mock
func (s *mockerTestSuite) TestVarApply() {
s.Run("var mock apply", func() {
mock := mocker.Create()
mock.Var(&test.GlobalVar).Apply(func() int {
return 2
})
s.Equal(2, test.GlobalVar)
mock.Reset()
s.Equal(1, test.GlobalVar)
})
}
// TestUnitSystemFuncApply 测试系统函数的 mock
// 需要加上 -gcflags="-l"
// 在bazel 构建环境下, 因为系统库不支持开启 gcflags=-l ,所以暂不支持系统库中的短函数 mock
func (s *mockerTestSuite) TestUnitSystemFuncApply() {
s.Run("success", func() {
mock := mocker.Create()
defer mock.Reset()
mock.Func(rand.Int31).Return(int32(3))
date, _ := time.Parse("2006-01-02 15:04:05", "2020-07-30 00:00:00")
mock.Func(time.Now).Return(date)
s.Equal(int32(3), rand.Int31(), "foo mock check")
s.Equal(date, time.Now(), "foo mock check")
})
}