1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef STUB
10 
11 /** \group STUB
12  *
13  *  A set of useful macros to create stub_* files.
14  *
15  * Intended for use building unit tests, if a stubbed function is called
16  * by any code it is linked to it will abort with a message indicating
17  * which API file is missing from the linked dependencies.
18  *
19  * Usage:
20  *    at the top of your intended stub file define STUB_API to be the
21  *    name of the .cc file or library you are providing a stub of
22  *    then include this STUB.h header.
23  *
24  *   #define STUB_API "foo/libexample.la"
25  *   #include "tests/STUB.h"
26  */
27 #include <iostream>
28 
29 // Internal Special: the STUB framework requires this function
30 #define stub_fatal(m) { std::cerr<<"FATAL: "<<(m)<<" for use of "<<__FUNCTION__<<"\n"; exit(1); }
31 
32 /// macro to stub a void function.
33 #define STUB { stub_fatal(STUB_API " required"); }
34 
35 /// macro to stub a void function without a fatal message
36 /// Intended for registration pattern APIs where the function result does not matter to the test
37 #define STUB_NOP { std::cerr<<"SKIP: "<<STUB_API<<" "<<__FUNCTION__<<" (not implemented).\n"; }
38 
39 /// macro to stub a function with return value.
40 /// Aborts unit tests requiring its definition with a message about the missing linkage
41 #define STUB_RETVAL(x) { stub_fatal(STUB_API " required"); return x; }
42 
43 /// macro to stub a void function without a fatal message and with a return value
44 /// Intended for registration pattern APIs where the function result does not matter to the test
45 #define STUB_RETVAL_NOP(x) { std::cerr<<"SKIP: "<<STUB_API<<" "<<__FUNCTION__<<" (not implemented).\n"; return x; }
46 
47 /** macro to stub a function which returns a reference to dynamic
48  *  Aborts unit tests requiring its definition with a message about the missing linkage
49  *  \param x underlying or "referred to" type
50  */
51 #define STUB_RETREF(x) { stub_fatal(STUB_API " required"); return *(x *)nullptr; }
52 
53 /** Same as STUB_RETREF(). TODO: Remove */
54 #define STUB_RETSTATREF(x) STUB_RETREF(x)
55 
56 #endif /* STUB */
57 
58