-
Notifications
You must be signed in to change notification settings - Fork 3
/
premock.hpp
648 lines (503 loc) · 19.9 KB
/
premock.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/**
This header library makes it possible to replace implementations of C
functions with C++ callables for unit testing.
It works by using the preprocessor to redefine the functions to be
mocked in the files to be tested by prepending `ut_premock_` to them instead
of calling the "real" implementation. This `ut_premock_` function then
forwards to a `std::function` of the appropriate type that can be
changed at runtime to a C++ callable.
An example of mocking the BSD socket API `send` function would be to
have a header like this:
```c
#ifndef MOCK_NETWORK_H
#define MOCK_NETWORK_H
# define send ut_premock_send
#endif
```
The build system would then insert this header before any other
`#includes` via an option to the compiler (`-include` for gcc/clang).
This could also be done with `-D` but in the case of multiple
functions it's easier to have all the redefinitions in one header.
Now all calls to `send` are actually to `ut_premock_send`. This will fail to
link since `ut_premock_send` doesn't exist. To implement it, the test binary
should be linked with an object file from compiling this code
(e.g. called `mock_network.cpp`):
```c++
#include "mock_network.hpp"
extern "C" IMPL_MOCK(4, send); // the 4 is the number of parameters "send" takes
```
This will only compile if a header called `mock_network.hpp` exists with the
following contents:
```c++
#include "premock.hpp"
#include <sys/socket.h> // to have access to send, the original function
DECL_MOCK(send); // the declaration for the implementation in the cpp file
```
Now test code can do this:
```c++
#include "mock_network.hpp"
TEST(send, replace) {
REPLACE(send, [](auto...) { return 7; });
// any function that calls send from here until the end of scope
// will call our lambda instead and always return 7
}
TEST(send, mock) {
auto m = MOCK(send);
m.returnValue(42);
// any function that calls send from here until the end of scope
// will get a return value of 42
function_that_calls_send();
// check last call to send only
m.expectCalled().withValues(3, nullptr, 0, 0); //checks values of last call
// check last 3 calls:
m.expectCalled(3).withValues({make_tuple(3, nullptr, 0, 0),
make_tuple(5, nullptr, 0, 0),
make_tuple(7, nullptr, 0, 0)});
}
TEST(send, for_reals) {
//no MOCK or REPLACE, calling a function that calls send
//will call the real McCoy
function_that_calls_send(); //this will probably send packets
}
If neither `REPLACE` nor `MOCK` are used, the original implementation
will be used.
```
*/
#ifndef MOCK_HPP_
#define MOCK_HPP_
#include <functional>
#include <type_traits>
#include <tuple>
#include <deque>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <cstring>
/**
RAII class for setting a mock to a callable until the end of scope
*/
template<typename T>
class MockScope {
public:
// assumes T is a std::function
using ReturnType = typename T::result_type;
/**
Replace func with scopeFunc until the end of scope.
*/
template<typename F>
MockScope(T& func, F scopeFunc):
_func{func},
_oldFunc{std::move(func)} {
_func = std::move(scopeFunc);
}
/**
Restore func to its original value
*/
~MockScope() {
_func = std::move(_oldFunc);
}
private:
T& _func;
T _oldFunc;
};
/**
Helper function to create a MockScope
*/
template<typename T, typename F>
MockScope<T> mockScope(T& func, F scopeFunc) {
return {func, scopeFunc};
}
#define CONCATENATE_DETAIL(x, y) x##y
#define CONCATENATE(x, y) CONCATENATE_DETAIL(x, y)
#define MAKE_UNIQUE(x) CONCATENATE(x, __LINE__)
/**
Temporarily replace func with the passed-in lambda:
e.g.
REPLACE(send, [](auto...) { return -1; });
This causes every call to `send` in the production code to
return -1 no matter what
*/
#define REPLACE(func, lambda) auto MAKE_UNIQUE(_) = mockScope(mock_##func, lambda)
template<typename T>
struct Slice {
void* ptr = nullptr;
size_t length = 0;
};
template<typename>
struct StdFunctionTraits {};
/**
Pattern match on std::function to get its parameter types
*/
template<typename R, typename... A>
struct StdFunctionTraits<std::function<R(A...)>> {
using TupleType = std::tuple<std::remove_reference_t<A>...>;
using OutputTupleType = std::tuple<Slice<std::remove_reference_t<A>> ...>;
};
/**
An exception class to throw when a mock expectation isn't met
*/
class MockException: public std::exception {
public:
MockException(std::string s):_what{std::move(s)} {}
const char* what() const noexcept override { return _what.c_str(); }
std::string _what;
};
// Visual Studio, you make life so "interesting"...
#ifdef _MSC_VER
# if _MSC_VER < 1910
# define PREMOCK_NEED_VOID_T
# endif
#else
# ifndef __cpp_lib_void_t
# define PREMOCK_NEED_VOID_T
# endif
#endif
#ifdef PREMOCK_NEED_VOID_T
namespace std {
template<typename... Ts> struct make_void { typedef void type;};
template<typename... Ts> using void_t = typename make_void<Ts...>::type;
}
#endif
// primary template, default to false for every type
template<typename, typename = std::void_t<>>
struct CanBeStreamed: std::false_type {};
// detects when ostream operator<< works on a type
template<typename T>
struct CanBeStreamed<T, std::void_t<decltype(std::cout << std::declval<T&>())> >: std::true_type {};
// default implementation for types that don't define operator<<
template<typename T>
std::string toString(const T&, typename std::enable_if<!CanBeStreamed<T>::value>::type* = nullptr) {
return "<cannot print>";
}
// implementation for types that can be streamed (and therefore printed)
template<typename T>
std::string toString(const T& value, typename std::enable_if<CanBeStreamed<T>::value>::type* = nullptr) {
std::stringstream stream;
stream << value;
return stream.str();
}
// helper class to print std::tuple. A class is used instead of a function to
// enable partial template specialization
template<int I>
struct TuplePrinter {
template<typename... A>
static std::string toString(const std::tuple<A...>& values) {
// since C++ doesn't allow forward loops in compile-time,
// we recurse from N down, and print from 0 up
return ::toString(std::get<sizeof...(A) - I>(values)) +
", " + TuplePrinter<I - 1>::toString(values);
}
};
// recursion terminator. This ends at 1 because the last value to be printed
// is the number of elements in the tuple - 1. It's "backwards" recursion
template<>
struct TuplePrinter<1> {
template<typename... A>
static std::string toString(const std::tuple<A...>& values) {
return ::toString(std::get<sizeof...(A) - 1>(values));
}
};
// template specialization for std::tuple
template<typename... A>
std::string toString(const std::tuple<A...>& values) {
return std::string{"("} + TuplePrinter<sizeof...(A)>::toString(values) + ")";
}
// primary template, default to false for every type
template<typename, typename = std::void_t<>>
struct CanBeOverwritten: std::false_type {};
// detects when ostream operator<< works on a type
template<typename T>
struct CanBeOverwritten<T, std::void_t<decltype(memcpy(std::declval<T>(), std::declval<T>(), 0))>>: std::true_type {};
/**
A mock class to verify expectations of how the mock was called.
Supports verification of the number of times called, setting
return values and checking the values passed to it.
*/
template<typename T>
class Mock {
public:
using ReturnType = typename MockScope<T>::ReturnType;
using ParamTupleType = typename StdFunctionTraits<T>::TupleType;
using OutputTupleType = typename StdFunctionTraits<T>::OutputTupleType;
/**
Enables checks on parameter values passed to function invocations
*/
class ParamChecker {
public:
ParamChecker(std::deque<ParamTupleType> v):_values{v} {}
/**
Verifies the parameter values passed in the last invocation
*/
template<typename... A>
void withValues(A&&... args) {
withValues({std::make_tuple(std::forward<A>(args)...)}, _values.size() - 1, _values.size());
}
/**
Verifies the parameter values passed in all invocations since the last
call to `expectCalled`, optionally between the start-th and end-th
invocations
*/
void withValues(std::initializer_list<ParamTupleType> args,
size_t start = 0, size_t end = 0) {
if(end == 0) end = _values.size();
std::deque<ParamTupleType> expected{std::move(args)};
const auto expectedArgsSize = end - start;
if(expected.size() != expectedArgsSize)
throw std::logic_error("ParamChecker::withValues called with " +
capitalize(expected.size(), "value") + ", expected " +
std::to_string(expectedArgsSize));
for(size_t i = start; i < end; ++i) {
const auto& expValues = expected.at(i - start);
const auto& actValues = _values.at(i);
if(expValues != actValues)
throw MockException(std::string{"Invocation values do not match\n"} +
"Expected: " + toString(expValues) + "\n" +
"Actual: " + toString(actValues) + "\n");
}
}
private:
std::deque<ParamTupleType> _values;
std::string capitalize(int val, const std::string& word) {
return val == 1 ? "1 " + word : std::to_string(val) + " " + word + "s";
}
};
/**
Constructor. Pass in the mock_ std::function to replace until
the end of scope.
*/
Mock(T& func):
_mockScope{func,
[this](auto... args) {
_values.emplace_back(args...);
this->setOutputParameters<sizeof...(args)>(args...);
auto ret = _returns.at(0);
if(_returns.size() > 1) _returns.pop_front();
// it may seem odd to cast to the return type here, but the only
// reason it's needed is when the mocked function's return type
// is void. This makes it work
return static_cast<ReturnType>(ret);
}},
_returns(1) {
}
/**
Set the next N return values
*/
template<typename... A>
void returnValue(A&&... args) {
_returns.clear();
returnValueImpl(std::forward<A>(args)...);
}
/**
Set the output parameter at position I
*/
template<int I, typename A>
void outputParam(A valuePtr) {
outputArray<I>(valuePtr, 1);
}
template<size_t I, typename A>
void outputArray(A ptr, size_t length) {
std::get<I>(_outputs) = Slice<A>{ptr, length * sizeof(*ptr)};
}
/**
Verify the mock was called n times. Returns a ParamChecker so that
assertions can be made on the passed in parameter values
*/
ParamChecker expectCalled(size_t n = 1) {
if(_values.size() != n)
throw MockException(std::string{"Was not called enough times\n"} +
"Expected: " + std::to_string(n) + "\n" +
"Actual: " + std::to_string(_values.size()) + "\n");
auto ret = _values;
_values.clear();
return ret;
}
template<int N, typename A, typename... As>
std::enable_if_t<std::is_pointer<std::remove_reference_t<A>>::value && CanBeOverwritten<A>::value>
setOutputParameters(A outputParam, As&&... args) {
setOutputParametersImpl<N>(std::forward<A>(outputParam));
setOutputParameters<N - 1>(std::forward<As>(args)...);
}
private:
MockScope<T> _mockScope;
// the _returns would be static if'ed out for void return type if it were allowed in C++
// since it isn't, we change the return type to void* in that case
std::deque<std::conditional_t<std::is_void<ReturnType>::value, void*, ReturnType>> _returns;
std::deque<ParamTupleType> _values;
OutputTupleType _outputs{};
template<typename A, typename... As>
void returnValueImpl(A&& arg, As&&... args) {
_returns.emplace_back(arg);
returnValueImpl(std::forward<As>(args)...);
}
void returnValueImpl() {}
template<int N, typename A>
std::enable_if_t<std::is_pointer<std::remove_reference_t<A>>::value && CanBeOverwritten<A>::value>
setOutputParameters(A&& outputParam) {
setOutputParametersImpl<N>(std::forward<A>(outputParam));
}
template<int N, typename A>
void setOutputParametersImpl(A outputParam) {
constexpr auto paramIndex = std::tuple_size<decltype(_outputs)>::value - N;
const auto& data = std::get<paramIndex>(_outputs);
if(data.ptr && data.length) {
memcpy(outputParam, data.ptr, data.length);
}
}
template<int N, typename A, typename... As>
std::enable_if_t<!std::is_pointer<std::remove_reference_t<A>>::value || !CanBeOverwritten<A>::value>
setOutputParameters(A, As&&... args) {
setOutputParameters<N - 1>(std::forward<As>(args)...);
}
template<int N, typename A>
std::enable_if_t<!std::is_pointer<std::remove_reference_t<A>>::value || !CanBeOverwritten<A>::value>
setOutputParameters(A) { }
template<int N>
void setOutputParameters() { }
};
/**
Helper function to create a Mock<T>
*/
template<typename T>
Mock<T> mock(T& func) {
return {func};
}
/**
Helper macro to mock a particular "real" function
*/
#define MOCK(func) mock(mock_##func)
/**
Traits class for function pointers
*/
template<typename>
struct FunctionTraits;
template<typename R, typename... A>
struct FunctionTraits<R(*)(A...)> {
/**
The corresponding type of a std::function that the function could be assigned to
*/
using StdFunctionType = std::function<R(A...)>;
/**
The return type of the function
*/
using ReturnType = R;
/**
Helper struct to get the type of the Nth argument to the function
*/
template<int N>
struct Arg {
using Type = typename std::tuple_element<N, std::tuple<A...>>::type;
};
};
/**
Declares a mock function for "real" function func. This is simply the
declaration, the implementation is done with IMPL_C_MOCK. If foo has signature:
int foo(int, float);
Then DECL_MOCK(foo) is:
extern std::function<int(int, float)> mock_foo;
*/
#define DECL_MOCK(func) extern "C" thread_local FunctionTraits<decltype(&func)>::StdFunctionType mock_##func
/**
Definition of the std::function that will store the implementation. e.g. given:
int foo(int, float);
Then MOCK_STORAGE(foo) is:
std::function<int(int, float)> mock_foo;
*/
#define MOCK_STORAGE(func) thread_local decltype(mock_##func) mock_##func
/**
Definition of the std::function that will store the implementation.
Defaults to the "real" function. e.g. given:
int foo(int, float);
Then MOCK_STORAGE_DEFAULT(foo) is:
std::function<int(int, float)> mock_foo = foo;
*/
#define MOCK_STORAGE_DEFAULT(func) thread_local decltype(mock_##func) mock_##func = func
/**
A name for the ut_premock_ function argument at position index
*/
#define UT_FUNC_ARG(index) arg_##index
/**
Type and name for ut_premock_ function argument at position index. If foo has signature:
int foo(int, float);
Then:
UT_FUNC_TYPE_AND_ARG(foo, 0) = int arg_0
UT_FUNC_TYPE_AND_ARG(foo, 1) = float arg_1
*/
#define UT_FUNC_TYPE_AND_ARG(func, index) FunctionTraits<decltype(&func)>::Arg<index>::Type UT_FUNC_ARG(index)
/**
Helper macros to generate the code for the ut_premock_ functions.
UT_FUNC_ARGS_N generates the parameter list in the function declaration,
with types and parameter names, e.g. (int arg0, float arg1, ...)
UT_FUNC_FWD_N generates just the parameter names so that the ut_premock_
function can forward the call to the equivalent mock_, e.g.
(arg0, arg1, ...)
*/
#define UT_FUNC_ARGS_0(func)
#define UT_FUNC_FWD_0
#define UT_FUNC_ARGS_1(func) UT_FUNC_ARGS_0(func) UT_FUNC_TYPE_AND_ARG(func, 0)
#define UT_FUNC_FWD_1 UT_FUNC_FWD_0 UT_FUNC_ARG(0)
#define UT_FUNC_ARGS_2(func) UT_FUNC_ARGS_1(func), UT_FUNC_TYPE_AND_ARG(func, 1)
#define UT_FUNC_FWD_2 UT_FUNC_FWD_1, UT_FUNC_ARG(1)
#define UT_FUNC_ARGS_3(func) UT_FUNC_ARGS_2(func), UT_FUNC_TYPE_AND_ARG(func, 2)
#define UT_FUNC_FWD_3 UT_FUNC_FWD_2, UT_FUNC_ARG(2)
#define UT_FUNC_ARGS_4(func) UT_FUNC_ARGS_3(func), UT_FUNC_TYPE_AND_ARG(func, 3)
#define UT_FUNC_FWD_4 UT_FUNC_FWD_3, UT_FUNC_ARG(3)
#define UT_FUNC_ARGS_5(func) UT_FUNC_ARGS_4(func), UT_FUNC_TYPE_AND_ARG(func, 4)
#define UT_FUNC_FWD_5 UT_FUNC_FWD_4, UT_FUNC_ARG(4)
#define UT_FUNC_ARGS_6(func) UT_FUNC_ARGS_5(func), UT_FUNC_TYPE_AND_ARG(func, 5)
#define UT_FUNC_FWD_6 UT_FUNC_FWD_5, UT_FUNC_ARG(5)
#define UT_FUNC_ARGS_7(func) UT_FUNC_ARGS_6(func), UT_FUNC_TYPE_AND_ARG(func, 6)
#define UT_FUNC_FWD_7 UT_FUNC_FWD_6, UT_FUNC_ARG(6)
#define UT_FUNC_ARGS_8(func) UT_FUNC_ARGS_7(func), UT_FUNC_TYPE_AND_ARG(func, 7)
#define UT_FUNC_FWD_8 UT_FUNC_FWD_7, UT_FUNC_ARG(7)
#define UT_FUNC_ARGS_9(func) UT_FUNC_ARGS_8(func), UT_FUNC_TYPE_AND_ARG(func, 8)
#define UT_FUNC_FWD_9 UT_FUNC_FWD_8, UT_FUNC_ARG(8)
/**
The implementation of the C++ mock for function func. This version makes the mock
function default to the real implementation.
This writes code to:
1. Define the global mock_func std::function variable to hold the current implementation
2. Assign this global to a pointer to the "real" implementation
3. Writes the ut_premock_ function called by production code to automatically forward to the mock
The number of arguments that the function takes must be specified. This could be deduced
with template metaprogramming but there is no way to feed that information back to
the preprocessor. Since the production calling code thinks it's calling a function
whose name begins with ut_premock_, that function must exist or there'll be a linker error.
The only way to not have to write the function by hand is to use the preprocessor.
An example of a call to IMPL_MOCK(4, send) (where send is the BSD socket function)
assuming the macro is used in an extern "C" block:
extern "C" ssize_t ut_premock_send(int arg0, const void* arg1, size_t arg2, int arg3) {
return mock_send(arg0, arg1, arg2, arg3);
}
std::function<ssize_t(int, const void*, size_t, int)> mock_send = send
*/
#define IMPL_MOCK_DEFAULT(num_args, func) \
FunctionTraits<decltype(&func)>::ReturnType ut_premock_##func(UT_FUNC_ARGS_##num_args(func)) { \
return mock_##func(UT_FUNC_FWD_##num_args); \
} \
MOCK_STORAGE_DEFAULT(func)
/**
The implementation of the C++ mock for function func. This version makes the mock
function default to nullptr.
This writes code to:
1. Define the global mock_func std::function variable to hold the current implementation
2. Assign this global to a pointer to the "real" implementation
3. Writes the ut_premock_ function called by production code to automatically forward to the mock
The number of arguments that the function takes must be specified. This could be deduced
with template metaprogramming but there is no way to feed that information back to
the preprocessor. Since the production calling code thinks it's calling a function
whose name begins with ut_premock_, that function must exist or there'll be a linker error.
The only way to not have to write the function by hand is to use the preprocessor.
An example of a call to IMPL_MOCK(4, send) (where send is the BSD socket function)
assuming the macro is used in an extern "C" block:
extern "C" ssize_t ut_premock_send(int arg0, const void* arg1, size_t arg2, int arg3) {
return mock_send(arg0, arg1, arg2, arg3);
}
std::function<ssize_t(int, const void*, size_t, int)> mock_send = send
*/
#define IMPL_MOCK(num_args, func) \
FunctionTraits<decltype(&func)>::ReturnType ut_premock_##func(UT_FUNC_ARGS_##num_args(func)) { \
return mock_##func(UT_FUNC_FWD_##num_args); \
} \
MOCK_STORAGE(func)
#endif // MOCK_HPP_