Skip to content

Commit 749a920

Browse files
philIipfacebook-github-bot
authored andcommitted
introduce RCTMockDef
Summary: here's a way we can mock C apis - however i am not sure if the flag i'm using is correct used in D31949237 Changelog: [General][Added] - add macros to be able to stub C functions in tests Reviewed By: RSNara Differential Revision: D31949238 fbshipit-source-id: 0f18a65f810f1b855dbc844f11f5a304c1e5ecea
1 parent 9c5e177 commit 749a920

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ REACT_PUBLIC_HEADERS = {
253253
"React/RCTLayoutAnimationGroup.h": RCTMODULES_PATH + "RCTLayoutAnimationGroup.h",
254254
"React/RCTLog.h": RCTBASE_PATH + "RCTLog.h",
255255
"React/RCTManagedPointer.h": RCTBASE_PATH + "RCTManagedPointer.h",
256+
"React/RCTMockDef.h": RCTBASE_PATH + "RCTMockDef.h",
256257
"React/RCTModalHostViewController.h": RCTVIEWS_PATH + "RCTModalHostViewController.h",
257258
"React/RCTModalHostViewManager.h": RCTVIEWS_PATH + "RCTModalHostViewManager.h",
258259
"React/RCTModalManager.h": RCTVIEWS_PATH + "RCTModalManager.h",

React/Base/RCTMockDef.h

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)