-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_mock_mock.cpp
404 lines (339 loc) · 10.2 KB
/
test_mock_mock.cpp
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "../source/h2_unit.cpp"
#include "test_types.hpp"
SUITE(mock function)
{
Case(once)
{
MOCK(foobar2, int(int, const char*)).Once(1, "A").Return(11);
OK(11, foobar2(1, "A"));
}
Case(twice)
{
MOCK(foobar2, int(int, const char*)).Twice(Eq(1), _).Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "BC"));
}
Case(3 times)
{
MOCK(foobar2, int(int, const char*)).Times(3).With(Ge(1), "A").Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
}
Case(any 0)
{
MOCK(foobar2, int(int, const char*)).Any(1, "A");
}
Case(any 1)
{
MOCK(foobar2, int(int, const char*)).Any().With(1, "A").Return(11);
OK(11, foobar2(1, "A"));
}
Case(any 2)
{
MOCK(foobar2, int(int, const char*)).Any().With(1, "A").Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
}
Case(atleast 2)
{
MOCK(foobar2, int(int, const char*)).Atleast(2).With(1, "A").Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
}
Case(atleast 3)
{
MOCK(foobar2, int(int, const char*)).Atleast(2).With(1, "A").Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
}
Case(atmost 1)
{
MOCK(foobar2, int(int, const char*)).Atmost(2).With(1, "A").Return(11);
OK(11, foobar2(1, "A"));
}
Case(atmost 2)
{
MOCK(foobar2, int(int, const char*)).Atmost(2).With(1, "A").Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
}
Case(between)
{
MOCK(foobar2, int(int, const char*)).Between(2, 4).With(1, "A").Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
}
Case(void return)
{
MOCK(foobar21, void(int& a, char*)).Once(1, (char*)"A");
char t[32] = "A";
int a1 = 1;
foobar21(a1, t);
}
Case(Th0)
{
MOCK(foobar21, void(int& a, char*)).Once().Th0(1);
char t[32] = "";
int a1 = 1;
foobar21(a1, t);
}
Case(Th1)
{
MOCK(foobar21, void(int& a, char*)).Once().Th1((char*)"A");
char t[32] = "A";
int a1 = 1;
foobar21(a1, t);
}
Case(zero parameter)
{
MOCK(foobar22, void()).Once();
foobar22();
}
Case(Is Null Matcher)
{
MOCK(foobar2, int(int, const char*)).Once(1, NULL).Return(11);
OK(11, foobar2(1, NULL));
}
Case(Substr Matcher)
{
MOCK(foobar2, int(int, const char*)).Once(1, Substr("BC")).Return(11);
OK(11, foobar2(1, "ABCD"));
}
Case(Not Matcher)
{
MOCK(foobar2, int(int, const char*)).Once(Not(2), _).Return(11);
OK(11, foobar2(1, "A"));
}
Case(AllOf Matcher)
{
MOCK(foobar2, int(int, const char*)).Once(AllOf(1, Ge(1)), _).Return(11);
OK(11, foobar2(1, "A"));
}
Case(AnyOf Matcher)
{
MOCK(foobar2, int(int, const char*)).Once(AnyOf(Le(1), Gt(2)), _).Return(11);
OK(11, foobar2(1, "A"));
}
Case(NoneOf Matcher)
{
MOCK(foobar2, int(int, const char*)).Once(NoneOf(1, Ge(1)), _).Return(11);
OK(11, foobar2(0, "A"));
}
Case(arguments up to 15)
{
MOCK(foobar16, int(int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int)).Once().Return(11);
OK(11, foobar16(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
}
}
SUITE(multi line)
{
Case(1 checkin)
{
MOCK(foobar2, int(int, const char*)).Once(1, "A").Return(11).Once().With(2, "B").Return(22).Twice(3, "C").Return(33);
OK(11, foobar2(1, "A"));
OK(22, foobar2(2, "B"));
OK(33, foobar2(3, "C"));
OK(33, foobar2(3, "C"));
}
Case(2 checkin)
{
MOCK(foobar2, int(int, const char*)).Once(1, "A").Return(11).Twice().With(2, "B").Return(22);
OK(11, foobar2(1, "A"));
OK(22, foobar2(2, "B"));
OK(22, foobar2(2, "B"));
}
}
SUITE(mock greed)
{
Case(greed default true)
{
MOCK(foobar2, int(int, const char*))
// .greed(true) // default is true
.Between(1, 3)
.With(1, "A")
.Return(11)
.Once(1, _)
.Return(22);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
OK(22, foobar2(1, "A"));
}
Case(greed false)
{
MOCK(foobar2, int(int, const char*)).greed(false).Between(1, 3).With(1, "A").Return(11).Once(1, _).Return(22);
OK(11, foobar2(1, "A"));
OK(22, foobar2(1, "A"));
}
}
SUITE(mock Return)
{
Case(delegate to origin without Return)
{
MOCK(foobar2, int(int, const char*)).Once(1, "A");
OK(2, foobar2(1, "A"));
}
}
SUITE(mock member function)
{
B_DerivedClass b;
Case(static member function)
{
MOCK(B_DerivedClass::static_f2, const char*(int a, int b)).Once(1, 2).Return("+B.static_f2");
OK("+B.static_f2", B_DerivedClass::static_f2(1, 2));
}
Case(normal member function)
{
MOCK(B_DerivedClass, normal_f2, const char*(int, int)).Once(1, 2).Return("+B.normal_f2");
OK("+B.normal_f2", b.normal_f2(1, 2));
}
Case(const member function)
{
MOCK(B_DerivedClass, const_f, const char*(int)).Once(1, 2).Return("+B.const_f");
OK("+B.const_f", b.const_f(1));
}
Case(noexcept member function)
{
MOCK(B_DerivedClass, noexcept_f, const char*(int)).Once(1, 2).Return("+B.noexcept_f");
OK("+B.noexcept_f", b.noexcept_f(1));
}
Case(const noexcept member function)
{
MOCK(B_DerivedClass, const_noexcept_f, const char*(int)).Once(1, 2).Return("+B.const_noexcept_f");
OK("+B.const_noexcept_f", b.const_noexcept_f(1));
}
Case(overload member function)
{
C_OverrideClass c;
// MOCK(C_OverrideClass, overload_f, const char*()).Once().Return("+C.overload_f0");
// OK("+C.overload_f0", c.overload_f());
MOCK(C_OverrideClass, overload_f, const char*(int, int)).Once(1, 2).Return("+C.overload_f2");
OK("+C.overload_f2", c.overload_f(1, 2));
MOCK(C_OverrideClass, overload_f, const char*(int, int, int)).Once(1, 2, 3).Return("+C.overload_f3");
OK("+C.overload_f3", c.overload_f(1, 2, 3));
}
Case(virtual member function)
{
MOCK(B_DerivedClass, virtual_f2, const char*(int, int)).Once(1, 2).Return("+B.virtual_f2");
OK("+B.virtual_f2", b.virtual_f2(1, 2));
}
#if !defined WIN32
Case(no default constructor)
{
MOCK(D_NoConstructorClass, virtual_f3, const char*(int, int, int)).Once().Return("+D.virtual_f3");
D_NoConstructorClass d(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
OK("+D.virtual_f3", d.virtual_f3(1, 2, 3));
}
Case(abstract class)
{
MOCK(A_AbstractClass, virtual_f1, const char*(int)).Once().Return("+A.virtual_f1");
OK("+A.virtual_f1", b.virtual_f1(1));
}
#endif
Case(abstract virtual member function with object)
{
B_DerivedClass b;
OK("A.virtual_f1(1)a", b.virtual_f1(1));
A_AbstractClass* a = dynamic_cast<A_AbstractClass*>(&b);
MOCK(a, A_AbstractClass, virtual_f1, const char*(int)).Once().Return("+A.virtual_f1");
OK("+A.virtual_f1", b.virtual_f1(1));
}
Case(no default constructor with object)
{
D_NoConstructorClass d(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
MOCK(d, D_NoConstructorClass, virtual_f3, const char*(int, int, int)).Once().Return("+D.virtual_f3");
OK("+D.virtual_f3", d.virtual_f3(1, 2, 3));
}
}
SUITE(mock template function)
{
Case(function 1 typename)
{
OK(4, foobar4<int>(0));
MOCK(foobar4<int>, int(int a)).Once().Return(-4);
OK(-4, foobar4<int>(0));
}
Case(function 2 typename)
{
OK(5, (foobar5<int, int>(1, 2)));
MOCK((foobar5<int, int>), int(int a, int b)).Once().Return(-5);
OK(-5, (foobar5<int, int>(1, 2)));
}
}
SUITE(mock template member function)
{
Case(member function 1 typename)
{
F_TemplateClass<int> f;
OK("F.static_f1(1)", f.static_f1(1));
OK("F.normal_f1(1)f", f.normal_f1(1));
OK("F.virtual_f1(1)f", f.virtual_f1(1));
MOCK(F_TemplateClass<int>::static_f1, const char*(int a)).Return("+F.static_f1");
OK("+F.static_f1", f.static_f1(0));
MOCK(F_TemplateClass<int>, normal_f1<int>, const char*(int a)).Return("+F.normal_f1");
OK("+F.normal_f1", f.normal_f1(0));
MOCK(F_TemplateClass<int>, virtual_f1, const char*(int a)).Return("+F.virtual_f1");
OK("+F.virtual_f1", f.virtual_f1(0));
}
Case(member function 2 typename)
{
G_TemplateClass<int, int> g;
OK("G.static_f2(1,2)", (g.static_f2<int, int>(1, 2)));
OK("G.normal_f2(1,2)g", (g.normal_f2<int, int>(1, 2)));
OK(Pair("G.virtual_f2", 12.7), (g.virtual_f2<int, int>(1, 2)));
MOCK((G_TemplateClass<int, int>::static_f2<int, int>), const char*(int a, int b)).Return("+G.static_f2");
OK("+G.static_f2", (g.static_f2<int, int>(0, 0)));
MOCK((G_TemplateClass<int, int>), (normal_f2<int, int>), const char*(int a, int b)).Return("+G.normal_f2");
OK("+G.normal_f2", (g.normal_f2<int, int>(0, 0)));
#if !defined WIN32 // Suck when member return Object
MOCK((G_TemplateClass<int, int>), (virtual_f2<int, int>), (std::pair<const char*, double>(int a, int b))).Return(std::make_pair("+G.virtual_f2", 0.0));
OK(Pair("+G.virtual_f2", 0.0), (g.virtual_f2<int, int>(0, 0)));
#endif
}
}
SUITE(mock omit checkin)
{
Case(only With)
{
MOCK(foobar2, int(int, const char*)).With(1, "A").Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
}
Case(only Th0)
{
MOCK(foobar2, int(int, const char*)).Th0(1).Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
}
Case(only Return)
{
MOCK(foobar2, int(int, const char*)).Return(22);
OK(22, foobar2(1, "A"));
OK(22, foobar2(1, "A"));
}
Case(only None)
{
MOCK(foobar2, int(int, const char*)).Return(11);
OK(11, foobar2(1, "A"));
OK(11, foobar2(1, "A"));
}
}
SUITE(mock by function name)
{
Todo(time) // nm undefined time()
{
MOCK("time", time_t(time_t*)).Once().Return(11);
OK(42, time(NULL));
}
Todo(time)
{
MOCK(time, time_t(time_t*)).Once().Return((time_t)0);
}
Case(foobar)
{
MOCK("foobar0", int()).Once().Return(1);
OK(1, foobar0());
}
}