-
Notifications
You must be signed in to change notification settings - Fork 170
/
StubbingProgress.hpp
508 lines (415 loc) · 18 KB
/
StubbingProgress.hpp
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* Copyright (c) 2014 Eran Pe'er.
*
* This program is made available under the terms of the MIT License.
*
* Created on Mar 10, 2014
*/
#pragma once
#include <functional>
#include <type_traits>
#include <stdexcept>
#include <utility>
#include "mockutils/DefaultValue.hpp"
#include "mockutils/Macros.hpp"
#include "mockutils/type_utils.hpp"
#include "fakeit/FakeitExceptions.hpp"
#include "fakeit/ActualInvocation.hpp"
#include "fakeit/Quantifier.hpp"
#include "fakeit/Action.hpp"
namespace fakeit {
namespace helper
{
template <typename T, int N>
struct ArgValue;
template <int max_index, int tuple_index>
struct ArgValidator;
template<int arg_index, typename current_arg, typename ...T, int ...N, typename ... arglist>
static void
Assign(std::tuple<ArgValue<T, N>...> arg_vals, current_arg &&p, arglist &&... args);
template<int N>
struct ParamWalker;
} // namespace helper
template<typename R, typename ... arglist>
struct MethodStubbingProgress {
virtual ~MethodStubbingProgress() THROWS {
}
template<typename U = R>
typename std::enable_if<!std::is_reference<U>::value, MethodStubbingProgress<R, arglist...> &>::type
Return(const R &r) {
return Do([r](const typename fakeit::test_arg<arglist>::type...) -> R { return r; });
}
template<typename U = R>
typename std::enable_if<std::is_reference<U>::value, MethodStubbingProgress<R, arglist...> &>::type
Return(const R &r) {
return Do([&r](const typename fakeit::test_arg<arglist>::type...) -> R { return r; });
}
template<typename U = R>
typename std::enable_if<!std::is_copy_constructible<U>::value, MethodStubbingProgress<R, arglist...>&>::type
Return(R&& r) {
auto store = std::make_shared<R>(std::move(r)); // work around for lack of move_only_funciton( C++23) - move into a shared_ptr which we can copy.
return Do([store](const typename fakeit::test_arg<arglist>::type...) mutable -> R {
return std::move(*store);
});
}
MethodStubbingProgress<R, arglist...> &
Return(const Quantifier<R> &q) {
const R &value = q.value;
auto method = [value](const arglist &...) -> R { return value; };
return DoImpl(new Repeat<R, arglist...>(method, q.quantity));
}
template<typename first, typename second, typename ... tail>
MethodStubbingProgress<R, arglist...> &
Return(const first &f, const second &s, const tail &... t) {
Return(f);
return Return(s, t...);
}
template<typename U = R>
typename std::enable_if<!std::is_reference<U>::value, void>::type
AlwaysReturn(const R &r) {
return AlwaysDo([r](const typename fakeit::test_arg<arglist>::type...) -> R { return r; });
}
template<typename U = R>
typename std::enable_if<std::is_reference<U>::value, void>::type
AlwaysReturn(const R &r) {
return AlwaysDo([&r](const typename fakeit::test_arg<arglist>::type...) -> R { return r; });
}
MethodStubbingProgress<R, arglist...> &
Return() {
return Do([](const typename fakeit::test_arg<arglist>::type...) -> R { return DefaultValue<R>::value(); });
}
void AlwaysReturn() {
return AlwaysDo([](const typename fakeit::test_arg<arglist>::type...) -> R { return DefaultValue<R>::value(); });
}
template<typename E>
MethodStubbingProgress<R, arglist...> &Throw(const E &e) {
return Do([e](const typename fakeit::test_arg<arglist>::type...) -> R { throw e; });
}
template<typename E>
MethodStubbingProgress<R, arglist...> &
Throw(const Quantifier<E> &q) {
const E &value = q.value;
auto method = [value](const arglist &...) -> R { throw value; };
return DoImpl(new Repeat<R, arglist...>(method, q.quantity));
}
template<typename first, typename second, typename ... tail>
MethodStubbingProgress<R, arglist...> &
Throw(const first &f, const second &s, const tail &... t) {
Throw(f);
return Throw(s, t...);
}
template<typename E>
void AlwaysThrow(const E &e) {
return AlwaysDo([e](const typename fakeit::test_arg<arglist>::type...) -> R { throw e; });
}
template<typename ... valuelist>
MethodStubbingProgress<R, arglist...> &
ReturnAndSet(R &&r, valuelist &&... arg_vals) {
return Do(GetAssigner(std::forward<R>(r),
std::forward<valuelist>(arg_vals)...));
}
template<typename ... valuelist>
void AlwaysReturnAndSet(R &&r, valuelist &&... arg_vals) {
AlwaysDo(GetAssigner(std::forward<R>(r),
std::forward<valuelist>(arg_vals)...));
}
virtual MethodStubbingProgress<R, arglist...> &
Do(std::function<R(const typename fakeit::test_arg<arglist>::type...)> method) {
return DoImpl(new Repeat<R, arglist...>(method));
}
template<typename F>
MethodStubbingProgress<R, arglist...> &
Do(const Quantifier<F> &q) {
return DoImpl(new Repeat<R, arglist...>(q.value, q.quantity));
}
template<typename first, typename second, typename ... tail>
MethodStubbingProgress<R, arglist...> &
Do(const first &f, const second &s, const tail &... t) {
Do(f);
return Do(s, t...);
}
virtual void AlwaysDo(std::function<R(const typename fakeit::test_arg<arglist>::type...)> method) {
DoImpl(new RepeatForever<R, arglist...>(method));
}
protected:
virtual MethodStubbingProgress<R, arglist...> &DoImpl(Action<R, arglist...> *action) = 0;
private:
MethodStubbingProgress &operator=(const MethodStubbingProgress &other) = delete;
template<typename ... valuelist>
#if __cplusplus >= 201402L
auto
#else
std::function<R (typename fakeit::test_arg<arglist>::type...)>
#endif
GetAssigner(R &&r, valuelist &&... arg_vals) {
class Lambda {
public:
Lambda(R &&r, valuelist &&... arg_vals)
: vals_tuple{std::forward<R>(r), std::forward<valuelist>(arg_vals)...} {}
R operator()(typename fakeit::test_arg<arglist>::type... args) {
helper::ParamWalker<sizeof...(valuelist)>::Assign(vals_tuple,
std::forward<arglist>(args)...);
return std::get<0>(vals_tuple);
}
private:
ArgumentsTuple<R, valuelist...> vals_tuple;
};
return Lambda(std::forward<R>(r), std::forward<valuelist>(arg_vals)...);
}
template<typename ...T, int ...N>
#if __cplusplus >= 201402L
auto
#else
std::function<R (typename fakeit::test_arg<arglist>::type...)>
#endif
GetAssigner(R &&r, helper::ArgValue<T, N>... arg_vals) {
class Lambda {
public:
Lambda(R &&r, helper::ArgValue<T, N>... arg_vals)
: ret{std::forward<R>(r)}
, vals_tuple{std::forward<helper::ArgValue<T, N>>(arg_vals)...} {}
R operator()(typename fakeit::test_arg<arglist>::type... args) {
helper::ArgValidator<sizeof...(arglist), sizeof...(T) - 1>::CheckPositions(vals_tuple);
helper::Assign<1>(vals_tuple, std::forward<arglist>(args)...);
return std::get<0>(ret);
}
private:
std::tuple<R> ret;
ArgumentsTuple<helper::ArgValue<T, N>...> vals_tuple;
};
return Lambda(std::forward<R>(r), std::forward<helper::ArgValue<T, N>>(arg_vals)...);
}
};
template<typename ... arglist>
struct MethodStubbingProgress<void, arglist...> {
virtual ~MethodStubbingProgress() THROWS {
}
MethodStubbingProgress<void, arglist...> &Return() {
auto lambda = [](const typename fakeit::test_arg<arglist>::type...) -> void {
return DefaultValue<void>::value();
};
return Do(lambda);
}
virtual MethodStubbingProgress<void, arglist...> &Do(
std::function<void(const typename fakeit::test_arg<arglist>::type...)> method) {
return DoImpl(new Repeat<void, arglist...>(method));
}
void AlwaysReturn() {
return AlwaysDo([](const typename fakeit::test_arg<arglist>::type...) -> void { return DefaultValue<void>::value(); });
}
MethodStubbingProgress<void, arglist...> &
Return(const Quantifier<void> &q) {
auto method = [](const arglist &...) -> void { return DefaultValue<void>::value(); };
return DoImpl(new Repeat<void, arglist...>(method, q.quantity));
}
template<typename E>
MethodStubbingProgress<void, arglist...> &Throw(const E &e) {
return Do([e](const typename fakeit::test_arg<arglist>::type...) -> void { throw e; });
}
template<typename E>
MethodStubbingProgress<void, arglist...> &
Throw(const Quantifier<E> &q) {
const E &value = q.value;
auto method = [value](const typename fakeit::test_arg<arglist>::type...) -> void { throw value; };
return DoImpl(new Repeat<void, arglist...>(method, q.quantity));
}
template<typename first, typename second, typename ... tail>
MethodStubbingProgress<void, arglist...> &
Throw(const first &f, const second &s, const tail &... t) {
Throw(f);
return Throw(s, t...);
}
template<typename E>
void AlwaysThrow(const E e) {
return AlwaysDo([e](const typename fakeit::test_arg<arglist>::type...) -> void { throw e; });
}
template<typename ... valuelist>
MethodStubbingProgress<void, arglist...> &
ReturnAndSet(valuelist &&... arg_vals) {
return Do(GetAssigner(std::forward<valuelist>(arg_vals)...));
}
template<typename ... valuelist>
void AlwaysReturnAndSet(valuelist &&... arg_vals) {
AlwaysDo(GetAssigner(std::forward<valuelist>(arg_vals)...));
}
template<typename F>
MethodStubbingProgress<void, arglist...> &
Do(const Quantifier<F> &q) {
return DoImpl(new Repeat<void, arglist...>(q.value, q.quantity));
}
template<typename first, typename second, typename ... tail>
MethodStubbingProgress<void, arglist...> &
Do(const first &f, const second &s, const tail &... t) {
Do(f);
return Do(s, t...);
}
virtual void AlwaysDo(std::function<void(const typename fakeit::test_arg<arglist>::type...)> method) {
DoImpl(new RepeatForever<void, arglist...>(method));
}
protected:
virtual MethodStubbingProgress<void, arglist...> &DoImpl(Action<void, arglist...> *action) = 0;
private:
MethodStubbingProgress &operator=(const MethodStubbingProgress &other) = delete;
template<typename ... valuelist>
#if __cplusplus >= 201402L
auto
#else
std::function<void (typename fakeit::test_arg<arglist>::type...)>
#endif
GetAssigner(valuelist &&... arg_vals) {
class Lambda {
public:
Lambda(valuelist &&... arg_vals)
: vals_tuple{std::forward<valuelist>(arg_vals)...} {}
void operator()(typename fakeit::test_arg<arglist>::type... args) {
helper::ParamWalker<sizeof...(valuelist)>::Assign(vals_tuple,
std::forward<arglist>(args)...);
}
private:
ArgumentsTuple<valuelist...> vals_tuple;
};
return Lambda(std::forward<valuelist>(arg_vals)...);
}
template<typename ...T, int ...N>
#if __cplusplus >= 201402L
auto
#else
std::function<void (typename fakeit::test_arg<arglist>::type...)>
#endif
GetAssigner(helper::ArgValue<T, N>... arg_vals) {
class Lambda {
public:
Lambda(helper::ArgValue<T, N>... arg_vals)
: vals_tuple{std::forward<helper::ArgValue<T, N>>(arg_vals)...} {}
void operator()(typename fakeit::test_arg<arglist>::type... args) {
helper::ArgValidator<sizeof...(arglist), sizeof...(T) - 1>::CheckPositions(vals_tuple);
helper::Assign<1>(vals_tuple, std::forward<arglist>(args)...);
}
private:
ArgumentsTuple<helper::ArgValue<T, N>...> vals_tuple;
};
return Lambda(std::forward<helper::ArgValue<T, N>>(arg_vals)...);
}
};
namespace helper
{
template <typename T, int N>
struct ArgValue
{
ArgValue(T &&v): value ( std::forward<T>(v) ) {}
constexpr static int pos = N;
T value;
};
template <int max_index, int tuple_index>
struct ArgValidator
{
template <typename ...T, int ...N>
static void CheckPositions(const std::tuple<ArgValue<T, N>...> arg_vals)
{
#if __cplusplus >= 201402L && !defined(_WIN32)
static_assert(std::get<tuple_index>(arg_vals).pos <= max_index,
"Argument index out of range");
ArgValidator<max_index, tuple_index - 1>::CheckPositions(arg_vals);
#else
(void)arg_vals; // to suppress unused variable warning in C++11
#endif
}
};
template <int max_index>
struct ArgValidator<max_index, -1>
{
template <typename T>
static void CheckPositions(T) {}
};
template <typename current_arg>
typename std::enable_if<std::is_pointer<current_arg>::value,
typename std::remove_pointer<current_arg>::type &>::type
GetArg(current_arg &&t)
{
return *t;
}
template <typename current_arg>
typename std::enable_if<!std::is_pointer<current_arg>::value, current_arg>::type
GetArg(current_arg &&t)
{
return std::forward<current_arg>(t);
}
template<int N>
struct ParamWalker {
template<typename current_arg, typename ... valuelist, typename ... arglist>
static void
Assign(ArgumentsTuple<valuelist...> arg_vals, current_arg &&p, arglist&&... args) {
ParamWalker<N - 1>::template Assign(arg_vals, std::forward<arglist>(args)...);
GetArg(std::forward<current_arg>(p)) = std::get<sizeof...(valuelist) - N>(arg_vals);
}
};
template<>
struct ParamWalker<0> {
template<typename ... valuelist, typename ... arglist>
static void Assign(ArgumentsTuple<valuelist...>, arglist... ) {}
};
template<int arg_index, int check_index>
struct ArgLocator {
template<typename current_arg, typename ...T, int ...N>
static void AssignArg(current_arg &&p, std::tuple<ArgValue<T, N>...> arg_vals) {
#if __cplusplus >= 201703L && !defined (_WIN32)
if constexpr (std::get<check_index>(arg_vals).pos == arg_index)
GetArg(std::forward<current_arg>(p)) = std::get<check_index>(arg_vals).value;
#else
if (std::get<check_index>(arg_vals).pos == arg_index)
Set(std::forward<current_arg>(p), std::get<check_index>(arg_vals).value);
#endif
else if (check_index > 0)
ArgLocator<arg_index, check_index - 1>::AssignArg(std::forward<current_arg>(p), arg_vals);
}
#if __cplusplus < 201703L || defined (_WIN32)
private:
template<typename T, typename U>
static
typename std::enable_if<std::is_assignable<decltype(GetArg(std::declval<T>())), U>::value, void>::type
Set(T &&p, U &&v)
{
GetArg(std::forward<T>(p)) = v;
}
template<typename T, typename U>
static
typename std::enable_if<!std::is_assignable<decltype(GetArg(std::declval<T>())), U>::value, void>::type
Set(T &&, U &&)
{
throw std::logic_error("ReturnAndSet(): Invalid value type");
}
#endif
};
template<int arg_index>
struct ArgLocator<arg_index, -1> {
template<typename current_arg, typename T>
static void AssignArg(current_arg, T) {
}
};
template<int arg_index, typename current_arg, typename ...T, int ...N, typename ... arglist>
static void
Assign(std::tuple<ArgValue<T, N>...> arg_vals, current_arg &&p, arglist &&... args) {
ArgLocator<arg_index, sizeof...(N) - 1>::AssignArg(std::forward<current_arg>(p), arg_vals);
Assign<arg_index + 1>(arg_vals, std::forward<arglist>(args)...);
}
template<int arg_index, typename ... valuelist>
static void Assign(std::tuple<valuelist...>) {}
} // namespace helper
/*
* Users might use our placeholders so that our operator<= is automatically
* picked up using ADL. They might use std placeholders instead, but they
* should make our operator<= from fakeit namespace visible to their code.
*/
namespace placeholders
{
using namespace std::placeholders;
template <typename PlaceHolder, typename ArgType,
typename std::enable_if<static_cast<bool>(std::is_placeholder<PlaceHolder>::value), bool>::type = true>
helper::ArgValue<ArgType, std::is_placeholder<PlaceHolder>::value>
operator<=(PlaceHolder, ArgType &&arg)
{
return { std::forward<ArgType>(arg) };
}
} // namespace placeholders
using placeholders::operator <=;
}