fix typo
[tor.git] / src / common / testsupport.h
blob9fc96199b4432e81e35e63da19831db7f3bad34c
1 /* Copyright (c) 2013-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #ifndef TOR_TESTSUPPORT_H
5 #define TOR_TESTSUPPORT_H
7 #ifdef TOR_UNIT_TESTS
8 #define STATIC
9 #define EXTERN(type, name) extern type name;
10 #else
11 #define STATIC static
12 #define EXTERN(type, name)
13 #endif
15 /** Quick and dirty macros to implement test mocking.
17 * To use them, suppose that you have a function you'd like to mock
18 * with the signature "void writebuf(size_t n, char *buf)". You can then
19 * declare the function as:
21 * MOCK_DECL(void, writebuf, (size_t n, char *buf));
23 * and implement it as:
25 * MOCK_IMPL(void,
26 * writebuf,(size_t n, char *buf))
27 * {
28 * ...
29 * }
31 * For the non-testing build, this will expand simply into:
33 * void writebuf(size_t n, char *buf);
34 * void
35 * writebuf(size_t n, char *buf)
36 * {
37 * ...
38 * }
40 * But for the testing case, it will expand into:
42 * void writebuf__real(size_t n, char *buf);
43 * extern void (*writebuf)(size_t n, char *buf);
45 * void (*writebuf)(size_t n, char *buf) = writebuf__real;
46 * void
47 * writebuf__real(size_t n, char *buf)
48 * {
49 * ...
50 * }
52 * This is not a great mocking system! It is deliberately "the simplest
53 * thing that could work", and pays for its simplicity in its lack of
54 * features, and in its uglification of the Tor code. Replacing it with
55 * something clever would be a fine thing.
57 * @{ */
58 #ifdef TOR_UNIT_TESTS
59 #define MOCK_DECL(rv, funcname, arglist) \
60 rv funcname ##__real arglist; \
61 extern rv(*funcname) arglist
62 #define MOCK_IMPL(rv, funcname, arglist) \
63 rv(*funcname) arglist = funcname ##__real; \
64 rv funcname ##__real arglist
65 #define MOCK_DECL_ATTR(rv, funcname, arglist, attr) \
66 rv funcname ##__real arglist attr; \
67 extern rv(*funcname) arglist
68 #define MOCK_IMPL(rv, funcname, arglist) \
69 rv(*funcname) arglist = funcname ##__real; \
70 rv funcname ##__real arglist
71 #define MOCK(func, replacement) \
72 do { \
73 (func) = (replacement); \
74 } while (0)
75 #define UNMOCK(func) \
76 do { \
77 func = func ##__real; \
78 } while (0)
79 #else
80 #define MOCK_DECL(rv, funcname, arglist) \
81 rv funcname arglist
82 #define MOCK_DECL_ATTR(rv, funcname, arglist, attr) \
83 rv funcname arglist attr
84 #define MOCK_IMPL(rv, funcname, arglist) \
85 rv funcname arglist
86 #endif
87 /** @} */
89 #endif