|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#import <React/RCTDefines.h> |
| 9 | + |
| 10 | +/* These macros are used to stub C functions. Here's an example: |
| 11 | + * |
| 12 | + * Helpers.h |
| 13 | + * ------ |
| 14 | + * boolean ReturnsTrueOrFalse(void); |
| 15 | + * |
| 16 | + * FileToBeTested.h |
| 17 | + * ------ |
| 18 | + * RCT_MOCK_DEF(Testing, ReturnsTrueOrFalse); |
| 19 | + * #define ReturnsTrueOrFalse RCT_MOCK_USE(Testing, ReturnsTrueOrFalse) |
| 20 | + * |
| 21 | + * int FunctionToBeTested(int input) { |
| 22 | + * return ReturnsTrueOrFalse() ? input + 1 : input - 1; |
| 23 | + * } |
| 24 | + * |
| 25 | + * Test.h |
| 26 | + * ----- |
| 27 | + * RCT_MOCK_GET(Testing, ReturnsTrueOrFalse); |
| 28 | + * |
| 29 | + * boolean _ReturnsTrue(void) { return true; } |
| 30 | + * boolean _ReturnsFalse(void) { return false; } |
| 31 | + * |
| 32 | + * void TestFunctionTrue(void) { |
| 33 | + * RCT_MOCK_SET(Testing, ReturnsTrueOrFalse, _ReturnsTrue); |
| 34 | + * assert(FunctionToBeTested(5) == 6); |
| 35 | + * RCT_MOCK_RESET(Testing, ReturnsTrueOrFalse); |
| 36 | + * } |
| 37 | + * |
| 38 | + * void TestFunctionFalse(void) { |
| 39 | + * RCT_MOCK_SET(Testing, ReturnsTrueOrFalse, _ReturnsFalse); |
| 40 | + * assert(FunctionToBeTested(5) == 4); |
| 41 | + * RCT_MOCK_RESET(Testing, ReturnsTrueOrFalse); |
| 42 | + * } |
| 43 | + * |
| 44 | + */ |
| 45 | + |
| 46 | +#ifdef RCT_DEV |
| 47 | + #define RCT_MOCK_DEF(context, api) __typeof(__typeof(api) *) mockptr_ ## context ## _ ## api = &api; |
| 48 | + #define RCT_MOCK_REF(context, api) extern __typeof(__typeof(api) *) mockptr_ ## context ## _ ## api; |
| 49 | + #define RCT_MOCK_SET(context, api, mockapi) (mockptr_ ## context ## _ ## api = &mockapi) |
| 50 | + #define RCT_MOCK_RESET(context, api) (mockptr_ ## context ## _ ## api = &api) |
| 51 | + #define RCT_MOCK_USE(context, api) (*mockptr_ ## context ## _ ## api) |
| 52 | +#else |
| 53 | + #define RCT_MOCK_DEF(context, api) |
| 54 | + #define RCT_MOCK_REF(context, api) |
| 55 | + #define RCT_MOCK_SET(context, api, mockapi) |
| 56 | + #define RCT_MOCK_RESET(context, api) |
| 57 | + #define RCT_MOCK_USE(context, api) api |
| 58 | +#endif |
0 commit comments