]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/STUB.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / tests / STUB.h
1 #ifndef STUB
2
3 /** \group STUB
4 *
5 * A set of useful macros to create stub_* files.
6 *
7 * Intended for use building unit tests, if a stubbed function is called
8 * by any code it is linked to it will abort with a message indicating
9 * which API file is missing from the linked dependencies.
10 *
11 * Usage:
12 * at the top of your intended stub file define STUB_API to be the
13 * name of the .cc file or library you are providing a stub of
14 * then include this STUB.h header.
15 *
16 * #define STUB_API "foo/libexample.la"
17 * #include "tests/STUB.h"
18 */
19 #include <iostream>
20
21 // Internal Special: the STUB framework requires this function
22 #define stub_fatal(m) { std::cerr<<"FATAL: "<<(m)<<" for use of "<<__FUNCTION__<<"\n"; exit(1); }
23
24 /// macro to stub a void function.
25 #define STUB { stub_fatal(STUB_API " required"); }
26
27 /// macro to stub a void function without a fatal message
28 /// Intended for registration pattern APIs where the function result does not matter to the test
29 #define STUB_NOP { std::cerr<<"SKIP: "<<STUB_API<<" "<<__FUNCTION__<<" (not implemented).\n"; }
30
31 /// macro to stub a function with return value.
32 /// Aborts unit tests requiring its definition with a message about the missing linkage
33 #define STUB_RETVAL(x) { stub_fatal(STUB_API " required"); return x; }
34
35 /// macro to stub a void function without a fatal message and with a return value
36 /// Intended for registration pattern APIs where the function result does not matter to the test
37 #define STUB_RETVAL_NOP(x) { std::cerr<<"SKIP: "<<STUB_API<<" "<<__FUNCTION__<<" (not implemented).\n"; return x; }
38
39 /** macro to stub a function which returns a reference to dynamic
40 * Aborts unit tests requiring its definition with a message about the missing linkage
41 * This macro uses 'new x' to construct a stack vailable for the reference, may leak.
42 * \param x may be the type to define or a constructor call with parameter values
43 */
44 #define STUB_RETREF(x) { stub_fatal(STUB_API " required"); return new x; }
45
46 /** macro to stub a function which returns a reference to static
47 * Aborts unit tests requiring its definition with a message about the missing linkage
48 * This macro uses static variable definition to avoid leaks.
49 * \param x the type name to define
50 */
51 #define STUB_RETSTATREF(x) { stub_fatal(STUB_API " required"); static x v; return v; }
52
53 #endif /* STUB */